my_john_deere_api 2.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (153) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +813 -0
  3. data/Rakefile +9 -0
  4. data/lib/my_john_deere_api.rb +17 -0
  5. data/lib/my_john_deere_api/authorize.rb +69 -0
  6. data/lib/my_john_deere_api/client.rb +151 -0
  7. data/lib/my_john_deere_api/consumer.rb +90 -0
  8. data/lib/my_john_deere_api/errors.rb +7 -0
  9. data/lib/my_john_deere_api/errors/invalid_record_error.rb +29 -0
  10. data/lib/my_john_deere_api/errors/missing_contribution_definition_id_error.rb +12 -0
  11. data/lib/my_john_deere_api/errors/not_yet_implemented_error.rb +12 -0
  12. data/lib/my_john_deere_api/errors/type_mismatch_error.rb +12 -0
  13. data/lib/my_john_deere_api/errors/unsupported_environment_error.rb +18 -0
  14. data/lib/my_john_deere_api/helpers.rb +6 -0
  15. data/lib/my_john_deere_api/helpers/case_conversion.rb +37 -0
  16. data/lib/my_john_deere_api/helpers/environment_helper.rb +25 -0
  17. data/lib/my_john_deere_api/helpers/uri_helpers.rb +19 -0
  18. data/lib/my_john_deere_api/helpers/validate_contribution_definition.rb +18 -0
  19. data/lib/my_john_deere_api/model.rb +10 -0
  20. data/lib/my_john_deere_api/model/asset.rb +69 -0
  21. data/lib/my_john_deere_api/model/asset_location.rb +19 -0
  22. data/lib/my_john_deere_api/model/base.rb +97 -0
  23. data/lib/my_john_deere_api/model/contribution_definition.rb +15 -0
  24. data/lib/my_john_deere_api/model/contribution_product.rb +33 -0
  25. data/lib/my_john_deere_api/model/field.rb +40 -0
  26. data/lib/my_john_deere_api/model/flag.rb +36 -0
  27. data/lib/my_john_deere_api/model/organization.rb +43 -0
  28. data/lib/my_john_deere_api/net_http_retry.rb +2 -0
  29. data/lib/my_john_deere_api/net_http_retry/decorator.rb +53 -0
  30. data/lib/my_john_deere_api/net_http_retry/max_retries_exceeded_error.rb +20 -0
  31. data/lib/my_john_deere_api/request.rb +6 -0
  32. data/lib/my_john_deere_api/request/collection.rb +10 -0
  33. data/lib/my_john_deere_api/request/collection/asset_locations.rb +27 -0
  34. data/lib/my_john_deere_api/request/collection/assets.rb +34 -0
  35. data/lib/my_john_deere_api/request/collection/base.rb +91 -0
  36. data/lib/my_john_deere_api/request/collection/contribution_definitions.rb +26 -0
  37. data/lib/my_john_deere_api/request/collection/contribution_products.rb +26 -0
  38. data/lib/my_john_deere_api/request/collection/fields.rb +26 -0
  39. data/lib/my_john_deere_api/request/collection/flags.rb +33 -0
  40. data/lib/my_john_deere_api/request/collection/organizations.rb +26 -0
  41. data/lib/my_john_deere_api/request/create.rb +5 -0
  42. data/lib/my_john_deere_api/request/create/asset.rb +57 -0
  43. data/lib/my_john_deere_api/request/create/asset_location.rb +110 -0
  44. data/lib/my_john_deere_api/request/create/base.rb +67 -0
  45. data/lib/my_john_deere_api/request/individual.rb +8 -0
  46. data/lib/my_john_deere_api/request/individual/asset.rb +19 -0
  47. data/lib/my_john_deere_api/request/individual/base.rb +52 -0
  48. data/lib/my_john_deere_api/request/individual/contribution_definition.rb +19 -0
  49. data/lib/my_john_deere_api/request/individual/contribution_product.rb +19 -0
  50. data/lib/my_john_deere_api/request/individual/field.rb +19 -0
  51. data/lib/my_john_deere_api/request/individual/organization.rb +19 -0
  52. data/lib/my_john_deere_api/request/update.rb +4 -0
  53. data/lib/my_john_deere_api/request/update/asset.rb +40 -0
  54. data/lib/my_john_deere_api/request/update/base.rb +67 -0
  55. data/lib/my_john_deere_api/validators.rb +5 -0
  56. data/lib/my_john_deere_api/validators/asset.rb +35 -0
  57. data/lib/my_john_deere_api/validators/asset_location.rb +34 -0
  58. data/lib/my_john_deere_api/validators/base.rb +67 -0
  59. data/lib/my_john_deere_api/version.rb +3 -0
  60. data/test/lib/my_john_deere_api/authorize_test.rb +81 -0
  61. data/test/lib/my_john_deere_api/client_test.rb +225 -0
  62. data/test/lib/my_john_deere_api/consumer_test.rb +58 -0
  63. data/test/lib/my_john_deere_api/errors/invalid_record_error_test.rb +26 -0
  64. data/test/lib/my_john_deere_api/errors/max_retries_exceeded_error_test.rb +13 -0
  65. data/test/lib/my_john_deere_api/errors/missing_contribution_definition_id_error_test.rb +14 -0
  66. data/test/lib/my_john_deere_api/errors/not_yet_implemented_error_test.rb +13 -0
  67. data/test/lib/my_john_deere_api/errors/type_mismatch_error_test.rb +13 -0
  68. data/test/lib/my_john_deere_api/errors/unsupported_environment_error_test.rb +18 -0
  69. data/test/lib/my_john_deere_api/errors_test.rb +25 -0
  70. data/test/lib/my_john_deere_api/helpers/case_conversion_test.rb +100 -0
  71. data/test/lib/my_john_deere_api/helpers/environment_helper_test.rb +67 -0
  72. data/test/lib/my_john_deere_api/helpers/uri_helpers_test.rb +58 -0
  73. data/test/lib/my_john_deere_api/helpers/validate_contribution_definition_test.rb +74 -0
  74. data/test/lib/my_john_deere_api/helpers_test.rb +21 -0
  75. data/test/lib/my_john_deere_api/model/asset_location_test.rb +38 -0
  76. data/test/lib/my_john_deere_api/model/asset_test.rb +133 -0
  77. data/test/lib/my_john_deere_api/model/base_test.rb +76 -0
  78. data/test/lib/my_john_deere_api/model/contribution_definition_test.rb +52 -0
  79. data/test/lib/my_john_deere_api/model/contribution_product_test.rb +82 -0
  80. data/test/lib/my_john_deere_api/model/field_test.rb +65 -0
  81. data/test/lib/my_john_deere_api/model/flag_test.rb +48 -0
  82. data/test/lib/my_john_deere_api/model/organization_test.rb +84 -0
  83. data/test/lib/my_john_deere_api/model_test.rb +37 -0
  84. data/test/lib/my_john_deere_api/net_http_retry/decorator_test.rb +163 -0
  85. data/test/lib/my_john_deere_api/request/collection/asset_locations_test.rb +102 -0
  86. data/test/lib/my_john_deere_api/request/collection/assets_test.rb +99 -0
  87. data/test/lib/my_john_deere_api/request/collection/base_test.rb +27 -0
  88. data/test/lib/my_john_deere_api/request/collection/contribution_definitions_test.rb +88 -0
  89. data/test/lib/my_john_deere_api/request/collection/contribution_products_test.rb +88 -0
  90. data/test/lib/my_john_deere_api/request/collection/fields_test.rb +86 -0
  91. data/test/lib/my_john_deere_api/request/collection/flags_test.rb +92 -0
  92. data/test/lib/my_john_deere_api/request/collection/organizations_test.rb +87 -0
  93. data/test/lib/my_john_deere_api/request/collection_test.rb +37 -0
  94. data/test/lib/my_john_deere_api/request/create/asset_location_test.rb +163 -0
  95. data/test/lib/my_john_deere_api/request/create/asset_test.rb +182 -0
  96. data/test/lib/my_john_deere_api/request/create/base_test.rb +29 -0
  97. data/test/lib/my_john_deere_api/request/create_test.rb +17 -0
  98. data/test/lib/my_john_deere_api/request/individual/asset_test.rb +33 -0
  99. data/test/lib/my_john_deere_api/request/individual/base_test.rb +18 -0
  100. data/test/lib/my_john_deere_api/request/individual/contribution_definition_test.rb +33 -0
  101. data/test/lib/my_john_deere_api/request/individual/contribution_product_test.rb +33 -0
  102. data/test/lib/my_john_deere_api/request/individual/field_test.rb +37 -0
  103. data/test/lib/my_john_deere_api/request/individual/organization_test.rb +33 -0
  104. data/test/lib/my_john_deere_api/request/individual_test.rb +29 -0
  105. data/test/lib/my_john_deere_api/request/update/asset_test.rb +99 -0
  106. data/test/lib/my_john_deere_api/request/update/base_test.rb +60 -0
  107. data/test/lib/my_john_deere_api/request/update_test.rb +13 -0
  108. data/test/lib/my_john_deere_api/request_test.rb +21 -0
  109. data/test/lib/my_john_deere_api/validators/asset_location_test.rb +61 -0
  110. data/test/lib/my_john_deere_api/validators/asset_test.rb +93 -0
  111. data/test/lib/my_john_deere_api/validators/base_test.rb +92 -0
  112. data/test/lib/my_john_deere_api/validators_test.rb +17 -0
  113. data/test/lib/my_john_deere_api/version_test.rb +9 -0
  114. data/test/my_john_deere_api_test.rb +37 -0
  115. data/test/support/helper.rb +97 -0
  116. data/test/support/vcr/accessor/delete_failed.yml +327 -0
  117. data/test/support/vcr/accessor/delete_max_failed.yml +615 -0
  118. data/test/support/vcr/accessor/delete_retry.yml +191 -0
  119. data/test/support/vcr/accessor/delete_retry_too_soon.yml +191 -0
  120. data/test/support/vcr/accessor/get_failed.yml +390 -0
  121. data/test/support/vcr/accessor/get_max_failed.yml +734 -0
  122. data/test/support/vcr/accessor/get_retry.yml +226 -0
  123. data/test/support/vcr/accessor/get_retry_too_soon.yml +226 -0
  124. data/test/support/vcr/accessor/post_failed.yml +417 -0
  125. data/test/support/vcr/accessor/post_max_failed.yml +785 -0
  126. data/test/support/vcr/accessor/post_retry.yml +241 -0
  127. data/test/support/vcr/accessor/post_retry_too_soon.yml +241 -0
  128. data/test/support/vcr/accessor/put_failed.yml +372 -0
  129. data/test/support/vcr/accessor/put_max_failed.yml +700 -0
  130. data/test/support/vcr/accessor/put_retry.yml +216 -0
  131. data/test/support/vcr/accessor/put_retry_too_soon.yml +216 -0
  132. data/test/support/vcr/catalog.yml +89 -0
  133. data/test/support/vcr/delete_asset.yml +82 -0
  134. data/test/support/vcr/get_access_token.yml +41 -0
  135. data/test/support/vcr/get_asset.yml +144 -0
  136. data/test/support/vcr/get_asset_locations.yml +196 -0
  137. data/test/support/vcr/get_assets.yml +152 -0
  138. data/test/support/vcr/get_contribution_definition.yml +90 -0
  139. data/test/support/vcr/get_contribution_definitions.yml +91 -0
  140. data/test/support/vcr/get_contribution_product.yml +91 -0
  141. data/test/support/vcr/get_contribution_products.yml +91 -0
  142. data/test/support/vcr/get_field.yml +144 -0
  143. data/test/support/vcr/get_fields.yml +146 -0
  144. data/test/support/vcr/get_flags.yml +47 -0
  145. data/test/support/vcr/get_organization.yml +90 -0
  146. data/test/support/vcr/get_organizations.yml +149 -0
  147. data/test/support/vcr/get_request_token.yml +83 -0
  148. data/test/support/vcr/post_asset_locations.yml +244 -0
  149. data/test/support/vcr/post_assets.yml +192 -0
  150. data/test/support/vcr/put_asset.yml +87 -0
  151. data/test/support/vcr/warning.txt +28 -0
  152. data/test/support/vcr_setup.rb +488 -0
  153. metadata +277 -0
