restful_objects 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/lib/restful_objects.rb +1 -2
  3. data/lib/restful_objects/domain_model/domain_model.rb +14 -11
  4. data/lib/restful_objects/domain_model/helpers/link_generator.rb +102 -90
  5. data/lib/restful_objects/domain_model/mixins/object.rb +10 -13
  6. data/lib/restful_objects/domain_model/mixins/object_actions.rb +106 -117
  7. data/lib/restful_objects/domain_model/mixins/object_base.rb +175 -131
  8. data/lib/restful_objects/domain_model/mixins/object_collections.rb +58 -71
  9. data/lib/restful_objects/domain_model/mixins/object_macros.rb +20 -26
  10. data/lib/restful_objects/domain_model/mixins/object_properties.rb +85 -98
  11. data/lib/restful_objects/domain_model/mixins/service.rb +9 -17
  12. data/lib/restful_objects/domain_model/types/action_description.rb +80 -82
  13. data/lib/restful_objects/domain_model/types/collection_description.rb +39 -40
  14. data/lib/restful_objects/domain_model/types/domain_type.rb +102 -105
  15. data/lib/restful_objects/domain_model/types/parameter_description.rb +42 -47
  16. data/lib/restful_objects/domain_model/types/parameter_description_list.rb +9 -12
  17. data/lib/restful_objects/domain_model/types/property_description.rb +62 -64
  18. data/lib/restful_objects/router.rb +6 -0
  19. data/lib/restful_objects/router/base.rb +23 -32
  20. data/lib/restful_objects/router/domain_object_resources.rb +89 -94
  21. data/lib/restful_objects/router/domain_type_resources.rb +24 -29
  22. data/lib/restful_objects/router/supporting_resources.rb +26 -31
  23. data/lib/restful_objects/version.rb +1 -1
  24. data/spec/integration/domain-types_actions_spec.rb +1 -3
  25. data/spec/integration/domain-types_collections_spec.rb +1 -39
  26. data/spec/integration/domain-types_properties_spec.rb +1 -78
  27. data/spec/integration/domain-types_spec.rb +39 -0
  28. data/spec/integration/{server-root_spec.rb → homepage_spec.rb} +2 -3
  29. data/spec/integration/objects_actions_spec.rb +7 -0
  30. data/spec/integration/objects_collections_spec.rb +169 -0
  31. data/spec/integration/objects_properties_spec.rb +122 -0
  32. data/spec/{acceptance/generate_json_representations_spec.rb → integration/objects_spec.rb} +12 -4
  33. data/spec/unit/object_actions_spec.rb +5 -5
  34. data/spec/unit/object_collections_spec.rb +1 -1
  35. data/spec/unit/object_properties_spec.rb +6 -6
  36. data/spec/unit/object_spec.rb +9 -9
  37. data/spec/unit/service_spec.rb +5 -5
  38. metadata +54 -35
  39. data/spec/integration/domain_model_spec.rb +0 -35
  40. data/spec/unit/type_list_spec.rb +0 -37
@@ -1,7 +1,6 @@
1
- # encoding: utf-8
2
1
  require_relative '../spec_helper'
3
2
 
4
- describe 'DomainObject properties' do
3
+ describe '=> /domain-types/:type/properties/' do
5
4
  before(:all) do
6
5
  RestfulObjects::DomainModel.current.reset
7
6
  end
@@ -37,80 +36,4 @@ describe 'DomainObject properties' do
37
36
  get '/domain-types/PropertyTest/properties/name'
38
37
  last_response.body.should match_json_expression expected
39
38
  end
