my_john_deere_api 2.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +813 -0
- data/Rakefile +9 -0
- data/lib/my_john_deere_api.rb +17 -0
- data/lib/my_john_deere_api/authorize.rb +69 -0
- data/lib/my_john_deere_api/client.rb +151 -0
- data/lib/my_john_deere_api/consumer.rb +90 -0
- data/lib/my_john_deere_api/errors.rb +7 -0
- data/lib/my_john_deere_api/errors/invalid_record_error.rb +29 -0
- data/lib/my_john_deere_api/errors/missing_contribution_definition_id_error.rb +12 -0
- data/lib/my_john_deere_api/errors/not_yet_implemented_error.rb +12 -0
- data/lib/my_john_deere_api/errors/type_mismatch_error.rb +12 -0
- data/lib/my_john_deere_api/errors/unsupported_environment_error.rb +18 -0
- data/lib/my_john_deere_api/helpers.rb +6 -0
- data/lib/my_john_deere_api/helpers/case_conversion.rb +37 -0
- data/lib/my_john_deere_api/helpers/environment_helper.rb +25 -0
- data/lib/my_john_deere_api/helpers/uri_helpers.rb +19 -0
- data/lib/my_john_deere_api/helpers/validate_contribution_definition.rb +18 -0
- data/lib/my_john_deere_api/model.rb +10 -0
- data/lib/my_john_deere_api/model/asset.rb +69 -0
- data/lib/my_john_deere_api/model/asset_location.rb +19 -0
- data/lib/my_john_deere_api/model/base.rb +97 -0
- data/lib/my_john_deere_api/model/contribution_definition.rb +15 -0
- data/lib/my_john_deere_api/model/contribution_product.rb +33 -0
- data/lib/my_john_deere_api/model/field.rb +40 -0
- data/lib/my_john_deere_api/model/flag.rb +36 -0
- data/lib/my_john_deere_api/model/organization.rb +43 -0
- data/lib/my_john_deere_api/net_http_retry.rb +2 -0
- data/lib/my_john_deere_api/net_http_retry/decorator.rb +53 -0
- data/lib/my_john_deere_api/net_http_retry/max_retries_exceeded_error.rb +20 -0
- data/lib/my_john_deere_api/request.rb +6 -0
- data/lib/my_john_deere_api/request/collection.rb +10 -0
- data/lib/my_john_deere_api/request/collection/asset_locations.rb +27 -0
- data/lib/my_john_deere_api/request/collection/assets.rb +34 -0
- data/lib/my_john_deere_api/request/collection/base.rb +91 -0
- data/lib/my_john_deere_api/request/collection/contribution_definitions.rb +26 -0
- data/lib/my_john_deere_api/request/collection/contribution_products.rb +26 -0
- data/lib/my_john_deere_api/request/collection/fields.rb +26 -0
- data/lib/my_john_deere_api/request/collection/flags.rb +33 -0
- data/lib/my_john_deere_api/request/collection/organizations.rb +26 -0
- data/lib/my_john_deere_api/request/create.rb +5 -0
- data/lib/my_john_deere_api/request/create/asset.rb +57 -0
- data/lib/my_john_deere_api/request/create/asset_location.rb +110 -0
- data/lib/my_john_deere_api/request/create/base.rb +67 -0
- data/lib/my_john_deere_api/request/individual.rb +8 -0
- data/lib/my_john_deere_api/request/individual/asset.rb +19 -0
- data/lib/my_john_deere_api/request/individual/base.rb +52 -0
- data/lib/my_john_deere_api/request/individual/contribution_definition.rb +19 -0
- data/lib/my_john_deere_api/request/individual/contribution_product.rb +19 -0
- data/lib/my_john_deere_api/request/individual/field.rb +19 -0
- data/lib/my_john_deere_api/request/individual/organization.rb +19 -0
- data/lib/my_john_deere_api/request/update.rb +4 -0
- data/lib/my_john_deere_api/request/update/asset.rb +40 -0
- data/lib/my_john_deere_api/request/update/base.rb +67 -0
- data/lib/my_john_deere_api/validators.rb +5 -0
- data/lib/my_john_deere_api/validators/asset.rb +35 -0
- data/lib/my_john_deere_api/validators/asset_location.rb +34 -0
- data/lib/my_john_deere_api/validators/base.rb +67 -0
- data/lib/my_john_deere_api/version.rb +3 -0
- data/test/lib/my_john_deere_api/authorize_test.rb +81 -0
- data/test/lib/my_john_deere_api/client_test.rb +225 -0
- data/test/lib/my_john_deere_api/consumer_test.rb +58 -0
- data/test/lib/my_john_deere_api/errors/invalid_record_error_test.rb +26 -0
- data/test/lib/my_john_deere_api/errors/max_retries_exceeded_error_test.rb +13 -0
- data/test/lib/my_john_deere_api/errors/missing_contribution_definition_id_error_test.rb +14 -0
- data/test/lib/my_john_deere_api/errors/not_yet_implemented_error_test.rb +13 -0
- data/test/lib/my_john_deere_api/errors/type_mismatch_error_test.rb +13 -0
- data/test/lib/my_john_deere_api/errors/unsupported_environment_error_test.rb +18 -0
- data/test/lib/my_john_deere_api/errors_test.rb +25 -0
- data/test/lib/my_john_deere_api/helpers/case_conversion_test.rb +100 -0
- data/test/lib/my_john_deere_api/helpers/environment_helper_test.rb +67 -0
- data/test/lib/my_john_deere_api/helpers/uri_helpers_test.rb +58 -0
- data/test/lib/my_john_deere_api/helpers/validate_contribution_definition_test.rb +74 -0
- data/test/lib/my_john_deere_api/helpers_test.rb +21 -0
- data/test/lib/my_john_deere_api/model/asset_location_test.rb +38 -0
- data/test/lib/my_john_deere_api/model/asset_test.rb +133 -0
- data/test/lib/my_john_deere_api/model/base_test.rb +76 -0
- data/test/lib/my_john_deere_api/model/contribution_definition_test.rb +52 -0
- data/test/lib/my_john_deere_api/model/contribution_product_test.rb +82 -0
- data/test/lib/my_john_deere_api/model/field_test.rb +65 -0
- data/test/lib/my_john_deere_api/model/flag_test.rb +48 -0
- data/test/lib/my_john_deere_api/model/organization_test.rb +84 -0
- data/test/lib/my_john_deere_api/model_test.rb +37 -0
- data/test/lib/my_john_deere_api/net_http_retry/decorator_test.rb +163 -0
- data/test/lib/my_john_deere_api/request/collection/asset_locations_test.rb +102 -0
- data/test/lib/my_john_deere_api/request/collection/assets_test.rb +99 -0
- data/test/lib/my_john_deere_api/request/collection/base_test.rb +27 -0
- data/test/lib/my_john_deere_api/request/collection/contribution_definitions_test.rb +88 -0
- data/test/lib/my_john_deere_api/request/collection/contribution_products_test.rb +88 -0
- data/test/lib/my_john_deere_api/request/collection/fields_test.rb +86 -0
- data/test/lib/my_john_deere_api/request/collection/flags_test.rb +92 -0
- data/test/lib/my_john_deere_api/request/collection/organizations_test.rb +87 -0
- data/test/lib/my_john_deere_api/request/collection_test.rb +37 -0
- data/test/lib/my_john_deere_api/request/create/asset_location_test.rb +163 -0
- data/test/lib/my_john_deere_api/request/create/asset_test.rb +182 -0
- data/test/lib/my_john_deere_api/request/create/base_test.rb +29 -0
- data/test/lib/my_john_deere_api/request/create_test.rb +17 -0
- data/test/lib/my_john_deere_api/request/individual/asset_test.rb +33 -0
- data/test/lib/my_john_deere_api/request/individual/base_test.rb +18 -0
- data/test/lib/my_john_deere_api/request/individual/contribution_definition_test.rb +33 -0
- data/test/lib/my_john_deere_api/request/individual/contribution_product_test.rb +33 -0
- data/test/lib/my_john_deere_api/request/individual/field_test.rb +37 -0
- data/test/lib/my_john_deere_api/request/individual/organization_test.rb +33 -0
- data/test/lib/my_john_deere_api/request/individual_test.rb +29 -0
- data/test/lib/my_john_deere_api/request/update/asset_test.rb +99 -0
- data/test/lib/my_john_deere_api/request/update/base_test.rb +60 -0
- data/test/lib/my_john_deere_api/request/update_test.rb +13 -0
- data/test/lib/my_john_deere_api/request_test.rb +21 -0
- data/test/lib/my_john_deere_api/validators/asset_location_test.rb +61 -0
- data/test/lib/my_john_deere_api/validators/asset_test.rb +93 -0
- data/test/lib/my_john_deere_api/validators/base_test.rb +92 -0
- data/test/lib/my_john_deere_api/validators_test.rb +17 -0
- data/test/lib/my_john_deere_api/version_test.rb +9 -0
- data/test/my_john_deere_api_test.rb +37 -0
- data/test/support/helper.rb +97 -0
- data/test/support/vcr/accessor/delete_failed.yml +327 -0
- data/test/support/vcr/accessor/delete_max_failed.yml +615 -0
- data/test/support/vcr/accessor/delete_retry.yml +191 -0
- data/test/support/vcr/accessor/delete_retry_too_soon.yml +191 -0
- data/test/support/vcr/accessor/get_failed.yml +390 -0
- data/test/support/vcr/accessor/get_max_failed.yml +734 -0
- data/test/support/vcr/accessor/get_retry.yml +226 -0
- data/test/support/vcr/accessor/get_retry_too_soon.yml +226 -0
- data/test/support/vcr/accessor/post_failed.yml +417 -0
- data/test/support/vcr/accessor/post_max_failed.yml +785 -0
- data/test/support/vcr/accessor/post_retry.yml +241 -0
- data/test/support/vcr/accessor/post_retry_too_soon.yml +241 -0
- data/test/support/vcr/accessor/put_failed.yml +372 -0
- data/test/support/vcr/accessor/put_max_failed.yml +700 -0
- data/test/support/vcr/accessor/put_retry.yml +216 -0
- data/test/support/vcr/accessor/put_retry_too_soon.yml +216 -0
- data/test/support/vcr/catalog.yml +89 -0
- data/test/support/vcr/delete_asset.yml +82 -0
- data/test/support/vcr/get_access_token.yml +41 -0
- data/test/support/vcr/get_asset.yml +144 -0
- data/test/support/vcr/get_asset_locations.yml +196 -0
- data/test/support/vcr/get_assets.yml +152 -0
- data/test/support/vcr/get_contribution_definition.yml +90 -0
- data/test/support/vcr/get_contribution_definitions.yml +91 -0
- data/test/support/vcr/get_contribution_product.yml +91 -0
- data/test/support/vcr/get_contribution_products.yml +91 -0
- data/test/support/vcr/get_field.yml +144 -0
- data/test/support/vcr/get_fields.yml +146 -0
- data/test/support/vcr/get_flags.yml +47 -0
- data/test/support/vcr/get_organization.yml +90 -0
- data/test/support/vcr/get_organizations.yml +149 -0
- data/test/support/vcr/get_request_token.yml +83 -0
- data/test/support/vcr/post_asset_locations.yml +244 -0
- data/test/support/vcr/post_assets.yml +192 -0
- data/test/support/vcr/put_asset.yml +87 -0
- data/test/support/vcr/warning.txt +28 -0
- data/test/support/vcr_setup.rb +488 -0
- metadata +277 -0
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'support/helper'
|
2
|
+
|
3
|
+
describe 'MyJohnDeereApi::Request::Collection' do
|
4
|
+
describe 'loading dependencies' do
|
5
|
+
it 'loads Request::Collection::Base' do
|
6
|
+
assert JD::Request::Collection::Base
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'loads Request::Collection::Assets' do
|
10
|
+
assert JD::Request::Collection::Assets
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'loads Request::Collection::AssetLocations' do
|
14
|
+
assert JD::Request::Collection::AssetLocations
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'loads Request::Collection::ContributionProducts' do
|
18
|
+
assert JD::Request::Collection::ContributionProducts
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'loads Request::Collection::ContributionDefinitions' do
|
22
|
+
assert JD::Request::Collection::ContributionDefinitions
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'loads Request::Collection::Organizations' do
|
26
|
+
assert JD::Request::Collection::Organizations
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'loads Request::Collection::Fields' do
|
30
|
+
assert JD::Request::Collection::Fields
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'loads Request::Collection::Flags' do
|
34
|
+
assert JD::Request::Collection::Flags
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,163 @@
|
|
1
|
+
require 'support/helper'
|
2
|
+
require 'date'
|
3
|
+
|
4
|
+
describe 'MyJohnDeereApi::Request::Create::AssetLocation' do
|
5
|
+
def attributes_without(*keys)
|
6
|
+
keys = keys.to_a
|
7
|
+
attributes.reject{|k,v| keys.include?(k)}
|
8
|
+
end
|
9
|
+
|
10
|
+
let(:valid_attributes) do
|
11
|
+
CONFIG.asset_location_attributes.merge(
|
12
|
+
asset_id: asset_id
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:attributes) { valid_attributes }
|
17
|
+
|
18
|
+
let(:klass) { JD::Request::Create::AssetLocation }
|
19
|
+
let(:object) { klass.new(client, attributes) }
|
20
|
+
|
21
|
+
inherits_from MyJohnDeereApi::Request::Create::Base
|
22
|
+
|
23
|
+
describe '#initialize(client, attributes)' do
|
24
|
+
it 'accepts a client and attributes' do
|
25
|
+
assert_equal client, object.client
|
26
|
+
assert_equal accessor, object.accessor
|
27
|
+
assert_equal attributes, object.attributes
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'accepts simple coordinates and generates the geometry' do
|
31
|
+
attributes = {
|
32
|
+
asset_id: asset_id,
|
33
|
+
timestamp: valid_attributes[:timestamp],
|
34
|
+
coordinates: CONFIG.coordinates,
|
35
|
+
measurement_data: valid_attributes[:measurement_data]
|
36
|
+
}
|
37
|
+
|
38
|
+
object = klass.new(client, attributes)
|
39
|
+
assert_equal valid_attributes[:geometry].to_json, object.attributes[:geometry]
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'defaults timestamp to current time' do
|
43
|
+
attributes = valid_attributes.slice(:asset_id, :geometry, :measurement_data)
|
44
|
+
object = klass.new(client, attributes)
|
45
|
+
|
46
|
+
expected_stamp = Time.now.utc.to_i
|
47
|
+
actual_stamp = DateTime.parse(object.attributes[:timestamp]).to_time.to_i
|
48
|
+
|
49
|
+
assert_in_delta expected_stamp, actual_stamp, 1
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe '#valid?' do
|
54
|
+
it 'returns true when all required attributes are present' do
|
55
|
+
assert object.valid?
|
56
|
+
assert_empty object.errors
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'requires asset_id' do
|
60
|
+
object = klass.new(client, attributes_without(:asset_id))
|
61
|
+
|
62
|
+
refute object.valid?
|
63
|
+
assert_equal 'is required', object.errors[:asset_id]
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'requires geometry' do
|
67
|
+
object = klass.new(client, attributes_without(:geometry))
|
68
|
+
|
69
|
+
refute object.valid?
|
70
|
+
assert_equal 'is required', object.errors[:geometry]
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'requires measurement_data' do
|
74
|
+
object = klass.new(client, attributes_without(:measurement_data))
|
75
|
+
|
76
|
+
refute object.valid?
|
77
|
+
assert_equal 'is required', object.errors[:measurement_data]
|
78
|
+
end
|
79
|
+
|
80
|
+
describe 'validating measurement_data' do
|
81
|
+
it 'must be an array' do
|
82
|
+
object = klass.new(client, attributes.merge(measurement_data: 'something'))
|
83
|
+
|
84
|
+
refute object.valid?
|
85
|
+
assert_equal 'must be an array', object.errors[:measurement_data]
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'must include a name' do
|
89
|
+
without_attr = [attributes[:measurement_data].first.reject{|k,v| k == :name}]
|
90
|
+
object = klass.new(client, attributes.merge(measurement_data: without_attr))
|
91
|
+
|
92
|
+
refute object.valid?
|
93
|
+
assert_equal 'must include name', object.errors[:measurement_data]
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'must include a value' do
|
97
|
+
without_attr = [attributes[:measurement_data].first.reject{|k,v| k == :value}]
|
98
|
+
object = klass.new(client, attributes.merge(measurement_data: without_attr))
|
99
|
+
|
100
|
+
refute object.valid?
|
101
|
+
assert_equal 'must include value', object.errors[:measurement_data]
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'must include a unit' do
|
105
|
+
without_attr = [attributes[:measurement_data].first.reject{|k,v| k == :unit}]
|
106
|
+
object = klass.new(client, attributes.merge(measurement_data: without_attr))
|
107
|
+
|
108
|
+
refute object.valid?
|
109
|
+
assert_equal 'must include unit', object.errors[:measurement_data]
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe '#validate!' do
|
115
|
+
it 'raises an error when invalid' do
|
116
|
+
object = klass.new(client, attributes_without(:asset_id))
|
117
|
+
|
118
|
+
exception = assert_raises(JD::InvalidRecordError) { object.validate! }
|
119
|
+
assert_includes exception.message, 'Record is invalid'
|
120
|
+
assert_includes exception.message, 'asset_id is required'
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe '#request_body' do
|
125
|
+
it 'properly forms the request body' do
|
126
|
+
body = object.send(:request_body)
|
127
|
+
|
128
|
+
expected_stamp = DateTime.parse(attributes[:timestamp]).strftime('%Y-%m-%dT%H:%M:%S.000Z')
|
129
|
+
|
130
|
+
assert_kind_of Array, body
|
131
|
+
assert_equal expected_stamp, body.first[:timestamp]
|
132
|
+
assert_equal attributes[:geometry], body.first[:geometry]
|
133
|
+
assert_equal attributes[:measurement_data], body.first[:measurementData]
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
describe '#request' do
|
138
|
+
it 'makes the request' do
|
139
|
+
VCR.use_cassette('post_asset_locations') { object.request }
|
140
|
+
|
141
|
+
assert_kind_of Net::HTTPCreated, object.response
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
describe '#object' do
|
146
|
+
it 'returns the asset location model instance' do
|
147
|
+
result = VCR.use_cassette('post_asset_locations') { object.object }
|
148
|
+
|
149
|
+
assert_kind_of JD::Model::AssetLocation, result
|
150
|
+
|
151
|
+
# API returns seconds with decimals, even though they're always zero
|
152
|
+
integer_stamp = DateTime.parse(result.timestamp).strftime('%Y-%m-%dT%H:%M:%SZ')
|
153
|
+
expected_stamp = DateTime.parse(attributes[:timestamp]).strftime('%Y-%m-%dT%H:%M:%SZ')
|
154
|
+
|
155
|
+
# API returns string keys and an extra '@type' key
|
156
|
+
result_measurement_data = result.measurement_data.first.transform_keys{|k| k.to_sym}.slice(:name, :value, :unit)
|
157
|
+
|
158
|
+
assert_equal expected_stamp, integer_stamp
|
159
|
+
assert_equal attributes[:geometry], result.geometry.to_json
|
160
|
+
assert_equal attributes[:measurement_data].first, result_measurement_data
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
@@ -0,0 +1,182 @@
|
|
1
|
+
require 'support/helper'
|
2
|
+
|
3
|
+
describe 'MyJohnDeereApi::Request::Create::Asset' do
|
4
|
+
def attributes_without(*keys)
|
5
|
+
keys = keys.to_a
|
6
|
+
attributes.reject{|k,v| keys.include?(k)}
|
7
|
+
end
|
8
|
+
|
9
|
+
let(:valid_attributes) do
|
10
|
+
CONFIG.asset_attributes.merge(
|
11
|
+
organization_id: organization_id,
|
12
|
+
)
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:klass) { JD::Request::Create::Asset }
|
16
|
+
let(:object) { klass.new(client, attributes) }
|
17
|
+
|
18
|
+
let(:attributes) { valid_attributes }
|
19
|
+
|
20
|
+
inherits_from MyJohnDeereApi::Request::Create::Base
|
21
|
+
|
22
|
+
describe '#initialize(client, attributes)' do
|
23
|
+
it 'accepts a client and attributes' do
|
24
|
+
assert_equal client, object.client
|
25
|
+
assert_equal accessor, object.accessor
|
26
|
+
assert_equal attributes, object.attributes
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#valid?' do
|
31
|
+
it 'returns true when all required attributes are present' do
|
32
|
+
assert object.valid?
|
33
|
+
assert_empty object.errors
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'requires organization_id' do
|
37
|
+
object = klass.new(client, attributes_without(:organization_id))
|
38
|
+
|
39
|
+
refute object.valid?
|
40
|
+
assert_equal 'is required', object.errors[:organization_id]
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'requires title' do
|
44
|
+
object = klass.new(client, attributes_without(:title))
|
45
|
+
|
46
|
+
refute object.valid?
|
47
|
+
assert_equal 'is required', object.errors[:title]
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'requires a valid category' do
|
51
|
+
object = klass.new(client, attributes.merge(asset_category: 'TURTLES'))
|
52
|
+
|
53
|
+
refute object.valid?
|
54
|
+
assert_equal 'requires valid combination of category/type/subtype', object.errors[:asset_category]
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'requires a valid type' do
|
58
|
+
object = klass.new(client, attributes.merge(asset_type: 'TURTLES'))
|
59
|
+
|
60
|
+
refute object.valid?
|
61
|
+
assert_equal 'requires valid combination of category/type/subtype', object.errors[:asset_category]
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'requires a valid subtype' do
|
65
|
+
object = klass.new(client, attributes.merge(asset_sub_type: 'TURTLES'))
|
66
|
+
|
67
|
+
refute object.valid?
|
68
|
+
assert_equal 'requires valid combination of category/type/subtype', object.errors[:asset_category]
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe '#validate!' do
|
73
|
+
it 'raises an error when invalid' do
|
74
|
+
object = klass.new(client, attributes_without(:organization_id))
|
75
|
+
|
76
|
+
exception = assert_raises(JD::InvalidRecordError) { object.validate! }
|
77
|
+
assert_includes exception.message, 'Record is invalid'
|
78
|
+
assert_includes exception.message, 'organization_id is required'
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe '#valid_categories?(category, type, subtype)' do
|
83
|
+
it 'only allows pre-defined combinations' do
|
84
|
+
object = klass.new(client, {})
|
85
|
+
|
86
|
+
valid_combos = [
|
87
|
+
['DEVICE', 'SENSOR', 'ENVIRONMENTAL'],
|
88
|
+
['DEVICE', 'SENSOR', 'GRAIN_BIN'],
|
89
|
+
['DEVICE', 'SENSOR', 'IRRIGATION_PIVOT'],
|
90
|
+
['DEVICE', 'SENSOR', 'OTHER'],
|
91
|
+
['EQUIPMENT', 'MACHINE', 'PICKUP_TRUCK'],
|
92
|
+
['EQUIPMENT', 'MACHINE', 'UTILITY_VEHICLE'],
|
93
|
+
['EQUIPMENT', 'OTHER', 'ANHYDROUS_AMMONIA_TANK'],
|
94
|
+
['EQUIPMENT', 'OTHER', 'NURSE_TRUCK'],
|
95
|
+
['EQUIPMENT', 'OTHER', 'NURSE_WAGON'],
|
96
|
+
['EQUIPMENT', 'OTHER', 'TECHNICIAN_TRUCK']
|
97
|
+
]
|
98
|
+
|
99
|
+
# cycle through all possible permutations, only proving valid if
|
100
|
+
# listed above
|
101
|
+
|
102
|
+
['DEVICE', 'EQUIPMENT', 'RANDOM_INVALID'].each do |category|
|
103
|
+
['MACHINE', 'OTHER', 'SENSOR', 'RANDOM_INVALID'].each do |type|
|
104
|
+
[
|
105
|
+
'ANHYDROUS_AMMONIA_TANK', 'ENVIRONMENTAL', 'GRAIN_BIN',
|
106
|
+
'IRRIGATION_PIVOT', 'NURSE_TRUCK', 'NURSE_WAGON',
|
107
|
+
'OTHER', 'PICKUP_TRUCK', 'TECHNICIAN_TRUCK',
|
108
|
+
'UTILITY_VEHICLE', 'RANDOM_INVALID'
|
109
|
+
].each do |subtype|
|
110
|
+
if valid_combos.include?([category, type, subtype])
|
111
|
+
assert object.send(:valid_categories?, category, type, subtype)
|
112
|
+
else
|
113
|
+
refute object.send(:valid_categories?, category, type, subtype)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
describe '#resource' do
|
122
|
+
it 'is built from the organization id' do
|
123
|
+
object = klass.new(client, attributes)
|
124
|
+
assert_equal "/organizations/#{organization_id}/assets", object.send(:resource)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
describe '#request_body' do
|
129
|
+
it 'properly forms the request body' do
|
130
|
+
object = klass.new(client, attributes)
|
131
|
+
body = object.send(:request_body)
|
132
|
+
|
133
|
+
assert_equal attributes[:title], body[:title]
|
134
|
+
assert_equal attributes[:asset_category], body[:assetCategory]
|
135
|
+
assert_equal attributes[:asset_type], body[:assetType]
|
136
|
+
assert_equal attributes[:asset_sub_type], body[:assetSubType]
|
137
|
+
|
138
|
+
assert_kind_of Array, body[:links]
|
139
|
+
assert_equal 1, body[:links].size
|
140
|
+
|
141
|
+
assert_kind_of Hash, body[:links].first
|
142
|
+
assert_equal 'Link', body[:links].first['@type']
|
143
|
+
assert_equal 'contributionDefinition', body[:links].first['rel']
|
144
|
+
assert_equal "#{base_url}/contributionDefinitions/#{contribution_definition_id}",
|
145
|
+
body[:links].first['uri']
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'raises an exception when contribution_definition_id is not set' do
|
149
|
+
client.contribution_definition_id = nil
|
150
|
+
object = klass.new(client, attributes)
|
151
|
+
|
152
|
+
assert_raises(JD::MissingContributionDefinitionIdError) do
|
153
|
+
object.send(:request_body)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
describe '#request' do
|
159
|
+
it 'makes the request' do
|
160
|
+
VCR.use_cassette('post_assets') { object.request }
|
161
|
+
|
162
|
+
assert_kind_of Net::HTTPCreated, object.response
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
describe '#object' do
|
167
|
+
it 'returns the asset model instance' do
|
168
|
+
object = klass.new(client, attributes)
|
169
|
+
result = VCR.use_cassette('post_assets') { object.object }
|
170
|
+
|
171
|
+
assert_kind_of JD::Model::Asset, result
|
172
|
+
|
173
|
+
expected_id = object.response['location'].split('/').last
|
174
|
+
|
175
|
+
assert_equal expected_id, result.id
|
176
|
+
assert_equal attributes[:title], result.title
|
177
|
+
assert_equal attributes[:asset_category], result.asset_category
|
178
|
+
assert_equal attributes[:asset_type], result.asset_type
|
179
|
+
assert_equal attributes[:asset_sub_type], result.asset_sub_type
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'support/helper'
|
2
|
+
|
3
|
+
describe 'MyJohnDeereApi::Request::Create::Base' do
|
4
|
+
let(:klass) { JD::Request::Create::Base }
|
5
|
+
let(:attributes) { {} }
|
6
|
+
|
7
|
+
describe '#initialize(client, attributes)' do
|
8
|
+
it 'accepts a client and attributes' do
|
9
|
+
object = klass.new(client, attributes)
|
10
|
+
|
11
|
+
assert_equal client, object.client
|
12
|
+
assert_equal accessor, object.accessor
|
13
|
+
assert_equal attributes, object.attributes
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#headers' do
|
18
|
+
it 'sets the accept and content-type headers' do
|
19
|
+
object = klass.new(client, attributes)
|
20
|
+
headers = object.send(:headers)
|
21
|
+
|
22
|
+
expected = 'application/vnd.deere.axiom.v3+json'
|
23
|
+
|
24
|
+
assert_kind_of Hash, headers
|
25
|
+
assert_equal expected, headers['Accept']
|
26
|
+
assert_equal expected, headers['Content-Type']
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'support/helper'
|
2
|
+
|
3
|
+
describe 'MyJohnDeereApi::Request::Create' do
|
4
|
+
describe 'loading dependencies' do
|
5
|
+
it 'loads Request::Create::Base' do
|
6
|
+
assert JD::Request::Create::Base
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'loads Request::Create::Asset' do
|
10
|
+
assert JD::Request::Create::Asset
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'loads Request::Create::AssetLocation' do
|
14
|
+
assert JD::Request::Create::AssetLocation
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'support/helper'
|
2
|
+
require 'yaml'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
describe 'MyJohnDeereApi::Request::Individual::Asset' do
|
6
|
+
let(:object) { JD::Request::Individual::Asset.new(client, asset_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 asset_id as id' do
|
17
|
+
assert_equal asset_id, object.id
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#resource' do
|
22
|
+
it 'returns /assets/<asset_id>' do
|
23
|
+
assert_equal "/assets/#{asset_id}", object.resource
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe '#object' do
|
28
|
+
it 'returns all records' do
|
29
|
+
asset = VCR.use_cassette('get_asset') { object.object }
|
30
|
+
assert_kind_of JD::Model::Asset, asset
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|