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.
@@ -1,113 +0,0 @@
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
@@ -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
@@ -1,47 +0,0 @@
1
- $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
-
3
- require 'rhcp'
4
-
5
- require 'net/http'
6
- require 'logger'
7
-
8
- $logger = Logger.new($stdout)
9
-
10
- broker = RHCP::Broker.new()
11
- broker.register_command(RHCP::Command.new("test", "just a test command", lambda { |req,res| "testing" }))
12
- broker.register_command(
13
- RHCP::Command.new("reverse", "reversing input strings",
14
- lambda { |req,res| req.get_param_value("input").reverse }
15
- ).add_param(RHCP::CommandParam.new("input", "the string to reverse", {
16
- :lookup_method => lambda { [ "zaphod", "beeblebrox" ] }
17
- }))
18
- )
19
- broker.register_command RHCP::Command.new("cook", "cook something nice out of some ingredients",
20
- lambda { |req,res|
21
- ingredients = req.get_param_value("ingredient").join(" ")
22
- puts "cooking something with #{ingredients}"
23
- ingredients
24
- }
25
- ).add_param(RHCP::CommandParam.new("ingredient", "something to cook with",
26
- {
27
- :lookup_method => lambda { [ "mascarpone", "chocolate", "eggs", "butter", "marzipan" ] },
28
- :allows_multiple_values => true,
29
- :mandatory => true
30
- }
31
- )
32
- )
33
-
34
- exporter = RHCP::HttpExporter.new(broker, :port => 42000)
35
-
36
- trap("INT") {
37
- exporter.stop
38
- Kernel.exit 0
39
- }
40
-
41
- $logger.info "launching http exporter..."
42
- exporter.start()
43
- while (true) do
44
- sleep 30
45
- puts "."
46
- end
47
- $logger.info "exiting"
@@ -1,124 +0,0 @@
1
- $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
-
3
- require 'test/unit'
4
- require 'rhcp'
5
-
6
- require 'rubygems'
7
- require 'json'
8
-
9
- class RequestTest < Test::Unit::TestCase
10
-
11
- def test_construction
12
- command = RHCP::Command.new("test", "a test", nil)
13
- assert_raise(RHCP::RhcpException) { RHCP::Request.new(nil, Hash.new()) }
14
- request = RHCP::Request.new(command, Hash.new())
15
- assert_not_nil request
16
- end
17
-
18
- def test_get_param_value
19
- command = RHCP::Command.new("test", "a test", nil)
20
- command.add_param(RHCP::CommandParam.new("first_param", "this is the first param"))
21
- command.add_param(RHCP::CommandParam.new("second_param", "this is the second param", { :allows_multiple_values => true }))
22
- request = RHCP::Request.new(
23
- command,
24
- {
25
- "first_param" => ["foo"],
26
- "second_param" => ["foo", "bar", "baz"]
27
- }
28
- )
29
- assert_not_nil request
30
- assert_equal "foo", request.get_param_value("first_param")
31
- assert_equal ["foo", "bar", "baz"], request.get_param_value("second_param")
32
- end
33
-
34
- def test_has_param_value
35
- command = RHCP::Command.new("test", "a test", nil)
36
- command.add_param(RHCP::CommandParam.new("first_param", "this is the first param"))
37
- command.add_param(RHCP::CommandParam.new("second_param", "this is the second param", { :allows_multiple_values => true }))
38
- request = RHCP::Request.new(
39
- command,
40
- {
41
- "first_param" => ["foo"],
42
- "second_param" => ["foo", "bar", "baz"]
43
- }
44
- )
45
- assert request.has_param_value("first_param")
46
- assert ! request.has_param_value("third_param")
47
- end
48
-
49
- def test_invalid_param_name
50
- command = RHCP::Command.new("test", "another test", lambda {})
51
- command.add_param(RHCP::CommandParam.new("real_param", "this param does exist"))
52
- assert_not_nil RHCP::Request.new(command, { "real_param" => [ "value for the real param" ] })
53
- assert_raise(RHCP::RhcpException) { RHCP::Request.new(command, {
54
- "does_not_exist" => [ "single value for non-existing param" ]
55
- }) }
56
- end
57
-
58
- def test_invalid_param_values
59
- command = RHCP::Command.new("test", "another test", lambda {})
60
- command.add_param(RHCP::CommandParam.new("real_param", "this param does exist"))
61
- assert_raise(RHCP::RhcpException) { RHCP::Request.new(command, {
62
- "real_param" => [ "value for the real param", "another value for the real param" ]
63
- }) }
64
- end
65
-
66
- def test_missing_mandatory_param
67
- command = RHCP::Command.new("test", "another test", lambda {})
68
- command.add_param(RHCP::CommandParam.new("first_param", "this is the first param", { :mandatory => true }))
69
- command.add_param(RHCP::CommandParam.new("second_param", "this param is optional", { :mandatory => false }))
70
-
71
- # test1 : we should not have to specify optional parameters
72
- assert_not_nil RHCP::Request.new(command, { "first_param" => [ "bla" ]})
73
-
74
- # test2 : but we need to specify mandatory params
75
- assert_raise(RHCP::RhcpException) { RHCP::Request.new(command, {})}
76
- end
77
-
78
- def test_json
79
- command = RHCP::Command.new("request_test", "a test", nil)
80
- command.add_param(RHCP::CommandParam.new("first_param", "this is the first param"))
81
- command.add_param(RHCP::CommandParam.new("second_param", "this is the second param", { :allows_multiple_values => true }))
82
- broker = RHCP::Broker.new()
83
- broker.register_command(command)
84
- r= RHCP::Request.new(
85
- command,
86
- {
87
- "first_param" => ["foo"],
88
- "second_param" => ["foo", "bar", "baz"]
89
- }
90
- )
91
- json = r.to_json
92
- puts "request as JSON : >>#{json}<<"
93
- assert_not_nil json
94
- r2 = RHCP::Request.reconstruct_from_json(broker, json)
95
- assert_instance_of RHCP::Request, r2
96
- assert_equal r.command, r2.command
97
- assert_equal r.param_values, r2.param_values
98
- end
99
-
100
- def test_execute
101
- command = RHCP::Command.new("request_test", "a test", lambda { |req,res| "jehova" })
102
- r= RHCP::Request.new(
103
- command
104
- )
105
- res = r.execute
106
- assert_not_nil res
107
- assert_equal "jehova", res.data
108
- end
109
-
110
- def test_execute_with_params
111
- command = RHCP::Command.new("request_test", "a test", lambda { |req,res| "***" + req.get_param_value("first_one") + "***" }
112
- ).add_param(RHCP::CommandParam.new("first_one", "pole position"))
113
- r= RHCP::Request.new(
114
- command,
115
- {
116
- "first_one" => "lucky bastard"
117
- }
118
- )
119
- res = r.execute()
120
- assert_not_nil res
121
- assert_equal "***lucky bastard***", res.data
122
- end
123
-
124
- end
@@ -1,45 +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/response'
10
-
11
- class ResponseTest < Test::Unit::TestCase
12
-
13
- def test_simple
14
- response = RHCP::Response.new()
15
- assert_not_nil response
16
- response.mark_as_error("something went wrong")
17
- assert_equal "something went wrong", response.error_text
18
- assert_equal "", response.error_detail
19
- end
20
-
21
- def test_payload
22
- response = RHCP::Response.new()
23
- assert_not_nil response
24
- response.set_payload("some data")
25
- assert_equal "some data", response.data
26
- end
27
-
28
- def test_json
29
- response = RHCP::Response.new()
30
- response.set_payload("some data")
31
- response.mark_as_error("something went wrong", "things got really fucked up")
32
-
33
- json = response.to_json()
34
- assert_not_nil json
35
- r2 = RHCP::Response.reconstruct_from_json(json)
36
- #r2 = JSON.parse(json)
37
- assert_not_nil r2
38
- assert_instance_of RHCP::Response, r2
39
- assert_equal RHCP::Response::Status::ERROR, r2.status
40
- assert_equal "something went wrong", r2.error_text
41
- assert_equal "things got really fucked up", r2.error_detail
42
- assert_equal "some data", r2.data
43
- end
44
-
45
- end