40
-
41
- describe 'json representation of property reference' do
42
- before :each do
43
- class ReferenceType
44
- include RestfulObjects::Object
45
- end
46
- class PropertyRefTest
47
- include RestfulObjects::Object
48
- property :reference, { object: ReferenceType }
49
- end
50
- @referenced = ReferenceType.new
51
- @object = PropertyRefTest.new
52
- end
53
-
54
- it 'gets representation' do
55
- @object.reference = @referenced
56
- expect(@object.get_property_as_json(:reference)).to match_json_expression(
57
- { 'reference' =>
58
- { 'value' =>
59
- { 'rel' => 'urn:org.restfulobjects:rels/value;property="reference"',
60
- 'href' => "http://localhost/objects/ReferenceType/#{@referenced.object_id}",
61
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
62
- 'method' => 'GET',
63
- 'title' => @referenced.title
64
- }
65
- }
66
- }
67
- )
68
- end
69
-
70
- it 'lists choices' do
71
- choices = [ReferenceType.new, ReferenceType.new, ReferenceType.new]
72
- @object.define_singleton_method(:reference_choices) { choices }
73
- expect(@object.get_property_as_json(:reference)).to match_json_expression(
74
- { 'reference' =>
75
- { 'value' => nil,
76
- 'choices' => [
77
- { 'rel' => 'urn:org.restfulobjects:rels/value;property="reference"',
78
- 'href' => "http://localhost/objects/ReferenceType/#{choices[0].object_id}",
79
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
80
- 'method' => 'GET',
81
- 'title' => choices[0].title },
82
- { 'rel' => 'urn:org.restfulobjects:rels/value;property="reference"',
83
- 'href' => "http://localhost/objects/ReferenceType/#{choices[1].object_id}",
84
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
85
- 'method' => 'GET',
86
- 'title' => choices[1].title },
87
- { 'rel' => 'urn:org.restfulobjects:rels/value;property="reference"',
88
- 'href' => "http://localhost/objects/ReferenceType/#{choices[2].object_id}",
89
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
90
- 'method' => 'GET',
91
- 'title' => choices[2].title }
92
- ]
93
- }
94
- }
95
- )
96
- end
97
-
98
- it 'puts representation' do
99
- json = { 'value' => { 'href' => @referenced.ro_absolute_url } }.to_json
100
- expect(@object.put_property_as_json(:reference, json)).to match_json_expression(
101
- { 'reference' =>
102
- { 'value' =>
103
- { 'rel' => 'urn:org.restfulobjects:rels/value;property="reference"',
104
- 'href' => "http://localhost/objects/ReferenceType/#{@referenced.object_id}",
105
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
106
- 'method' => 'GET',
107
- 'title' => @referenced.title
108
- }
109
- }
110
- }
111
- )
112
- expect(@object.reference).to eq @referenced
113
- end
114
- end
115
39
  end
116
-
@@ -0,0 +1,39 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe '=> /domain-types' do
4
+ before(:all) do
5
+ RestfulObjects::DomainModel.current.reset
6
+
7
+ class DomainObject
8
+ include RestfulObjects::Object
9
+ end
10
+ end
11
+
12
+ describe 'GET /domain-types' do
13
+ it 'generates response with a domain types in value arrray' do
14
+ get '/domain-types'
15
+ last_response.body.should match_json_expression(
16
+ { value:
17
+ [ { rel: 'urn:org.restfulobjects:rels/domain-type',
18
+ href: "http://localhost/domain-types/DomainObject",
19
+ type: 'application/json;profile="urn:org.restfulobjects:repr-types/domain-type"',
20
+ method: 'GET' } ]
21
+ })
22
+ end
23
+
24
+ it 'generates response links to homepage & sellf' do
25
+ get '/domain-types'
26
+ last_response.body.should match_json_expression(
27
+ { links: [
28
+ { rel: 'self',
29
+ href: 'http://localhost/domain-types',
30
+ type: 'application/json;profile="urn:org.restfulobjects:repr-types/type-list"',
31
+ method: 'GET' },
32
+ { rel: 'up',
33
+ href: 'http://localhost/',
34
+ type: 'application/json;profile="urn:org.restfulobjects:repr-types/homepage"',
35
+ method: 'GET' } ]
36
+ })
37
+ end
38
+ end
39
+ end
@@ -1,7 +1,6 @@
1
- # encoding: utf-8
2
1
  require_relative '../spec_helper'
3
2
 
4
- describe '/' do
3
+ describe '=> /' do
5
4
  before(:all) do
