schemata-dea 0.0.1.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. data/lib/schemata/common/error.rb +12 -0
  2. data/lib/schemata/common/msgbase.rb +281 -0
  3. data/lib/schemata/common/msgtypebase.rb +144 -0
  4. data/lib/schemata/common/parsed_msg.rb +43 -0
  5. data/lib/schemata/dea/advertise_message/advertise_message_v1.rb +40 -0
  6. data/lib/schemata/dea/advertise_message.rb +13 -0
  7. data/lib/schemata/dea/dea_status_response/dea_status_response_v1.rb +48 -0
  8. data/lib/schemata/dea/dea_status_response.rb +13 -0
  9. data/lib/schemata/dea/discover_request/discover_request_v1.rb +93 -0
  10. data/lib/schemata/dea/discover_request.rb +13 -0
  11. data/lib/schemata/dea/droplet_status_response/droplet_status_response_v1.rb +38 -0
  12. data/lib/schemata/dea/droplet_status_response.rb +13 -0
  13. data/lib/schemata/dea/exit_message/exit_message_v1.rb +48 -0
  14. data/lib/schemata/dea/exit_message.rb +13 -0
  15. data/lib/schemata/dea/find_droplet_request/find_droplet_request_v1.rb +52 -0
  16. data/lib/schemata/dea/find_droplet_request.rb +13 -0
  17. data/lib/schemata/dea/find_droplet_response/find_droplet_response_v1.rb +107 -0
  18. data/lib/schemata/dea/find_droplet_response.rb +13 -0
  19. data/lib/schemata/dea/heartbeat_response/heartbeat_response_v1.rb +58 -0
  20. data/lib/schemata/dea/heartbeat_response.rb +13 -0
  21. data/lib/schemata/dea/hello_message/hello_message_v1.rb +40 -0
  22. data/lib/schemata/dea/hello_message.rb +13 -0
  23. data/lib/schemata/dea/start_request/start_request_v1.rb +141 -0
  24. data/lib/schemata/dea/start_request.rb +13 -0
  25. data/lib/schemata/dea/stop_request/stop_request_v1.rb +39 -0
  26. data/lib/schemata/dea/stop_request.rb +13 -0
  27. data/lib/schemata/dea/update_request/update_request_v1.rb +33 -0
  28. data/lib/schemata/dea/update_request.rb +13 -0
  29. data/lib/schemata/dea/version.rb +5 -0
  30. data/lib/schemata/dea.rb +64 -0
  31. data/lib/schemata/helpers/hash_copy.rb +28 -0
  32. data/lib/schemata/helpers/stringify.rb +26 -0
  33. data/spec/cloud_controller/cc_bar_spec.rb +140 -0
  34. data/spec/common/helpers_spec.rb +115 -0
  35. data/spec/common/parsed_msg_spec.rb +46 -0
  36. data/spec/component/aux_data_spec.rb +37 -0
  37. data/spec/component/component_bar_spec.rb +140 -0
  38. data/spec/component/component_foo_spec.rb +630 -0
  39. data/spec/component/foo_spec.rb +214 -0
  40. data/spec/dea/advertise_message_spec.rb +10 -0
  41. data/spec/dea/dea_spec.rb +6 -0
  42. data/spec/dea/dea_status_response_spec.rb +10 -0
  43. data/spec/dea/discover_request_spec.rb +10 -0
  44. data/spec/dea/droplet_status_response_spec.rb +10 -0
  45. data/spec/dea/exit_message_spec.rb +10 -0
  46. data/spec/dea/find_droplet_request_spec.rb +27 -0
  47. data/spec/dea/find_droplet_response_spec.rb +10 -0
  48. data/spec/dea/heartbeat_response_spec.rb +10 -0
  49. data/spec/dea/hello_message_spec.rb +10 -0
  50. data/spec/dea/start_request_spec.rb +10 -0
  51. data/spec/dea/stop_request_spec.rb +10 -0
  52. data/spec/dea/update_request_spec.rb +0 -0
  53. data/spec/router/register_request_spec.rb +10 -0
  54. data/spec/router/router_spec.rb +6 -0
  55. data/spec/router/start_message_spec.rb +10 -0
  56. data/spec/spec_helper.rb +1 -0
  57. data/spec/staging/staging_message_spec.rb +14 -0
  58. data/spec/staging/staging_spec.rb +6 -0
  59. data/spec/support/component_helpers.rb +51 -0
  60. data/spec/support/helpers.rb +54 -0
  61. data/spec/support/message_helpers.rb +138 -0
  62. data/spec/support/message_type_helpers.rb +171 -0
  63. metadata +234 -0
