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,630 @@
1
+ require 'schemata/component/component'
2
+ require 'support/helpers'
3
+
4
+ describe Schemata::Component do
5
+ before :each do
6
+ @v10_msg = '{
7
+ "min_version": 10,
8
+ "V10": {
9
+ "foo1": "hello",
10
+ "foo2": 1
11
+ }
12
+ }'
13
+
14
+ @v11_msg = '{
15
+ "min_version": 10,
16
+ "V11": {
17
+ "foo1": "hello",
18
+ "foo2": 1,
19
+ "foo3": 1
20
+ },
21
+ "V10": {}
22
+ }'
23
+
24
+ @v12_msg = '{
25
+ "min_version" : 10,
26
+ "V12": {
27
+ "foo1": "hello",
28
+ "foo2": 1,
29
+ "foo3": [1]
30
+ },
31
+ "V11": {
32
+ "foo3": 1
33
+ },
34
+ "V10": {}
35
+ }'
36
+
37
+ @v13_msg = '{
38
+ "min_version": 13,
39
+ "V13": {
40
+ "foo1": "hello",
41
+ "foo3": [1],
42
+ "foo4": "foo"
43
+ }
44
+ }'
45
+
46
+ @v14_msg = '{
47
+ "min_version": 13,
48
+ "V14" : {
49
+ "foo1" : "hello",
50
+ "foo3" : [1]
51
+ },
52
+ "V13" : { "foo4" : "foo" }
53
+ }'
54
+
55
+ @v10_hash = {
56
+ "foo1" => "hello",
57
+ "foo2" => 1
58
+ }
59
+
60
+ @v11_hash = {
61
+ "foo1" => "hello",
62
+ "foo2" => 1,
63
+ "foo3" => 1
64
+ }
65
+
66
+ @v12_hash = {
67
+ "foo1" => "hello",
68
+ "foo2" => 1,
69
+ "foo3" => [1]
70
+ }
71
+
72
+ @v13_hash = {
73
+ "foo1" => "hello",
74
+ "foo3" => [1],
75
+ "foo4" => "foo"
76
+ }
77
+
78
+ @v14_hash = {
79
+ "foo1" => "hello",
80
+ "foo3" => [1]
81
+ }
82
+
83
+ end
84
+
85
+ describe "#decode" do
86
+ it "should raise an error if the 'min_version' is missing" do
87
+ msg_hash = {
88
+ "V10" => {},
89
+ "V11" => {
90
+ "foo1" => "foo",
91
+ "foo2" => 1,
92
+ "foo3" => 1
93
+ }
94
+ }
95
+ json_msg = Yajl::Encoder.encode msg_hash
96
+ expect {
97
+ foo_obj = Schemata::Component::Foo.decode(json_msg)
98
+ }.to raise_error(Schemata::DecodeError)
99
+ end
100
+
101
+ it "should raise an error if the message has no Vxx key" do
102
+ msg_hash = {
103
+ "min_version" => 10
104
+ }
105
+ json_msg = Yajl::Encoder.encode msg_hash
106
+ expect {
107
+ foo_obj = Schemata::Component::Foo.decode(json_msg)
108
+ }.to raise_error(Schemata::DecodeError)
109
+ end
110
+
111
+ describe "(current version is 10)" do
112
+ before :each do
113
+ set_current_version(Schemata::Component::Foo, 10)
114
+ end
115
+
116
+ after :each do
117
+ reset_version(Schemata::Component::Foo)
118
+ end
119
+
120
+ it "should take a v10 message and return a correct Foo::V10 object" do
121
+ foo_obj = Schemata::Component::Foo.decode(@v10_msg)
122
+ foo_obj.class.should == Schemata::Component::Foo::V10
123
+ foo_obj.foo1.should == "hello"
124
+ foo_obj.foo2.should == 1
125
+ end
126
+
127
+ it "should take a v11 message and return a correct Foo::V10 object" do
128
+ foo_obj = Schemata::Component::Foo.decode(@v11_msg)
129
+ foo_obj.class.should == Schemata::Component::Foo::V10
130
+ foo_obj.foo1.should == "hello"
131
+ foo_obj.foo2.should == 1
132
+ end
133
+
134
+ it "should take a v12 message and return a correct Foo::V10 object" do
135
+ foo_obj = Schemata::Component::Foo.decode(@v12_msg)
136
+ foo_obj.class.should == Schemata::Component::Foo::V10
137
+ foo_obj.foo1.should == "hello"
138
+ foo_obj.foo2.should == 1
139
+ end
140
+
141
+ it "should raise an error when a v10 message has an invalid V10 hash" do
142
+ msg_hash = {
143
+ "min_version" => 10,
144
+ "V10" => {
145
+ "foo1" => "foo",
146
+ }
147
+ }
148
+ json_msg = Yajl::Encoder.encode(msg_hash)
149
+ expect {
150
+ foo_obj = Schemata::Component::Foo.decode(json_msg)
151
+ }.to raise_error(Schemata::DecodeError)
152
+ end
153
+
154
+ it "should raise an error when a v11 message has an invalid union \
155
+ of V11 and V10 hashes" do
156
+ msg_hash = {
157
+ "min_version" => 10,
158
+ "V10" => {},
159
+ "V11" => {
160
+ "foo1" => "foo",
161
+ "foo2" => "bar",
162
+ "foo3" => 1
163
+ }
164
+ }
165
+ json_msg = Yajl::Encoder.encode(msg_hash)
166
+ expect {
167
+ foo_obj = Schemata::Component::Foo.decode(
168
+ json_msg)
169
+ }.to raise_error(Schemata::DecodeError)
170
+ end
171
+
172
+ it "should raise an error when a v12 message has an invalid union \
173
+ of V10, V11, and V12 hashes" do
174
+ msg_hash = {
175
+ "min_version" => 10,
176
+ "V10" => {},
177
+ "V11" => {"foo3" => 1},
178
+ "V12" => {
179
+ "foo1" => "foo",
180
+ "foo2" => "bar",
181
+ "foo3" => [1]
182
+ }
183
+ }
184
+ json_msg = Yajl::Encoder.encode msg_hash
185
+ expect {
186
+ foo_obj = Schemata::Component::Foo.decode(json_msg)
187
+ }.to raise_error(Schemata::DecodeError)
188
+
189
+ msg_hash["V11"]["foo2"] = "bar"
190
+ msg_hash["V12"]["foo2"] = 1
191
+ json_msg = Yajl::Encoder.encode msg_hash
192
+ expect {
193
+ foo_obj = Schemata::Component::Foo.decode(json_msg)
194
+ }.to raise_error(Schemata::DecodeError)
195
+ end
196
+
197
+ it "should raise an IncompatibleVersionError when it gets a v13 msg" do
198
+ expect {
199
+ foo_obj = Schemata::Component::Foo.decode(@v13_msg)
200
+ }.to raise_error(Schemata::IncompatibleVersionError)
201
+ end
202
+ end
203
+
204
+ describe "(current version is 11)" do
205
+ before :each do
206
+ set_current_version(Schemata::Component::Foo, 11)
207
+ end
208
+
209
+ after :each do
210
+ reset_version(Schemata::Component::Foo)
211
+ end
212
+
213
+ it "should take a v10 message, upvert, and return a correct V11 object" do
214
+ foo_obj = Schemata::Component::Foo.decode(@v10_msg)
215
+ foo_obj.class.should == Schemata::Component::Foo::V11
216
+ foo_obj.foo1.should == "hello"
217
+ foo_obj.foo2.should == 1
218
+ foo_obj.foo3.should == 1
219
+ end
220
+
221
+ it "should take a v11 message and return a correct V11 object" do
222
+ foo_obj = Schemata::Component::Foo.decode(@v11_msg)
223
+ foo_obj.class.should == Schemata::Component::Foo::V11
224
+ foo_obj.foo1.should == "hello"
225
+ foo_obj.foo2.should == 1
226
+ foo_obj.foo3.should == 1
227
+ end
228
+
229
+ it "should take a v12 message and return a correct V11 object" do
230
+ foo_obj = Schemata::Component::Foo.decode(@v12_msg)
231
+ foo_obj.class.should == Schemata::Component::Foo::V11
232
+ foo_obj.foo1.should == "hello"
233
+ foo_obj.foo2.should == 1
234
+ foo_obj.foo3.should == 1
235
+ end
236
+
237
+ it "should raise an error when a v10 message has an invalid V10 hash" do
238
+ msg_hash = {
239
+ "min_version" => 10,
240
+ "V10" => {
241
+ "foo1" => "foo",
242
+ }
243
+ }
244
+ json_msg = Yajl::Encoder.encode msg_hash
245
+ expect {
246
+ foo_obj = Schemata::Component::Foo.decode(json_msg)
247
+ }.to raise_error(Schemata::DecodeError)
248
+ end
249
+
250
+ it "should raise an error when a v11 message has an invalid V11 hash" do
251
+ msg_hash = {
252
+ "min_version" => 10,
253
+ "V10" => {},
254
+ "V11" => {
255
+ "foo1" => "foo",
256
+ "foo3" => 1
257
+ }
258
+ }
259
+ json_msg = Yajl::Encoder.encode msg_hash
260
+ expect {
261
+ foo_obj = Schemata::Component::Foo.decode(json_msg)
262
+ }.to raise_error(Schemata::DecodeError)
263
+ end
264
+
265
+ it "should raise an error when a v12 message has an invalid union \
266
+ of V10, V11, and V12 hashes" do
267
+ msg_hash = {
268
+ "min_version" => 10,
269
+ "V10" => {},
270
+ "V11" => {"foo3" => [1]},
271
+ "V12" => {
272
+ "foo1" => "foo",
273
+ "foo2" => 1,
274
+ "foo3" => [1]
275
+ }
276
+ }
277
+ json_msg = Yajl::Encoder.encode msg_hash
278
+ expect {
279
+ foo_obj = Schemata::Component::Foo.decode(json_msg)
280
+ }.to raise_error(Schemata::DecodeError)
281
+ end
282
+
283
+ it "should raise an IncompatibleVersionError on a v13 message" do
284
+ expect {
285
+ foo_obj = Schemata::Component::Foo.decode(@v13_msg)
286
+ }.to raise_error(Schemata::IncompatibleVersionError)
287
+ end
288
+ end
289
+
290
+ describe "(current version is 12)" do
291
+ before :each do
292
+ set_current_version(Schemata::Component::Foo, 12)
293
+ end
294
+
295
+ after :each do
296
+ reset_version(Schemata::Component::Foo)
297
+ end
298
+
299
+ it "should take a v10 message, upvert twice, and return a correct V12 object" do
300
+ foo_obj = Schemata::Component::Foo.decode(@v10_msg)
301
+ foo_obj.class.should == Schemata::Component::Foo::V12
302
+ foo_obj.foo1.should == "hello"
303
+ foo_obj.foo2.should == 1
304
+ foo_obj.foo3.should == [1]
305
+ end
306
+
307
+ it "should take a v11 message, upvert, and return a correct V12 object" do
308
+ foo_obj = Schemata::Component::Foo.decode(@v11_msg)
309
+ foo_obj.class.should == Schemata::Component::Foo::V12
310
+ foo_obj.foo1.should == "hello"
311
+ foo_obj.foo2.should == 1
312
+ foo_obj.foo3.should == [1]
313
+ end
314
+
315
+ it "should take a v12 message and return a correct V12 object" do
316
+ foo_obj = Schemata::Component::Foo.decode(@v12_msg)
317
+ foo_obj.class.should == Schemata::Component::Foo::V12
318
+ foo_obj.foo1.should == "hello"
319
+ foo_obj.foo2.should == 1
320
+ foo_obj.foo3.should == [1]
321
+ end
322
+
323
+ it "should raise an error when a v10 message has an invalid V10 hash" do
324
+ msg_hash = {
325
+ "min_version" => 10,
326
+ "V10" => {
327
+ "foo1" => "foo"
328
+ }
329
+ }
330
+ json_msg = Yajl::Encoder.encode(msg_hash)
331
+ expect {
332
+ foo_obj = Schemata::Component::Foo.decode(json_msg)
333
+ }.to raise_error(Schemata::DecodeError)
334
+ end
335
+
336
+ it "should raise an error when a v11 messsage an invalid V11 hash" do
337
+ msg_hash = {
338
+ "min_version" => 10,
339
+ "V10" => {},
340
+ "V11" => {}
341
+ }
342
+ json_msg = Yajl::Encoder.encode(msg_hash)
343
+ expect {
344
+ foo_obj = Schemata::Component::Foo.decode(json_msg)
345
+ }.to raise_error(Schemata::DecodeError)
346
+ end
347
+
348
+ it "should raise an error when a v12 message has an invalid V12 hash" do
349
+ msg_hash = {
350
+ "min_version" => 10,
351
+ "V10" => {},
352
+ "V11" => {"foo3" => 1},
353
+ "V12" => {}
354
+ }
355
+ json_msg = Yajl::Encoder.encode(msg_hash)
356
+ expect {
357
+ foo_obj = Schemata::Component::Foo.decode(json_msg)
358
+ }.to raise_error(Schemata::DecodeError)
359
+ end
360
+
361
+ it "should raise an IncompatibleVersionError on a v13 message" do
362
+ expect {
363
+ foo_obj = Schemata::Component::Foo.decode(@v13_msg)
364
+ }.to raise_error(Schemata::IncompatibleVersionError)
365
+ end
366
+ end
367
+
368
+ describe "(current version is 13)" do
369
+ before :each do
370
+ set_current_version(Schemata::Component::Foo, 13)
371
+ end
372
+
373
+ after :each do
374
+ reset_version(Schemata::Component::Foo)
375
+ end
376
+
377
+ it "should validate a v10 message and return a correct V13 object" do
378
+ foo_obj = Schemata::Component::Foo.decode(@v10_msg)
379
+ foo_obj.class.should == Schemata::Component::Foo::V13
380
+ foo_obj.foo1.should == "hello"
381
+ foo_obj.foo3.should == [1]
382
+ foo_obj.foo4.should == "foo"
383
+ end
384
+
385
+ it "should validate a v11 message and return a correct V13 object" do
386
+ foo_obj = Schemata::Component::Foo.decode(@v11_msg)
387
+ foo_obj.class.should == Schemata::Component::Foo::V13
388
+ foo_obj.foo1.should == "hello"
389
+ foo_obj.foo3.should == [1]
390
+ foo_obj.foo4.should == "foo"
391
+ end
392
+
393
+ it "should validate a v12 message and return a correct V13 object" do
394
+ foo_obj = Schemata::Component::Foo.decode(@v12_msg)
395
+ foo_obj.class.should == Schemata::Component::Foo::V13
396
+ foo_obj.foo1.should == "hello"
397
+ foo_obj.foo3.should == [1]
398
+ foo_obj.foo4.should == "foo"
399
+ end
400
+
401
+ it "should validate a v13 message and return a correct V13 object" do
402
+ foo_obj =Schemata::Component::Foo.decode(@v13_msg)
403
+ foo_obj.class.should == Schemata::Component::Foo::V13
404
+ foo_obj.foo1.should == "hello"
405
+ foo_obj.foo3.should == [1]
406
+ foo_obj.foo4.should == "foo"
407
+ end
408
+
409
+ it "should validate a v14 message and return a correct v13 object" do
410
+ foo_obj = Schemata::Component::Foo.decode(@v14_msg)
411
+ foo_obj.class.should == Schemata::Component::Foo::V13
412
+ foo_obj.foo1.should == "hello"
413
+ foo_obj.foo3.should == [1]
414
+ foo_obj.foo4.should == "foo"
415
+ end
416
+ end
417
+
418
+ describe "(current version is 14)" do
419
+ before :each do
420
+ Schemata::Component::Foo.stub(:current_version).and_return(14)
421
+ @curr_class = Schemata::Component::Foo.current_class
422
+ end
423
+
424
+ it "should validate a v13 message and return a correct v14 object" do
425
+ foo_obj = Schemata::Component::Foo.decode(@v13_msg)
426
+ foo_obj.class.should == Schemata::Component::Foo::V14
427
+ foo_obj.foo1.should == "hello"
428
+ foo_obj.foo3.should == [1]
429
+ end
430
+
431
+ it "should validate a v14 message and return a correct v14 object" do
432
+ foo_obj = Schemata::Component::Foo.decode(@v14_msg)
433
+ foo_obj.class.should == Schemata::Component::Foo::V14
434
+ foo_obj.foo1.should == "hello"
435
+ foo_obj.foo3.should == [1]
436
+ end
437
+ end
438
+ end
439
+
440
+ describe "#encode" do
441
+ describe "(current version is 10)" do
442
+ before :each do
443
+ set_current_version(Schemata::Component::Foo, 10)
444
+ end
445
+
446
+ after :each do
447
+ reset_version(Schemata::Component::Foo)
448
+ end
449
+
450
+ it "should take a v10 obj and return the correct json string" do
451
+ v10_obj = Schemata::Component::Foo::V10.new(@v10_hash)
452
+ json = v10_obj.encode
453
+ returned_hash = Yajl::Parser.parse json
454
+ returned_hash.keys.should =~ ['min_version', 'V10']
455
+
456
+ returned_hash['min_version'].should ==
457
+ @curr_class::MIN_VERSION_ALLOWED
458
+
459
+ v10 = returned_hash['V10']
460
+ v10.each do |k, v|
461
+ v10[k].should == @v10_hash[k]
462
+ end
463
+ end
464
+
465
+ it "should raise an error if the msg_obj is incomplete" do
466
+ msg_obj = Schemata::Component::Foo::V10.new({"foo1" => "foo"})
467
+ expect {
468
+ json = msg_obj.encode
469
+ }.to raise_error(Schemata::EncodeError)
470
+ end
471
+ end
472
+
473
+ describe "(current version is 11)" do
474
+ before :each do
475
+ set_current_version(Schemata::Component::Foo, 11)
476
+ end
477
+
478
+ after:each do
479
+ reset_version(Schemata::Component::Foo)
480
+ end
481
+
482
+ it "should take a v11 obj and return the correct json string" do
483
+ v11_obj = Schemata::Component::Foo::V11.new @v11_hash
484
+ json = v11_obj.encode
485
+ returned_hash = Yajl::Parser.parse json
486
+
487
+ returned_hash.keys.should =~ ['min_version', 'V10', 'V11']
488
+
489
+ returned_hash['min_version'].should ==
490
+ @curr_class::MIN_VERSION_ALLOWED
491
+
492
+ v10 = returned_hash["V10"]
493
+ v10.each do |k, v|
494
+ v10[k].should == @v10_hash[k]
495
+ end
496
+
497
+ v11 = returned_hash["V11"]
498
+ v11.each do |k, v|
499
+ v11[k].should == @v11_hash[k]
500
+ end
501
+ end
502
+
503
+ it "should raise an error if the msg_obj is incomplete" do
504
+ msg_obj = Schemata::Component::Foo::V11.new({"foo1" => "foo"})
505
+ expect {
506
+ json = msg_obj.encode
507
+ }.to raise_error(Schemata::EncodeError)
508
+ end
509
+ end
510
+
511
+ describe "(current version is 12)" do
512
+ before :each do
513
+ set_current_version(Schemata::Component::Foo, 12)
514
+ end
515
+
516
+ after :each do
517
+ reset_version(Schemata::Component::Foo)
518
+ end
519
+
520
+ it "should take a v12 obj and return the correct json string" do
521
+ v12_obj = Schemata::Component::Foo::V12.new @v12_hash
522
+ json = v12_obj.encode
523
+ returned_hash = Yajl::Parser.parse json
524
+
525
+ returned_hash.keys.should =~ ['min_version', 'V10', 'V11', 'V12']
526
+
527
+ returned_hash['min_version'].should ==
528
+ @curr_class::MIN_VERSION_ALLOWED
529
+
530
+ v10 = returned_hash['V10']
531
+ v10.each do |k, v|
532
+ v10[k].should == @v10_hash[k]
533
+ end
534
+
535
+ v11 = returned_hash['V11']
536
+ v11.each do |k, v|
537
+ v11[k].should == @v11_hash[k]
538
+ end
539
+
540
+ v12 = returned_hash['V12']
541
+ v12.each do |k, v|
542
+ v12[k].should == @v12_hash[k]
543
+ end
544
+ end
545
+
546
+ it "should raise an error if the msg_obj is incomplete" do
547
+ msg_obj = Schemata::Component::Foo::V12.new({"foo1" => "foo"})
548
+ expect {
549
+ json = msg_obj.encode
550
+ }.to raise_error(Schemata::EncodeError)
551
+ end
552
+ end
553
+
554
+ describe "(current version is 13)" do
555
+ before :each do
556
+ set_current_version(Schemata::Component::Foo, 13)
557
+ end
558
+
559
+ after :each do
560
+ reset_version(Schemata::Component::Foo)
561
+ end
562
+
563
+ it "should take a v13 obj and return the correct json string" do
564
+ v13_obj = Schemata::Component::Foo::V13.new @v13_hash
565
+ json = v13_obj.encode
566
+ returned_hash = Yajl::Parser.parse json
567
+
568
+ returned_hash.keys.should =~ ['min_version', 'V13']
569
+
570
+ returned_hash['min_version'].should ==
571
+ @curr_class::MIN_VERSION_ALLOWED
572
+
573
+ v13 = returned_hash['V13']
574
+ v13.each do |k, v|
575
+ v13[k].should == @v13_hash[k]
576
+ end
577
+ end
578
+
579
+ it "should raise an error if the msg_obj is incomplete" do
580
+ msg_obj = Schemata::Component::Foo::V13.new({"foo1" => "foo"})
581
+ expect {
582
+ json = msg_obj.encode
583
+ }.to raise_error(Schemata::EncodeError)
584
+ end
585
+ end
586
+
587
+ describe "(current version 14)" do
588
+ before :each do
589
+ Schemata::Component::Foo.stub(:current_version).and_return(14)
590
+ @curr_class = Schemata::Component::Foo.current_class
591
+ end
592
+
593
+ it "should take a v14 obj and return the correct json string" do
594
+ aux_data = { "foo4" => "foo" }
595
+ v14_obj = Schemata::Component::Foo::V14.new(@v14_hash, aux_data)
596
+ json = v14_obj.encode
597
+ returned_hash = Yajl::Parser.parse(json)
598
+
599
+ returned_hash.keys.should =~ ['min_version', 'V13', 'V14']
600
+ returned_hash['min_version'].should ==
601
+ @curr_class::MIN_VERSION_ALLOWED
602
+
603
+ v13 = returned_hash['V13']
604
+ v13.keys.should =~ ['foo4']
605
+ v13['foo4'].should == "foo"
606
+
607
+ v14 = returned_hash['V14']
608
+ v14.each do |k, v|
609
+ v14[k].should == @v14_hash[k]
610
+ end
611
+ end
612
+
613
+ it "should raise an error if the msg_obj is incomplete" do
614
+ msg_obj = Schemata::Component::Foo::V14.new({"foo1" => "foo"})
615
+ expect {
616
+ json = msg_obj.encode
617
+ }.to raise_error(Schemata::EncodeError)
618
+
619
+ msg_obj = Schemata::Component::Foo::V14.new({
620
+ "foo1" => "foo",
621
+ "foo3" => [1],
622
+ })
623
+ expect {
624
+ # aux data is missing, and therefore an error is expected.
625
+ json = msg_obj.encode
626
+ }.to raise_error(Schemata::EncodeError)
627
+ end
628
+ end
629
+ end
630
+ end