6
5
  RestfulObjects::DomainModel.current.reset
7
6
  end
@@ -69,7 +68,7 @@ describe '/' do
69
68
  include RestfulObjects::Service
70
69
  end
71
70
 
72
- RestfulObjects::DomainModel.current.services['ServiceTest'].title = 'Service Test Title'
71
+ RestfulObjects::DomainModel.current.services['ServiceTest'].ro_title = 'Service Test Title'
73
72
 
74
73
  services_list = {
75
74
  'links' => [
@@ -0,0 +1,7 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe '=> /objects/:type/:instance_id/actions/' do
4
+ before(:all) do
5
+ RestfulObjects::DomainModel.current.reset
6
+ end
7
+ end
@@ -0,0 +1,169 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe '=> /objects/:type/:instance_id/collections/' do
4
+ before(:all) do
5
+ RestfulObjects::DomainModel.current.reset
6
+
7
+ class ItemClass
8
+ include RestfulObjects::Object
9
+ end
10
+
11
+ class CollectionTest
12
+ include RestfulObjects::Object
13
+ collection :items, 'ItemClass', friendly_name: 'item collection', description: 'a collection description'
14
+ end
15
+ end
16
+
17
+ before(:each) do
18
+ @collection_object = CollectionTest.new
19
+ @item_a, @item_b, @item_c = ItemClass.new, ItemClass.new, ItemClass.new
20
+ @item_a.ro_title = 'Item A'
21
+ @item_b.ro_title = 'Item B'
22
+ @item_c.ro_title = 'Item C'
23
+ end
24
+
25
+ describe 'GET /objects/:type/collections/:collection' do
26
+ it 'generates response with status code 200' do
27
+ get "/objects/CollectionTest/#{@collection_object.ro_instance_id}/collections/items"
28
+ expect(last_response.status).to eq 200
29
+ end
30
+
31
+ it 'generates response with header Content-Type of object-collection' do
32
+ get "/objects/CollectionTest/#{@collection_object.ro_instance_id}/collections/items"
33
+ expect(last_response.headers['Content-Type']).to eq(
34
+ 'application/json;profile="urn:org.restfulobjects:repr-types/object-collection";x-ro-element-type="ItemClass"')
35
+ end
36
+
37
+ context 'when collection is empty,' do
38
+ it 'generates response with an empty value array' do
39
+ get "/objects/CollectionTest/#{@collection_object.ro_instance_id}/collections/items"
40
+ expect(last_response.body).to match_json_expression({ value: [] })
41
+ end
42
+ end
43
+
44
+ context 'when there are items in the collection,' do
45
+ it 'generates response with a value array with the item list' do
46
+ @collection_object.items << @item_a << @item_b << @item_c
47
+
48
+ get "/objects/CollectionTest/#{@collection_object.ro_instance_id}/collections/items"
49
+
50
+ expect(last_response.body).to match_json_expression({
51
+ value: [
52
+ { href: "http://localhost/objects/ItemClass/#{@item_a.ro_instance_id}" },
53
+ { href: "http://localhost/objects/ItemClass/#{@item_b.ro_instance_id}" },
54
+ { href: "http://localhost/objects/ItemClass/#{@item_c.ro_instance_id}" },
55
+ ]
56
+ })
57
+ end
58
+
59
+ it 'generates response with a value array with the item metadata' do
60
+ item = ItemClass.new
61
+ item.ro_title = 'An item'
62
+ object = CollectionTest.new
63
+ object.items << item
64
+
65
+ get "/objects/CollectionTest/#{object.ro_instance_id}/collections/items"
66
+
67
+ expect(last_response.body).to match_json_expression({
68
+ value: [ {
69
+ rel: 'urn:org.restfulobjects:rels/value;collection="items"',
70
+ href: "http://localhost/objects/ItemClass/#{item.ro_instance_id}",
71
+ method: 'GET',
72
+ type: 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
73
+ title: 'An item'
74
+ } ]
75
+ })
76
+ end
77
+
78
+ it 'generates response with object list' do
79
+ a, b = ItemClass.new, ItemClass.new
80
+ object = CollectionTest.new
81
+ object.items << a << b
82
+
83
+ get '/domain-types/CollectionTest/collections/items'
84
+ end
85
+
86
+
87
+ it 'generates response with links' do
88
+
89
+ end
90
+ end
91
+ end
92
+
93
+ describe 'POST /objects/:type/:instance_id/collections/:collection' do
94
+ it 'appends objecto to collection with list semantics' do
95
+ collection = CollectionTest.new
96
+ item = ItemClass.new
97
+
98
+ post "/objects/CollectionTest/#{collection.ro_instance_id}/collections/items",
99
+ '{ "value": { "href": "/objects/ItemClass/' + item.ro_instance_id.to_s + '" } }'
100
+
101
+ expect(collection.items).to include(item)
102
+ end
103
+ end
104
+
105
+ describe 'PUT /objects/:type/:instance_id/collections/:collection' do
106
+ it 'appends object to collection with set semantics' do
107
+ collection = CollectionTest.new
108
+ item = ItemClass.new
109
+
110
+ put "/objects/CollectionTest/#{collection.ro_instance_id}/collections/items",
111
+ '{ "value": { "href": "/objects/ItemClass/' + item.ro_instance_id.to_s + '" } }'
112
+
113
+ expect(collection.items).to include(item)
114
+ end
115
+ end
116
+
117
+ describe 'DELETE /objects/:type/:instance_id/collections/:collection' do
118
+ it 'removes object from to collection' do
119
+ collection = CollectionTest.new
120
+ item = ItemClass.new
121
+ collection.items << item
122
+
123
+ delete "/objects/CollectionTest/#{collection.ro_instance_id}/collections/items",
124
+ '{ "value": { "href": "/objects/ItemClass/' + item.ro_instance_id.to_s + '" } }'
125
+
126
+ expect(collection.items).not_to include(item)
127
+ end
128
+ end
129
+ end
130
+
131
+ describe '=> /domain-types/:type/collections/' do
132
+ before(:all) do
133
+ RestfulObjects::DomainModel.current.reset
134
+
135
+ class CollectionTest
136
+ include RestfulObjects::Object
137
+ collection :items, 'ItemClass', friendly_name: 'item collection', description: 'a collection description'
138
+ end
139
+ end
140
+
141
+ it 'generates response with collection metadata' do
142
+ get '/domain-types/CollectionTest/collections/items'
143
+
144
+ last_response.body.should match_json_expression({
145
+ 'id' => 'items',
146
+ 'friendlyName' => 'item collection',
147
+ 'description' => 'a collection description',
148
+ 'memberOrder' => 1,
149
+ 'links' => [
150
+ { 'rel' => 'self',
151
+ 'href' => 'http://localhost/domain-types/CollectionTest/collections/items',
152
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/collection-description"',
153
+ 'method' => 'GET' },
154
+ { 'rel' => 'up',
155
+ 'href' => 'http://localhost/domain-types/CollectionTest',
156
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/domain-type"',
157
+ 'method' => 'GET' },
158
+ { 'rel' => 'urn:org.restfulobjects:rels/return-type',
159
+ 'href' => 'http://localhost/domain-types/list',
160
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/domain-type"',
161
+ 'method' => 'GET' },
162
+ { 'rel' => 'urn:org.restfulobjects:rels/element-type',
163
+ 'href' => 'http://localhost/domain-types/ItemClass',
164
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/domain-type"',
165
+ 'method' => 'GET' }
166
+ ]
167
+ })
168
+ end
169
+ end
@@ -0,0 +1,122 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe '=> /objects/:type/:instance_id/properties/' do
4
+ describe 'simple properties,' do
5
+ it 'updates simple property' do
6
+ class TestObject
7
+ include RestfulObjects::Object
8
+ property :name, :string
9
+ end
10
+ test_object = TestObject.new
11
+
12
+ put "/objects/TestObject/#{test_object.ro_instance_id}", { 'name' => { 'value' => 'john smith' } }.to_json
13
+
14
+ expect(last_response.body).to match_json_expression({ members: { name: { value: 'john smith' } } })
15
+ end
16
+
17
+ it 'updates multiple properties' do
18
+ class TestObject
19
+ include RestfulObjects::Object
20
+ property :name, :string
21
+ property :age, :int
22
+ property :weight, :decimal
23
+ end
24
+ test_object = TestObject.new
25
+
26
+ json = { 'name' => { 'value' => 'john smith' }, 'age' => { 'value' => '29' }, 'weight' => { 'value' => '71.5' } }.to_json
27
+
28
+ put "/objects/TestObject/#{test_object.ro_instance_id}", json
29
+
30
+ expect(last_response.body).to match_json_expression({
31
+ members: {
32
+ name: { value: 'john smith' },
33
+ age: { value: 29 },
34
+ weight: { value: 71.5 }
35
+ }
36
+ })
37
+ end
38
+ end
39
+
40
+ describe 'reference properties,' do
41
+ before(:each) do
42
+ class ReferenceType
43
+ include RestfulObjects::Object
44
+ end
45
+
46
+ class PropertyRefTest
47
+ include RestfulObjects::Object
48
+ property :reference, { object: ReferenceType }
49
+ end
50
+
51
+ @referenced = ReferenceType.new
52
+ @object = PropertyRefTest.new
53
+ end
54
+
55
+ it 'gets representation' do
56
+ @object.reference = @referenced
57
+
58
+ get "/objects/PropertyRefTest/#{@object.ro_instance_id}/properties/reference"
59
+
60
+ expect(last_response.body).to match_json_expression(
61
+ { reference:
62
+ { value:
63
+ { rel: 'urn:org.restfulobjects:rels/value;property="reference"',
64
+ href: "http://localhost/objects/ReferenceType/#{@referenced.object_id}",
65
+ type: 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
66
+ method: 'GET',
67
+ title: @referenced.ro_title
68
+ }
69
+ }
70
+ }
71
+ )
72
+ end
73
+
74
+ it 'lists choices' do
75
+ choices = [ReferenceType.new, ReferenceType.new, ReferenceType.new]
76
+ @object.define_singleton_method(:reference_choices) { choices }
77
+
78
+ get "/objects/PropertyRefTest/#{@object.ro_instance_id}/properties/reference"
79
+
80
+ expect(last_response.body).to match_json_expression(
81
+ { reference:
82
+ { value: nil,
83
+ choices: [
84
+ { rel: 'urn:org.restfulobjects:rels/value;property="reference"',
85
+ href: "http://localhost/objects/ReferenceType/#{choices[0].object_id}",
86
+ type: 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
87
+ method: 'GET',
88
+ title: choices[0].ro_title },
89
+ { rel: 'urn:org.restfulobjects:rels/value;property="reference"',
90
+ href: "http://localhost/objects/ReferenceType/#{choices[1].object_id}",
91
+ type: 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
92
+ method: 'GET',
93
+ title: choices[1].ro_title },
94
+ { rel: 'urn:org.restfulobjects:rels/value;property="reference"',
95
+ href: "http://localhost/objects/ReferenceType/#{choices[2].object_id}",
96
+ type: 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
97
+ method: 'GET',
98
+ title: choices[2].ro_title }
99
+ ]
100
+ }
101
+ }
102
+ )
103
+ end
104
+
105
+ it 'puts representation' do
106
+ json = { 'value' => { 'href' => @referenced.ro_absolute_url } }.to_json
107
+ expect(@object.ro_put_property_and_get_response(:reference, json).last).to match_json_expression(
108
+ { 'reference' =>
109
+ { 'value' =>
110
+ { 'rel' => 'urn:org.restfulobjects:rels/value;property="reference"',
111
+ 'href' => "http://localhost/objects/ReferenceType/#{@referenced.object_id}",
112
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
113
+ 'method' => 'GET',
114
+ 'title' => @referenced.ro_title
115
+ }
116
+ }
117
+ }
118
+ )
119
+ expect(@object.reference).to eq @referenced
120
+ end
121
+ end
122
+ end