rhcp 0.1.9 → 0.2.14

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,25 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'..','..','lib')
2
+
3
+ require 'test/unit'
4
+ require 'logger'
5
+ require 'mocha'
6
+
7
+ $logger = Logger.new(STDOUT)
8
+
9
+ class TestBase < Test::Unit::TestCase
10
+
11
+ def test_fine
12
+ $logger.debug "There's absolutely no cause for alarm."
13
+ end
14
+
15
+ def read_file(filename)
16
+ file_name = File.dirname(__FILE__) + '/data/' + filename
17
+ File.new(file_name).read
18
+ end
19
+
20
+ def read_yaml(filename)
21
+ file_name = File.dirname(__FILE__) + '/data/' + filename
22
+ YAML::load(File.open(file_name))
23
+ end
24
+
25
+ end
@@ -0,0 +1,186 @@
1
+ module TestsForBrokers
2
+
3
+ def test_command_list_with_context
4
+ command = RHCP::Command.new("test", "a test command that is enabled for hosts only", lambda {})
5
+ command.enabled_through_context_keys = ['host']
6
+ @broker.register_command command
7
+ command2 = RHCP::Command.new("test2", "a test command should always be enabled", lambda {})
8
+ @broker.register_command command2
9
+
10
+ assert_equal 1, @test_broker.get_command_list().size()
11
+ context = RHCP::Context.new()
12
+ context.cookies['host'] = 'deepthought'
13
+ assert_equal 2, @test_broker.get_command_list(context).size()
14
+ end
15
+
16
+ # it should not be possible to call get_command() for a command that is
17
+ # disabled through the context
18
+ def test_get_command_with_context
19
+ command = RHCP::Command.new("test", "a test command that is enabled for hosts only", lambda {})
20
+ command.enabled_through_context_keys = ['host']
21
+ @broker.register_command command
22
+ command2 = RHCP::Command.new("test2", "a test command should always be enabled", lambda {})
23
+ @broker.register_command command2
24
+
25
+ assert @test_broker.get_command("test2")
26
+ assert_raise(RHCP::RhcpException) {
27
+ @test_broker.get_command("test")
28
+ }
29
+ context = RHCP::Context.new()
30
+ context.cookies['host'] = 'deepthought'
31
+ assert @test_broker.get_command("test", context)
32
+ end
33
+
34
+ def test_get_lookup_values
35
+ command = RHCP::Command.new("test", "a test command with lookup values", lambda {})
36
+ command.add_param RHCP::CommandParam.new("first_param", "first param",
37
+ {
38
+ :is_default_param => true,
39
+ :lookup_method => lambda {
40
+ [ "wind", "sun", "boat" ]
41
+ }
42
+ }
43
+ )
44
+ command.add_param RHCP::CommandParam.new("second_param", "this param's lookup values depend on the first param",
45
+ {
46
+ :lookup_method => lambda { |request|
47
+ $logger.debug "in lookup : #{request}"
48
+ if request.has_param_value("first_param")
49
+ request.get_param_value("first_param").map do |item|
50
+ item.reverse
51
+ end
52
+ else
53
+ [ "wind", "sun", "boat" ]
54
+ end
55
+ }
56
+ }
57
+ )
58
+ command.add_param RHCP::CommandParam.new("host", "host dummy param",
59
+ {
60
+ :mandatory => false,
61
+ :autofill_context_key => "host"
62
+ }
63
+ )
64
+ command.add_param RHCP::CommandParam.new("third_param", "this param's lookup values depend on context",
65
+ {
66
+ :lookup_method => lambda { |request|
67
+ result = []
68
+ if request.has_param_value("host")
69
+ 1.upto(3) do |loop|
70
+ result << request.get_param_value("host") + "_service#{loop}"
71
+ end
72
+ else
73
+ result = [ "wind", "sun", "boat" ]
74
+ end
75
+ result
76
+ }
77
+ }
78
+ )
79
+ @broker.register_command command
80
+
81
+ # no values collected so far, no context
82
+ request = RHCP::Request.new(command, { })
83
+ assert_equal [ "wind", "sun", "boat" ].sort, @test_broker.get_lookup_values(request, "first_param").sort
84
+ assert_equal [ "wind", "sun", "boat" ].sort, @test_broker.get_lookup_values(request, "second_param").sort
85
+ # lookup values can depend on other params' values
86
+ # context = RHCP::Context.new()
87
+ # context.collected_values["first_param"] = "zaphod"
88
+ request = RHCP::Request.new(command, { "first_param" => "wind" })
89
+ assert_equal [ "dniw" ], @test_broker.get_lookup_values(request, "second_param")
90
+ # or on the context
91
+ request = RHCP::Request.new(command, {}, RHCP::Context.new({"host" => "endeavour"}))
92
+ assert_equal [ "endeavour_service1", "endeavour_service2", "endeavour_service3" ].sort,
93
+ @test_broker.get_lookup_values(request, "third_param").sort
94
+ end
95
+
96
+ def test_param_is_valid
97
+ command = RHCP::Command.new("test", "a test command with lookup values", lambda {})
98
+ command.add_param RHCP::CommandParam.new("first_param", "first param",
99
+ {
100
+ :is_default_param => true,
101
+ :lookup_method => lambda {
102
+ [ "wind", "sun", "boat" ]
103
+ }
104
+ }
105
+ )
106
+ @broker.register_command command
107
+
108
+ request = RHCP::Request.new(command, { })
109
+ assert @test_broker.check_param_is_valid(request, "first_param", [ "wind" ])
110
+ assert_raise(RHCP::RhcpException) {
111
+ @test_broker.check_param_is_valid(request, "first_param", ["zaphod"])
112
+ }
113
+ end
114
+
115
+ def test_param_is_valid_without_lookup_values
116
+ command = RHCP::Command.new("test", "a test command with lookup values", lambda {})
117
+ command.add_param RHCP::CommandParam.new("first_param", "first param",
118
+ {
119
+ :is_default_param => true,
120
+ }
121
+ )
122
+ @broker.register_command command
123
+
124
+ request = RHCP::Request.new(command, { })
125
+ assert @test_broker.check_param_is_valid(request, "first_param", [ "wind" ])
126
+ assert @test_broker.check_param_is_valid(request, "first_param", ["zaphod"])
127
+ end
128
+
129
+ def test_mandatory_prefilled_params
130
+ command = RHCP::Command.new("test_duplicate", "command for testing param duplicates", lambda {})
131
+ command.add_param(RHCP::CommandParam.new("first_param", "first param", {
132
+ :mandatory => false
133
+ }))
134
+ command.add_param(RHCP::CommandParam.new("second_param", "second param", {
135
+ :mandatory => true
136
+ }))
137
+ command.add_param(RHCP::CommandParam.new("third_param", "third param", {
138
+ :mandatory => true,
139
+ :autofill_context_key => 'dessert'
140
+ }))
141
+ @broker.register_command command
142
+
143
+ assert_equal ["second_param", "third_param"], @test_broker.get_mandatory_params("test_duplicate").map { |param| param.name }.sort()
144
+ context = RHCP::Context.new()
145
+ context.cookies['dessert'] = 'mascarpone'
146
+ context.cookies['host'] = 'deepthought'
147
+ assert_equal ["second_param"], @test_broker.get_mandatory_params("test_duplicate", context).map { |param| param.name }.sort()
148
+ context.cookies.delete('dessert') #sigh
149
+ #assert_equal ["second_param", "third_param"], command.get_mandatory_params(context).map { |param| param.name }.sort()
150
+ assert_equal ["second_param", "third_param"], @test_broker.get_mandatory_params("test_duplicate", context).map { |param| param.name }.sort()
151
+ end
152
+
153
+ def test_execute
154
+ command = RHCP::Command.new("test", "a command for testing", lambda { |request,response|
155
+ first_param = request.get_param_value("first_param")
156
+ puts "just testing : #{first_param}"
157
+ first_param.reverse
158
+ })
159
+ command.add_param(RHCP::CommandParam.new("first_param", "this is the first param"))
160
+ @broker.register_command command
161
+
162
+ request = RHCP::Request.new(command, {"first_param" => "thing"})
163
+ response = @test_broker.execute(request)
164
+ assert_equal "thing".reverse, response.data
165
+ end
166
+
167
+ def test_execute_with_context
168
+ command = RHCP::Command.new("testcontext", "a command for context testing", lambda { |request,response|
169
+ first_param = request.get_param_value("first_param")
170
+ puts "just testing : #{first_param}"
171
+ response.set_context({'end' => 'happy'})
172
+ first_param.reverse
173
+ })
174
+ command.add_param(RHCP::CommandParam.new("first_param", "this is the first param", {
175
+ :mandatory => true,
176
+ :autofill_context_key => 'spooky'
177
+ }))
178
+ @broker.register_command command
179
+
180
+ context = RHCP::Context.new({'spooky' => 'coincidence'})
181
+ request = RHCP::Request.new(command, {}, context)
182
+ response = @test_broker.execute(request)
183
+ assert_equal "coincidence".reverse, response.data
184
+ end
185
+
186
+ end
@@ -0,0 +1,22 @@
1
+ module TestsForWritableBrokers
2
+
3
+ def test_register_commands
4
+ assert_not_nil @broker
5
+ commands = @broker.get_command_list
6
+ assert_not_nil commands
7
+ assert_equal 0, commands.size
8
+
9
+ command = RHCP::Command.new("test", "a test command", lambda {})
10
+ @broker.register_command(command)
11
+
12
+ commands = @test_broker.get_command_list
13
+ assert_equal 1, commands.size
14
+ assert_equal command, commands["test"]
15
+ end
16
+
17
+ def test_register_duplicate
18
+ @broker.register_command RHCP::Command.new("test", "a test command", lambda {})
19
+ assert_raise(RHCP::RhcpException) { @broker.register_command RHCP::Command.new("test", "a command with the same name", lambda {}) }
20
+ end
21
+
22
+ end
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rhcp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ hash: 11
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 14
10
+ version: 0.2.14
5
11
  platform: ruby
