@ndustrial/contxt-sdk 4.3.4 → 5.1.0

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 (169) hide show
  1. package/.gitattributes +1 -0
  2. package/.nvmrc +1 -1
  3. package/CHANGELOG.md +8 -0
  4. package/docs/Facilities.md +0 -144
  5. package/docs/FacilityGroupings.md +0 -160
  6. package/docs/Nionic.md +63 -0
  7. package/docs/README.md +3 -29
  8. package/docs/Typedefs.md +0 -101
  9. package/esm/config/audiences.js +13 -13
  10. package/esm/config/audiences.js.map +1 -1
  11. package/esm/index.js +3 -6
  12. package/esm/index.js.map +1 -1
  13. package/esm/nionic/index.js +126 -0
  14. package/esm/nionic/index.js.map +1 -0
  15. package/lib/config/audiences.js +13 -13
  16. package/lib/config/audiences.js.map +1 -1
  17. package/lib/index.js +19 -13
  18. package/lib/index.js.map +1 -1
  19. package/lib/nionic/index.js +130 -0
  20. package/lib/nionic/index.js.map +1 -0
  21. package/meta.yaml +11 -0
  22. package/package.json +9 -10
  23. package/src/bus/index.spec.js +3 -2
  24. package/src/bus/webSocketConnection.spec.js +31 -31
  25. package/src/config/audiences.js +13 -13
  26. package/src/config/index.spec.js +6 -6
  27. package/src/coordinator/applications.spec.js +8 -8
  28. package/src/coordinator/consent.spec.js +1 -1
  29. package/src/coordinator/edgeNodes.spec.js +3 -3
  30. package/src/coordinator/organizations.spec.js +4 -4
  31. package/src/coordinator/permissions.spec.js +4 -4
  32. package/src/coordinator/roles.spec.js +3 -3
  33. package/src/coordinator/users.spec.js +14 -15
  34. package/src/events/index.spec.js +16 -16
  35. package/src/files/index.spec.js +7 -7
  36. package/src/index.js +3 -6
  37. package/src/index.spec.js +8 -22
  38. package/src/iot/feeds.spec.js +4 -4
  39. package/src/iot/fieldCategories.spec.js +4 -4
  40. package/src/iot/fieldGroupings.spec.js +5 -5
  41. package/src/iot/outputs.spec.js +6 -6
  42. package/src/nionic/index.js +151 -0
  43. package/src/request.spec.js +3 -3
  44. package/src/sessionTypes/auth0WebAuth.spec.js +2 -2
  45. package/src/sessionTypes/index.spec.js +3 -3
  46. package/src/sessionTypes/machineAuth.spec.js +3 -3
  47. package/src/sessionTypes/passwordGrantAuth.spec.js +1 -1
  48. package/src/utils/iot/formatOutputFieldDataFromServer.spec.js +6 -6
  49. package/src/utils/iot/parseOutputFieldNextPageUrlMetadata.spec.js +2 -2
  50. package/src/utils/pagination/formatPaginatedDataFromServer.spec.js +2 -2
  51. package/src/utils/url/stringifyParams.spec.js +2 -3
  52. package/support/fixtures/factories/IOSDevice.js +3 -3
  53. package/support/fixtures/factories/applicationGrouping.js +4 -4
  54. package/support/fixtures/factories/applicationModule.js +2 -2
  55. package/support/fixtures/factories/asset.js +1 -1
  56. package/support/fixtures/factories/assetAttribute.js +2 -2
  57. package/support/fixtures/factories/assetAttributeValue.js +2 -2
  58. package/support/fixtures/factories/assetMetric.js +1 -1
  59. package/support/fixtures/factories/assetMetricValue.js +3 -3
  60. package/support/fixtures/factories/assetMetricValueCompact.js +2 -2
  61. package/support/fixtures/factories/assetType.js +1 -1
  62. package/support/fixtures/factories/authResults.js +3 -3
  63. package/support/fixtures/factories/channel.js +1 -1
  64. package/support/fixtures/factories/contxtApplication.js +5 -5
  65. package/support/fixtures/factories/contxtOrganization.js +2 -2
  66. package/support/fixtures/factories/contxtOrganizationFeaturedApplication.js +3 -3
  67. package/support/fixtures/factories/contxtProject.js +2 -2
  68. package/support/fixtures/factories/contxtProjectEnvironment.js +2 -2
  69. package/support/fixtures/factories/contxtRole.js +3 -3
  70. package/support/fixtures/factories/contxtRoleApplication.js +1 -1
  71. package/support/fixtures/factories/contxtRoleProjectEnvironment.js +1 -1
  72. package/support/fixtures/factories/contxtUser.js +2 -2
  73. package/support/fixtures/factories/contxtUserApplication.js +1 -1
  74. package/support/fixtures/factories/contxtUserFavoriteApplication.js +2 -2
  75. package/support/fixtures/factories/contxtUserPermissions.js +5 -5
  76. package/support/fixtures/factories/contxtUserProjectEnvironment.js +1 -1
  77. package/support/fixtures/factories/contxtUserRole.js +2 -2
  78. package/support/fixtures/factories/costCenter.js +2 -2
  79. package/support/fixtures/factories/costCenterFacility.js +3 -3
  80. package/support/fixtures/factories/edgeNode.js +1 -1
  81. package/support/fixtures/factories/event.js +5 -5
  82. package/support/fixtures/factories/eventType.js +4 -4
  83. package/support/fixtures/factories/eventUser.js +5 -5
  84. package/support/fixtures/factories/facility.js +4 -4
  85. package/support/fixtures/factories/facilityGrouping.js +3 -3
  86. package/support/fixtures/factories/facilityGroupingFacility.js +3 -3
  87. package/support/fixtures/factories/facilityInfo.js +2 -2
  88. package/support/fixtures/factories/facilityTag.js +1 -1
  89. package/support/fixtures/factories/feedStatus.js +1 -1
  90. package/support/fixtures/factories/feedType.js +1 -1
  91. package/support/fixtures/factories/feeds.js +5 -5
  92. package/support/fixtures/factories/fieldCategory.js +3 -3
  93. package/support/fixtures/factories/fieldGrouping.js +4 -4
  94. package/support/fixtures/factories/fieldGroupingField.js +3 -3
  95. package/support/fixtures/factories/fieldGroupingStatus.js +1 -1
  96. package/support/fixtures/factories/file.js +1 -1
  97. package/support/fixtures/factories/fileUploadInfo.js +1 -1
  98. package/support/fixtures/factories/organization.js +1 -1
  99. package/support/fixtures/factories/outputField.js +10 -10
  100. package/support/fixtures/factories/outputFieldData.js +1 -1
  101. package/support/fixtures/factories/owner.js +2 -2
  102. package/support/fixtures/factories/paginationMetadata.js +2 -2
  103. package/support/fixtures/factories/triggeredEvent.js +4 -4
  104. package/support/fixtures/factories/userEventSubscription.js +1 -1
  105. package/support/fixtures/factories/userMobileNumber.js +1 -1
  106. package/support/fixtures/factories/userPermissionsMap.js +2 -2
  107. package/.envrc +0 -0
  108. package/.huskyrc.js +0 -11
  109. package/.nycrc +0 -12
  110. package/esm/assets/assetAttributes.js +0 -574
  111. package/esm/assets/assetAttributes.js.map +0 -1
  112. package/esm/assets/assetMetrics.js +0 -690
  113. package/esm/assets/assetMetrics.js.map +0 -1
  114. package/esm/assets/assetTypes.js +0 -273
  115. package/esm/assets/assetTypes.js.map +0 -1
  116. package/esm/assets/index.js +0 -283
  117. package/esm/assets/index.js.map +0 -1
  118. package/esm/facilities/costCenters.js +0 -306
  119. package/esm/facilities/costCenters.js.map +0 -1
  120. package/esm/facilities/groupings.js +0 -323
  121. package/esm/facilities/groupings.js.map +0 -1
  122. package/esm/facilities/index.js +0 -343
  123. package/esm/facilities/index.js.map +0 -1
  124. package/esm/health/index.js +0 -229
  125. package/esm/health/index.js.map +0 -1
  126. package/esm/utils/facilities/formatFacilityWithInfoFromServer.js +0 -65
  127. package/esm/utils/facilities/formatFacilityWithInfoFromServer.js.map +0 -1
  128. package/esm/utils/facilities/index.js +0 -4
  129. package/esm/utils/facilities/index.js.map +0 -1
  130. package/lib/assets/assetAttributes.js +0 -588
  131. package/lib/assets/assetAttributes.js.map +0 -1
  132. package/lib/assets/assetMetrics.js +0 -705
  133. package/lib/assets/assetMetrics.js.map +0 -1
  134. package/lib/assets/assetTypes.js +0 -287
  135. package/lib/assets/assetTypes.js.map +0 -1
  136. package/lib/assets/index.js +0 -302
  137. package/lib/assets/index.js.map +0 -1
  138. package/lib/facilities/costCenters.js +0 -316
  139. package/lib/facilities/costCenters.js.map +0 -1
  140. package/lib/facilities/groupings.js +0 -333
  141. package/lib/facilities/groupings.js.map +0 -1
  142. package/lib/facilities/index.js +0 -360
  143. package/lib/facilities/index.js.map +0 -1
  144. package/lib/health/index.js +0 -232
  145. package/lib/health/index.js.map +0 -1
  146. package/lib/utils/facilities/formatFacilityWithInfoFromServer.js +0 -71
  147. package/lib/utils/facilities/formatFacilityWithInfoFromServer.js.map +0 -1
  148. package/lib/utils/facilities/index.js +0 -15
  149. package/lib/utils/facilities/index.js.map +0 -1
  150. package/src/assets/assetAttributes.js +0 -619
  151. package/src/assets/assetAttributes.spec.js +0 -1143
  152. package/src/assets/assetMetrics.js +0 -736
  153. package/src/assets/assetMetrics.spec.js +0 -1180
  154. package/src/assets/assetTypes.js +0 -260
  155. package/src/assets/assetTypes.spec.js +0 -556
  156. package/src/assets/index.js +0 -271
  157. package/src/assets/index.spec.js +0 -515
  158. package/src/facilities/costCenters.js +0 -295
  159. package/src/facilities/costCenters.spec.js +0 -529
  160. package/src/facilities/groupings.js +0 -320
  161. package/src/facilities/groupings.spec.js +0 -567
  162. package/src/facilities/index.js +0 -343
  163. package/src/facilities/index.spec.js +0 -551
  164. package/src/health/index.js +0 -200
  165. package/src/health/index.spec.js +0 -501
  166. package/src/utils/facilities/formatFacilityWithInfoFromServer.js +0 -62
  167. package/src/utils/facilities/formatFacilityWithInfoFromServer.spec.js +0 -73
  168. package/src/utils/facilities/index.js +0 -3
  169. package/tmp/package.json +0 -11
