occi-core 4.1.3 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. data/.gitignore +1 -0
  2. data/Gemfile +8 -0
  3. data/README.md +49 -17
  4. data/lib/occi/collection.rb +37 -21
  5. data/lib/occi/core/action.rb +5 -5
  6. data/lib/occi/core/action_instance.rb +45 -3
  7. data/lib/occi/core/actions.rb +2 -1
  8. data/lib/occi/core/attributes.rb +253 -73
  9. data/lib/occi/core/categories.rb +1 -0
  10. data/lib/occi/core/category.rb +25 -8
  11. data/lib/occi/core/entities.rb +1 -0
  12. data/lib/occi/core/entity.rb +51 -74
  13. data/lib/occi/core/kind.rb +15 -11
  14. data/lib/occi/core/kinds.rb +1 -1
  15. data/lib/occi/core/link.rb +14 -15
  16. data/lib/occi/core/links.rb +1 -1
  17. data/lib/occi/core/mixin.rb +5 -5
  18. data/lib/occi/core/mixins.rb +2 -2
  19. data/lib/occi/core/properties.rb +90 -12
  20. data/lib/occi/core/resource.rb +7 -3
  21. data/lib/occi/core/resources.rb +2 -2
  22. data/lib/occi/errors/attribute_definitions_converted_error.rb +5 -0
  23. data/lib/occi/errors/attribute_missing_error.rb +5 -0
  24. data/lib/occi/errors/attribute_name_invalid_error.rb +5 -0
  25. data/lib/occi/errors/attribute_not_defined_error.rb +5 -0
  26. data/lib/occi/errors/attribute_property_type_error.rb +5 -0
  27. data/lib/occi/errors/attribute_type_error.rb +5 -0
  28. data/lib/occi/errors/kind_not_defined_error.rb +5 -0
  29. data/lib/occi/errors/parser_input_error.rb +5 -0
  30. data/lib/occi/errors/parser_type_error.rb +5 -0
  31. data/lib/occi/errors.rb +1 -0
  32. data/lib/occi/extensions/hashie.rb +25 -0
  33. data/lib/occi/helpers/comparators/action_instance.rb +22 -0
  34. data/lib/occi/helpers/comparators/attributes.rb +22 -0
  35. data/lib/occi/helpers/comparators/categories.rb +22 -0
  36. data/lib/occi/helpers/comparators/category.rb +22 -0
  37. data/lib/occi/helpers/comparators/collection.rb +40 -0
  38. data/lib/occi/helpers/comparators/entities.rb +22 -0
  39. data/lib/occi/helpers/comparators/entity.rb +22 -0
  40. data/lib/occi/helpers/comparators/properties.rb +26 -0
  41. data/lib/occi/helpers/comparators.rb +1 -0
  42. data/lib/occi/infrastructure/compute.rb +11 -9
  43. data/lib/occi/infrastructure/network.rb +27 -27
  44. data/lib/occi/infrastructure/networkinterface.rb +22 -23
  45. data/lib/occi/infrastructure/os_tpl.rb +1 -1
  46. data/lib/occi/infrastructure/resource_tpl.rb +1 -1
  47. data/lib/occi/infrastructure/storage.rb +7 -6
  48. data/lib/occi/infrastructure/storagelink.rb +4 -4
  49. data/lib/occi/log.rb +13 -10
  50. data/lib/occi/model.rb +9 -8
  51. data/lib/occi/parser/json.rb +11 -9
  52. data/lib/occi/parser/ova.rb +12 -6
  53. data/lib/occi/parser/ovf.rb +173 -116
  54. data/lib/occi/parser/text/constants.rb +87 -0
  55. data/lib/occi/parser/text.rb +161 -200
  56. data/lib/occi/parser/xml.rb +10 -8
  57. data/lib/occi/parser.rb +100 -50
  58. data/lib/occi/settings.rb +2 -1
  59. data/lib/occi/version.rb +1 -1
  60. data/lib/occi-core.rb +6 -4
  61. data/occi-core.gemspec +0 -7
  62. data/spec/occi/collection_samples/collection1.json +1 -0
  63. data/spec/occi/collection_samples/directory2/collection2.json +1 -0
  64. data/spec/occi/collection_spec.rb +961 -31
  65. data/spec/occi/core/action_instance_spec.rb +317 -0
  66. data/spec/occi/core/action_spec.rb +71 -0
  67. data/spec/occi/core/attributes_spec.rb +582 -27
  68. data/spec/occi/core/category_spec.rb +194 -18
  69. data/spec/occi/core/entities_spec.rb +96 -0
  70. data/spec/occi/core/entity_spec.rb +317 -28
  71. data/spec/occi/core/kind_spec.rb +127 -16
  72. data/spec/occi/core/link_spec.rb +35 -0
  73. data/spec/occi/core/links_spec.rb +130 -0
  74. data/spec/occi/core/mixins_spec.rb +107 -0
  75. data/spec/occi/core/properties_spec.rb +167 -0
  76. data/spec/occi/core/resource_spec.rb +23 -9
  77. data/spec/occi/core_spec.rb +12 -0
  78. data/spec/occi/infrastructure/compute_spec.rb +218 -18
  79. data/spec/occi/infrastructure/network_spec.rb +96 -0
  80. data/spec/occi/infrastructure/networkinterface_spec.rb +96 -0
  81. data/spec/occi/infrastructure/storage_spec.rb +33 -0
  82. data/spec/occi/infrastructure/storagelink_spec.rb +45 -0
  83. data/spec/occi/log_spec.rb +104 -1
  84. data/spec/occi/model_spec.rb +251 -39
  85. data/spec/occi/{test.json → parser/json_samples/test.json} +0 -0
  86. data/spec/occi/parser/ova_samples/test.dump +0 -0
  87. data/spec/occi/{test.ova → parser/ova_samples/test.ova} +0 -0
  88. data/spec/occi/parser/ovf_samples/test.dump +0 -0
  89. data/spec/occi/{test.ovf → parser/ovf_samples/test.ovf} +0 -0
  90. data/spec/occi/parser/text_samples/occi_categories.dump +0 -0
  91. data/spec/occi/parser/text_samples/occi_categories.text +2 -0
  92. data/spec/occi/parser/text_samples/occi_compute_rocci_server.dump +0 -0
  93. data/spec/occi/parser/text_samples/occi_compute_rocci_server.resource.dump +0 -0
  94. data/spec/occi/parser/text_samples/occi_compute_rocci_server.text +10 -0
  95. data/spec/occi/parser/text_samples/occi_link_resource_instance.dump +0 -0
  96. data/spec/occi/parser/text_samples/occi_link_resource_instance.text +7 -0
  97. data/spec/occi/parser/text_samples/occi_link_simple.dump +0 -0
  98. data/spec/occi/parser/text_samples/occi_link_simple.link_string.dump +0 -0
  99. data/spec/occi/parser/text_samples/occi_link_simple.text +1 -0
  100. data/spec/occi/parser/text_samples/occi_link_w_attributes.dump +0 -0
  101. data/spec/occi/parser/text_samples/occi_link_w_attributes.text +7 -0
  102. data/spec/occi/parser/text_samples/occi_link_w_category.dump +0 -0
  103. data/spec/occi/parser/text_samples/occi_link_w_category.text +3 -0
  104. data/spec/occi/parser/text_samples/occi_model_rocci_server.dump +0 -0
  105. data/spec/occi/parser/text_samples/occi_model_rocci_server.text +51 -0
  106. data/spec/occi/parser/text_samples/occi_network_rocci_server.dump +0 -0
  107. data/spec/occi/parser/text_samples/occi_network_rocci_server.resource.dump +0 -0
  108. data/spec/occi/parser/text_samples/occi_network_rocci_server.text +11 -0
  109. data/spec/occi/parser/text_samples/occi_resource_w_attributes.dump +0 -0
  110. data/spec/occi/parser/text_samples/occi_resource_w_attributes.text +11 -0
  111. data/spec/occi/parser/text_samples/occi_resource_w_inline_links.dump +0 -0
  112. data/spec/occi/parser/text_samples/occi_resource_w_inline_links.text +16 -0
  113. data/spec/occi/parser/text_samples/occi_resource_w_inline_links_only.dump +0 -0
  114. data/spec/occi/parser/text_samples/occi_resource_w_inline_links_only.text +13 -0
  115. data/spec/occi/parser/text_samples/occi_storage_rocci_server.dump +0 -0
  116. data/spec/occi/parser/text_samples/occi_storage_rocci_server.resource.dump +0 -0
  117. data/spec/occi/parser/text_samples/occi_storage_rocci_server.text +9 -0
  118. data/spec/occi/parser/text_spec.rb +274 -78
  119. data/spec/occi/parser/xml_samples/test.xml +352 -0
  120. data/spec/occi/parser_spec.rb +255 -104
  121. data/spec/occi-core_spec.rb +31 -0
  122. data/spec/spec_helper.rb +6 -2
  123. metadata +110 -111
  124. checksums.yaml +0 -7
  125. data/spec/occi/core/attribute_spec.rb +0 -0