6
12
  authors:
7
13
  - Philipp Traeder
@@ -9,19 +15,25 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2009-02-21 00:00:00 +01:00
18
+ date: 2011-07-26 00:00:00 +00:00
13
19
  default_executable:
14
20
  dependencies:
15
21
  - !ruby/object:Gem::Dependency
16
22
  name: json
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
20
26
  requirements:
21
27
  - - ">="
22
28
  - !ruby/object:Gem::Version
29
+ hash: 31
30
+ segments:
31
+ - 0
32
+ - 0
33
+ - 0
23
34
  version: 0.0.0
24
- version:
35
+ type: :runtime
36
+ version_requirements: *id001
25
37
  description:
26
38
  email: philipp at hitchhackers.net
27
39
  executables:
@@ -32,58 +44,72 @@ extra_rdoc_files: []
32
44
 
33
45
  files:
34
46
  - bin/rhcp_test_server
35
- - lib/rhcp
36
- - lib/rhcp/client
37
- - lib/rhcp/client/http_broker.rb
38
- - lib/rhcp/client/command_param_stub.rb
39
- - lib/rhcp/client/command_stub.rb
40
- - lib/rhcp/command.rb
41
- - lib/rhcp/command_param.rb
42
- - lib/rhcp/response.rb
43
47
  - lib/rhcp/dispatching_broker.rb
