rhcp 0.1.2 → 0.1.3

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.
data/lib/rhcp/response.rb DELETED
@@ -1,62 +0,0 @@
1
- require 'rubygems'
2
- require 'json'
3
-
4
- module RHCP
5
-
6
- class Response
7
-
8
- class Status
9
- OK="ok"
10
- ERROR="error"
11
- end
12
-
13
- # TODO these should be attr_reader's, but then we've got to solve json_create() differently
14
- attr_accessor :status
15
- attr_accessor :error_text
16
- attr_accessor :error_detail
17
- attr_accessor :data
18
-
19
- # textual description of the result (optional)
20
- attr_accessor :result_text
21
-
22
- def initialize
23
- @status = Status::OK
24
- @error_text = ""
25
- @error_detail = ""
26
- @result_text = "";
27
- end
28
-
29
- def mark_as_error(text, detail="")
30
- @status = Status::ERROR
31
- @error_text = text
32
- @error_detail = detail
33
- end
34
-
35
- def set_payload(data)
36
- @data = data
37
- end
38
-
39
- def self.reconstruct_from_json(json_data)
40
- object = JSON.parse(json_data)
41
- instance = self.new()
42
- instance.status = object["status"]
43
- instance.error_text = object["error_text"]
44
- instance.error_detail = object["error_detail"]
45
- instance.set_payload(object["data"])
46
- instance.result_text = object['result_text']
47
- instance
48
- end
49
-
50
- def to_json(*args)
51
- {
52
- 'status' => @status,
53
- 'error_text' => @error_text,
54
- 'error_detail' => @error_detail,
55
- 'data' => @data, # TODO what about JSONinification of data? (probably data should be JSON-ish data only, i.e. no special objects)
56
- 'result_text' => @result_text
57
- }.to_json(*args)
58
- end
59
-
60
- end
61
-
62
- end
@@ -1,6 +0,0 @@
1
- module RHCP
2
-
3
- class RhcpException < Exception
4
- end
5
-
6
- end
@@ -1,29 +0,0 @@
1
- $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
-
3
- require 'test/unit'
4
-
5
- require 'rhcp'
6
-
7
- class BrokerTest < Test::Unit::TestCase
8
-
9
- def test_register_commands
10
- broker = RHCP::Broker.new()
11
- assert_not_nil broker
12
- commands = broker.get_command_list
13
- assert_not_nil commands
14
- assert_equal 0, commands.size
15
-
16
- command = RHCP::Command.new("test", "a test command", lambda {})
17
- broker.register_command(command)
18
- commands = broker.get_command_list
19
- assert_equal 1, commands.size
20
- assert_equal command, commands["test"]
21
- end
22
-
23
- def test_register_duplicate
24
- broker = RHCP::Broker.new()
25
- broker.register_command RHCP::Command.new("test", "a test command", lambda {})
26
- assert_raise(RHCP::RhcpException) { broker.register_command RHCP::Command.new("test", "a command with the same name", lambda {}) }
27
- end
28
-
29
- end
@@ -1,73 +0,0 @@
1
- $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
-
3
- require 'test/unit'
4
- require 'rhcp/client/command_param_stub'
5
- require 'rhcp/command_param'
6
-
7
- class CommandParamStubTest < Test::Unit::TestCase
8
-
9
- def param_lookup
10
- ["foo", "bar", "baz"]
11
- end
12
-
13
- def setup
14
- @p = RHCP::CommandParam.new("test", "this param is used for testing purposes only",
15
- :mandatory => true,
16
- :allows_multiple_values => true,
17
- :lookup_method => self.method(:param_lookup),
18
- :is_default_param => true
19
- )
20
- end
21
-
22
- def test_json
23
- json = @p.to_json
24
- puts "json : >>#{json}<<"
25
- p2 = RHCP::Client::CommandParamStub.reconstruct_from_json(json)
26
- assert_not_nil p2
27
- assert_instance_of RHCP::Client::CommandParamStub, p2
28
- assert_equal @p.name, p2.name
29
- assert_equal @p.description, p2.description
30
- assert_equal @p.allows_multiple_values, p2.allows_multiple_values
31
- assert_equal @p.has_lookup_values, p2.has_lookup_values
32
- assert_equal @p.is_default_param, p2.is_default_param
33
- assert_equal @p.mandatory, p2.mandatory
34
-
35
- json_hash = JSON.parse(json)
36
- p3 = RHCP::Client::CommandParamStub.reconstruct_from_json(json_hash)
37
- assert_instance_of RHCP::Client::CommandParamStub, p3
38
- assert_equal @p.name, p3.name
39
- assert_equal @p.description, p3.description
40
- assert_equal @p.allows_multiple_values, p3.allows_multiple_values
41
- end
42
-
43
- # when a param is "stubbed", it should be possible to inject a method
44
- # for retrieving the lookup values
45
- def test_stubbing
46
- json = @p.to_json
47
- stub = RHCP::Client::CommandParamStub.reconstruct_from_json(json)
48
- stub.get_lookup_values_block = lambda {
49
- |partial_value|
50
- [ "mascarpone", "limoncello" ]
51
- }
52
- lookup_values = stub.get_lookup_values()
53
- assert_equal [ "mascarpone", "limoncello" ], lookup_values
54
- end
55
-
56
- def test_stubbing_without_lookup_values
57
- p = RHCP::CommandParam.new("test", "without lookup values",
58
- :mandatory => true,
59
- :allows_multiple_values => true,
60
- :is_default_param => true
61
- )
62
- stub = RHCP::Client::CommandParamStub.reconstruct_from_json(p.to_json())
63
- has_been_invoked = false
64
- stub.get_lookup_values_block = lambda {
65
- |partial_value|
66
- has_been_invoked = true
67
- }
68
- stub.get_lookup_values()
69
- assert_equal false, has_been_invoked
70
- end
71
-
72
-
73
- end
@@ -1,40 +0,0 @@
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
- json = c.to_json
23
- puts "JSON : >>#{JSON.pretty_generate(c)}<<"
24
- assert_not_nil json
25
- c2 = RHCP::Client::CommandStub.reconstruct_from_json(json)
26
- assert_not_nil c2
27
- assert_instance_of RHCP::Client::CommandStub, c2
28
- assert_equal c.name, c2.name
29
- assert_equal c.description, c2.description
30
- assert_equal c.params.size, c2.params.size
31
-
32
- json_hash = JSON.parse(json)
33
- c3 = RHCP::Client::CommandStub.reconstruct_from_json(json_hash)
34
- assert_instance_of RHCP::Client::CommandStub, c3
35
- assert_equal c.name, c3.name
36
- assert_equal c.description, c3.description
37
- assert_equal c.params.size, c3.params.size
38
- end
39
-
40
- end
@@ -1,72 +0,0 @@
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
@@ -1,136 +0,0 @@
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
-
@@ -1,41 +0,0 @@
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
-