@@ -1,1143 +0,0 @@
1
- import omit from 'lodash.omit';
2
- import AssetAttributes from './assetAttributes';
3
- import * as objectUtils from '../utils/objects';
4
- import * as paginationUtils from '../utils/pagination';
5
-
6
- describe('Assets/Attributes', function() {
7
- let baseRequest;
8
- let baseSdk;
9
- let expectedHost;
10
-
11
- beforeEach(function() {
12
- baseRequest = {
13
- delete: sinon.stub().resolves(),
14
- get: sinon.stub().resolves(),
15
- post: sinon.stub().resolves(),
16
- put: sinon.stub().resolves()
17
- };
18
- baseSdk = {
19
- config: {
20
- audiences: {
21
- facilities: fixture.build('audience')
22
- }
23
- }
24
- };
25
- expectedHost = faker.internet.url();
26
- });
27
-
28
- afterEach(function() {
29
- sinon.restore();
30
- });
31
-
32
- describe('constructor', function() {
33
- let assetAttributes;
34
-
35
- beforeEach(function() {
36
- assetAttributes = new AssetAttributes(baseSdk, baseRequest, expectedHost);
37
- });
38
-
39
- it('sets a base url for the class instance', function() {
40
- expect(assetAttributes._baseUrl).to.equal(expectedHost);
41
- });
42
-
43
- it('appends the supplied request module to the class instance', function() {
44
- expect(assetAttributes._request).to.deep.equal(baseRequest);
45
- });
46
-
47
- it('appends the supplied sdk to the class instance', function() {
48
- expect(assetAttributes._sdk).to.deep.equal(baseSdk);
49
- });
50
- });
51
-
52
- describe('create', function() {
53
- context('when all required information is supplied', function() {
54
- let assetAttributeFromServerAfterFormat;
55
- let assetAttributeFromServerBeforeFormat;
56
- let assetAttributeToServerAfterFormat;
57
- let assetAttributeToServerBeforeFormat;
58
- let assetTypeId;
59
- let promise;
60
- let request;
61
- let toCamelCase;
62
- let toSnakeCase;
63
-
64
- beforeEach(function() {
65
- assetAttributeFromServerAfterFormat = fixture.build('assetAttribute');
66
- assetAttributeFromServerBeforeFormat = fixture.build(
67
- 'assetAttribute',
68
- null,
69
- { fromServer: true }
70
- );
71
- assetAttributeToServerAfterFormat = fixture.build(
72
- 'assetAttribute',
73
- null,
74
- { fromServer: true }
75
- );
76
- assetAttributeToServerBeforeFormat = fixture.build('assetAttribute');
77
-
78
- assetTypeId = fixture.build('assetType').id;
79
-
80
- request = {
81
- ...baseRequest,
82
- post: sinon.stub().resolves(assetAttributeFromServerBeforeFormat)
83
- };
84
- toCamelCase = sinon
85
- .stub(objectUtils, 'toCamelCase')
86
- .returns(assetAttributeFromServerAfterFormat);
87
- toSnakeCase = sinon
88
- .stub(objectUtils, 'toSnakeCase')
89
- .returns(assetAttributeToServerAfterFormat);
90
-
91
- const assetAttributes = new AssetAttributes(
92
- baseSdk,
93
- request,
94
- expectedHost
95
- );
96
-
97
- promise = assetAttributes.create(
98
- assetTypeId,
99
- assetAttributeToServerBeforeFormat
100
- );
101
- });
102
-
103
- it('formats the submitted asset attribute object to send to the server', function() {
104
- expect(toSnakeCase).to.be.deep.calledWith(
105
- assetAttributeToServerBeforeFormat
106
- );
107
- });
108
-
109
- it('creates a new asset attribute', function() {
110
- expect(request.post).to.be.deep.calledWith(
111
- `${expectedHost}/assets/types/${assetTypeId}/attributes`,
112
- assetAttributeToServerAfterFormat
113
- );
114
- });
115
-
116
- it('formats the returned asset attribute object', function() {
117
- return promise.then(() => {
118
- expect(toCamelCase).to.be.deep.calledWith(
119
- assetAttributeFromServerBeforeFormat
120
- );
121
- });
122
- });
123
-
124
- it('returns a fulfilled promise with the new asset attribute information', function() {
125
- return expect(promise).to.be.fulfilled.and.to.eventually.deep.equal(
126
- assetAttributeFromServerAfterFormat
127
- );
128
- });
129
- });
130
-
131
- context('when creating a global attribute', function() {
132
- let assetTypeId;
133
- let attributeFromServerAfterFormat;
134
- let attributeFromServerBeforeFormat;
135
- let attributeToServerAfterFormat;
136
- let attributeToServerBeforeFormat;
137
- let promise;
138
- let request;
139
- let toCamelCase;
140
- let toSnakeCase;
141
-
142
- beforeEach(function() {
143
- attributeFromServerBeforeFormat = fixture.build('assetAttribute', {
144
- organizationId: null
145
- });
146
- attributeFromServerAfterFormat = fixture.build(
147
- 'assetAttribute',
148
- attributeToServerBeforeFormat,
149
- { fromServer: true }
150
- );
151
- attributeToServerBeforeFormat = fixture.build('assetAttribute', {
152
- organizationId: null
153
- });
154
- attributeToServerAfterFormat = fixture.build(
155
- 'assetAttribute',
156
- attributeToServerBeforeFormat,
157
- { fromServer: true }
158
- );
159
-
160
- assetTypeId = fixture.build('assetType').id;
161
-
162
- request = {
163
- ...baseRequest,
164
- post: sinon.stub().resolves(attributeFromServerBeforeFormat)
165
- };
166
- toCamelCase = sinon
167
- .stub(objectUtils, 'toCamelCase')
168
- .returns(attributeFromServerAfterFormat);
169
- toSnakeCase = sinon
170
- .stub(objectUtils, 'toSnakeCase')
171
- .returns(attributeToServerAfterFormat);
172
-
173
- const attributes = new AssetAttributes(baseSdk, request, expectedHost);
174
-
175
- promise = attributes.create(assetTypeId, attributeToServerBeforeFormat);
176
- });
177
-
178
- it('formats the submitted attribute object to send to the server', function() {
179
- expect(toSnakeCase).to.be.deep.calledWith(
180
- attributeToServerBeforeFormat
181
- );
182
- });
183
-
184
- it('creates a new attribute', function() {
185
- expect(request.post).to.be.deep.calledWith(
186
- `${expectedHost}/assets/types/${assetTypeId}/attributes`,
187
- attributeToServerAfterFormat
188
- );
189
- });
190
-
191
- it('formats the returned attribute object', function() {
192
- return promise.then(() => {
193
- expect(toCamelCase).to.be.deep.calledWith(
194
- attributeFromServerBeforeFormat
195
- );
196
- });
197
- });
198
-
199
- it('returns a fulfilled promise with the new attribute information', function() {
200
- return expect(promise).to.be.fulfilled.and.to.eventually.deep.equal(
201
- attributeFromServerAfterFormat
202
- );
203
- });
204
- });
205
-
206
- context('when there is missing required information', function() {
207
- let assetAttribute;
208
- let assetAttributes;
209
- let assetTypeId;
210
- let promise;
211
-
212
- beforeEach(function() {
213
- assetAttribute = fixture.build('assetAttribute');
214
- assetAttributes = new AssetAttributes(
215
- baseSdk,
216
- baseRequest,
217
- expectedHost
218
- );
219
- assetTypeId = fixture.build('assetType').id;
220
- });
221
-
222
- it('throws an error if there is no asset type ID provided', function() {
223
- promise = assetAttributes.create(null, assetAttribute);
224
-
225
- return expect(promise).to.be.rejectedWith(
226
- 'An asset type ID is required to create a new asset attribute.'
227
- );
228
- });
229
-
230
- ['description', 'label', 'organizationId'].forEach((field) => {
231
- it(`throws an error when ${field} is missing`, function() {
232
- promise = assetAttributes.create(
233
- assetTypeId,
234
- omit(assetAttribute, [field])
235
- );
236
-
237
- return expect(promise).to.be.rejectedWith(
238
- `A ${field} is required to create a new asset attribute.`
239
- );
240
- });
241
- });
242
- });
243
- });
244
-
245
- describe('delete', function() {
246
- context('when all required information is supplied', function() {
247
- let expectedAssetAttributeId;
248
- let promise;
249
-
250
- beforeEach(function() {
251
- expectedAssetAttributeId = fixture.build('assetAttribute').id;
252
-
253
- const assetAttributes = new AssetAttributes(
254
- baseSdk,
255
- baseRequest,
256
- expectedHost
257
- );
258
-
259
- promise = assetAttributes.delete(expectedAssetAttributeId);
260
- });
261
-
262
- it('requests to delete the asset attribute', function() {
263
- expect(baseRequest.delete).to.be.calledWith(
264
- `${expectedHost}/assets/attributes/${expectedAssetAttributeId}`
265
- );
266
- });
267
-
268
- it('returns a fulfilled promise', function() {
269
- return expect(promise).to.be.fulfilled;
270
- });
271
- });
272
-
273
- context('when there is missing required information', function() {
274
- it('throws an error when the asset attribute ID is missing', function() {
275
- const assetAttributes = new AssetAttributes(
276
- baseSdk,
277
- baseRequest,
278
- expectedHost
279
- );
280
-
281
- const promise = assetAttributes.delete();
282
-
283
- return expect(promise).to.be.rejectedWith(
284
- 'An asset attribute ID is required for deleting an asset attribute.'
285
- );
286
- });
287
- });
288
- });
289
-
290
- describe('get', function() {
291
- context('the asset attribute ID is provided', function() {
292
- let assetAttributeFromServerAfterFormat;
293
- let assetAttributeFromServerBeforeFormat;
294
- let expectedAssetAttributeId;
295
- let promise;
296
- let request;
297
- let toCamelCase;
298
-
299
- beforeEach(function() {
300
- expectedAssetAttributeId = fixture.build('assetAttribute').id;
301
- assetAttributeFromServerAfterFormat = fixture.build('assetAttribute', {
302
- id: expectedAssetAttributeId
303
- });
304
- assetAttributeFromServerBeforeFormat = fixture.build(
305
- 'assetAttribute',
306
- assetAttributeFromServerAfterFormat,
307
- { fromServer: true }
308
- );
309
-
310
- request = {
311
- ...baseRequest,
312
- get: sinon.stub().resolves(assetAttributeFromServerBeforeFormat)
313
- };
314
- toCamelCase = sinon
315
- .stub(objectUtils, 'toCamelCase')
316
- .returns(assetAttributeFromServerAfterFormat);
317
-
318
- const assetAttributes = new AssetAttributes(
319
- baseSdk,
320
- request,
321
- expectedHost
322
- );
323
-
324
- promise = assetAttributes.get(expectedAssetAttributeId);
325
- });
326
-
327
- it('gets the asset attribute from the server', function() {
328
- expect(request.get).to.be.calledWith(
329
- `${expectedHost}/assets/attributes/${expectedAssetAttributeId}`
330
- );
331
- });
332
-
333
- it('formats the asset attribute object', function() {
334
- return promise.then(() => {
335
- expect(toCamelCase).to.be.deep.calledWith(
336
- assetAttributeFromServerBeforeFormat
337
- );
338
- });
339
- });
340
-
341
- it('returns the requested asset attribute', function() {
342
- return expect(promise).to.be.fulfilled.and.to.eventually.deep.equal(
343
- assetAttributeFromServerAfterFormat
344
- );
345
- });
346
- });
347
-
348
- context('the asset attribute ID is not provided', function() {
349
- it('throws an error', function() {
350
- const assetAttributes = new AssetAttributes(
351
- baseSdk,
352
- baseRequest,
353
- expectedHost
354
- );
355
- const promise = assetAttributes.get();
356
-
357
- return expect(promise).to.be.rejectedWith(
358
- 'An asset attribute ID is required for getting information about an asset attribute.'
359
- );
360
- });
361
- });
362
- });
363
-
364
- describe('getAll', function() {
365
- context('when all required information is supplied', function() {
366
- let assetTypeId;
367
- let formatPaginatedDataFromServer;
368
- let numberOfAssetAttributes;
369
- let paginationOptionsAfterFormat;
370
- let paginationOptionsBeforeFormat;
371
- let promise;
372
- let request;
373
- let toSnakeCase;
374
- let valuesFromServerAfterFormat;
375
- let valuesFromServerBeforeFormat;
376
-
377
- beforeEach(function() {
378
- numberOfAssetAttributes = faker.random.number({ min: 1, max: 10 });
379
- assetTypeId = fixture.build('assetType').id;
380
- paginationOptionsBeforeFormat = {
381
- limit: faker.random.number({ min: 10, max: 1000 }),
382
- offset: faker.random.number({ max: 1000 })
383
- };
384
- paginationOptionsAfterFormat = {
385
- limit: faker.random.number({ min: 10, max: 1000 }),
386
- offset: faker.random.number({ max: 1000 })
387
- };
388
- valuesFromServerAfterFormat = {
389
- _metadata: fixture.build('paginationMetadata'),
390
- records: fixture.buildList(
391
- 'assetAttribute',
392
- numberOfAssetAttributes,
393
- { assetTypeId }
394
- )
395
- };
396
- valuesFromServerBeforeFormat = {
397
- ...valuesFromServerAfterFormat,
398
- records: valuesFromServerAfterFormat.records.map((values) =>
399
- fixture.build('assetAttribute', values, { fromServer: true })
400
- )
401
- };
402
-
403
- formatPaginatedDataFromServer = sinon
404
- .stub(paginationUtils, 'formatPaginatedDataFromServer')
405
- .returns(valuesFromServerAfterFormat);
406
- request = {
407
- ...baseRequest,
408
- get: sinon.stub().resolves(valuesFromServerBeforeFormat)
409
- };
410
- toSnakeCase = sinon
411
- .stub(objectUtils, 'toSnakeCase')
412
- .returns(paginationOptionsAfterFormat);
413
-
414
- const assetAttributes = new AssetAttributes(
415
- baseSdk,
416
- request,
417
- expectedHost
418
- );
419
- promise = assetAttributes.getAll(
420
- assetTypeId,
421
- paginationOptionsBeforeFormat
422
- );
423
- });
424
-
425
- it('formats the pagination options sent to the server', function() {
426
- expect(toSnakeCase).to.be.calledWith(paginationOptionsBeforeFormat);
427
- });
428
-
429
- it('gets a list of the asset attributes from the server', function() {
430
- expect(request.get).to.be.calledWith(
431
- `${expectedHost}/assets/types/${assetTypeId}/attributes`,
432
- { params: paginationOptionsAfterFormat }
433
- );
434
- });
435
-
436
- it('formats the asset attribute data', function() {
437
- return promise.then(() => {
438
- expect(formatPaginatedDataFromServer).to.be.calledWith(
439
- valuesFromServerBeforeFormat
440
- );
441
- });
442
- });
443
-
444
- it('resolves with a list of asset attributes', function() {
445
- return expect(promise).to.be.fulfilled.and.to.eventually.deep.equal(
446
- valuesFromServerAfterFormat
447
- );
448
- });
449
- });
450
-
451
- context('when there is missing required information', function() {
452
- let promise;
453
-
454
- beforeEach(function() {
455
- const assetAttributes = new AssetAttributes(
456
- baseSdk,
457
- baseRequest,
458
- expectedHost
459
- );
460
-
461
- promise = assetAttributes.getAll();
462
- });
463
- it('throws an error when the asset type ID is missing', function() {
464
- return expect(promise).to.be.rejectedWith(
465
- 'An asset type ID is required to get a list of all asset attributes.'
466
- );
467
- });
468
- });
469
- });
470
-
471
- describe('update', function() {
472
- context('when all required information is supplied', function() {
473
- let assetAttributeToServerAfterFormat;
474
- let assetAttributeToServerBeforeFormat;
475
- let toSnakeCase;
476
- let promise;
477
-
478
- beforeEach(function() {
479
- assetAttributeToServerAfterFormat = fixture.build(
480
- 'assetAttribute',
481
- null,
482
- { fromServer: true }
483
- );
484
- assetAttributeToServerBeforeFormat = fixture.build('assetAttribute');
485
-
486
- toSnakeCase = sinon
487
- .stub(objectUtils, 'toSnakeCase')
488
- .returns(assetAttributeToServerAfterFormat);
489
-
490
- const assetAttributes = new AssetAttributes(
491
- baseSdk,
492
- baseRequest,
493
- expectedHost
494
- );
495
-
496
- promise = assetAttributes.update(
497
- assetAttributeToServerBeforeFormat.id,
498
- assetAttributeToServerBeforeFormat
499
- );
500
- });
501
-
502
- it('formats the data into the right format', function() {
503
- expect(toSnakeCase).to.be.deep.calledWith(
504
- assetAttributeToServerBeforeFormat,
505
- { excludeKeys: ['assetTypeId', 'id', 'organizationId'] }
506
- );
507
- });
508
-
509
- it('updates the asset attribute', function() {
510
- expect(baseRequest.put).to.be.deep.calledWith(
511
- `${expectedHost}/assets/attributes/${
512
- assetAttributeToServerBeforeFormat.id
513
- }`,
514
- assetAttributeToServerAfterFormat
515
- );
516
- });
517
-
518
- it('returns a fulfilled promise', function() {
519
- return expect(promise).to.be.fulfilled;
520
- });
521
- });
522
-
523
- context(
524
- 'when there is missing or malformed required information',
525
- function() {
526
- let assetAttributes;
527
-
528
- beforeEach(function() {
529
- assetAttributes = new AssetAttributes(
530
- baseSdk,
531
- baseRequest,
532
- expectedHost
533
- );
534
- });
535
-
536
- it('throws an error when there is not provided asset attribute ID', function() {
537
- const assetAttributeUpdate = fixture.build('assetAttribute');
538
- const promise = assetAttributes.update(null, assetAttributeUpdate);
539
-
540
- return expect(promise).to.be.rejectedWith(
541
- 'An asset attribute ID is required to update an asset attribute.'
542
- );
543
- });
544
-
545
- it('throws an error when there is no update provided', function() {
546
- const assetAttributeUpdate = fixture.build('assetAttribute');
547
- const promise = assetAttributes.update(assetAttributeUpdate.id);
548
-
549
- return expect(promise).to.be.rejectedWith(
550
- 'An update is required to update an asset attribute.'
551
- );
552
- });
553
-
554
- it('throws an error when the update is not a well-formed object', function() {
555
- const assetAttributeUpdate = fixture.build('assetAttribute');
556
- const promise = assetAttributes.update(assetAttributeUpdate.id, [
557
- assetAttributeUpdate
558
- ]);
559
-
560
- return expect(promise).to.be.rejectedWith(
561
- 'The asset attribute update must be a well-formed object with the data you wish to update.'
562
- );
563
- });
564
- }
565
- );
566
- });
567
-
568
- describe('createValue', function() {
569
- context('when all required information is supplied', function() {
570
- let assetId;
571
- let promise;
572
- let request;
573
- let toCamelCase;
574
- let toSnakeCase;
575
- let valueFromServerAfterFormat;
576
- let valueFromServerBeforeFormat;
577
- let valueToServerAfterFormat;
578
- let valueToServerBeforeFormat;
579
-
580
- beforeEach(function() {
581
- valueToServerBeforeFormat = fixture.build('assetAttributeValue');
582
- valueToServerAfterFormat = fixture.build(
583
- 'assetAttributeValue',
584
- valueToServerBeforeFormat,
585
- {
586
- fromServer: true
587
- }
588
- );
589
- valueFromServerAfterFormat = fixture.build('assetAttributeValue');
590
- valueFromServerBeforeFormat = fixture.build(
591
- 'assetAttributeValue',
592
- valueFromServerAfterFormat,
593
- { fromServer: true }
594
- );
595
- assetId = fixture.build('asset').id;
596
-
597
- request = {
598
- ...baseRequest,
599
- post: sinon.stub().resolves(valueFromServerBeforeFormat)
600
- };
601
- toCamelCase = sinon
602
- .stub(objectUtils, 'toCamelCase')
603
- .returns(valueFromServerAfterFormat);
604
- toSnakeCase = sinon
605
- .stub(objectUtils, 'toSnakeCase')
606
- .returns(valueToServerAfterFormat);
607
-
608
- const assetAttributes = new AssetAttributes(
609
- baseSdk,
610
- request,
611
- expectedHost
612
- );
613
-
614
- promise = assetAttributes.createValue(
615
- assetId,
616
- valueToServerBeforeFormat
617
- );
618
- });
619
-
620
- it('formats the submitted asset attribute value object to send to the server ', function() {
621
- expect(toSnakeCase).to.be.calledWith(valueToServerBeforeFormat);
622
- });
623
-
624
- it('creates a new asset attribute value', function() {
625
- expect(request.post).to.be.calledWith(
626
- `${expectedHost}/assets/${assetId}/attributes/${
627
- valueToServerBeforeFormat.assetAttributeId
628
- }/values`,
629
- valueToServerAfterFormat
630
- );
631
- });
632
-
633
- it('formats the returned asset attribute object', function() {
634
- return promise.then(() => {
635
- expect(toCamelCase).to.be.calledWith(valueFromServerBeforeFormat);
636
- });
637
- });
638
-
639
- it('returns a fulfilled promise with the new asset attribute value information', function() {
640
- return expect(promise).to.be.fulfilled.and.to.eventually.deep.equal(
641
- valueFromServerAfterFormat
642
- );
643
- });
644
- });
645
-
646
- context('when there is missing required information', function() {
647
- let value;
648
- let assetAttributes;
649
-
650
- beforeEach(function() {
651
- value = fixture.build('assetAttributeValue');
652
-
653
- assetAttributes = new AssetAttributes(
654
- baseSdk,
655
- baseRequest,
656
- expectedHost
657
- );
658
- });
659
-
660
- it('throws an error if there is no asset type ID provided', function() {
661
- const promise = assetAttributes.createValue(null, value);
662
-
663
- return expect(promise).to.be.rejectedWith(
664
- 'An asset ID is required to create a new asset attribute value.'
665
- );
666
- });
667
-
668
- ['assetAttributeId', 'effectiveDate', 'value'].forEach(function(field) {
669
- it(`throws an error when ${field} is missing`, function() {
670
- const promise = assetAttributes.createValue(
671
- value.id,
672
- omit(value, [field])
673
- );
674
-
675
- return expect(promise).to.be.rejectedWith(
676
- `A ${field} is required to create a new asset attribute value.`
677
- );
678
- });
679
- });
680
- });
681
- });
682
-
683
- describe('deleteValue', function() {
684
- context('when all required information is supplied', function() {
685
- let valueId;
686
- let promise;
687
-
688
- beforeEach(function() {
689
- valueId = fixture.build('assetAttributeValue').id;
690
-
691
- const assetAttributes = new AssetAttributes(
692
- baseSdk,
693
- baseRequest,
694
- expectedHost
695
- );
696
-
697
- promise = assetAttributes.deleteValue(valueId);
698
- });
699
-
700
- it('requests to delete the asset attribute value', function() {
701
- expect(baseRequest.delete).to.be.calledWith(
702
- `${expectedHost}/assets/attributes/values/${valueId}`
703
- );
704
- });
705
-
706
- it('returns a fulfilled promise', function() {
707
- return expect(promise).to.be.fulfilled;
708
- });
709
- });
710
-
711
- context('when there is missing required information', function() {
712
- it('throws an error when the asset attribute value ID is missing', function() {
713
- const assetAttributes = new AssetAttributes(
714
- baseSdk,
715
- baseRequest,
716
- expectedHost
717
- );
718
-
719
- const promise = assetAttributes.deleteValue();
720
-
721
- return expect(promise).to.be.rejectedWith(
722
- 'An asset attribute value ID is required for deleting an asset attribute value.'
723
- );
724
- });
725
- });
726
- });
727
-
728
- describe('getEffectiveValuesByAssetId', function() {
729
- context('when all required information is supplied', function() {
730
- let assetId;
731
- let promise;
732
- let request;
733
- let toCamelCase;
734
- let toSnakeCase;
735
- let valueFiltersToServerAfterFormat;
736
- let valueFiltersToServerBeforeFormat;
737
- let valuesFromServerAfterFormat;
738
- let valuesFromServerBeforeFormat;
739
-
740
- beforeEach(function() {
741
- assetId = fixture.build('asset').id;
742
- valueFiltersToServerAfterFormat = {
743
- attribute_label: faker.hacker.phrase(),
744
- effective_date: faker.date.recent().toISOString()
745
- };
746
- valueFiltersToServerBeforeFormat = {
747
- attributeLabel: faker.hacker.phrase(),
748
- effectiveDate: faker.date.recent().toISOString()
749
- };
750
- valuesFromServerAfterFormat = fixture.buildList(
751
- 'assetAttributeValue',
752
- faker.random.number({ min: 1, max: 20 }),
753
- { assetId }
754
- );
755
- valuesFromServerBeforeFormat = valuesFromServerAfterFormat.map(
756
- (values) =>
757
- fixture.build('assetAttributeValue', values, { fromServer: true })
758
- );
759
-
760
- request = {
761
- ...baseRequest,
762
- get: sinon.stub().resolves(valuesFromServerBeforeFormat)
763
- };
764
- toCamelCase = sinon
765
- .stub(objectUtils, 'toCamelCase')
766
- .returns(valuesFromServerAfterFormat);
767
- toSnakeCase = sinon
768
- .stub(objectUtils, 'toSnakeCase')
769
- .returns(valueFiltersToServerAfterFormat);
770
-
771
- const assetAttributes = new AssetAttributes(
772
- baseSdk,
773
- request,
774
- expectedHost
775
- );
776
- promise = assetAttributes.getEffectiveValuesByAssetId(
777
- assetId,
778
- valueFiltersToServerBeforeFormat
779
- );
780
- });
781
-
782
- it('formats the filters sent to the server', function() {
783
- expect(toSnakeCase).to.be.calledWith(valueFiltersToServerBeforeFormat);
784
- });
785
-
786
- it('gets a list of asset attributes from the server', function() {
787
- expect(request.get).to.be.calledWith(
788
- `${expectedHost}/assets/${assetId}/attributes/values`,
789
- { params: valueFiltersToServerAfterFormat }
790
- );
791
- });
792
-
793
- it('formats the asset attribute values', function() {
794
- return promise.then(() => {
795
- expect(toCamelCase).to.be.calledWith(valuesFromServerBeforeFormat);
796
- });
797
- });
798
-
799
- it('resolves with a list of asset attribute values', function() {
800
- return expect(promise).to.be.fulfilled.and.to.eventually.deep.equal(
801
- valuesFromServerAfterFormat
802
- );
803
- });
804
- });
805
-
806
- context('when there is missing required information', function() {
807
- it('throws an error when the asset ID is missing', function() {
808
- const assetAttributes = new AssetAttributes(
809
- baseSdk,
810
- baseRequest,
811
- expectedHost
812
- );
813
- const promise = assetAttributes.getEffectiveValuesByAssetId(null);
814
-
815
- return expect(promise).to.be.rejectedWith(
816
- 'An asset ID is required to get a list of asset attribute values.'
817
- );
818
- });
819
- });
820
- });
821
-
822
- describe('getEffectiveValuesByOrganizationId', function() {
823
- context('when all required information is supplied', function() {
824
- let formatPaginatedDataFromServer;
825
- let organizationId;
826
- let paginationOptionsBeforeFormat;
827
- let paginationOptionsAfterFormat;
828
- let promise;
829
- let request;
830
- let toSnakeCase;
831
- let valuesFromServerAfterFormat;
832
- let valuesFromServerBeforeFormat;
833
-
834
- beforeEach(function() {
835
- organizationId = fixture.build('organization').id;
836
- paginationOptionsBeforeFormat = {
837
- limit: faker.random.number({ min: 10, max: 1000 }),
838
- offset: faker.random.number({ max: 1000 })
839
- };
840
- paginationOptionsAfterFormat = {
841
- limit: faker.random.number({ min: 10, max: 1000 }),
842
- offset: faker.random.number({ max: 1000 })
843
- };
844
- valuesFromServerAfterFormat = {
845
- _metadata: fixture.build('paginationMetadata'),
846
- records: fixture.buildList(
847
- 'assetAttributeValue',
848
- faker.random.number({ min: 1, max: 20 })
849
- )
850
- };
851
- valuesFromServerBeforeFormat = {
852
- ...valuesFromServerAfterFormat,
853
- records: valuesFromServerAfterFormat.records.map((values) =>
854
- fixture.build('assetAttributeValue', values, { fromServer: true })
855
- )
856
- };
857
-
858
- formatPaginatedDataFromServer = sinon
859
- .stub(paginationUtils, 'formatPaginatedDataFromServer')
860
- .returns(valuesFromServerAfterFormat);
861
- request = {
862
- ...baseRequest,
863
- get: sinon.stub().resolves(valuesFromServerBeforeFormat)
864
- };
865
- toSnakeCase = sinon
866
- .stub(objectUtils, 'toSnakeCase')
867
- .returns(paginationOptionsAfterFormat);
868
-
869
- const assetAttributes = new AssetAttributes(
870
- baseSdk,
871
- request,
872
- expectedHost
873
- );
874
- promise = assetAttributes.getEffectiveValuesByOrganizationId(
875
- organizationId,
876
- paginationOptionsBeforeFormat
877
- );
878
- });
879
-
880
- it('formats the pagination options sent to the server', function() {
881
- expect(toSnakeCase).to.be.calledWith(paginationOptionsBeforeFormat);
882
- });
883
-
884
- it('gets a list of asset attribute values from the server', function() {
885
- expect(request.get).to.be.calledWith(
886
- `${expectedHost}/organizations/${organizationId}/attributes/values`,
887
- { params: paginationOptionsAfterFormat }
888
- );
889
- });
890
-
891
- it('formats the asset attribute value data', function() {
892
- return promise.then(() => {
893
- expect(formatPaginatedDataFromServer).to.be.calledWith(
894
- valuesFromServerBeforeFormat
895
- );
896
- });
897
- });
898
-
899
- it('resolves with a list of asset attribute values', function() {
900
- return expect(promise).to.be.fulfilled.and.to.eventually.deep.equal(
901
- valuesFromServerAfterFormat
902
- );
903
- });
904
- });
905
-
906
- context('when there is missing required information', function() {
907
- it('throws an error when the organization ID is missing', function() {
908
- const assetAttributes = new AssetAttributes(
909
- baseSdk,
910
- baseRequest,
911
- expectedHost
912
- );
913
- const promise = assetAttributes.getEffectiveValuesByOrganizationId();
914
-
915
- return expect(promise).to.be.rejectedWith(
916
- 'An organization ID is required to get a list of asset attribute values.'
917
- );
918
- });
919
- });
920
- });
921
-
922
- describe('getValuesByAttributeId', function() {
923
- context('when all required information is supplied', function() {
924
- let assetId;
925
- let attributeId;
926
- let formatPaginatedDataFromServer;
927
- let paginationOptionsBeforeFormat;
928
- let paginationOptionsAfterFormat;
929
- let promise;
930
- let request;
931
- let toSnakeCase;
932
- let valuesFromServerAfterFormat;
933
- let valuesFromServerBeforeFormat;
934
-
935
- beforeEach(function() {
936
- assetId = fixture.build('asset').id;
937
- attributeId = fixture.build('assetAttribute').id;
938
- paginationOptionsBeforeFormat = {
939
- limit: faker.random.number({ min: 10, max: 1000 }),
940
- offset: faker.random.number({ max: 1000 })
941
- };
942
- paginationOptionsAfterFormat = {
943
- limit: faker.random.number({ min: 10, max: 1000 }),
944
- offset: faker.random.number({ max: 1000 })
945
- };
946
- valuesFromServerAfterFormat = {
947
- _metadata: fixture.build('paginationMetadata'),
948
- records: fixture.buildList(
949
- 'assetAttributeValue',
950
- faker.random.number({ min: 1, max: 20 }),
951
- {
952
- assetId,
953
- assetAttributeId: attributeId
954
- }
955
- )
956
- };
957
- valuesFromServerBeforeFormat = {
958
- ...valuesFromServerAfterFormat,
959
- records: valuesFromServerAfterFormat.records.map((values) =>
960
- fixture.build('assetAttributeValue', values, { fromServer: true })
961
- )
962
- };
963
-
964
- formatPaginatedDataFromServer = sinon
965
- .stub(paginationUtils, 'formatPaginatedDataFromServer')
966
- .returns(valuesFromServerAfterFormat);
967
- request = {
968
- ...baseRequest,
969
- get: sinon.stub().resolves(valuesFromServerBeforeFormat)
970
- };
971
- toSnakeCase = sinon
972
- .stub(objectUtils, 'toSnakeCase')
973
- .returns(paginationOptionsAfterFormat);
974
-
975
- const assetAttributes = new AssetAttributes(
976
- baseSdk,
977
- request,
978
- expectedHost
979
- );
980
- promise = assetAttributes.getValuesByAttributeId(
981
- assetId,
982
- attributeId,
983
- paginationOptionsBeforeFormat
984
- );
985
- });
986
-
987
- it('formats the pagination options sent to the server', function() {
988
- expect(toSnakeCase).to.be.calledWith(paginationOptionsBeforeFormat);
989
- });
990
-
991
- it('gets a list of asset attribute values from the server', function() {
992
- expect(request.get).to.be.calledWith(
993
- `${expectedHost}/assets/${assetId}/attributes/${attributeId}/values`,
994
- { params: paginationOptionsAfterFormat }
995
- );
996
- });
997
-
998
- it('formats the asset attribute value data', function() {
999
- return promise.then(() => {
1000
- expect(formatPaginatedDataFromServer).to.be.calledWith(
1001
- valuesFromServerBeforeFormat
1002
- );
1003
- });
1004
- });
1005
-
1006
- it('resolves with a list of asset attribute values', function() {
1007
- return expect(promise).to.be.fulfilled.and.to.eventually.deep.equal(
1008
- valuesFromServerAfterFormat
1009
- );
1010
- });
1011
- });
1012
-
1013
- context('when there is missing required information', function() {
1014
- it('throws an error when the asset ID is missing', function() {
1015
- const assetAttributes = new AssetAttributes(
1016
- baseSdk,
1017
- baseRequest,
1018
- expectedHost
1019
- );
1020
- const promise = assetAttributes.getValuesByAttributeId(
1021
- null,
1022
- fixture.build('assetAttribute').id
1023
- );
1024
-
1025
- return expect(promise).to.be.rejectedWith(
1026
- 'An asset ID is required to get a list of asset attribute values.'
1027
- );
1028
- });
1029
-
1030
- it('throws an error when the asset attribute ID is missing', function() {
1031
- const assetAttributes = new AssetAttributes(
1032
- baseSdk,
1033
- baseRequest,
1034
- expectedHost
1035
- );
1036
- const promise = assetAttributes.getValuesByAttributeId(
1037
- fixture.build('asset').id,
1038
- null
1039
- );
1040
-
1041
- return expect(promise).to.be.rejectedWith(
1042
- 'An asset attribute ID is required to get a list of asset attribute values.'
1043
- );
1044
- });
1045
- });
1046
- });
1047
-
1048
- describe('updateValue', function() {
1049
- context('when all required information is supplied', function() {
1050
- let promise;
1051
- let toSnakeCase;
1052
- let valueToServerAfterFormat;
1053
- let valueToServerBeforeFormat;
1054
-
1055
- beforeEach(function() {
1056
- valueToServerBeforeFormat = fixture.build('assetAttributeValue');
1057
- valueToServerAfterFormat = fixture.build(
1058
- 'assetAttributeValue',
1059
- valueToServerBeforeFormat,
1060
- { fromServer: true }
1061
- );
1062
-
1063
- toSnakeCase = sinon
1064
- .stub(objectUtils, 'toSnakeCase')
1065
- .returns(valueToServerAfterFormat);
1066
-
1067
- const assetAttributes = new AssetAttributes(
1068
- baseSdk,
1069
- baseRequest,
1070
- expectedHost
1071
- );
1072
-
1073
- promise = assetAttributes.updateValue(
1074
- valueToServerBeforeFormat.id,
1075
- valueToServerBeforeFormat
1076
- );
1077
- });
1078
-
1079
- it('formats the data into the right format', function() {
1080
- expect(toSnakeCase).to.calledWith(valueToServerBeforeFormat, {
1081
- excludeKeys: ['assetAttributeId', 'assetId', 'id']
1082
- });
1083
- });
1084
-
1085
- it('updates the asset attribute value', function() {
1086
- expect(baseRequest.put).to.be.calledWith(
1087
- `${expectedHost}/assets/attributes/values/${
1088
- valueToServerAfterFormat.id
1089
- }`,
1090
- valueToServerAfterFormat
1091
- );
1092
- });
1093
-
1094
- it('returns a fulfilled promise', function() {
1095
- return expect(promise).to.be.fulfilled;
1096
- });
1097
- });
1098
-
1099
- context(
1100
- 'when there is missing or malformed required information',
1101
- function() {
1102
- let assetAttributes;
1103
-
1104
- beforeEach(function() {
1105
- assetAttributes = new AssetAttributes(
1106
- baseSdk,
1107
- baseRequest,
1108
- expectedHost
1109
- );
1110
- });
1111
-
1112
- it('throws an error when there is not provided asset attribute value ID', function() {
1113
- const valueUpdate = fixture.build('assetAttributeValue');
1114
- const promise = assetAttributes.updateValue(null, valueUpdate);
1115
-
1116
- return expect(promise).to.be.rejectedWith(
1117
- 'An asset attribute value ID is required to update an asset attribute value.'
1118
- );
1119
- });
1120
-
1121
- it('throws an error when there is no update provided', function() {
1122
- const valueUpdate = fixture.build('assetAttributeValue');
1123
- const promise = assetAttributes.updateValue(valueUpdate.id);
1124
-
1125
- return expect(promise).to.be.rejectedWith(
1126
- 'An update is required to update an asset attribute value.'
1127
- );
1128
- });
1129
-
1130
- it('throws an error when the update is not a well-formed object', function() {
1131
- const valueUpdate = fixture.build('assetAttributeValue');
1132
- const promise = assetAttributes.updateValue(valueUpdate.id, [
1133
- valueUpdate
1134
- ]);
1135
-
1136
- return expect(promise).to.be.rejectedWith(
1137
- 'The asset attribute value update must be a well-formed object with the data you wish to update.'
1138
- );
1139
- });
1140
- }
1141
- );
1142
- });
1143
- });