rhcp 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,56 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
+
3
+ require 'rubygems'
4
+ require 'json'
5
+
6
+ require 'test/unit'
7
+ require 'rhcp'
8
+ require 'rhcp'
9
+ require 'rhcp'
10
+
11
+ class CommandStubTest < Test::Unit::TestCase
12
+
13
+ def command_method(request, response)
14
+ first_param = request.get_param_value("first_param")
15
+ puts "just testing : #{first_param}"
16
+ first_param.reverse
17
+ end
18
+
19
+ def test_json
20
+ c = RHCP::Command.new("test", "a command for testing", self.method(:command_method))
21
+ c.add_param(RHCP::CommandParam.new("first_param", "this is the first param"))
22
+ c.result_hints[:display_type] = "table"
23
+ json = c.to_json
24
+ puts "JSON : >>#{JSON.pretty_generate(c)}<<"
25
+ assert_not_nil json
26
+ c2 = RHCP::Client::CommandStub.reconstruct_from_json(json)
27
+ assert_not_nil c2
28
+ assert_instance_of RHCP::Client::CommandStub, c2
29
+ assert_equal c.name, c2.name
30
+ assert_equal c.description, c2.description
31
+ assert_equal c.params.size, c2.params.size
32
+ assert_equal c.result_hints, c2.result_hints
33
+
34
+ json_hash = JSON.parse(json)
35
+ c3 = RHCP::Client::CommandStub.reconstruct_from_json(json_hash)
36
+ assert_instance_of RHCP::Client::CommandStub, c3
37
+ assert_equal c.name, c3.name
38
+ assert_equal c.description, c3.description
39
+ assert_equal c.params.size, c3.params.size
40
+ end
41
+
42
+ def test_json_without_result_hints
43
+ c = RHCP::Command.new("test", "no hints this time", lambda {})
44
+ json = c.to_json
45
+ puts "JSON : >>#{JSON.pretty_generate(c)}<<"
46
+ assert_not_nil json
47
+ c2 = RHCP::Client::CommandStub.reconstruct_from_json(json)
48
+ assert_not_nil c2
49
+ assert_instance_of RHCP::Client::CommandStub, c2
50
+ assert_equal c.name, c2.name
51
+ assert_equal c.description, c2.description
52
+ assert_equal c.params.size, c2.params.size
53
+ assert_equal c.result_hints, c2.result_hints
54
+ end
55
+
56
+ end
@@ -0,0 +1,72 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
+
3
+ require 'test/unit'
4
+ require 'rhcp/command_param'
5
+
6
+ class CommandParamTest < Test::Unit::TestCase
7
+
8
+ def param_lookup
9
+ ["foo", "bar", "baz"]
10
+ end
11
+
12
+ def test_creation
13
+ param = RHCP::CommandParam.new("test", "this param is used for testing purposes only")
14
+ assert_not_nil param, "a param should not be nil after creation"
15
+ assert_equal false, param.mandatory, "by default, params should not be mandatory"
16
+ assert_equal false, param.has_lookup_values, "by default, a param does not have lookup values"
17
+ assert_equal false, param.allows_multiple_values, "by default, a param should not allow multiple values"
18
+ assert_equal false, param.is_default_param
19
+ end
20
+
21
+ def test_options
22
+ param = RHCP::CommandParam.new("test", "this param is used for testing purposes only",
23
+ :mandatory => true,
24
+ :allows_multiple_values => true,
25
+ :lookup_method => self.method(:param_lookup),
26
+ :is_default_param => true
27
+ )
28
+ assert_not_nil param, "a param should not be nil after creation"
29
+ assert_equal true, param.mandatory
30
+ assert_equal true, param.allows_multiple_values
31
+ assert_equal true, param.has_lookup_values
32
+ assert_equal true, param.is_default_param
33
+ end
34
+
35
+ def test_lookup_values
36
+ param = RHCP::CommandParam.new("lookup_test", "testing if lookup values are working",
37
+ :lookup_method => self.method(:param_lookup)
38
+ )
39
+ assert_not_nil param
40
+ assert_equal param_lookup(), param.get_lookup_values()
41
+ end
42
+
43
+ def test_partial_lookup_values
44
+ param = RHCP::CommandParam.new("lookup_test", "testing if partial lookup values are working",
45
+ :lookup_method => self.method(:param_lookup)
46
+ )
47
+ assert_not_nil param
48
+ assert_equal ["bar", "baz"], param.get_lookup_values("ba")
49
+ end
50
+
51
+ def test_get_lookup_values_without_lookup
52
+ param = RHCP::CommandParam.new("lookup_test", "testing if partial lookup values are working")
53
+ assert_not_nil param
54
+ assert_equal [], param.get_lookup_values()
55
+ end
56
+
57
+ def test_check_param_is_valid
58
+ param = RHCP::CommandParam.new("validity_test", "testing if valid values are valid")
59
+ assert_not_nil param
60
+ assert param.check_param_is_valid([ "bla" ])
61
+ assert_raise(RHCP::RhcpException) { param.check_param_is_valid [ "bla", "blubb" ] }
62
+ end
63
+
64
+ # values that aren't part of the lookup values are invalid
65
+ def test_check_param_is_valid_lookup_values
66
+ param = RHCP::CommandParam.new("lookup_test", "testing if partial lookup values are working",
67
+ :lookup_method => self.method(:param_lookup)
68
+ )
69
+ assert_raise(RHCP::RhcpException) { param.check_param_is_valid(["zaphod"]) }
70
+ end
71
+
72
+ end
@@ -0,0 +1,136 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
+
3
+ require 'test/unit'
4
+ require 'rhcp'
5
+
6
+ class CommandTest < Test::Unit::TestCase
7
+
8
+ def command_method(request, response)
9
+ first_param = request.get_param_value("first_param")
10
+ puts "just testing : #{first_param}"
11
+ first_param.reverse
12
+ end
13
+
14
+ def command_method_with_error(request, response)
15
+ raise "got a problem here!"
16
+ end
17
+
18
+ def test_construction
19
+ command = RHCP::Command.new("test", "a command for testing", self.method(:command_method))
20
+ assert_not_nil command
21
+ assert_equal "test", command.name
22
+ assert_equal "a command for testing", command.description
23
+ assert_not_nil command.params
24
+ assert_instance_of Hash, command.params
25
+ assert_equal 0, command.params.size
26
+
27
+ command.add_param(RHCP::CommandParam.new("first_param", "this is the first param"))
28
+ command.add_param(RHCP::CommandParam.new("second_param", "this is the second param"))
29
+ assert_equal 2, command.params.size
30
+ first_param = command.get_param("first_param")
31
+ assert_not_nil first_param
32
+ assert_equal "first_param", first_param.name
33
+ assert_equal "this is the first param", first_param.description
34
+ assert_raise(RHCP::RhcpException) { command.get_param("does not exist") }
35
+ end
36
+
37
+ def test_execute_request
38
+ command = RHCP::Command.new("test", "a command for testing", self.method(:command_method))
39
+ command.add_param(RHCP::CommandParam.new("first_param", "this is the first param"))
40
+ request = RHCP::Request.new(command, { "first_param" => [ "the_value" ] })
41
+ response = command.execute_request(request)
42
+ assert_equal "the_value".reverse, response.data
43
+ assert_equal RHCP::Response::Status::OK, response.status
44
+ end
45
+
46
+ def test_execute_error
47
+ command = RHCP::Command.new("not working", "told you so", self.method(:command_method_with_error))
48
+ request = RHCP::Request.new(command, {})
49
+ response = command.execute_request(request)
50
+ assert_equal RHCP::Response::Status::ERROR, response.status
51
+ end
52
+
53
+ def test_execute
54
+ command = RHCP::Command.new("test", "a command for testing", self.method(:command_method))
55
+ command.add_param(RHCP::CommandParam.new("first_param", "this is the first param"))
56
+ response = command.execute({"first_param" => "thing"})
57
+ assert_equal "thing".reverse, response.data
58
+ end
59
+
60
+ def test_duplicate_params
61
+ command = RHCP::Command.new("test_duplicate", "command for testing param duplicates", lambda {})
62
+ command.add_param(RHCP::CommandParam.new("first_param", "first param"))
63
+ assert_raise(RuntimeError) { command.add_param(RHCP::CommandParam.new("first_param", "i am a duplicate")) }
64
+ end
65
+
66
+ # it should not be possible to add more than one default param
67
+ def test_multiple_default_params
68
+ command = RHCP::Command.new("test", "command for testing multiple default params", lambda {})
69
+ command.add_param RHCP::CommandParam.new("first_param", "first param",
70
+ {
71
+ :is_default_param => true
72
+ }
73
+ )
74
+ assert_raise(RHCP::RhcpException) {
75
+ command.add_param RHCP::CommandParam.new("second_param", "second default param",
76
+ {
77
+ :is_default_param => true
78
+ }
79
+ )
80
+ }
81
+ end
82
+
83
+ def test_get_default_param
84
+ command = RHCP::Command.new("test_duplicate", "command for testing get_default_param", lambda {})
85
+ assert_nil command.default_param
86
+ the_param = RHCP::CommandParam.new("first_param", "first param",
87
+ {
88
+ :is_default_param => true
89
+ }
90
+ )
91
+ command.add_param the_param
92
+ assert_equal the_param, command.default_param
93
+ end
94
+
95
+ def test_listener
96
+ command = RHCP::Command.new("test_listener", "just some dumb command for the listener", lambda {
97
+ |req,res|
98
+ puts "While I think there could be something important to say right now, I just cannot remember what it might have been..."
99
+ res.result_text = "the test is working."
100
+ })
101
+
102
+ @listener1_fired = false
103
+ @listener2_fired = false
104
+
105
+ RHCP::Command.clear_post_exec_listeners()
106
+ RHCP::Command.register_post_exec_listener(lambda { |req,res| @listener1_fired = true })
107
+ RHCP::Command.register_post_exec_listener(lambda { |req,res| @listener2_fired = true })
108
+
109
+ command.execute()
110
+
111
+ assert @listener1_fired
112
+ assert @listener2_fired
113
+ end
114
+
115
+ def test_failing_listener
116
+ command = RHCP::Command.new("test_listener", "just some dumb command for the listener", lambda {
117
+ |req,res|
118
+ puts "While I think there could be something important to say right now, I just cannot remember what it might have been..."
119
+ res.result_text = "the test is working"
120
+ })
121
+
122
+ RHCP::Command.clear_post_exec_listeners()
123
+ RHCP::Command.register_post_exec_listener(lambda { |req,res| raise "this listener is dying"})
124
+
125
+ @listener1_fired = false
126
+ @listener2_fired = false
127
+
128
+ response = command.execute()
129
+ assert_not_nil response
130
+ assert_equal "the test is working", response.result_text
131
+ assert ! @listener1_fired
132
+ assert ! @listener2_fired
133
+ end
134
+
135
+ end
136
+
@@ -0,0 +1,41 @@
1
+
2
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
3
+
4
+ require 'test/unit'
5
+ #require 'rhcp/dispatching_broker'
6
+ #require 'rhcp/broker'
7
+ #require 'rhcp/command'
8
+ #require 'rhcp/command_param'
9
+ require 'rhcp'
10
+
11
+ class DispatchingBrokerTest < Test::Unit::TestCase
12
+
13
+ def setup
14
+ @broker1 = RHCP::Broker.new()
15
+ @broker1.register_command RHCP::Command.new("test", "just testing (broker1)", lambda{})
16
+ @broker1.register_command RHCP::Command.new("test_more", "another fancy command from broker1", lambda{})
17
+ @broker2 = RHCP::Broker.new()
18
+ @broker2.register_command RHCP::Command.new("echo", "says hello (broker2)", lambda{})
19
+ @broker2.register_command RHCP::Command.new("help", "is no help at all (broker2)", lambda{})
20
+ @broker2.register_command RHCP::Command.new("red button", "don't press it (broker2)", lambda{})
21
+ end
22
+
23
+ def test_add_brokers
24
+ dispatcher = RHCP::DispatchingBroker.new()
25
+ assert_not_nil dispatcher
26
+ assert_equal 0, dispatcher.get_command_list.size
27
+ dispatcher.add_broker(@broker1)
28
+ assert_equal 2, dispatcher.get_command_list.size
29
+ dispatcher.add_broker(@broker2)
30
+ assert_equal 5, dispatcher.get_command_list.size
31
+ end
32
+
33
+ def test_duplicate_commands
34
+ dispatcher = RHCP::DispatchingBroker.new()
35
+ dispatcher.add_broker(@broker1)
36
+ assert_equal 2, dispatcher.get_command_list.size
37
+ assert_raise(RHCP::RhcpException) { dispatcher.add_broker(@broker1) }
38
+ end
39
+
40
+ end
41
+
@@ -0,0 +1,113 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
+
3
+ require 'test/unit'
4
+ require 'net/http'
5
+
6
+ require 'rhcp'
7
+
8
+ class HttpExporterTest < Test::Unit::TestCase
9
+
10
+
11
+ def self.suite
12
+ puts "gonna setup"
13
+ broker = RHCP::Broker.new()
14
+ broker.register_command(RHCP::Command.new("test", "just a test command", lambda { |req,res| "testing" }))
15
+ broker.register_command(
16
+ RHCP::Command.new("reverse", "reversing input strings",
17
+ lambda { |req,res| req.get_param_value("input").reverse }
18
+ ).add_param(RHCP::CommandParam.new("input", "the string to reverse", {
19
+ :lookup_method => lambda { [ "zaphod", "beeblebrox" ] }
20
+ }))
21
+ )
22
+
23
+ @@broker = broker
24
+
25
+ # TODO test other setup options
26
+ @@exporter = RHCP::HttpExporter.new(broker, :port => 42000)
27
+ @@exporter.start()
28
+ super
29
+ end
30
+
31
+ def setup
32
+ @url = URI.parse("http://localhost:42000")
33
+ end
34
+
35
+ def teardown
36
+ #@@exporter.stop
37
+ end
38
+
39
+ def test_available
40
+ res = Net::HTTP.new(@url.host, @url.port).start { |http| http.get("/rhcp/") }
41
+ assert_equal "200", res.code
42
+ end
43
+
44
+ # def test_info
45
+ # sleep 2
46
+ # res = Net::HTTP.new(@url.host, @url.port).start { |http| http.get("/rhcp/info/index2.html") }
47
+ #
48
+ # puts res.body
49
+ # assert_equal "200", res.code
50
+ # end
51
+
52
+ def test_get_commands
53
+ res = Net::HTTP.new(@url.host, @url.port).start { |http| http.get("/rhcp/get_commands") }
54
+
55
+ assert_equal "200", res.code
56
+ commands = JSON.parse(res.body)
57
+
58
+ # +commands+ is an array => convert it back to a has
59
+ command_hash = Hash.new()
60
+ commands.each do |command_string|
61
+ command = RHCP::Client::CommandStub.reconstruct_from_json(command_string)
62
+ command_hash[command.name] = command
63
+ end
64
+
65
+ assert_equal [ "test", "reverse" ].sort, command_hash.keys.sort
66
+ assert_kind_of RHCP::Command, command_hash["reverse"]
67
+ end
68
+
69
+ def test_get_lookup_values
70
+ res = Net::HTTP.new(@url.host, @url.port).start { |http| http.get("/rhcp/get_lookup_values?command=reverse&param=input") }
71
+ puts res.body
72
+ assert_equal "200", res.code
73
+ lookups = JSON.parse(res.body)
74
+ assert_equal [ "zaphod", "beeblebrox" ].sort, lookups.sort
75
+ end
76
+
77
+ def test_get_lookup_values_invalid_command
78
+ res = Net::HTTP.new(@url.host, @url.port).start { |http| http.get("/rhcp/get_lookup_values?command=do_the_twist") }
79
+ puts "error response #{res.body}"
80
+ assert_equal "500", res.code
81
+ result = JSON.parse(res.body)
82
+ assert(/^no such command/.match(result[0]))
83
+ end
84
+
85
+ def test_get_lookup_values_without_params
86
+ res = Net::HTTP.new(@url.host, @url.port).start { |http| http.get("/rhcp/get_lookup_values") }
87
+ puts "error response #{res.body}"
88
+ assert_equal "500", res.code
89
+ end
90
+
91
+ def test_get_lookup_values_partial
92
+ res = Net::HTTP.new(@url.host, @url.port).start { |http| http.get("/rhcp/get_lookup_values?command=reverse&param=input&partial=zap") }
93
+ puts res.body
94
+ assert_equal "200", res.code
95
+ lookups = JSON.parse(res.body)
96
+ assert_equal [ "zaphod" ].sort, lookups.sort
97
+ end
98
+
99
+ def test_execute
100
+ request = RHCP::Request.new(@@broker.get_command("reverse"), {
101
+ "input" => [ "zaphod" ]
102
+ })
103
+
104
+ res = Net::HTTP.new(@url.host, @url.port).start { |http| http.post("/rhcp/execute", request.to_json) }
105
+ puts "execute response #{res.body}"
106
+ assert_equal "200", res.code
107
+ end
108
+
109
+ def test_execute_fails
110
+ # TODO write test
111
+ end
112
+
113
+ end
@@ -0,0 +1,73 @@
1
+ #
2
+ # To change this template, choose Tools | Templates
3
+ # and open the template in the editor.
4
+
5
+
6
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
7
+
8
+ require 'test/unit'
9
+ require 'rhcp'
10
+
11
+ class HttpBrokerTest < Test::Unit::TestCase
12
+
13
+ def self.suite
14
+ # setup an http exporter we can connect to
15
+ puts "gonna setup"
16
+ broker = RHCP::Broker.new()
17
+ @@command_test = RHCP::Command.new("test", "just a test command", lambda { |req,res| "testing" })
18
+ broker.register_command @@command_test
19
+
20
+ @@command_reverse = RHCP::Command.new("reverse", "reversing input strings",
21
+ lambda { |req,res|
22
+ req.get_param_value("input").reverse
23
+ }
24
+ ).add_param(RHCP::CommandParam.new("input", "the string to reverse",
25
+ {
26
+ :lookup_method => lambda { [ "zaphod", "beeblebrox" ] }
27
+ }
28
+ )
29
+ )
30
+ broker.register_command @@command_reverse
31
+
32
+ @@exporter = RHCP::HttpExporter.new(broker, :port => 42001)
33
+ @@exporter.start()
34
+ super
35
+ end
36
+
37
+ def setup
38
+ url = URI.parse("http://localhost:42001")
39
+ @http_broker = RHCP::Client::HttpBroker.new(url)
40
+ assert_not_nil @http_broker
41
+ end
42
+
43
+ def test_get_commands
44
+ commands = @http_broker.get_command_list
45
+ assert_not_nil commands
46
+ assert 2, commands.size
47
+ assert @@command_test, commands["test"]
48
+ assert @@command_reverse, commands["reverse"]
49
+ puts "command reverse : >>#{commands["reverse"]}<<"
50
+ end
51
+
52
+ # the CommandParam should be wrapped in a way that we can retrieve lookup values
53
+ def test_get_lookup_values
54
+ command = @http_broker.get_command_list["reverse"]
55
+ param = command.get_param("input")
56
+ assert_equal [ "zaphod", "beeblebrox" ], param.get_lookup_values()
57
+ end
58
+
59
+ def test_execute
60
+ command = @http_broker.get_command_list["reverse"]
61
+ assert_instance_of RHCP::Client::CommandStub, command
62
+ collected_params = {
63
+ "input" => [ "zaphod" ]
64
+ }
65
+ request = RHCP::Request.new(command, collected_params)
66
+ response = command.execute_request(request)
67
+ assert_not_nil response
68
+ assert_equal RHCP::Response::Status::OK, response.status
69
+ assert_equal "dohpaz", response.data
70
+ end
71
+
72
+
73
+ end