restful_objects 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. checksums.yaml +8 -8
  2. data/README.md +3 -2
  3. data/bin/restful_server.rb +0 -0
  4. data/lib/restful_objects/action_description.rb +96 -82
  5. data/lib/restful_objects/action_list.rb +17 -17
  6. data/lib/restful_objects/collection_description.rb +47 -47
  7. data/lib/restful_objects/collection_list.rb +17 -17
  8. data/lib/restful_objects/domain_model.rb +83 -79
  9. data/lib/restful_objects/http_response.rb +11 -11
  10. data/lib/restful_objects/link_generator.rb +104 -104
  11. data/lib/restful_objects/object.rb +20 -20
  12. data/lib/restful_objects/object_actions.rb +134 -134
  13. data/lib/restful_objects/object_base.rb +10 -0
  14. data/lib/restful_objects/object_collections.rb +84 -84
  15. data/lib/restful_objects/object_list.rb +16 -16
  16. data/lib/restful_objects/object_macros.rb +35 -35
  17. data/lib/restful_objects/object_properties.rb +78 -78
  18. data/lib/restful_objects/parameter_description.rb +60 -60
  19. data/lib/restful_objects/parameter_description_list.rb +15 -15
  20. data/lib/restful_objects/property_description.rb +68 -68
  21. data/lib/restful_objects/property_list.rb +17 -17
  22. data/lib/restful_objects/service.rb +21 -21
  23. data/lib/restful_objects/service_list.rb +55 -55
  24. data/lib/restful_objects/type.rb +110 -110
  25. data/lib/restful_objects/type_list.rb +30 -30
  26. data/lib/restful_objects/user.rb +30 -30
  27. data/lib/restful_objects/version.rb +1 -1
  28. data/spec/integration/domain-types_actions_spec.rb +43 -43
  29. data/spec/integration/domain-types_collections_spec.rb +45 -45
  30. data/spec/integration/domain-types_properties_spec.rb +41 -41
  31. data/spec/integration/server-root_spec.rb +98 -98
  32. data/spec/spec_helper.rb +52 -52
  33. data/spec/unit/object_actions_spec.rb +380 -380
  34. data/spec/unit/object_collections_spec.rb +190 -190
  35. data/spec/unit/object_properties_spec.rb +215 -206
  36. data/spec/unit/object_spec.rb +228 -228
  37. data/spec/unit/service_spec.rb +125 -125
  38. data/spec/unit/type_list_spec.rb +37 -37
  39. metadata +2 -2