@@ -1,37 +1,967 @@
1
1
  module Occi
2
2
  describe Collection do
3
3
 
4
- it "creates a new Occi collection including the Occi base objects" do
5
- collection = Occi::Collection.new
6
- collection.kinds << "http://schemas.ogf.org/occi/infrastructure#compute"
7
- collection.mixins << "http://example.com/occi/tags#my_mixin"
8
- collection.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#start"
9
- collection.action = Occi::Core::ActionInstance.new
10
- collection.resources << Occi::Core::Resource.new
11
- collection.links << Occi::Core::Link.new
12
- collection.kinds.first.should be_kind_of Occi::Core::Kind
13
- collection.mixins.first.should be_kind_of Occi::Core::Mixin
14
- collection.actions.first.should be_kind_of Occi::Core::Action
15
- collection.resources.first.should be_kind_of Occi::Core::Resource
16
- collection.links.first.should be_kind_of Occi::Core::Link
17
- collection.action.should be_kind_of Occi::Core::ActionInstance
18
- end
19
-
20
- it "registers a model, creates a new OCCI Resource and checks it against the model" do
21
- collection = Occi::Collection.new
22
- collection.model.should be_kind_of Occi::Model
23
- collection.resources.create 'http://schemas.ogf.org/occi/core#resource'
24
- collection.resources.first.should be_kind_of Occi::Core::Resource
25
- expect { collection.check }.to_not raise_error
26
- end
27
-
28
- it 'successfully gets all categories related to a category' do
29
- collection = Occi::Collection.new
30
- collection.kinds << Occi::Core::Resource.kind
31
- collection.kinds << Occi::Core::Link.kind
32
- collection.get_related_to(Occi::Core::Entity.kind).should == collection
33
- collection.get_related_to(Occi::Core::Resource.kind).kinds.first.should == Occi::Core::Resource.kind
34
- collection.get_related_to(Occi::Core::Link.kind).kinds.first.should == Occi::Core::Link.kind
4
+ context 'initialization' do
5
+ let(:collection){ collection = Occi::Collection.new }
6
+
7
+ context 'with base objects' do
8
+ before(:each) {
9
+ collection.kinds << "http://schemas.ogf.org/occi/infrastructure#compute"
10
+ collection.mixins << "http://example.com/occi/tags#my_mixin"
11
+ collection.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#start"
12
+ collection.action = Occi::Core::ActionInstance.new
13
+ collection.resources << Occi::Core::Resource.new
14
+ collection.links << Occi::Core::Link.new
15
+ }
16
+
17
+ it 'calssifies kind correctly' do
18
+ expect(collection.kinds.first).to be_kind_of Occi::Core::Kind
19
+ end
20
+
21
+ it 'calssifies mixin correctly' do
22
+ expect(collection.mixins.first).to be_kind_of Occi::Core::Mixin
23
+ end
24
+
25
+ it 'calssifies action correctly' do
26
+ expect(collection.actions.first).to be_kind_of Occi::Core::Action
27
+ end
28
+
29
+ it 'calssifies resource correctly' do
30
+ expect(collection.resources.first).to be_kind_of Occi::Core::Resource
31
+ end
32
+
33
+ it 'calssifies link correctly' do
34
+ expect(collection.links.first).to be_kind_of Occi::Core::Link
35
+ end
36
+
37
+ it 'calssifies action instance correctly' do
38
+ expect(collection.action).to be_kind_of Occi::Core::ActionInstance
39
+ end
40
+ end
41
+ end
42
+
43
+ context '#model' do
44
+ let(:collection){ collection = Occi::Collection.new }
45
+ it 'registers a model' do
46
+ expect(collection.model).to be_kind_of Occi::Model
47
+ end
48
+ end
49
+
50
+ context '#resources' do
51
+ let(:collection){ collection = Occi::Collection.new }
52
+ it 'can create a new OCCI Resource' do
53
+ collection.resources.create 'http://schemas.ogf.org/occi/core#resource'
54
+ expect(collection.resources.first).to be_kind_of Occi::Core::Resource
55
+ end
56
+ end
57
+
58
+ context '#check' do
59
+ let(:collection){ collection = Occi::Collection.new }
60
+ it 'checks against model without failure' do
61
+ collection.resources.create 'http://schemas.ogf.org/occi/core#resource'
62
+ expect{ collection.check }.to_not raise_error
63
+ end
64
+ end
65
+
66
+ context '#get_related_to' do
67
+ let(:collection){ collection = Occi::Collection.new }
68
+ before(:each){
69
+ collection.kinds << Occi::Core::Resource.kind
70
+ collection.kinds << Occi::Core::Link.kind
71
+ }
72
+ it 'gets Entity as a related kind' do
73
+ expect(collection.get_related_to(Occi::Core::Entity.kind)).to eql collection
74
+ end
75
+
76
+ it 'gets Resource as a related kind' do
77
+ expect(collection.get_related_to(Occi::Core::Resource.kind).kinds.first).to eql Occi::Core::Resource.kind
78
+ end
79
+
80
+ it 'gets Link as a related kind' do
81
+ expect(collection.get_related_to(Occi::Core::Link.kind).kinds.first).to eql Occi::Core::Link.kind
82
+ end
83
+ end
84
+
85
+ context '#merge' do
86
+ let(:collection){ collection = Occi::Collection.new }
87
+ before(:each) {
88
+ collection.kinds << "http://schemas.ogf.org/occi/infrastructure#compute"
89
+ collection.mixins << "http://example.com/occi/tags#my_mixin"
90
+ collection.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#start"
91
+ collection.action = Occi::Core::ActionInstance.new
92
+ collection.resources << Occi::Core::Resource.new
93
+ collection.links << Occi::Core::Link.new
94
+ }
95
+ context 'two fully initiated collections' do
96
+ let(:action){ Occi::Core::Action.new scheme='http://schemas.ogf.org/occi/core/entity/action#', term='testaction', title='testaction action' }
97
+ let(:coll2){
98
+ coll2 = Occi::Collection.new
99
+ coll2.kinds << "http://schemas.ogf.org/occi/infrastructure#storage"
100
+ coll2.mixins << "http://example.com/occi/tags#another_mixin"
101
+ coll2.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#stop"
102
+ coll2.action = Occi::Core::ActionInstance.new action
103
+ coll2.resources << Occi::Core::Resource.new
104
+ coll2.links << Occi::Core::Link.new
105
+ coll2
106
+ }
107
+ let(:merged){ collection.merge(coll2, collection) }
108
+
109
+ context 'resulting collection' do
110
+ it 'has the correct number of kinds' do
111
+ expect(merged.kinds.count).to eql 2
112
+ end
113
+
114
+ it 'has the correct number of mixins' do
115
+ expect(merged.mixins.count).to eql 2
116
+ end
117
+
118
+ it 'has the correct number of actions' do
119
+ expect(merged.actions.count).to eql 2
120
+ end
121
+
122
+ it 'has the correct number of resources' do
123
+ expect(merged.resources.count).to eql 2
124
+ end
125
+
126
+ it 'has the correct number of links' do
127
+ expect(merged.links.count).to eql 2
128
+ end
129
+
130
+ it 'inherits action from the other collection' do
131
+ expect(merged.action.action.term).to eql "testaction"
132
+ end
133
+
134
+ it 'does not inherit action from first collection' do
135
+ expect(merged.action.action.term).to_not eql "action_instance"
136
+ end
137
+
138
+ it 'holds kinds from first collection' do
139
+ expect(collection.kinds.subset?(merged.kinds)).to eql true
140
+ end
141
+
142
+ it 'holds mixins from first collection' do
143
+ expect(collection.mixins.subset?(merged.mixins)).to eql true
144
+ end
145
+
146
+ it 'holds actions from first collection' do
147
+ expect(collection.actions.subset?(merged.actions)).to eql true
148
+ end
149
+
150
+ it 'holds resources from first collection' do
151
+ expect(collection.resources.subset?(merged.resources)).to eql true
152
+ end
153
+
154
+ it 'holds links from first collection' do
155
+ expect(collection.links.subset?(merged.links)).to eql true
156
+ end
157
+
158
+ it 'holds kinds from other collection' do
159
+ expect(coll2.kinds.subset?(merged.kinds)).to eql true
160
+ end
161
+
162
+ it 'holds mixins from other collection' do
163
+ expect(coll2.mixins.subset?(merged.mixins)).to eql true
164
+ end
165
+
166
+ it 'holds actions from other collection' do
167
+ expect(coll2.actions.subset?(merged.actions)).to eql true
168
+ end
169
+
170
+ it 'holds resources from other collection' do
171
+ expect(coll2.resources.subset?(merged.resources)).to eql true
172
+ end
173
+
174
+ it 'holds links from other collection' do
175
+ expect(coll2.links.subset?(merged.links)).to eql true
176
+ end
177
+
178
+ it 'does not replace first collection' do
179
+ expect(merged).to_not eql collection
180
+ end
181
+ end
182
+
183
+ context 'first original' do
184
+ it 'kept the correct number of kinds' do
185
+ expect(collection.kinds.count).to eql 1
186
+ end
187
+
188
+ it 'kept the correct number of mixins' do
189
+ expect(collection.mixins.count).to eql 1
190
+ end
191
+
192
+ it 'kept the correct number of actions' do
193
+ expect(collection.actions.count).to eql 1
194
+ end
195
+
196
+ it 'kept the correct number of resources' do
197
+ expect(collection.resources.count).to eql 1
198
+ end
199
+
200
+ it 'kept the correct number of links' do
201
+ expect(collection.links.count).to eql 1
202
+ end
203
+ end
204
+
205
+ context 'second original' do
206
+ it 'kept the correct number of kinds' do
207
+ expect(coll2.kinds.count).to eql 1
208
+ end
209
+
210
+ it 'kept the correct number of mixins' do
211
+ expect(coll2.mixins.count).to eql 1
212
+ end
213
+
214
+ it 'kept the correct number of actions' do
215
+ expect(coll2.actions.count).to eql 1
216
+ end
217
+
218
+ it 'kept the correct number of resources' do
219
+ expect(coll2.resources.count).to eql 1
220
+ end
221
+
222
+ it 'kept the correct number of links' do
223
+ expect(coll2.links.count).to eql 1
224
+ end
225
+ end
226
+ end
227
+
228
+ it 'copes with an empty collection' do
229
+ emptycol = Occi::Collection.new
230
+ expect{merged = collection.merge(emptycol, collection)}.to_not raise_error
231
+ end
232
+
233
+ it 'copes with both collections empty' do
234
+ empty1 = Occi::Collection.new
235
+ empty2 = Occi::Collection.new
236
+ expect{merged = collection.merge(empty1, empty2)}.to_not raise_error
237
+ end
238
+
239
+ end
240
+
241
+ context '#merge!' do
242
+ let(:collection){ collection = Occi::Collection.new
243
+ collection.kinds << "http://schemas.ogf.org/occi/infrastructure#compute"
244
+ collection.mixins << "http://example.com/occi/tags#my_mixin"
245
+ collection.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#start"
246
+ collection.action = Occi::Core::ActionInstance.new
247
+ collection.resources << Occi::Core::Resource.new
248
+ collection.links << Occi::Core::Link.new
249
+ collection
250
+ }
251
+ context 'two fully initiated collections' do
252
+ let(:action){ Occi::Core::Action.new scheme='http://schemas.ogf.org/occi/core/entity/action#', term='testaction', title='testaction action' }
253
+ let(:coll2){
254
+ coll2 = Occi::Collection.new
255
+ coll2.kinds << "http://schemas.ogf.org/occi/infrastructure#storage"
256
+ coll2.mixins << "http://example.com/occi/tags#another_mixin"
257
+ coll2.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#stop"
258
+ coll2.action = Occi::Core::ActionInstance.new action
259
+ coll2.resources << Occi::Core::Resource.new
260
+ coll2.links << Occi::Core::Link.new
261
+ coll2
262
+ }
263
+ before(:each) { collection.merge!(coll2) }
264
+ context 'resulting collection' do
265
+ it 'has the correct number of kinds' do
266
+ expect(collection.kinds.count).to eql 2
267
+ end
268
+
269
+ it 'has the correct number of mixins' do
270
+ expect(collection.mixins.count).to eql 2
271
+ end
272
+
273
+ it 'has the correct number of actions' do
274
+ expect(collection.actions.count).to eql 2
275
+ end
276
+
277
+ it 'has the correct number of resources' do
278
+ expect(collection.resources.count).to eql 2
279
+ end
280
+
281
+ it 'has the correct number of links' do
282
+ expect(collection.links.count).to eql 2
283
+ end
284
+
285
+ it 'inherits action from the other collection' do
286
+ expect(collection.action.action.term).to eql "testaction"
287
+ end
288
+
289
+ it 'does not inherit action from first collection' do
290
+ expect(collection.action.action.term).to_not eql "action_instance"
291
+ end
292
+
293
+ it 'holds kinds from other collection' do
294
+ expect(coll2.kinds.subset?(collection.kinds)).to eql true
295
+ end
296
+
297
+ it 'holds mixins from other collection' do
298
+ expect(coll2.mixins.subset?(collection.mixins)).to eql true
299
+ end
300
+
301
+ it 'holds actions from other collection' do
302
+ expect(coll2.actions.subset?(collection.actions)).to eql true
303
+ end
304
+
305
+ it 'holds resources from other collection' do
306
+ expect(coll2.resources.subset?(collection.resources)).to eql true
307
+ end
308
+
309
+ it 'holds links from other collection' do
310
+ expect(coll2.links.subset?(collection.links)).to eql true
311
+ end
312
+ end
313
+
314
+ context 'the other collection' do
315
+ it 'kept the correct number of kinds' do
316
+ expect(coll2.kinds.count).to eql 1
317
+ end
318
+
319
+ it 'kept the correct number of mixins' do
320
+ expect(coll2.mixins.count).to eql 1
321
+ end
322
+
323
+ it 'kept the correct number of actions' do
324
+ expect(coll2.actions.count).to eql 1
325
+ end
326
+
327
+ it 'kept the correct number of resources' do
328
+ expect(coll2.resources.count).to eql 1
329
+ end
330
+
331
+ it 'kept the correct number of links' do
332
+ expect(coll2.links.count).to eql 1
333
+ end
334
+ end
335
+ end
336
+ it 'copes with other collection empty' do
337
+ emptycol = Occi::Collection.new
338
+ expect{ collection.merge!(emptycol) }.to_not raise_error
339
+ end
340
+
341
+ it 'copes with self empty' do
342
+ emptycol = Occi::Collection.new
343
+ expect{ emptycol.merge!(collection) }.to_not raise_error
344
+ end
345
+
346
+ it 'copes with both collections empty' do
347
+ empty1 = Occi::Collection.new
348
+ empty2 = Occi::Collection.new
349
+ expect{ empty1.merge(empty2) }.to_not raise_error
350
+ end
351
+
352
+ it 'combines two empty sets into another empty one' do
353
+ empty1 = Occi::Collection.new
354
+ empty2 = Occi::Collection.new
355
+ empty1.merge(empty2)
356
+ expect(empty1.empty?).to eql true
357
+ end
358
+
359
+ end
360
+
361
+ context '#intersect' do
362
+ let(:collection){ collection = Occi::Collection.new }
363
+ before(:each) {
364
+ collection.kinds << "http://schemas.ogf.org/occi/infrastructure#compute"
365
+ collection.kinds << "http://schemas.ogf.org/occi/infrastructure#network"
366
+ collection.mixins << "http://example.com/occi/tags#my_mixin"
367
+ collection.mixins << "http://example.com/occi/tags#still_another_mixin"
368
+ collection.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#start"
369
+ collection.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#restart"
370
+ collection.action = Occi::Core::ActionInstance.new
371
+ collection.resources << Occi::Core::Resource.new
372
+ collection.resources << Occi::Core::Resource.new
373
+ collection.links << Occi::Core::Link.new
374
+ collection.links << Occi::Core::Link.new
375
+ }
376
+ context 'two fully initiated collections' do
377
+ let(:action){ Occi::Core::Action.new scheme='http://schemas.ogf.org/occi/core/entity/action#', term='testaction', title='testaction action' }
378
+ let(:coll2){
379
+ coll2 = Occi::Collection.new
380
+ coll2.kinds << "http://schemas.ogf.org/occi/infrastructure#storage"
381
+ coll2.kinds << "http://schemas.ogf.org/occi/infrastructure#compute"
382
+ coll2.mixins << "http://example.com/occi/tags#another_mixin"
383
+ coll2.mixins << "http://example.com/occi/tags#my_mixin"
384
+ coll2.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#stop"
385
+ coll2.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#restart"
386
+ coll2.action = Occi::Core::ActionInstance.new action
387
+ coll2.resources << Occi::Core::Resource.new
388
+ coll2.resources << collection.resources.first
389
+ coll2.links << Occi::Core::Link.new
390
+ coll2.links << collection.resources.first
391
+ coll2
392
+ }
393
+ let(:isection){ collection.intersect(coll2, collection) }
394
+
395
+ context 'resulting collection' do
396
+ it 'has the correct number of kinds' do
397
+ expect(isection.kinds.count).to eql 1
398
+ end
399
+
400
+ it 'has the correct number of mixins' do
401
+ expect(isection.mixins.count).to eql 1
402
+ end
403
+
404
+ it 'has the correct number of actions' do
405
+ expect(isection.actions.count).to eql 1
406
+ end
407
+
408
+ it 'has the correct number of resources' do
409
+ expect(isection.resources.count).to eql 1
410
+ end
411
+
412
+ it 'has the correct number of links' do
413
+ expect(isection.links.count).to eql 1
414
+ end
415
+
416
+ it 'does not include unequal actions' do
417
+ expect(isection.action.blank?).to eql true
418
+ end
419
+
420
+ it 'holds kinds from first collection' do
421
+ expect(isection.kinds.subset?(collection.kinds)).to eql true
422
+ end
423
+
424
+ it 'holds mixins from first collection' do
425
+ expect(isection.mixins.subset?(collection.mixins)).to eql true
426
+ end
427
+
428
+ it 'holds actions from first collection' do
429
+ expect(isection.actions.subset?(collection.actions)).to eql true
430
+ end
431
+
432
+ it 'holds resources from first collection' do
433
+ expect(isection.resources.subset?(collection.resources)).to eql true
434
+ end
435
+
436
+ it 'holds links from first collection' do
437
+ expect(isection.links.subset?(collection.links)).to eql true
438
+ end
439
+
440
+ it 'holds kinds from other collection' do
441
+ expect(isection.kinds.subset?(coll2.kinds)).to eql true
442
+ end
443
+
444
+ it 'holds mixins from other collection' do
445
+ expect(isection.mixins.subset?(coll2.mixins)).to eql true
446
+ end
447
+
448
+ it 'holds actions from other collection' do
449
+ expect(isection.actions.subset?(coll2.actions)).to eql true
450
+ end
451
+
452
+ it 'holds resources from other collection' do
453
+ expect(isection.resources.subset?(coll2.resources)).to eql true
454
+ end
455
+
456
+ it 'holds links from other collection' do
457
+ expect(isection.links.subset?(coll2.links)).to eql true
458
+ end
459
+
460
+ it 'does not replace first collection' do
461
+ expect(coll2.links.subset?(collection.links)).to_not eql true
462
+ end
463
+ end
464
+
465
+ context 'first original' do
466
+ it 'kept the correct number of kinds' do
467
+ expect(collection.kinds.count).to eql 2
468
+ end
469
+
470
+ it 'kept the correct number of mixins' do
471
+ expect(collection.mixins.count).to eql 2
472
+ end
473
+
474
+ it 'kept the correct number of actions' do
475
+ expect(collection.actions.count).to eql 2
476
+ end
477
+
478
+ it 'kept the correct number of resources' do
479
+ expect(collection.resources.count).to eql 2
480
+ end
481
+
482
+ it 'kept the correct number of links' do
483
+ expect(collection.links.count).to eql 2
484
+ end
485
+ end
486
+
487
+ context 'second original' do
488
+ it 'kept the correct number of kinds' do
489
+ expect(coll2.kinds.count).to eql 2
490
+ end
491
+
492
+ it 'kept the correct number of mixins' do
493
+ expect(coll2.mixins.count).to eql 2
494
+ end
495
+
496
+ it 'kept the correct number of actions' do
497
+ expect(coll2.actions.count).to eql 2
498
+ end
499
+
500
+ it 'kept the correct number of resources' do
501
+ expect(coll2.resources.count).to eql 2
502
+ end
503
+
504
+ it 'kept the correct number of links' do
505
+ expect(coll2.links.count).to eql 2
506
+ end
507
+ end
508
+ end
509
+
510
+ it 'copes with an empty collection' do
511
+ emptycol = Occi::Collection.new
512
+ expect{ isection = collection.intersect(emptycol, collection)}.to_not raise_error
513
+ end
514
+
515
+ it 'copes with both collections empty' do
516
+ empty1 = Occi::Collection.new
517
+ empty2 = Occi::Collection.new
518
+ expect{ isection = collection.intersect(empty1, empty2)}.to_not raise_error
519
+ end
520
+ end
521
+ context '#intersect!' do
522
+ let(:collection){
523
+ collection = Occi::Collection.new
524
+ collection.kinds << "http://schemas.ogf.org/occi/infrastructure#compute"
525
+ collection.kinds << "http://schemas.ogf.org/occi/infrastructure#network"
526
+ collection.mixins << "http://example.com/occi/tags#my_mixin"
527
+ collection.mixins << "http://example.com/occi/tags#still_another_mixin"
528
+ collection.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#start"
529
+ collection.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#restart"
530
+ collection.action = Occi::Core::ActionInstance.new
531
+ collection.resources << Occi::Core::Resource.new
532
+ collection.resources << Occi::Core::Resource.new
533
+ collection.links << Occi::Core::Link.new
534
+ collection.links << Occi::Core::Link.new
535
+ collection }
536
+ context 'two fully initiated collections' do
537
+ let(:action){ Occi::Core::Action.new scheme='http://schemas.ogf.org/occi/core/entity/action#', term='testaction', title='testaction action' }
538
+ let(:coll2){
539
+ coll2 = Occi::Collection.new
540
+ coll2.kinds << "http://schemas.ogf.org/occi/infrastructure#storage"
541
+ coll2.kinds << "http://schemas.ogf.org/occi/infrastructure#compute"
542
+ coll2.mixins << "http://example.com/occi/tags#another_mixin"
543
+ coll2.mixins << "http://example.com/occi/tags#my_mixin"
544
+ coll2.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#stop"
545
+ coll2.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#restart"
546
+ coll2.action = Occi::Core::ActionInstance.new action
547
+ coll2.resources << Occi::Core::Resource.new
548
+ coll2.resources << collection.resources.first
549
+ coll2.links << Occi::Core::Link.new
550
+ coll2.links << collection.resources.first
551
+ coll2
552
+ }
553
+ before(:each){ collection.intersect!(coll2) }
554
+
555
+ context 'resulting collection' do
556
+ it 'has the correct number of kinds' do
557
+ expect(collection.kinds.count).to eql 1
558
+ end
559
+
560
+ it 'has the correct number of mixins' do
561
+ expect(collection.mixins.count).to eql 1
562
+ end
563
+
564
+ it 'has the correct number of actions' do
565
+ expect(collection.actions.count).to eql 1
566
+ end
567
+
568
+ it 'has the correct number of resources' do
569
+ expect(collection.resources.count).to eql 1
570
+ end
571
+
572
+ it 'has the correct number of links' do
573
+ expect(collection.links.count).to eql 1
574
+ end
575
+
576
+ it 'does not include unequal actions' do
577
+ expect(collection.action.blank?).to eql true
578
+ end
579
+
580
+ it 'holds kinds from first collection' do
581
+ expect(collection.kinds.subset?(collection.kinds)).to eql true
582
+ end
583
+
584
+ it 'holds mixins from first collection' do
585
+ expect(collection.mixins.subset?(collection.mixins)).to eql true
586
+ end
587
+
588
+ it 'holds actions from first collection' do
589
+ expect(collection.actions.subset?(collection.actions)).to eql true
590
+ end
591
+
592
+ it 'holds resources from first collection' do
593
+ expect(collection.resources.subset?(collection.resources)).to eql true
594
+ end
595
+
596
+ it 'holds links from first collection' do
597
+ expect(collection.links.subset?(collection.links)).to eql true
598
+ end
599
+
600
+ it 'holds kinds from other collection' do
601
+ expect(collection.kinds.subset?(coll2.kinds)).to eql true
602
+ end
603
+
604
+ it 'holds mixins from other collection' do
605
+ expect(collection.mixins.subset?(coll2.mixins)).to eql true
606
+ end
607
+
608
+ it 'holds actions from other collection' do
609
+ expect(collection.actions.subset?(coll2.actions)).to eql true
610
+ end
611
+
612
+ it 'holds resources from other collection' do
613
+ expect(collection.resources.subset?(coll2.resources)).to eql true
614
+ end
615
+
616
+ it 'holds links from other collection' do
617
+ expect(collection.links.subset?(coll2.links)).to eql true
618
+ end
619
+ end
620
+
621
+ context 'second original' do
622
+ it 'kept the correct number of kinds' do
623
+ expect(coll2.kinds.count).to eql 2
624
+ end
625
+
626
+ it 'kept the correct number of mixins' do
627
+ expect(coll2.mixins.count).to eql 2
628
+ end
629
+
630
+ it 'kept the correct number of actions' do
631
+ expect(coll2.actions.count).to eql 2
632
+ end
633
+
634
+ it 'kept the correct number of resources' do
635
+ expect(coll2.resources.count).to eql 2
636
+ end
637
+
638
+ it 'kept the correct number of links' do
639
+ expect(coll2.links.count).to eql 2
640
+ end
641
+ end
642
+ end
643
+
644
+ context 'collections with no intersection' do
645
+ let(:action){ Occi::Core::Action.new scheme='http://schemas.ogf.org/occi/core/entity/action#', term='testaction', title='testaction action' }
646
+ let(:uniq1) {
647
+ uniq1 = Occi::Collection.new
648
+ uniq1.kinds << "http://schemas.ogf.org/occi/infrastructure#compute"
649
+ uniq1.mixins << "http://example.com/occi/tags#my_mixin"
650
+ uniq1.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#start"
651
+ uniq1.action = Occi::Core::ActionInstance.new
652
+ uniq1.resources << Occi::Core::Resource.new
653
+ uniq1.links << Occi::Core::Link.new
654
+ uniq1
655
+ }
656
+ let(:uniq2) {
657
+ uniq2 = Occi::Collection.new
658
+ uniq2.kinds << "http://schemas.ogf.org/occi/infrastructure#storage"
659
+ uniq2.mixins << "http://example.com/occi/tags#another_mixin"
660
+ uniq2.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#stop"
661
+ uniq2.action = Occi::Core::ActionInstance.new action
662
+ uniq2.resources << Occi::Core::Resource.new
663
+ uniq2.links << Occi::Core::Link.new
664
+ uniq2
665
+ }
666
+ it 'works with both collections populated' do
667
+ uniq1.intersect!(uniq2)
668
+ expect(uniq1.empty?).to eql true
669
+ end
670
+
671
+ it 'works with both collections empty' do
672
+ empty1 = Occi::Collection.new
673
+ empty2 = Occi::Collection.new
674
+
675
+ empty1.intersect!(empty2)
676
+ expect(empty1.empty?).to eql true
677
+ end
678
+
679
+ it 'works with first collection empty' do
680
+ empty = Occi::Collection.new
681
+
682
+ uniq1.intersect!(empty)
683
+ expect(empty.empty?).to eql true
684
+ end
685
+
686
+ it 'works with second collection empty' do
687
+ empty = Occi::Collection.new
688
+
689
+ empty.intersect!(uniq2)
690
+ expect(empty.empty?).to eql true
691
+ end
692
+ end
693
+ end
694
+
695
+ context '#get_by_...' do
696
+ let(:collection){ collection = Occi::Collection.new
697
+ collection.kinds << "http://schemas.ogf.org/occi/infrastructure#compute"
698
+ collection.mixins << "http://example.com/occi/tags#my_mixin"
699
+ collection.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#start"
700
+ collection.action = Occi::Core::ActionInstance.new
701
+ collection.resources << Occi::Core::Resource.new
702
+ collection.links << Occi::Core::Link.new
703
+ collection
704
+ }
705
+
706
+ context '#get_by_id' do
707
+ it 'finds entity by id' do
708
+ expect(collection.get_by_id(collection.links.first.id)).to eql collection.links.first
709
+ end
710
+
711
+ it 'finds category by id' do
712
+ expect(collection.get_by_id("http://example.com/occi/tags#my_mixin")).to eql collection.mixins.first
713
+ end
714
+
715
+ it 'fails gracefully' do
716
+ expect(collection.get_by_id("ID.notexist")).to eql nil
717
+ end
718
+
719
+ end
720
+
721
+ context '#get_by_location' do
722
+ it 'finds category by location' do
723
+ expect(collection.get_by_location("/mixins/my_mixin/")).to eql collection.mixins.first
724
+ end
725
+
726
+ it 'fails gracefully' do
727
+ expect(collection.get_by_location("/notexist/")).to eql nil
728
+ end
729
+ end
730
+ end
731
+
732
+ context '#empty?' do
733
+ let(:collection){ Occi::Collection.new }
734
+ it 'returns true for empty collection' do
735
+ expect(collection.empty?).to eql true
736
+ end
737
+
738
+ it 'does returns false for non-empty kinds' do
739
+ collection.kinds << "http://schemas.ogf.org/occi/infrastructure#compute"
740
+ expect(collection.empty?).to eql false
741
+ end
742
+
743
+ it 'does returns false for non-empty mixnis' do
744
+ collection.mixins << "http://example.com/occi/tags#my_mixin"
745
+ expect(collection.empty?).to eql false
746
+ end
747
+
748
+ it 'does returns false for non-empty actions' do
749
+ collection.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#start"
750
+ expect(collection.empty?).to eql false
751
+ end
752
+
753
+ it 'does returns false for non-empty action' do
754
+ collection.action = Occi::Core::ActionInstance.new
755
+ expect(collection.empty?).to eql false
756
+ end
757
+
758
+ it 'does returns false for non-empty resources' do
759
+ collection.resources << Occi::Core::Resource.new
760
+ expect(collection.empty?).to eql false
761
+ end
762
+
763
+ it 'does returns false for non-empty links' do
764
+ collection.links << Occi::Core::Link.new
765
+ expect(collection.empty?).to eql false
766
+ end
767
+ end
768
+
769
+ context '#as_json' do
770
+ it 'renders JSON correctly for a simple collection' do
771
+ collection = Occi::Collection.new
772
+ collection.kinds << "http://schemas.ogf.org/occi/infrastructure#compute"
773
+ collection.mixins << "http://example.com/occi/tags#my_mixin"
774
+ collection.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#start"
775
+ collection.action = Occi::Core::ActionInstance.new
776
+ collection.resources << Occi::Core::Resource.new
777
+ collection.links << Occi::Core::Link.new
778
+ expected = "{\"action\":{\"action\":\"http://schemas.ogf.org/occi/core#action_instance\",\"attributes\":{}},\"actions\":[{\"scheme\":\"http://schemas.ogf.org/occi/infrastructure/compute/action#\",\"term\":\"start\",\"attributes\":{}}],\"kinds\":[{\"parent\":\"http://schemas.ogf.org/occi/core#resource\",\"related\":[\"http://schemas.ogf.org/occi/core#resource\"],\"actions\":[\"http://schemas.ogf.org/occi/infrastructure/compute/action#start\",\"http://schemas.ogf.org/occi/infrastructure/compute/action#stop\",\"http://schemas.ogf.org/occi/infrastructure/compute/action#restart\",\"http://schemas.ogf.org/occi/infrastructure/compute/action#suspend\"],\"location\":\"/compute/\",\"scheme\":\"http://schemas.ogf.org/occi/infrastructure#\",\"term\":\"compute\",\"title\":\"compute resource\",\"attributes\":{\"occi\":{\"core\":{\"id\":{\"type\":\"string\",\"required\":false,\"mutable\":false,\"pattern\":\"[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}\"},\"title\":{\"type\":\"string\",\"required\":false,\"mutable\":true,\"pattern\":\".*\"},\"summary\":{\"type\":\"string\",\"required\":false,\"mutable\":true,\"pattern\":\".*\"}},\"compute\":{\"architecture\":{\"type\":\"string\",\"required\":false,\"mutable\":true,\"pattern\":\"x86|x64\"},\"cores\":{\"type\":\"number\",\"required\":false,\"mutable\":true,\"pattern\":\".*\"},\"hostname\":{\"type\":\"string\",\"required\":false,\"mutable\":true,\"pattern\":\"(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\\\-]*[a-zA-Z0-9])\\\\.)*\"},\"memory\":{\"type\":\"number\",\"required\":false,\"mutable\":true,\"pattern\":\".*\"},\"speed\":{\"type\":\"number\",\"required\":false,\"mutable\":true,\"pattern\":\".*\"},\"state\":{\"type\":\"string\",\"required\":false,\"mutable\":false,\"pattern\":\"inactive|active|suspended|error\"}}}}}],\"links\":[{\"kind\":\"http://schemas.ogf.org/occi/core#link\",\"attributes\":{\"occi\":{\"core\":{\"id\":\"#{collection.links.first.id}\"}}},\"id\":\"#{collection.links.first.id}\",\"rel\":\"http://schemas.ogf.org/occi/core#link\"}],\"mixins\":[{\"location\":\"/mixins/my_mixin/\",\"scheme\":\"http://example.com/occi/tags#\",\"term\":\"my_mixin\",\"attributes\":{}}],\"resources\":[{\"kind\":\"http://schemas.ogf.org/occi/core#resource\",\"attributes\":{\"occi\":{\"core\":{\"id\":\"#{collection.resources.first.id}\"}}},\"id\":\"#{collection.resources.first.id}\"}]}"
779
+
780
+ hash=Hashie::Mash.new(JSON.parse(expected))
781
+ expect(collection.as_json).to eql(hash)
782
+ end
783
+
784
+ it 'renders JSON for an empty collection' do
785
+ collection = Occi::Collection.new
786
+ hash=Hashie::Mash.new
787
+ expect(collection.as_json).to eql(hash)
788
+ end
789
+
790
+ it 'renders JSON for a collection with no resources' do
791
+ collection = Occi::Collection.new
792
+ collection.kinds << "http://schemas.ogf.org/occi/infrastructure#compute"
793
+ collection.mixins << "http://example.com/occi/tags#my_mixin"
794
+ collection.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#start"
795
+ collection.action = Occi::Core::ActionInstance.new
796
+ collection.links << Occi::Core::Link.new
797
+ expected = "{\"action\":{\"action\":\"http://schemas.ogf.org/occi/core#action_instance\",\"attributes\":{}},\"actions\":[{\"scheme\":\"http://schemas.ogf.org/occi/infrastructure/compute/action#\",\"term\":\"start\",\"attributes\":{}}],\"kinds\":[{\"parent\":\"http://schemas.ogf.org/occi/core#resource\",\"related\":[\"http://schemas.ogf.org/occi/core#resource\"],\"actions\":[\"http://schemas.ogf.org/occi/infrastructure/compute/action#start\",\"http://schemas.ogf.org/occi/infrastructure/compute/action#stop\",\"http://schemas.ogf.org/occi/infrastructure/compute/action#restart\",\"http://schemas.ogf.org/occi/infrastructure/compute/action#suspend\"],\"location\":\"/compute/\",\"scheme\":\"http://schemas.ogf.org/occi/infrastructure#\",\"term\":\"compute\",\"title\":\"compute resource\",\"attributes\":{\"occi\":{\"core\":{\"id\":{\"type\":\"string\",\"required\":false,\"mutable\":false,\"pattern\":\"[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}\"},\"title\":{\"type\":\"string\",\"required\":false,\"mutable\":true,\"pattern\":\".*\"},\"summary\":{\"type\":\"string\",\"required\":false,\"mutable\":true,\"pattern\":\".*\"}},\"compute\":{\"architecture\":{\"type\":\"string\",\"required\":false,\"mutable\":true,\"pattern\":\"x86|x64\"},\"cores\":{\"type\":\"number\",\"required\":false,\"mutable\":true,\"pattern\":\".*\"},\"hostname\":{\"type\":\"string\",\"required\":false,\"mutable\":true,\"pattern\":\"(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\\\-]*[a-zA-Z0-9])\\\\.)*\"},\"memory\":{\"type\":\"number\",\"required\":false,\"mutable\":true,\"pattern\":\".*\"},\"speed\":{\"type\":\"number\",\"required\":false,\"mutable\":true,\"pattern\":\".*\"},\"state\":{\"type\":\"string\",\"required\":false,\"mutable\":false,\"pattern\":\"inactive|active|suspended|error\"}}}}}],\"links\":[{\"kind\":\"http://schemas.ogf.org/occi/core#link\",\"attributes\":{\"occi\":{\"core\":{\"id\":\"#{collection.links.first.id}\"}}},\"id\":\"#{collection.links.first.id}\",\"rel\":\"http://schemas.ogf.org/occi/core#link\"}],\"mixins\":[{\"location\":\"/mixins/my_mixin/\",\"scheme\":\"http://example.com/occi/tags#\",\"term\":\"my_mixin\",\"attributes\":{}}]}"
798
+
799
+ hash=Hashie::Mash.new(JSON.parse(expected))
800
+ expect(collection.as_json).to eql(hash)
801
+ end
802
+ end
803
+
804
+ context '#to_text' do
805
+ let(:collection){ Occi::Collection.new }
806
+ it 'renders text correctly for a simple collection' do
807
+ collection.kinds << "http://schemas.ogf.org/occi/infrastructure#compute"
808
+ collection.mixins << "http://example.com/occi/tags#my_mixin"
809
+ collection.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#start"
810
+ collection.action = Occi::Core::ActionInstance.new
811
+ collection.resources << Occi::Core::Resource.new
812
+ collection.links << Occi::Core::Link.new
813
+
814
+ expected = "Category: compute;scheme=\"http://schemas.ogf.org/occi/infrastructure#\";class=\"kind\";title=\"compute resource\";rel=\"http://schemas.ogf.org/occi/core#resource\";location=\"/compute/\";attributes=\"occi.core.id occi.core.title occi.core.summary occi.compute.architecture occi.compute.cores occi.compute.hostname occi.compute.memory occi.compute.speed occi.compute.state\";actions=\"http://schemas.ogf.org/occi/infrastructure/compute/action#start http://schemas.ogf.org/occi/infrastructure/compute/action#stop http://schemas.ogf.org/occi/infrastructure/compute/action#restart http://schemas.ogf.org/occi/infrastructure/compute/action#suspend\"\nCategory: my_mixin;scheme=\"http://example.com/occi/tags#\";class=\"mixin\";location=\"/mixins/my_mixin/\"\nCategory: start;scheme=\"http://schemas.ogf.org/occi/infrastructure/compute/action#\";class=\"action\"\nCategory: resource;scheme=\"http://schemas.ogf.org/occi/core#\";class=\"kind\"\nX-OCCI-Attribute: occi.core.id=\"#{collection.resources.first.id}\"Link: <>;rel=\"http://schemas.ogf.org/occi/core#link\";self=\"/link/#{collection.links.first.id}\";category=\"http://schemas.ogf.org/occi/core#link\";occi.core.id=\"#{collection.links.first.id}\"Category: action_instance;scheme=\"http://schemas.ogf.org/occi/core#\";class=\"action\""
815
+ expect(collection.to_text).to eql(expected)
816
+ end
817
+
818
+ it 'renders text correctly for an empty collection' do
819
+ expected = ''
820
+ expect(collection.to_text).to eql(expected)
821
+ end
822
+
823
+ it 'renders text correctly, kinds only' do
824
+ collection.kinds << "http://schemas.ogf.org/occi/infrastructure#compute"
825
+ expected = "Category: compute;scheme=\"http://schemas.ogf.org/occi/infrastructure#\";class=\"kind\";title=\"compute resource\";rel=\"http://schemas.ogf.org/occi/core#resource\";location=\"/compute/\";attributes=\"occi.core.id occi.core.title occi.core.summary occi.compute.architecture occi.compute.cores occi.compute.hostname occi.compute.memory occi.compute.speed occi.compute.state\";actions=\"http://schemas.ogf.org/occi/infrastructure/compute/action#start http://schemas.ogf.org/occi/infrastructure/compute/action#stop http://schemas.ogf.org/occi/infrastructure/compute/action#restart http://schemas.ogf.org/occi/infrastructure/compute/action#suspend\"\n"
826
+ expect(collection.to_text).to eql(expected)
827
+ end
828
+
829
+ it 'renders text correctly, mixins only' do
830
+ collection.mixins << "http://example.com/occi/tags#my_mixin"
831
+ expected = "Category: my_mixin;scheme=\"http://example.com/occi/tags#\";class=\"mixin\";location=\"/mixins/my_mixin/\"\n"
832
+ expect(collection.to_text).to eql(expected)
833
+ end
834
+
835
+ it 'renders text correctly, actions only' do
836
+ collection.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#start"
837
+ collection.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#restart"
838
+ collection.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#stop"
839
+ expected = "Category: start;scheme=\"http://schemas.ogf.org/occi/infrastructure/compute/action#\";class=\"action\"\nCategory: restart;scheme=\"http://schemas.ogf.org/occi/infrastructure/compute/action#\";class=\"action\"\nCategory: stop;scheme=\"http://schemas.ogf.org/occi/infrastructure/compute/action#\";class=\"action\"\n"
840
+ expect(collection.to_text).to eql(expected)
841
+ end
842
+
843
+ it 'renders text correctly, action instance only' do
844
+ action = Occi::Core::Action.new scheme='http://schemas.ogf.org/occi/core/entity/action#', term='testaction', title='testaction action'
845
+ collection.action = Occi::Core::ActionInstance.new action
846
+ expected = "Category: testaction;scheme=\"http://schemas.ogf.org/occi/core/entity/action#\";class=\"action\""
847
+ expect(collection.to_text).to eql(expected)
848
+ end
849
+
850
+ it 'renders text correctly, resources only' do
851
+ collection.resources << Occi::Core::Resource.new
852
+ expected = "Category: resource;scheme=\"http://schemas.ogf.org/occi/core#\";class=\"kind\"\nX-OCCI-Attribute: occi.core.id=\"#{collection.resources.first.id}\""
853
+ expect(collection.to_text).to eql(expected)
854
+ end
855
+
856
+ it 'renders text correctly, links only' do
857
+ collection.links << Occi::Core::Link.new
858
+ expected = "Link: <>;rel=\"http://schemas.ogf.org/occi/core#link\";self=\"/link/#{collection.links.first.id}\";category=\"http://schemas.ogf.org/occi/core#link\";occi.core.id=\"#{collection.links.first.id}\""
859
+ expect(collection.to_text).to eql(expected)
860
+ end
861
+
862
+ end
863
+
864
+ context '#to_header' do
865
+ let(:collection){ Occi::Collection.new }
866
+ it 'renders header correctly for a simple collection' do
867
+ collection.kinds << "http://schemas.ogf.org/occi/infrastructure#compute"
868
+ collection.mixins << "http://example.com/occi/tags#my_mixin"
869
+ collection.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#start"
870
+ collection.action = Occi::Core::ActionInstance.new
871
+ collection.resources << Occi::Core::Resource.new
872
+ collection.links << Occi::Core::Link.new
873
+
874
+ expected=Hashie::Mash.new
875
+ expected["Category"] = "compute;scheme=\"http://schemas.ogf.org/occi/infrastructure#\";class=\"kind\",my_mixin;scheme=\"http://example.com/occi/tags#\";class=\"mixin\",start;scheme=\"http://schemas.ogf.org/occi/infrastructure/compute/action#\";class=\"action\",resource;scheme=\"http://schemas.ogf.org/occi/core#\";class=\"kind\",action_instance;scheme=\"http://schemas.ogf.org/occi/core#\";class=\"action\""
876
+ expected["Link"] = "<>;rel=\"http://schemas.ogf.org/occi/core#link\";self=\"/link/#{collection.links.first.id}\";category=\"http://schemas.ogf.org/occi/core#link\";occi.core.id=\"#{collection.links.first.id}\""
877
+ expected["X-OCCI-Attribute"] = "occi.core.id=\"#{collection.resources.first.id}\""
878
+ expect(collection.to_header).to eql(expected)
879
+ end
880
+
881
+ it 'renders text correctly for an empty collection' do
882
+ expected=Hashie::Mash.new
883
+ expect(collection.to_header).to eql(expected)
884
+ end
885
+
886
+ it 'renders text correctly, kinds only' do
887
+ collection.kinds << "http://schemas.ogf.org/occi/infrastructure#compute"
888
+ expected=Hashie::Mash.new
889
+ expected["Category"] = "compute;scheme=\"http://schemas.ogf.org/occi/infrastructure#\";class=\"kind\""
890
+ expect(collection.to_header).to eql(expected)
891
+ end
892
+
893
+ it 'renders text correctly, mixins only' do
894
+ collection.mixins << "http://example.com/occi/tags#my_mixin"
895
+ expected=Hashie::Mash.new
896
+ expected["Category"] = "my_mixin;scheme=\"http://example.com/occi/tags#\";class=\"mixin\""
897
+ expect(collection.to_header).to eql(expected)
898
+ end
899
+
900
+ it 'renders text correctly, actions only' do
901
+ collection.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#start"
902
+ collection.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#restart"
903
+ collection.actions << "http://schemas.ogf.org/occi/infrastructure/compute/action#stop"
904
+ expected=Hashie::Mash.new
905
+ expected["Category"] = "start;scheme=\"http://schemas.ogf.org/occi/infrastructure/compute/action#\";class=\"action\",restart;scheme=\"http://schemas.ogf.org/occi/infrastructure/compute/action#\";class=\"action\",stop;scheme=\"http://schemas.ogf.org/occi/infrastructure/compute/action#\";class=\"action\""
906
+ expect(collection.to_header).to eql(expected)
907
+ end
908
+
909
+ it 'renders text correctly, action instance only' do
910
+ action = Occi::Core::Action.new scheme='http://schemas.ogf.org/occi/core/entity/action#', term='testaction', title='testaction action'
911
+ collection.action = Occi::Core::ActionInstance.new action
912
+ expected=Hashie::Mash.new
913
+ expected["Category"] = "testaction;scheme=\"http://schemas.ogf.org/occi/core/entity/action#\";class=\"action\""
914
+ expect(collection.to_header).to eql(expected)
915
+ end
916
+
917
+ it 'renders text correctly, resources only' do
918
+ collection.resources << Occi::Core::Resource.new
919
+ expected=Hashie::Mash.new
920
+ expected["Category"] = "resource;scheme=\"http://schemas.ogf.org/occi/core#\";class=\"kind\""
921
+ expected["X-OCCI-Attribute"] = "occi.core.id=\"#{collection.resources.first.id}\""
922
+ expect(collection.to_header).to eql(expected)
923
+ end
924
+
925
+ it 'renders text correctly, links only' do
926
+ collection.links << Occi::Core::Link.new
927
+ expected=Hashie::Mash.new
928
+ expected["Link"] = "<>;rel=\"http://schemas.ogf.org/occi/core#link\";self=\"/link/#{collection.links.first.id}\";category=\"http://schemas.ogf.org/occi/core#link\";occi.core.id=\"#{collection.links.first.id}\""
929
+ expect(collection.to_header).to eql(expected)
930
+ end
931
+
932
+ end
933
+
934
+ context '.header_merge' do
935
+ let(:mash1){ mash1 = Hashie::Mash.new
936
+ mash1['a'] = 'a'
937
+ mash1['b'] = 'b'
938
+ mash1
939
+ }
940
+ let(:second){ second = Hashie::Mash.new
941
+ second['a'] = 'd'
942
+ second['c'] = 'c'
943
+ second
944
+ }
945
+ it 'merges two mashes correctly' do
946
+ Occi::Collection.header_merge(mash1, second)
947
+
948
+ expected = Hashie::Mash.new
949
+ expected['a'] = 'a,d'
950
+ expected['b'] = 'b'
951
+ expected['c'] = 'c'
952
+ expect(mash1).to eql expected
953
+ end
954
+
955
+ it 'merges two mashes correctly with custom separator' do
956
+ Occi::Collection.header_merge(mash1, second, ' : ')
957
+
958
+ expected = Hashie::Mash.new
959
+ expected['a'] = 'a : d'
960
+ expected['b'] = 'b'
961
+ expected['c'] = 'c'
962
+ expect(mash1).to eql expected
963
+ end
964
+
35
965
  end
36
966
 
37
967
  end