@@ -0,0 +1,214 @@
1
+ require 'schemata/component/foo'
2
+ require 'schemata/common/error'
3
+ require 'support/helpers'
4
+
5
+ describe Schemata::Component::Foo do
6
+ describe "V10" do
7
+ it "should define accessors for the schema" do
8
+ v10_obj = Schemata::Component::Foo::V10.new
9
+ v10_obj.respond_to?(:foo1).should be_true
10
+ v10_obj.respond_to?(:foo1=).should be_true
11
+ v10_obj.respond_to?(:foo2).should be_true
12
+ v10_obj.respond_to?(:foo2=).should be_true
13
+ end
14
+
15
+ it "should not raise an error if an incomplete but valid hash is given" do
16
+ v10_hash = {"foo1" => "foo"}
17
+ expect { Schemata::Component::Foo::V10.new(v10_hash) }.to_not raise_error
18
+ end
19
+
20
+ it "should raise an error if an incomplete and invalid hash is given" do
21
+ v10_hash = {"foo1" => 1}
22
+ expect {
23
+ Schemata::Component::Foo::V10.new(v10_hash)
24
+ }.to raise_error(Schemata::UpdateAttributeError)
25
+ end
26
+
27
+ it "should not raise an error if an attribute is correctly assigned" do
28
+ foo_obj = Schemata::Component.mock_foo 10
29
+ expect { foo_obj.foo1 = "new name" }.to_not raise_error
30
+ end
31
+
32
+ it "should raise an error if an attribute is incorrectly assigned" do
33
+ foo_obj = Schemata::Component.mock_foo 10
34
+ expect { foo_obj.foo1 = 1 }.to raise_error(Schemata::UpdateAttributeError)
35
+ end
36
+
37
+ it "should not raise an error if an incomplete msg_obj is updated \
38
+ correctly" do
39
+ v10_hash = {"foo1" => "foo"}
40
+ v10_obj = Schemata::Component::Foo::V10.new(v10_hash)
41
+ expect { v10_obj.foo1 = "new name" }.to_not raise_error
42
+ end
43
+
44
+ it "should raise an error if an incomplete msg_obj is updated \
45
+ incorrectly" do
46
+ v10_hash = {"foo1" => "foo"}
47
+ v10_obj = Schemata::Component::Foo::V10.new(v10_hash)
48
+ expect { v10_obj.foo1 = 1 }.to raise_error(Schemata::UpdateAttributeError)
49
+ end
50
+ end
51
+
52
+ describe "V11" do
53
+ it "should define accessors for the schema" do
54
+ v11_obj = Schemata::Component::Foo::V11.new
55
+ v11_obj.respond_to?(:foo1).should be_true
56
+ v11_obj.respond_to?(:foo1=).should be_true
57
+ v11_obj.respond_to?(:foo2).should be_true
58
+ v11_obj.respond_to?(:foo2=).should be_true
59
+ v11_obj.respond_to?(:foo3).should be_true
60
+ v11_obj.respond_to?(:foo3=).should be_true
61
+ end
62
+
63
+ it "should not raise an error if an incomplete but valid hash is given" do
64
+ v11_hash = {"foo1" => "foo"}
65
+ expect { Schemata::Component::Foo::V11.new(v11_hash) }.to_not raise_error
66
+ end
67
+
68
+ it "should raise an error if an incomplete and invalid hash is given" do
69
+ v11_hash = {"foo1" => 1}
70
+ expect {
71
+ v11_obj = Schemata::Component::Foo::V11.new(v11_hash)
72
+ }.to raise_error(Schemata::UpdateAttributeError)
73
+ end
74
+
75
+ it "should not raise an error if an attribute is correctly assigned" do
76
+ foo_obj = Schemata::Component.mock_foo 11
77
+ expect { foo_obj.foo2 = 10 }.to_not raise_error
78
+ end
79
+
80
+ it "should raise an error if an attribute is incorrectly assigned" do
81
+ foo_obj = Schemata::Component.mock_foo 11
82
+ expect {
83
+ foo_obj.foo2 = "foo"
84
+ }.to raise_error(Schemata::UpdateAttributeError)
85
+ end
86
+
87
+ it "should not raise an error if an incomplete msg_obj is updated \
88
+ correctly" do
89
+ v11_hash = {"foo1" => "foo"}
90
+ v11_obj = Schemata::Component::Foo::V11.new(v11_hash)
91
+ expect { v11_obj.foo1 = "new name" }.to_not raise_error
92
+ end
93
+
94
+ it "should raise an error if an incomplete msg_obj is updated \
95
+ incorrectly" do
96
+ v11_hash = {"foo1" => "foo"}
97
+ v11_obj = Schemata::Component::Foo::V11.new(v11_hash)
98
+ expect {
99
+ v11_obj.foo1 = 1
100
+ }.to raise_error(Schemata::UpdateAttributeError)
101
+ end
102
+ end
103
+
104
+ describe "V12" do
105
+ it "should define accessors for the schema" do
106
+ v12_obj = Schemata::Component::Foo::V12.new
107
+ v12_obj.respond_to?(:foo1).should be_true
108
+ v12_obj.respond_to?(:foo1=).should be_true
109
+ v12_obj.respond_to?(:foo2).should be_true
110
+ v12_obj.respond_to?(:foo2=).should be_true
111
+ v12_obj.respond_to?(:foo3).should be_true
112
+ v12_obj.respond_to?(:foo3=).should be_true
113
+ end
114
+
115
+ it "should not raise an error if an incomplete but valid hash is given" do
116
+ v12_hash = {"foo1" => "foo"}
117
+ expect { Schemata::Component::Foo::V12.new(v12_hash) }.to_not raise_error
118
+ end
119
+
120
+ it "should raise an error if an incomplete and invalid hash is given" do
121
+ v12_hash = {"foo1" => 1}
122
+ expect {
123
+ Schemata::Component::Foo::V12.new(v12_hash)
124
+ }.to raise_error(Schemata::UpdateAttributeError)
125
+ end
126
+
127
+ it "should not raise an error if an attribute is correctly assigned" do
128
+ foo_obj = Schemata::Component.mock_foo 12
129
+ expect { foo_obj.foo3 = [1, 2] }.to_not raise_error
130
+ end
131
+
132
+ it "should raise an error if an attribute is incorrectly assigned" do
133
+ foo_obj = Schemata::Component.mock_foo 12
134
+ expect {
135
+ foo_obj.foo3 = 1
136
+ }.to raise_error(Schemata::UpdateAttributeError)
137
+ end
138
+
139
+ it "should not raise an error if an incomplete msg_obj is updated \
140
+ correctly" do
141
+ v12_hash = {"foo1" => "foo"}
142
+ v12_obj = Schemata::Component::Foo::V12.new(v12_hash)
143
+ expect { v12_obj.foo1 = "new name" }.to_not raise_error
144
+ end
145
+
146
+ it "should raise an error if an incomplete msg_obj is updated \
147
+ incorrectly" do
148
+ v12_hash = {"foo1" => "foo"}
149
+ v12_obj = Schemata::Component::Foo::V12.new(v12_hash)
150
+ expect {
151
+ v12_obj.foo1 = 1
152
+ }.to raise_error(Schemata::UpdateAttributeError)
153
+ end
154
+ end
155
+
156
+ describe "V13" do
157
+ it "should define accessors for the schema" do
158
+ v13_obj = Schemata::Component::Foo::V13.new
159
+ v13_obj.respond_to?(:foo1).should be_true
160
+ v13_obj.respond_to?(:foo1=).should be_true
161
+ v13_obj.respond_to?(:foo3).should be_true
162
+ v13_obj.respond_to?(:foo3=).should be_true
163
+ v13_obj.respond_to?(:foo4).should be_true
164
+ v13_obj.respond_to?(:foo4=).should be_true
165
+ end
166
+
167
+ it "should not raise an error if an incomplete but valid hash is given" do
168
+ v13_hash = {"foo1" => "foo"}
169
+ expect { Schemata::Component::Foo::V13.new(v13_hash) }.to_not raise_error
170
+ end
171
+
172
+ it "should raise an error if an incomplete and invalid hash is given" do
173
+ v13_hash = {"foo1" => 1}
174
+ expect {
175
+ Schemata::Component::Foo::V13.new(v13_hash)
176
+ }.to raise_error(Schemata::UpdateAttributeError)
177
+ end
178
+
179
+ it "should not raise an error if an attribute is correctly assigned" do
180
+ foo_obj = Schemata::Component.mock_foo 13
181
+ expect { foo_obj.foo4 = "foobar" }.to_not raise_error
182
+ end
183
+
184
+ it "should raise an error if an attribute is incorrectly assigned" do
185
+ foo_obj = Schemata::Component.mock_foo 13
186
+ expect {
187
+ foo_obj.foo4 =1
188
+ }.to raise_error(Schemata::UpdateAttributeError)
189
+ end
190
+
191
+ it "should raise an error if an accessed field was removed" do
192
+ foo_obj = Schemata::Component.mock_foo 13
193
+ expect {
194
+ foo_obj.foo2
195
+ }.to raise_error
196
+ end
197
+
198
+ it "should not raise an error if an incomplete msg_obj is updated \
199
+ correctly" do
200
+ v13_hash = {"foo1" => "foo"}
201
+ v13_obj = Schemata::Component::Foo::V13.new(v13_hash)
202
+ expect { v13_obj.foo1 = "new name" }.to_not raise_error
203
+ end
204
+
205
+ it "should raise an error if an incomplete msg_obj is updated \
206
+ incorrectly" do
207
+ v13_hash = {"foo1" => "foo"}
208
+ v13_obj = Schemata::Component::Foo::V13.new(v13_hash)
209
+ expect {
210
+ v13_obj.foo1 = 1
211
+ }.to raise_error(Schemata::UpdateAttributeError)
212
+ end
213
+ end
214
+ end
@@ -0,0 +1,10 @@
1
+ require 'schemata/dea'
2
+ require 'spec_helper'
3
+
4
+ describe Schemata::DEA::AdvertiseMessage do
5
+ it_behaves_like "a message type"
6
+ end
7
+
8
+ describe Schemata::DEA::AdvertiseMessage::V1 do
9
+ it_behaves_like "a message"
10
+ end
@@ -0,0 +1,6 @@
1
+ require 'schemata/dea'
2
+ require 'spec_helper'
3
+
4
+ describe Schemata::DEA do
5
+ it_behaves_like "a schemata component"
6
+ end
@@ -0,0 +1,10 @@
1
+ require 'schemata/dea'
2
+ require 'spec_helper'
3
+
4
+ describe Schemata::DEA::DropletStatusResponse do
5
+ it_behaves_like "a message type"
6
+ end
7
+
8
+ describe Schemata::DEA::DropletStatusResponse::V1 do
9
+ it_behaves_like "a message"
10
+ end
@@ -0,0 +1,10 @@
1
+ require 'schemata/dea'
2
+ require 'spec_helper'
3
+
4
+ describe Schemata::DEA::DiscoverRequest do
5
+ it_behaves_like "a message type"
6
+ end
7
+
8
+ describe Schemata::DEA::DiscoverRequest::V1 do
9
+ it_behaves_like "a message"
10
+ end
@@ -0,0 +1,10 @@
1
+ require 'schemata/dea'
2
+ require 'spec_helper'
3
+
4
+ describe Schemata::DEA::DropletStatusResponse do
5
+ it_behaves_like "a message type"
6
+ end
7
+
8
+ describe Schemata::DEA::DropletStatusResponse::V1 do
9
+ it_behaves_like "a message"
10
+ end
@@ -0,0 +1,10 @@
1
+ require 'schemata/dea'
2
+ require 'spec_helper'
3
+
4
+ describe Schemata::DEA::ExitMessage do
5
+ it_behaves_like "a message type"
6
+ end
7
+
8
+ describe Schemata::DEA::ExitMessage::V1 do
9
+ it_behaves_like "a message"
10
+ end
@@ -0,0 +1,27 @@
1
+ require 'schemata/dea'
2
+ require 'spec_helper'
3
+
4
+ describe Schemata::DEA::FindDropletRequest do
5
+ it_behaves_like "a message type"
6
+ end
7
+
8
+ describe Schemata::DEA::FindDropletRequest::V1 do
9
+ it_behaves_like "a message"
10
+
11
+ it "should stringify the 'states' field when it is given to the constructor as a symbol" do
12
+ hash = {
13
+ "droplet" => "deadbeef",
14
+ "states" => [:RUNNING],
15
+ "version" => "0.1.0",
16
+ }
17
+
18
+ msg_obj = Schemata::DEA::FindDropletRequest::V1.new(hash)
19
+ msg_obj.states.should =~ ["RUNNING"]
20
+ end
21
+
22
+ it "should stringify the 'states' field when a symbold is passed ot the attr writer" do
23
+ msg_obj = Schemata::DEA.mock_find_droplet_request
24
+ msg_obj.states = [:RUNNING]
25
+ msg_obj.states.should =~ ["RUNNING"]
26
+ end
27
+ end
@@ -0,0 +1,10 @@
1
+ require 'schemata/dea'
2
+ require 'spec_helper'
3
+
4
+ describe Schemata::DEA::FindDropletResponse do
5
+ it_behaves_like "a message type"
6
+ end
7
+
8
+ describe Schemata::DEA::FindDropletResponse::V1 do
9
+ it_behaves_like "a message"
10
+ end
@@ -0,0 +1,10 @@
1
+ require 'schemata/dea'
2
+ require 'spec_helper'
3
+
4
+ describe Schemata::DEA::HeartbeatResponse do
5
+ it_behaves_like "a message type"
6
+ end
7
+
8
+ describe Schemata::DEA::HeartbeatResponse::V1 do
9
+ it_behaves_like "a message"
10
+ end
@@ -0,0 +1,10 @@
1
+ require 'schemata/dea'
2
+ require 'spec_helper'
3
+
4
+ describe Schemata::DEA::HelloMessage do
5
+ it_behaves_like "a message type"
6
+ end
7
+
8
+ describe Schemata::DEA::HelloMessage::V1 do
9
+ it_behaves_like "a message"
10
+ end
@@ -0,0 +1,10 @@
1
+ require 'schemata/dea'
2
+ require 'spec_helper'
3
+
4
+ describe Schemata::DEA::StartRequest do
5
+ it_behaves_like "a message type"
6
+ end
7
+
8
+ describe Schemata::DEA::StartRequest::V1 do
9
+ it_behaves_like "a message"
10
+ end
@@ -0,0 +1,10 @@
1
+ require 'schemata/dea'
2
+ require 'spec_helper'
3
+
4
+ describe Schemata::DEA::StopRequest do
5
+ it_behaves_like "a message type"
6
+ end
7
+
8
+ describe Schemata::DEA::StopRequest::V1 do
9
+ it_behaves_like "a message"
10
+ end
File without changes
@@ -0,0 +1,10 @@
1
+ require 'schemata/router'
2
+ require 'spec_helper'
3
+
4
+ describe Schemata::Router::RegisterRequest do
5
+ it_behaves_like "a message type"
6
+ end
7
+
8
+ describe Schemata::Router::RegisterRequest::V1 do
9
+ it_behaves_like "a message"
10
+ end
@@ -0,0 +1,6 @@
1
+ require 'schemata/router'
2
+ require 'spec_helper'
3
+
4
+ describe Schemata::Router do
5
+ it_behaves_like "a schemata component"
6
+ end
@@ -0,0 +1,10 @@
1
+ require 'schemata/router'
2
+ require 'spec_helper'
3
+
4
+ describe Schemata::Router::StartMessage do
5
+ it_behaves_like "a message type"
6
+ end
7
+
8
+ describe Schemata::Router::StartMessage::V1 do
9
+ it_behaves_like "a message"
10
+ end
@@ -0,0 +1 @@
1
+ Dir["./spec/support/**/*.rb"].sort.each { |f| require f }
@@ -0,0 +1,14 @@
1
+ require 'schemata/staging'
2
+ require 'spec_helper'
3
+
4
+ describe Schemata::Staging::Message do
5
+ it_behaves_like "a message type"
6
+ end
7
+
8
+ describe Schemata::Staging::Message::V1 do
9
+ it_behaves_like "a message"
10
+ end
11
+
12
+ describe Schemata::Staging::Message::V2 do
13
+ it_behaves_like "a message"
14
+ end
@@ -0,0 +1,6 @@
1
+ require 'schemata/staging'
2
+ require 'spec_helper'
3
+
4
+ describe Schemata::Staging do
5
+ it_behaves_like "a schemata component"
6
+ end
@@ -0,0 +1,51 @@
1
+ require 'support/helpers'
2
+
3
+ shared_examples "a schemata component" do
4
+
5
+ described_class.constants.select { |x| x != :VERSION }.each do |msg_type|
6
+ describe ".mock_#{decamelize(msg_type.to_s)}" do
7
+ versions = described_class::const_get(msg_type).constants.select { |x| x =~ /V[0-9]+/ }
8
+ versions.map { |x| x = x.to_s[1..-1].to_i }.each do |version|
9
+ it_behaves_like "a mocking method", version do
10
+ let(:message_type) { described_class::const_get(msg_type) }
11
+ let(:message_type_name) { msg_type.to_s }
12
+ let(:component) { described_class }
13
+ let(:component_name) { component.name.split("::")[1] }
14
+
15
+ let(:mock_method) { "mock_#{decamelize(msg_type)}"}
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ shared_examples "a mocking method" do |version|
23
+
24
+ context "when current_version is #{version}" do
25
+ before :each do
26
+ set_current_version(message_type, version)
27
+ end
28
+
29
+ after :each do
30
+ reset_version(message_type)
31
+ end
32
+
33
+ it "should return a V#{version} object if called with no argument" do
34
+ msg_obj = component.send(mock_method)
35
+ msg_obj.class.should == message_type::const_get("V#{version}")
36
+ end
37
+
38
+ 1.upto(version) do |i|
39
+ it "should return a V#{i} object if called with input #{i}" do
40
+ msg_obj = component.send(mock_method, i)
41
+ msg_obj.class.should == message_type::const_get("V#{i}")
42
+ end
43
+ end
44
+
45
+ it "should raise an error if called with input > #{version}" do
46
+ expect {
47
+ msg_obj = component.send(mock_method, version + 1)
48
+ }.to raise_error(NameError)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,54 @@
1
+ def set_current_version(msg_type, version)
2
+ msg_type.stub(:current_version).and_return(version)
3
+ @curr_class = msg_type.current_class
4
+
5
+ @future_versions = {}
6
+ msg_type::constants.each do |v|
7
+ next if !is_message_version?(v) || !greater_version?(v, version)
8
+ @future_versions[v] = msg_type::const_get(v)
9
+ msg_type.send(:remove_const, v)
10
+ end
11
+ end
12
+
13
+ def reset_version(msg_type)
14
+ @future_versions.each do |v, klass|
15
+ msg_type::const_set(v, klass)
16
+ end
17
+ @future_versions
18
+ end
19
+
20
+ def is_message_version?(constant)
21
+ !!(constant =~ /^V[0-9]+$/)
22
+ end
23
+
24
+ def greater_version?(version, benchmark)
25
+ version = version[1..-1].to_i
26
+ version > benchmark
27
+ end
28
+
29
+ def decamelize(str)
30
+ words = []
31
+ curr_word = ""
32
+ 0.upto(str.length - 1) do |i|
33
+ ch = str[i]
34
+ if ch =~ /[A-Z]/
35
+ words.push(curr_word)
36
+ curr_word = ""
37
+ end
38
+ curr_word += ch
39
+ end
40
+ words.push(curr_word)
41
+ words.map! { |x| x.downcase }
42
+
43
+ # If the first letter is capitalized, then the first word here is empty
44
+ words.shift if words[0] == ""
45
+
46
+ words.join('_')
47
+ end
48
+
49
+ def num_mandatory_fields(msg_obj)
50
+ optional = msg_obj.class.schema.optional_keys
51
+ mandatory = Set.new(msg_obj.class.schema.schemas.keys)
52
+ diff = mandatory - optional
53
+ return diff.size
54
+ end
@@ -0,0 +1,138 @@
1
+ shared_examples "a message" do
2
+
3
+ version = described_class.version
4
+
5
+ let(:component_name) { described_class.name.split("::")[1] }
6
+ let(:message_type_name) { described_class.name.split("::")[2] }
7
+ let(:message_name) { described_class.name.split("::")[3] }
8
+
9
+ let(:component) { Schemata::const_get(component_name) }
10
+ let(:message_type) { component::const_get(message_type_name) }
11
+ let(:message) { described_class }
12
+
13
+ let(:mock_method) { "mock_#{decamelize(message_type_name)}" }
14
+
15
+ before :each do
16
+ set_current_version(message_type, version)
17
+ end
18
+
19
+ after :each do
20
+ reset_version(message_type)
21
+ end
22
+
23
+ describe "#new" do
24
+ it "should create a #{described_class} object with an incomplete hash" do
25
+ mock_hash = component.send(mock_method, 1).contents
26
+ first_key = mock_hash.keys[0]
27
+ first_value = mock_hash[first_key]
28
+
29
+ msg_obj = message.new({first_key => first_value})
30
+ msg_obj.class.should == message
31
+ msg_obj.send(first_key).should == first_value
32
+ end
33
+
34
+ it "should raise an error if the hash contains incorrect types" do
35
+ mock_hash = component.send(mock_method, 1).contents
36
+ first_key = mock_hash.keys[0]
37
+ first_value = mock_hash[first_key]
38
+ if first_value.kind_of? Integer
39
+ bad_value = "foo"
40
+ else
41
+ bad_value = 1
42
+ end
43
+
44
+ expect {
45
+ msg_obj = message.new({first_key => bad_value})
46
+ }.to raise_error(Schemata::UpdateAttributeError)
47
+ end
48
+ end
49
+
50
+ describe "#encode" do
51
+ if version == 1
52
+ it "should return a Schemata-encoded json string, with a V1 hash also in the raw payload" do
53
+ msg_obj = component.send(mock_method, 1)
54
+ json = msg_obj.encode
55
+ json_hash = Yajl::Parser.parse(json)
56
+
57
+ json_hash.should have_key "V1"
58
+ json_hash.should have_key "min_version"
59
+
60
+ data = Schemata::HashCopyHelpers.deep_copy(json_hash["V1"])
61
+
62
+ json_hash.delete("V1")
63
+ json_hash.delete("min_version")
64
+
65
+ json_hash.should == data
66
+ end
67
+ else
68
+ it "should return a Schemata-encoded json string, with no raw payload" do
69
+ msg_obj = component.send(mock_method, version)
70
+ json = msg_obj.encode
71
+ json_hash = Yajl::Parser.parse(json)
72
+
73
+ 1.upto(version) do |i|
74
+ json_hash.should have_key "V#{i}"
75
+ end
76
+ json_hash.should have_key "min_version"
77
+
78
+ data = Schemata::HashCopyHelpers.deep_copy(json_hash["V#{version}"])
79
+ 1.upto(version) do |i|
80
+ json_hash.delete("V#{i}")
81
+ end
82
+ json_hash.delete("min_version")
83
+
84
+ json_hash.should be_empty
85
+ end
86
+ end
87
+ end
88
+
89
+ described_class.schema.schemas.keys.each do |attr|
90
+ describe "##{attr}" do
91
+ it "should the attribute if it was specified at instantiation" do
92
+ mock_value = component.send(mock_method, version).contents[attr]
93
+ msg_obj = message.new({ attr => mock_value })
94
+ msg_obj.send(attr).should == mock_value
95
+ end
96
+
97
+ it "should return the attribute if it was set with an attr writer" do
98
+ mock_value = component.send(mock_method, version).contents[attr]
99
+ msg_obj = message.new
100
+ msg_obj.send("#{attr}=", mock_value)
101
+ msg_obj.send(attr).should == mock_value
102
+ end
103
+
104
+ it "should return nil if the attribute was never set" do
105
+ msg_obj = message.new
106
+ msg_obj.send(attr).should be_nil
107
+ end
108
+ end
109
+
110
+ describe "##{attr}=" do
111
+ it "should change the attribute and return the new value" do
112
+ mock_value = component.send(mock_method, version).contents[attr]
113
+ msg_obj = message.new({ attr => mock_value })
114
+ ret = (msg_obj.send("#{attr}=", mock_value))
115
+ msg_obj.send(attr).should == mock_value
116
+ ret.should == mock_value
117
+ end
118
+
119
+ unless described_class.schema.schemas[attr].kind_of? Membrane::Schema::Any
120
+ it "should raise an error if the wrong type is written" do
121
+ mock_value = component.send(mock_method, version).contents[attr]
122
+
123
+ if mock_value.kind_of? Integer
124
+ bad_value = "foo"
125
+ else
126
+ bad_value = 1
127
+ end
128
+
129
+ msg_obj = message.new
130
+
131
+ expect {
132
+ msg_obj.send("#{attr}=", bad_value)
133
+ }.to raise_error(Schemata::UpdateAttributeError)
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end