@@ -0,0 +1,18 @@
1
+ require 'support/helper'
2
+ require 'yaml'
3
+ require 'json'
4
+
5
+ describe 'MyJohnDeereApi::Request::Individual::Base' do
6
+ let(:object) { JD::Request::Individual::Base.new(client, asset_id) }
7
+
8
+ describe '#initialize(client, asset_id)' do
9
+ it 'accepts a client' do
10
+ assert_equal client, object.client
11
+ assert_equal accessor, object.accessor
12
+ end
13
+
14
+ it 'accepts asset_id as id' do
15
+ assert_equal asset_id, object.id
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,33 @@
1
+ require 'support/helper'
2
+ require 'yaml'
3
+ require 'json'
4
+
5
+ describe 'MyJohnDeereApi::Request::Individual::ContributionDefinition' do
6
+ let(:object) { JD::Request::Individual::ContributionDefinition.new(client, contribution_definition_id) }
7
+
8
+ inherits_from JD::Request::Individual::Base
9
+
10
+ describe '#initialize(client, contribution_definition_id)' do
11
+ it 'accepts a client' do
12
+ assert_equal client, object.client
13
+ assert_equal accessor, object.accessor
14
+ end
15
+
16
+ it 'accepts contribution_definition_id as id' do
17
+ assert_equal contribution_definition_id, object.id
18
+ end
19
+ end
20
+
21
+ describe '#resource' do
22
+ it 'returns /contributionDefinitions/<definition_id>' do
23
+ assert_equal "/contributionDefinitions/#{contribution_definition_id}", object.resource
24
+ end
25
+ end
26
+
27
+ describe '#object' do
28
+ it 'returns all records' do
29
+ definition = VCR.use_cassette('get_contribution_definition') { object.object }
30
+ assert_kind_of JD::Model::ContributionDefinition, definition
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ require 'support/helper'
2
+ require 'yaml'
3
+ require 'json'
4
+
5
+ describe 'MyJohnDeereApi::Request::Individual::ContributionProduct' do
6
+ let(:object) { JD::Request::Individual::ContributionProduct.new(client, contribution_product_id) }
7
+
8
+ inherits_from JD::Request::Individual::Base
9
+
10
+ describe '#initialize(client, contribution_product_id)' do
11
+ it 'accepts a client' do
12
+ assert_equal client, object.client
13
+ assert_equal accessor, object.accessor
14
+ end
15
+
16
+ it 'accepts contribution_product_id as id' do
17
+ assert_equal contribution_product_id, object.id
18
+ end
19
+ end
20
+
21
+ describe '#resource' do
22
+ it 'returns /contributionProducts/<product_id>' do
23
+ assert_equal "/contributionProducts/#{contribution_product_id}", object.resource
24
+ end
25
+ end
26
+
27
+ describe '#object' do
28
+ it 'returns all records' do
29
+ product = VCR.use_cassette('get_contribution_product') { object.object }
30
+ assert_kind_of JD::Model::ContributionProduct, product
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,37 @@
1
+ require 'support/helper'
2
+ require 'yaml'
3
+ require 'json'
4
+
5
+ describe 'MyJohnDeereApi::Request::Individual::Field' do
6
+ let(:object) { JD::Request::Individual::Field.new(client, field_id, organization: organization_id) }
7
+
8
+ inherits_from JD::Request::Individual::Base
9
+
10
+ describe '#initialize(client, organization_id, field_id)' do
11
+ it 'accepts a client' do
12
+ assert_equal client, object.client
13
+ assert_equal accessor, object.accessor
14
+ end
15
+
16
+ it 'accepts organization_id as organization_id' do
17
+ assert_equal organization_id, object.associations[:organization]
18
+ end
19
+
20
+ it 'accepts field_id as id' do
21
+ assert_equal field_id, object.id
22
+ end
23
+ end
24
+
25
+ describe '#resource' do
26
+ it 'returns /organizations/<organization_id>/fields/<field_id>' do
27
+ assert_equal "/organizations/#{organization_id}/fields/#{field_id}", object.resource
28
+ end
29
+ end
30
+
31
+ describe '#object' do
32
+ it 'returns all records' do
33
+ field = VCR.use_cassette('get_field') { object.object }
34
+ assert_kind_of JD::Model::Field, field
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,33 @@
1
+ require 'support/helper'
2
+ require 'yaml'
3
+ require 'json'
4
+
5
+ describe 'MyJohnDeereApi::Request::Individual::Organization' do
6
+ let(:object) { JD::Request::Individual::Organization.new(client, organization_id) }
7
+
8
+ inherits_from JD::Request::Individual::Base
9
+
10
+ describe '#initialize(client, asset_id)' do
11
+ it 'accepts a client' do
12
+ assert_equal client, object.client
13
+ assert_equal accessor, object.accessor
14
+ end
15
+
16
+ it 'accepts organization_id as id' do
17
+ assert_equal organization_id, object.id
18
+ end
19
+ end
20
+
21
+ describe '#resource' do
22
+ it 'returns /organizations/<organization_id>' do
23
+ assert_equal "/organizations/#{organization_id}", object.resource
24
+ end
25
+ end
26
+
27
+ describe '#object' do
28
+ it 'returns all records' do
29
+ organization = VCR.use_cassette('get_organization') { object.object }
30
+ assert_kind_of JD::Model::Organization, organization
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,29 @@
1
+ require 'support/helper'
2
+
3
+ describe 'MyJohnDeereApi::Request::Individual' do
4
+ describe 'loading dependencies' do
5
+ it 'loads Request::Individual::Base' do
6
+ assert JD::Request::Individual::Base
7
+ end
8
+
9
+ it 'loads Request::Individual::Asset' do
10
+ assert JD::Request::Individual::Asset
11
+ end
12
+
13
+ it 'loads Request::Individual::ContributionProduct' do
14
+ assert JD::Request::Individual::ContributionProduct
15
+ end
16
+
17
+ it 'loads Request::Individual::ContributionDefinition' do
18
+ assert JD::Request::Individual::ContributionDefinition
19
+ end
20
+
21
+ it 'loads Request::Individual::Field' do
22
+ assert JD::Request::Individual::Field
23
+ end
24
+
25
+ it 'loads Request::Individual::Organization' do
26
+ assert JD::Request::Individual::Organization
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,99 @@
1
+ require 'support/helper'
2
+
3
+ describe 'MyJohnDeereApi::Request::Update::Asset' do
4
+ let(:klass) { JD::Request::Update::Asset }
5
+ let(:object) { klass.new(client, item, attributes) }
6
+ let(:item) { JD::Model::Asset.new(client, record) }
7
+
8
+ let(:attributes) do
9
+ {
10
+ organization_id: organization_id,
11
+ }
12
+ end
13
+
14
+ let(:record) do
15
+ {
16
+ '@type' => 'ContributedAsset',
17
+ 'id' => asset_id,
18
+ 'title' => 'Bob',
19
+ 'assetCategory' => 'DEVICE',
20
+ 'assetType' => 'SENSOR',
21
+ 'assetSubType' => 'ENVIRONMENTAL',
22
+ 'lastModifiedDate' => CONFIG.timestamp,
23
+ 'links' => []
24
+ }
25
+ end
26
+
27
+ inherits_from MyJohnDeereApi::Request::Update::Base
28
+
29
+ describe '#initialize(client, item, attributes)' do
30
+ it 'accepts a client, item and attributes' do
31
+ assert_equal client, object.client
32
+ assert_equal accessor, object.accessor
33
+ assert_equal item, object.item
34
+ assert_equal item.attributes.merge(attributes), object.attributes
35
+ end
36
+
37
+ it 'includes validation' do
38
+ [:validate!, :valid?].each do |method_name|
39
+ assert object.respond_to?(method_name)
40
+ end
41
+ end
42
+ end
43
+
44
+ describe '#resource' do
45
+ it 'is /assets/<asset_id>' do
46
+ assert_equal "/assets/#{asset_id}", object.send(:resource)
47
+ end
48
+ end
49
+
50
+ describe '#headers' do
51
+ it 'sets the accept and content-type headers' do
52
+ object = klass.new(client, item, attributes)
53
+ headers = object.send(:headers)
54
+
55
+ expected = 'application/vnd.deere.axiom.v3+json'
56
+
57
+ assert_kind_of Hash, headers
58
+ assert_equal expected, headers['Accept']
59
+ assert_equal expected, headers['Content-Type']
60
+ end
61
+ end
62
+
63
+ describe '#request_body' do
64
+ it 'properly forms the request body' do
65
+ body = object.send(:request_body)
66
+
67
+ assert_equal item.attributes[:title], body[:title]
68
+ assert_equal item.attributes[:asset_category], body[:assetCategory]
69
+ assert_equal item.attributes[:asset_type], body[:assetType]
70
+ assert_equal item.attributes[:asset_sub_type], body[:assetSubType]
71
+
72
+ assert_kind_of Array, body[:links]
73
+ assert_equal 1, body[:links].size
74
+
75
+ assert_kind_of Hash, body[:links].first
76
+ assert_equal 'Link', body[:links].first['@type']
77
+ assert_equal 'contributionDefinition', body[:links].first['rel']
78
+ assert_equal "#{base_url}/contributionDefinitions/#{contribution_definition_id}",
79
+ body[:links].first['uri']
80
+ end
81
+
82
+ it 'raises an exception when contribution_definition_id is not set' do
83
+ client.contribution_definition_id = nil
84
+ object = klass.new(client, item, attributes)
85
+
86
+ assert_raises(JD::MissingContributionDefinitionIdError) do
87
+ object.send(:request_body)
88
+ end
89
+ end
90
+ end
91
+
92
+ describe '#request' do
93
+ it 'makes the request' do
94
+ VCR.use_cassette('put_asset') { object.request }
95
+
96
+ assert_kind_of Net::HTTPNoContent, object.response
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,60 @@
1
+ require 'support/helper'
2
+ require 'yaml'
3
+ require 'json'
4
+
5
+ class UpdateBaseItem
6
+ attr_reader :attributes
7
+
8
+ def initialize
9
+ @attributes = {
10
+ name: 'Bob',
11
+ age: 21
12
+ }
13
+ end
14
+ end
15
+
16
+ describe 'MyJohnDeereApi::Request::Update::Base' do
17
+ let(:klass) { JD::Request::Update::Base }
18
+ let(:object) { klass.new(client, item, attributes) }
19
+ let(:item) { UpdateBaseItem.new }
20
+ let(:attributes) { {} }
21
+
22
+ describe '#initialize(client, item, attributes)' do
23
+ it 'accepts a client, item and attributes' do
24
+ assert_equal client, object.client
25
+ assert_equal accessor, object.accessor
26
+ assert_equal item, object.item
27
+ end
28
+
29
+ describe 'when a new attribute (ie, name) is passed in' do
30
+ let(:attributes) { {name: 'John'} }
31
+
32
+ it 'merges name with item attributes' do
33
+ assert_equal attributes[:name], object.attributes[:name]
34
+ assert_equal item.attributes[:age], object.attributes[:age]
35
+ end
36
+ end
37
+
38
+ describe 'when a new attribute (ie, age) is passed in' do
39
+ let(:attributes) { {age: 99} }
40
+
41
+ it 'merges name with item attributes' do
42
+ assert_equal item.attributes[:name], object.attributes[:name]
43
+ assert_equal attributes[:age], object.attributes[:age]
44
+ end
45
+ end
46
+ end
47
+
48
+ describe '#headers' do
49
+ it 'sets the accept and content-type headers' do
50
+ object = klass.new(client, item, attributes)
51
+ headers = object.send(:headers)
52
+
53
+ expected = 'application/vnd.deere.axiom.v3+json'
54
+
55
+ assert_kind_of Hash, headers
56
+ assert_equal expected, headers['Accept']
57
+ assert_equal expected, headers['Content-Type']
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,13 @@
1
+ require 'support/helper'
2
+
3
+ describe 'MyJohnDeereApi::Request::Update' do
4
+ describe 'loading dependencies' do
5
+ it 'loads Request::Update::Base' do
6
+ assert JD::Request::Update::Base
7
+ end
8
+
9
+ it 'loads Request::Update::Asset' do
10
+ assert JD::Request::Update::Asset
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ require 'support/helper'
2
+
3
+ describe 'MyJohnDeereApi::Request' do
4
+ describe 'loading dependencies' do
5
+ it 'loads Request::Collection' do
6
+ assert JD::Request::Collection
7
+ end
8
+
9
+ it 'loads Request::Create' do
10
+ assert JD::Request::Create
11
+ end
12
+
13
+ it 'loads Request::Individuaal' do
14
+ assert JD::Request::Individual
15
+ end
16
+
17
+ it 'loads Request::Update' do
18
+ assert JD::Request::Update
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,61 @@
1
+ require 'support/helper'
2
+
3
+ class AssetLocationValidatorTest
4
+ include JD::Validators::AssetLocation
5
+
6
+ attr_reader :attributes
7
+
8
+ def initialize(attributes)
9
+ @attributes = attributes
10
+ end
11
+ end
12
+
13
+ describe 'MyJohnDeereApi::Validators::AssetLocation' do
14
+ let(:klass) { AssetLocationValidatorTest }
15
+ let(:object) { klass.new(attributes) }
16
+ let(:attributes) { valid_attributes }
17
+
18
+ let(:valid_attributes) do
19
+ {
20
+ asset_id: asset_id,
21
+ timestamp: CONFIG.timestamp,
22
+ coordinates: CONFIG.coordinates,
23
+ measurement_data: CONFIG.measurement_data
24
+ }
25
+ end
26
+
27
+ it 'exists' do
28
+ assert JD::Validators::AssetLocation
29
+ end
30
+
31
+ it 'inherits from MyJohnDeereApi::Validators::Base' do
32
+ [:validate!, :valid?].each{ |method_name| assert object.respond_to?(method_name) }
33
+ end
34
+
35
+ it 'requires several attributes' do
36
+ [:asset_id, :timestamp, :geometry, :measurement_data].each do |attr|
37
+ object = klass.new(valid_attributes.merge(attr => nil))
38
+
39
+ refute object.valid?
40
+ exception = assert_raises(JD::InvalidRecordError) { object.validate! }
41
+
42
+ assert_includes exception.message, "#{attr} is required"
43
+ assert_includes object.errors[attr], 'is required'
44
+ end
45
+ end
46
+
47
+ it 'requires measurement_data to have the right keys' do
48
+ [:name, :value, :unit].each do |key|
49
+ attributes = Marshal.load(Marshal.dump(valid_attributes))
50
+ attributes[:measurement_data].first.delete(key)
51
+
52
+ object = klass.new(attributes)
53
+
54
+ refute object.valid?
55
+ exception = assert_raises(JD::InvalidRecordError) { object.validate! }
56
+
57
+ assert_includes exception.message, "measurement_data must include #{key}"
58
+ assert_includes object.errors[:measurement_data], "must include #{key}"
59
+ end
60
+ end
61
+ end