48
+ - lib/rhcp/context.rb
49
+ - lib/rhcp/memcached_broker.rb
50
+ - lib/rhcp/logging_broker.rb
44
51
  - lib/rhcp/broker.rb
52
+ - lib/rhcp/response.rb
53
+ - lib/rhcp/client/command_stub.rb
54
+ - lib/rhcp/client/command_param_stub.rb
55
+ - lib/rhcp/client/http_broker.rb
56
+ - lib/rhcp/client/context_aware_broker.rb
57
+ - lib/rhcp/command.rb
45
58
  - lib/rhcp/http_exporter.rb
46
- - lib/rhcp/request.rb
47
59
  - lib/rhcp/rhcp_exception.rb
60
+ - lib/rhcp/command_param.rb
61
+ - lib/rhcp/request.rb
48
62
  - lib/rhcp.rb
49
- - test/rhcp
50
- - test/rhcp/client
63
+ - test/rhcp/broker_test.rb
64
+ - test/rhcp/test_base.rb
65
+ - test/rhcp/http_exporter_test.rb
66
+ - test/rhcp/http_test_server.rb
67
+ - test/rhcp/response_test.rb
68
+ - test/rhcp/command_test.rb
69
+ - test/rhcp/tests_for_brokers.rb
70
+ - test/rhcp/request_test.rb
51
71
  - test/rhcp/client/command_param_stub_test.rb
52
72
  - test/rhcp/client/command_stub_test.rb
73
+ - test/rhcp/http_broker_test.rb
74
+ - test/rhcp/logging_broker_test.rb
75
+ - test/rhcp/context_test.rb
53
76
  - test/rhcp/command_param_test.rb
54
- - test/rhcp/command_test.rb
55
- - test/rhcp/response_test.rb
77
+ - test/rhcp/tests_for_writable_brokers.rb
56
78
  - test/rhcp/dispatching_broker_test.rb
57
- - test/rhcp/broker_test.rb
58
- - test/rhcp/http_test_server.rb
59
- - test/rhcp/http_exporter_test.rb
60
- - test/rhcp/http_registry_test.rb
61
- - test/rhcp/request_test.rb
79
+ - test/rhcp/context_aware_broker_test.rb
62
80
  has_rdoc: true
63
81
  homepage: http://rubyforge.org/projects/rhcp
82
+ licenses: []
83
+
64
84
  post_install_message:
65
85
  rdoc_options: []
66
86
 
67
87
  require_paths:
68
88
  - lib
69
89
  required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
70
91
  requirements:
71
92
  - - ">="
72
93
  - !ruby/object:Gem::Version
94
+ hash: 3
95
+ segments:
96
+ - 0
73
97
  version: "0"
74
- version:
75
98
  required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
76
100
  requirements:
77
101
  - - ">="
78
102
  - !ruby/object:Gem::Version
103
+ hash: 3
104
+ segments:
105
+ - 0
79
106
  version: "0"
80
- version:
81
107
  requirements: []
82
108
 
83
109
  rubyforge_project: rhcp
84
- rubygems_version: 1.3.1
110
+ rubygems_version: 1.3.7
85
111
  signing_key:
86
- specification_version: 2
112
+ specification_version: 3
87
113
  summary: RHCP is a protocol designed for building up a command-metadata-based communication infrastructure making it easier for application developers to export commands in applications to generic clients.
88
114
  test_files: []
89
115
 
@@ -1,73 +0,0 @@
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