@@ -1,190 +1,190 @@
1
- # encoding: utf-8
2
- require_relative '../spec_helper'
3
-
4
- describe RestfulObjects::ObjectActions do
5
- before :all do
6
- RestfulObjects::DomainModel.current.reset
7
-
8
- class Address
9
- include RestfulObjects::Object
10
-
11
- property :street, :string
12
- property :number, :int
13
- end
14
-
15
- class CollectionsTest
16
- include RestfulObjects::Object
17
-
18
- collection :addresses, Address
19
- collection :collection_full_metadata, Address
20
- end
21
- end
22
-
23
- before :each do
24
- @object = CollectionsTest.new
25
- end
26
-
27
- it 'should create a collection' do
28
- @object.addresses.is_a?(Enumerable).should be_true
29
- @object.addresses.empty?.should be_true
30
-
31
- a = Address.new
32
- a.street = 'Evergreen'
33
- a.number = 1234
34
-
35
- @object.addresses.push(a)
36
- @object.addresses.empty?.should be_false
37
- @object.addresses.count.should eq(1)
38
- end
39
-
40
- it 'should generate json for the collection' do
41
- a1 = Address.new
42
- @object.addresses.push a1
43
- a2 = Address.new
44
- @object.addresses.push a2
45
- a3 = Address.new
46
- @object.addresses.push a3
47
-
48
- expected = { 'id' => 'addresses',
49
- 'value' => [
50
- { 'rel' => 'urn:org.restfulobjects:rels/value;collection="addresses"',
51
- 'href' => "http://localhost/objects/Address/#{a1.object_id}",
52
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
53
- 'method' => 'GET' },
54
- { 'rel' => 'urn:org.restfulobjects:rels/value;collection="addresses"',
55
- 'href' => "http://localhost/objects/Address/#{a2.object_id}",
56
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
57
- 'method' => 'GET' },
58
- { 'rel' => 'urn:org.restfulobjects:rels/value;collection="addresses"',
59
- 'href' => "http://localhost/objects/Address/#{a3.object_id}",
60
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
61
- 'method' => 'GET' }
62
- ],
63
- 'links' => [
64
- { 'rel' => 'self',
65
- 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}/collections/addresses",
66
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-collection"',
67
- 'method' => 'GET' },
68
- { 'rel' => 'up',
69
- 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}",
70
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
71
- 'method' => 'GET' },
72
- { 'rel' => 'urn:org.restfulobjects:rels/add-to;collection="addresses"',
73
- 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}/collections/addresses",
74
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-collection"',
75
- 'method' => 'PUT',
76
- 'arguments' => { 'value' => nil } },
77
- { 'rel' => 'urn:org.restfulobjects:rels/remove-from;collection="addresses"',
78
- 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}/collections/addresses",
79
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-collection"',
80
- 'method' => 'DELETE',
81
- 'arguments' => { 'value' => nil } },
82
- ]
83
- }
84
-
85
- get "/objects/CollectionsTest/#{@object.object_id}/collections/addresses"
86
-
87
- last_response.body.should match_json_expression expected
88
- end
89
-
90
- it 'should add an object to a collection' do
91
- address = Address.new
92
-
93
- json = { 'value' => { 'href' => "http://localhost/objects/Address/#{address.object_id}" } }.to_json
94
-
95
- put "/objects/CollectionsTest/#{@object.object_id}/collections/addresses", {}, { input: json }
96
-
97
- @object.addresses.include?(address).should be_true
98
-
99
- expected = { 'id' => 'addresses',
100
- 'value' => [
101
- { 'rel' => 'urn:org.restfulobjects:rels/value;collection="addresses"',
102
- 'href' => "http://localhost/objects/Address/#{address.object_id}",
103
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
104
- 'method' => 'GET' } ],
105
- 'links' => [
106
- { 'rel' => 'self',
107
- 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}/collections/addresses",
108
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-collection"',
109
- 'method' => 'GET' },
110
- { 'rel' => 'up',
111
- 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}",
112
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
113
- 'method' => 'GET' },
114
- { 'rel' => 'urn:org.restfulobjects:rels/add-to;collection="addresses"',
115
- 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}/collections/addresses",
116
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-collection"',
117
- 'method' => 'PUT',
118
- 'arguments' => { 'value' => nil } },
119
- { 'rel' => 'urn:org.restfulobjects:rels/remove-from;collection="addresses"',
120
- 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}/collections/addresses",
121
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-collection"',
122
- 'method' => 'DELETE',
123
- 'arguments' => { 'value' => nil } },
124
- ]
125
- }
126
-
127
- last_response.body.should match_json_expression expected
128
- end
129
-
130
- it 'should remove an object from a collection' do
131
- address = Address.new
132
-
133
- @object.addresses.push(address)
134
-
135
- json = { 'value' => { 'href' => "http://localhost/objects/Address/#{address.object_id}" } }.to_json
136
-
137
- delete "/objects/CollectionsTest/#{@object.object_id}/collections/addresses", {}, { input: json }
138
-
139
- @object.addresses.include?(address).should be_false
140
-
141
- expected = { 'id' => 'addresses',
142
- 'value' => [],
143
- 'links' => [
144
- { 'rel' => 'self',
145
- 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}/collections/addresses",
146
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-collection"',
147
- 'method' => 'GET' },
148
- { 'rel' => 'up',
149
- 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}",
150
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
151
- 'method' => 'GET' },
152
- { 'rel' => 'urn:org.restfulobjects:rels/add-to;collection="addresses"',
153
- 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}/collections/addresses",
154
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-collection"',
155
- 'method' => 'PUT',
156
- 'arguments' => { 'value' => nil } },
157
- { 'rel' => 'urn:org.restfulobjects:rels/remove-from;collection="addresses"',
158
- 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}/collections/addresses",
159
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-collection"',
160
- 'method' => 'DELETE',
161
- 'arguments' => { 'value' => nil } },
162
- ]
163
- }
164
-
165
- last_response.body.should match_json_expression expected
166
- end
167
-
168
- it 'should generate metadata in extensions' do
169
- collection = @object.rs_type.collections['collection_full_metadata']
170
- collection.friendly_name = 'Friendly Collection'
171
- collection.description = 'Collection Description'
172
- collection.plural_form = 'Collections'
173
- expected = {
174
- 'id' => 'collection_full_metadata',
175
- 'extensions' => {
176
- 'friendlyName' => 'Friendly Collection',
177
- 'description' => 'Collection Description',
178
- 'returnType' => 'list',
179
- 'elementType' => 'Address',
180
- 'pluralForm' => 'Collections',
181
- 'memberOrder' => Fixnum
182
- }.strict!
183
- }
184
-
185
- get "/objects/ActionsTest/#{@object.object_id}/collections/collection_full_metadata"
186
-
187
- last_response.body.should match_json_expression expected
188
- end
189
- end
190
-
1
+ # encoding: utf-8
2
+ require_relative '../spec_helper'
3
+
4
+ describe RestfulObjects::ObjectActions do
5
+ before :all do
6
+ RestfulObjects::DomainModel.current.reset
7
+
8
+ class Address
9
+ include RestfulObjects::Object
10
+
11
+ property :street, :string
12
+ property :number, :int
13
+ end
14
+
15
+ class CollectionsTest
16
+ include RestfulObjects::Object
17
+
18
+ collection :addresses, Address
19
+ collection :collection_full_metadata, Address
20
+ end
21
+ end
22
+
23
+ before :each do
24
+ @object = CollectionsTest.new
25
+ end
26
+
27
+ it 'should create a collection' do
28
+ @object.addresses.is_a?(Enumerable).should be_true
29
+ @object.addresses.empty?.should be_true
30
+
31
+ a = Address.new
32
+ a.street = 'Evergreen'
33
+ a.number = 1234
34
+
35
+ @object.addresses.push(a)
36
+ @object.addresses.empty?.should be_false
37
+ @object.addresses.count.should eq(1)
38
+ end
39
+
40
+ it 'should generate json for the collection' do
41
+ a1 = Address.new
42
+ @object.addresses.push a1
43
+ a2 = Address.new
44
+ @object.addresses.push a2
45
+ a3 = Address.new
46
+ @object.addresses.push a3
47
+
48
+ expected = { 'id' => 'addresses',
49
+ 'value' => [
50
+ { 'rel' => 'urn:org.restfulobjects:rels/value;collection="addresses"',
51
+ 'href' => "http://localhost/objects/Address/#{a1.object_id}",
52
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
53
+ 'method' => 'GET' },
54
+ { 'rel' => 'urn:org.restfulobjects:rels/value;collection="addresses"',
55
+ 'href' => "http://localhost/objects/Address/#{a2.object_id}",
56
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
57
+ 'method' => 'GET' },
58
+ { 'rel' => 'urn:org.restfulobjects:rels/value;collection="addresses"',
59
+ 'href' => "http://localhost/objects/Address/#{a3.object_id}",
60
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
61
+ 'method' => 'GET' }
62
+ ],
63
+ 'links' => [
64
+ { 'rel' => 'self',
65
+ 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}/collections/addresses",
66
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-collection"',
67
+ 'method' => 'GET' },
68
+ { 'rel' => 'up',
69
+ 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}",
70
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
71
+ 'method' => 'GET' },
72
+ { 'rel' => 'urn:org.restfulobjects:rels/add-to;collection="addresses"',
73
+ 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}/collections/addresses",
74
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-collection"',
75
+ 'method' => 'PUT',
76
+ 'arguments' => { 'value' => nil } },
77
+ { 'rel' => 'urn:org.restfulobjects:rels/remove-from;collection="addresses"',
78
+ 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}/collections/addresses",
79
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-collection"',
80
+ 'method' => 'DELETE',
81
+ 'arguments' => { 'value' => nil } },
82
+ ]
83
+ }
84
+
85
+ get "/objects/CollectionsTest/#{@object.object_id}/collections/addresses"
86
+
87
+ last_response.body.should match_json_expression expected
88
+ end
89
+
90
+ it 'should add an object to a collection' do
91
+ address = Address.new
92
+
93
+ json = { 'value' => { 'href' => "http://localhost/objects/Address/#{address.object_id}" } }.to_json
94
+
95
+ put "/objects/CollectionsTest/#{@object.object_id}/collections/addresses", {}, { input: json }
96
+
97
+ @object.addresses.include?(address).should be_true
98
+
99
+ expected = { 'id' => 'addresses',
100
+ 'value' => [
101
+ { 'rel' => 'urn:org.restfulobjects:rels/value;collection="addresses"',
102
+ 'href' => "http://localhost/objects/Address/#{address.object_id}",
103
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
104
+ 'method' => 'GET' } ],
105
+ 'links' => [
106
+ { 'rel' => 'self',
107
+ 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}/collections/addresses",
108
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-collection"',
109
+ 'method' => 'GET' },
110
+ { 'rel' => 'up',
111
+ 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}",
112
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
113
+ 'method' => 'GET' },
114
+ { 'rel' => 'urn:org.restfulobjects:rels/add-to;collection="addresses"',
115
+ 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}/collections/addresses",
116
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-collection"',
117
+ 'method' => 'PUT',
118
+ 'arguments' => { 'value' => nil } },
119
+ { 'rel' => 'urn:org.restfulobjects:rels/remove-from;collection="addresses"',
120
+ 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}/collections/addresses",
121
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-collection"',
122
+ 'method' => 'DELETE',
123
+ 'arguments' => { 'value' => nil } },
124
+ ]
125
+ }
126
+
127
+ last_response.body.should match_json_expression expected
128
+ end
129
+
130
+ it 'should remove an object from a collection' do
131
+ address = Address.new
132
+
133
+ @object.addresses.push(address)
134
+
135
+ json = { 'value' => { 'href' => "http://localhost/objects/Address/#{address.object_id}" } }.to_json
136
+
137
+ delete "/objects/CollectionsTest/#{@object.object_id}/collections/addresses", {}, { input: json }
138
+
139
+ @object.addresses.include?(address).should be_false
140
+
141
+ expected = { 'id' => 'addresses',
142
+ 'value' => [],
143
+ 'links' => [
144
+ { 'rel' => 'self',
145
+ 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}/collections/addresses",
146
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-collection"',
147
+ 'method' => 'GET' },
148
+ { 'rel' => 'up',
149
+ 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}",
150
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
151
+ 'method' => 'GET' },
152
+ { 'rel' => 'urn:org.restfulobjects:rels/add-to;collection="addresses"',
153
+ 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}/collections/addresses",
154
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-collection"',
155
+ 'method' => 'PUT',
156
+ 'arguments' => { 'value' => nil } },
157
+ { 'rel' => 'urn:org.restfulobjects:rels/remove-from;collection="addresses"',
158
+ 'href' => "http://localhost/objects/CollectionsTest/#{@object.object_id}/collections/addresses",
159
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-collection"',
160
+ 'method' => 'DELETE',
161
+ 'arguments' => { 'value' => nil } },
162
+ ]
163
+ }
164
+
165
+ last_response.body.should match_json_expression expected
166
+ end
167
+
168
+ it 'should generate metadata in extensions' do
169
+ collection = @object.rs_type.collections['collection_full_metadata']
170
+ collection.friendly_name = 'Friendly Collection'
171
+ collection.description = 'Collection Description'
172
+ collection.plural_form = 'Collections'
173
+ expected = {
174
+ 'id' => 'collection_full_metadata',
175
+ 'extensions' => {
176
+ 'friendlyName' => 'Friendly Collection',
177
+ 'description' => 'Collection Description',
178
+ 'returnType' => 'list',
179
+ 'elementType' => 'Address',
180
+ 'pluralForm' => 'Collections',
181
+ 'memberOrder' => Fixnum
182
+ }.strict!
183
+ }
184
+
185
+ get "/objects/ActionsTest/#{@object.object_id}/collections/collection_full_metadata"
186
+
187
+ last_response.body.should match_json_expression expected
188
+ end
189
+ end
190
+
@@ -1,206 +1,215 @@
1
- require_relative '../spec_helper'
2
-
3
- describe RestfulObjects::ObjectProperties do
4
- before :all do
5
- RestfulObjects::DomainModel.current.reset
6
-
7
- class PropertiesTest
8
- include RestfulObjects::Object
9
-
10
- property :id, :int, read_only: true
11
- property :name, :string, max_length: 10
12
- property :string_prop, :string
13
- property :int_prop, :int
14
- property :decimal_prop, :decimal
15
- property :date_prop, :date
16
- property :blob_prop, :blob
17
-
18
- property :prop_full_metadata, :string, friendly_name: 'Meta Prop', description: 'To Test Metadata', max_length: 30,
19
- pattern: '.*abc.*', optional: false
20
-
21
- def initialize
22
- super
23
- @id = 99
24
- end
25
- end
26
- end
27
-
28
- before :each do
29
- @object = PropertiesTest.new
30
- end
31
-
32
- it 'should respond to properties' do
33
- @object.respond_to?(:string_prop).should be_true
34
- @object.respond_to?(:string_prop=).should be_true
35
- end
36
-
37
- it 'should not generate writer for read-only property' do
38
- @object.respond_to?(:id).should be_true
39
- @object.respond_to?(:id=).should be_false
40
- end
41
-
42
- it 'should generate json for a read-only property' do
43
- expected = { 'id' =>
44
- { 'value' => 99,
45
- 'links' => [
46
- { 'rel' => 'self',
47
- 'href' => "http://localhost/objects/PropertiesTest/#{@object.object_id}/properties/id",
48
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-property"',
49
- 'method' => 'GET'},
50
- { 'rel' => 'up',
51
- 'href' => "http://localhost/objects/PropertiesTest/#{@object.object_id}",
52
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
53
- 'method' => 'GET'}
54
- ],
55
- 'disabledReason' => 'read-only property'
56
- }
57
- }
58
-
59
- get "/objects/PropertiesTest/#{@object.object_id}/properties/id"
60
-
61
- last_response.body.should match_json_expression expected
62
- end
63
-
64
- it 'should generate json for a writable property' do
65
- @object.name = 'Mr. John'
66
-
67
- expected = { 'name' =>
68
- { 'value' => 'Mr. John',
69
- 'links' => [
70
- { 'rel' => 'self',
71
- 'href' => "http://localhost/objects/PropertiesTest/#{@object.object_id}/properties/name",
72
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-property"',
73
- 'method' => 'GET' },
74
- { 'rel' => 'up',
75
- 'href' => "http://localhost/objects/PropertiesTest/#{@object.object_id}",
76
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
77
- 'method' => 'GET' },
78
- { 'rel' => 'urn:org.restfulobjects:rels/modify;property="name"',
79
- 'href' => "http://localhost/objects/PropertiesTest/#{@object.object_id}/properties/name",
80
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-property"',
81
- 'method' => 'PUT',
82
- 'arguments' => { 'value' => nil } },
83
- { 'rel' => 'urn:org.restfulobjects:rels/clear;property="name"',
84
- 'href' => "http://localhost/objects/PropertiesTest/#{@object.object_id}/properties/name",
85
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-property"',
86
- 'method' => 'DELETE' }
87
- ]
88
- }
89
- }
90
-
91
- get "/objects/PropertiesTest/#{@object.object_id}/properties/name"
92
-
93
- last_response.body.should match_json_expression expected
94
- end
95
-
96
- it 'should enforce string property max_length' do
97
- @object.name = 'x' * 10
98
- @object.name.should eq('x' * 10)
99
- expect { @object.name = 'x' * 11 }.to raise_error
100
- end
101
-
102
- it 'should process different property types get' do
103
- @object.string_prop = 'A string'
104
- @object.int_prop = 1234
105
- @object.decimal_prop = 333.33
106
- @object.date_prop = Date.new(2012, 2, 29)
107
- @object.blob_prop = "\xE5\xA5\xB4\x30\xF2\x8C\x71\xD9"
108
-
109
- expected = {
110
- 'string_prop' => 'A string',
111
- 'int_prop' => 1234,
112
- 'decimal_prop' => 333.33,
113
- 'date_prop' => '2012-02-29',
114
- 'blob_prop' => '5aW0MPKMcdk=' }
115
-
116
- expected.each do |name, value|
117
- get "/objects/PropertiesTest/#{@object.object_id}/properties/#{name}"
118
- JSON.parse(last_response.body)[name]['value'].should eq value
119
- end
120
- end
121
-
122
- it 'should process different property types set' do
123
- values = {
124
- 'string_prop' => 'A string',
125
- 'int_prop' => 1234,
126
- 'decimal_prop' => 333.33,
127
- 'date_prop' => '2012-02-29',
128
- 'blob_prop' => '5aW0MPKMcdk=' }
129
-
130
- values.each do |name, value|
131
- put "/objects/PropertiesTest/#{@object.object_id}/properties/#{name}", { 'value' => value }.to_json
132
- end
133
-
134
- @object.string_prop.should eq 'A string'
135
- @object.int_prop.should eq 1234
136
- @object.decimal_prop.should eq 333.33
137
- @object.date_prop.should eq Date.new(2012, 2, 29)
138
- @object.blob_prop.should eq "\xE5\xA5\xB4\x30\xF2\x8C\x71\xD9"
139
- end
140
-
141
- it 'should process a property put with json' do
142
- json = { 'value' => 'masterkey' }.to_json
143
-
144
- expected = { 'name' =>
145
- { 'value' => 'masterkey',
146
- 'links' => [
147
- { 'rel' => 'self',
148
- 'href' => "http://localhost/objects/PropertiesTest/#{@object.object_id}/properties/name",
149
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-property"',
150
- 'method' => 'GET' },
151
- { 'rel' => 'up',
152
- 'href' => "http://localhost/objects/PropertiesTest/#{@object.object_id}",
153
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
154
- 'method' => 'GET' },
155
- { 'rel' => 'urn:org.restfulobjects:rels/modify;property="name"',
156
- 'href' => "http://localhost/objects/PropertiesTest/#{@object.object_id}/properties/name",
157
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-property"',
158
- 'method' => 'PUT',
159
- 'arguments' => { 'value' => nil } },
160
- { 'rel' => 'urn:org.restfulobjects:rels/clear;property="name"',
161
- 'href' => "http://localhost/objects/PropertiesTest/#{@object.object_id}/properties/name",
162
- 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-property"',
163
- 'method' => 'DELETE' }
164
- ]
165
- }
166
- }
167
-
168
- put "/objects/PropertiesTest/#{@object.object_id}/properties/name", json
169
-
170
- last_response.body.should match_json_expression expected
171
- end
172
-
173
- it 'should process a property delete' do
174
- @object.name = 'irrelevant'
175
-
176
- delete "/objects/PropertiesTest/#{@object.object_id}/properties/name"
177
-
178
- @object.name.should be_nil
179
-
180
- expected = { 'name' => { 'value' => nil } }
181
-
182
- last_response.body.should match_json_expression expected
183
- end
184
-
185
- it 'should generate metadata in extensions' do
186
- expected = {
187
- 'prop_full_metadata' => {
188
- 'extensions' => {
189
- 'friendlyName' => 'Meta Prop',
190
- 'description' => 'To Test Metadata',
191
- 'returnType' => 'string',
192
- 'format' => 'string',
193
- 'optional' => false,
194
- 'maxLength' => 30,
195
- 'pattern' => '.*abc.*',
196
- 'memberOrder' => Fixnum
197
- }.strict!
198
- }
199
- }
200
-
201
- get "/objects/PropertiesTest/#{@object.object_id}/properties/prop_full_metadata"
202
-
203
- last_response.body.should match_json_expression expected
204
- end
205
- end
206
-
1
+ require_relative '../spec_helper'
2
+
3
+ describe RestfulObjects::ObjectProperties do
4
+ before :all do
5
+ RestfulObjects::DomainModel.current.reset
6
+
7
+ class PropertiesTest
8
+ include RestfulObjects::Object
9
+
10
+ property :id, :int, read_only: true
11
+ property :name, :string, max_length: 10
12
+ property :string_prop, :string
13
+ property :int_prop, :int
14
+ property :bool_prop, :bool
15
+ property :decimal_prop, :decimal
16
+ property :date_prop, :date
17
+ property :blob_prop, :blob
18
+
19
+ property :prop_full_metadata, :string,
20
+ friendly_name: 'Meta Prop',
21
+ description: 'To Test Metadata',
22
+ max_length: 30,
23
+ pattern: '.*abc.*',
24
+ optional: false
25
+
26
+ def initialize
27
+ super
28
+ @id = 99
29
+ end
30
+ end
31
+ end
32
+
33
+ before :each do
34
+ @object = PropertiesTest.new
35
+ end
36
+
37
+ it 'should respond to properties' do
38
+ @object.respond_to?(:string_prop).should be_true
39
+ @object.respond_to?(:string_prop=).should be_true
40
+ end
41
+
42
+ it 'should not generate writer for read-only property' do
43
+ @object.respond_to?(:id).should be_true
44
+ @object.respond_to?(:id=).should be_false
45
+ end
46
+
47
+ it 'should generate json for a read-only property' do
48
+ expected = { 'id' =>
49
+ { 'value' => 99,
50
+ 'links' => [
51
+ { 'rel' => 'self',
52
+ 'href' => "http://localhost/objects/PropertiesTest/#{@object.object_id}/properties/id",
53
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-property"',
54
+ 'method' => 'GET'},
55
+ { 'rel' => 'up',
56
+ 'href' => "http://localhost/objects/PropertiesTest/#{@object.object_id}",
57
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
58
+ 'method' => 'GET'}
59
+ ],
60
+ 'disabledReason' => 'read-only property'
61
+ }
62
+ }
63
+
64
+ get "/objects/PropertiesTest/#{@object.object_id}/properties/id"
65
+
66
+ last_response.body.should match_json_expression expected
67
+ end
68
+
69
+ it 'should generate json for a writable property' do
70
+ @object.name = 'Mr. John'
71
+
72
+ expected = { 'name' =>
73
+ { 'value' => 'Mr. John',
74
+ 'links' => [
75
+ { 'rel' => 'self',
76
+ 'href' => "http://localhost/objects/PropertiesTest/#{@object.object_id}/properties/name",
77
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-property"',
78
+ 'method' => 'GET' },
79
+ { 'rel' => 'up',
80
+ 'href' => "http://localhost/objects/PropertiesTest/#{@object.object_id}",
81
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
82
+ 'method' => 'GET' },
83
+ { 'rel' => 'urn:org.restfulobjects:rels/modify;property="name"',
84
+ 'href' => "http://localhost/objects/PropertiesTest/#{@object.object_id}/properties/name",
85
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-property"',
86
+ 'method' => 'PUT',
87
+ 'arguments' => { 'value' => nil } },
88
+ { 'rel' => 'urn:org.restfulobjects:rels/clear;property="name"',
89
+ 'href' => "http://localhost/objects/PropertiesTest/#{@object.object_id}/properties/name",
90
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-property"',
91
+ 'method' => 'DELETE' }
92
+ ]
93
+ }
94
+ }
95
+
96
+ get "/objects/PropertiesTest/#{@object.object_id}/properties/name"
97
+
98
+ last_response.body.should match_json_expression expected
99
+ end
100
+
101
+ it 'should enforce string property max_length' do
102
+ @object.name = 'x' * 10
103
+ @object.name.should eq('x' * 10)
104
+ expect { @object.name = 'x' * 11 }.to raise_error
105
+ end
106
+
107
+ it 'should process different property types get' do
108
+ @object.string_prop = 'A string'
109
+ @object.int_prop = 1234
110
+ @object.bool_prop = true
111
+ @object.decimal_prop = 333.33
112
+ @object.date_prop = Date.new(2012, 2, 29)
113
+ @object.blob_prop = "\xE5\xA5\xB4\x30\xF2\x8C\x71\xD9"
114
+
115
+ expected = {
116
+ 'string_prop' => 'A string',
117
+ 'int_prop' => 1234,
118
+ 'bool_prop' => 'true',
119
+ 'decimal_prop' => 333.33,
120
+ 'date_prop' => '2012-02-29',
121
+ 'blob_prop' => '5aW0MPKMcdk=' }
122
+
123
+ expected.each do |name, value|
124
+ get "/objects/PropertiesTest/#{@object.object_id}/properties/#{name}"
125
+ JSON.parse(last_response.body)[name]['value'].should eq value
126
+ end
127
+ end
128
+
129
+ it 'should process different property types set' do
130
+ values = {
131
+ 'string_prop' => 'A string',
132
+ 'int_prop' => 1234,
133
+ 'bool_prop' => 'true',
134
+ 'decimal_prop' => 333.33,
135
+ 'date_prop' => '2012-02-29',
136
+ 'blob_prop' => '5aW0MPKMcdk=' }
137
+
138
+ values.each do |name, value|
139
+ put "/objects/PropertiesTest/#{@object.object_id}/properties/#{name}", { 'value' => value }.to_json
140
+ end
141
+
142
+ @object.string_prop.should eq 'A string'
143
+ @object.int_prop.should eq 1234
144
+ @object.bool_prop.should eq true
145
+ @object.decimal_prop.should eq 333.33
146
+ @object.date_prop.should eq Date.new(2012, 2, 29)
147
+ @object.blob_prop.should eq "\xE5\xA5\xB4\x30\xF2\x8C\x71\xD9"
148
+ end
149
+
150
+ it 'should process a property put with json' do
151
+ json = { 'value' => 'masterkey' }.to_json
152
+
153
+ expected = { 'name' =>
154
+ { 'value' => 'masterkey',
155
+ 'links' => [
156
+ { 'rel' => 'self',
157
+ 'href' => "http://localhost/objects/PropertiesTest/#{@object.object_id}/properties/name",
158
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-property"',
159
+ 'method' => 'GET' },
160
+ { 'rel' => 'up',
161
+ 'href' => "http://localhost/objects/PropertiesTest/#{@object.object_id}",
162
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object"',
163
+ 'method' => 'GET' },
164
+ { 'rel' => 'urn:org.restfulobjects:rels/modify;property="name"',
165
+ 'href' => "http://localhost/objects/PropertiesTest/#{@object.object_id}/properties/name",
166
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-property"',
167
+ 'method' => 'PUT',
168
+ 'arguments' => { 'value' => nil } },
169
+ { 'rel' => 'urn:org.restfulobjects:rels/clear;property="name"',
170
+ 'href' => "http://localhost/objects/PropertiesTest/#{@object.object_id}/properties/name",
171
+ 'type' => 'application/json;profile="urn:org.restfulobjects:repr-types/object-property"',
172
+ 'method' => 'DELETE' }
173
+ ]
174
+ }
175
+ }
176
+
177
+ put "/objects/PropertiesTest/#{@object.object_id}/properties/name", json
178
+
179
+ last_response.body.should match_json_expression expected
180
+ end
181
+
182
+ it 'should process a property delete' do
183
+ @object.name = 'irrelevant'
184
+
185
+ delete "/objects/PropertiesTest/#{@object.object_id}/properties/name"
186
+
187
+ @object.name.should be_nil
188
+
189
+ expected = { 'name' => { 'value' => nil } }
190
+
191
+ last_response.body.should match_json_expression expected
192
+ end
193
+
194
+ it 'should generate metadata in extensions' do
195
+ expected = {
196
+ 'prop_full_metadata' => {
197
+ 'extensions' => {
198
+ 'friendlyName' => 'Meta Prop',
199
+ 'description' => 'To Test Metadata',
200
+ 'returnType' => 'string',
201
+ 'format' => 'string',
202
+ 'optional' => false,
203
+ 'maxLength' => 30,
204
+ 'pattern' => '.*abc.*',
205
+ 'memberOrder' => Fixnum
206
+ }.strict!
207
+ }
208
+ }
209
+
210
+ get "/objects/PropertiesTest/#{@object.object_id}/properties/prop_full_metadata"
211
+
212
+ last_response.body.should match_json_expression expected
213
+ end
214
+ end
215
+