iotagent-node-lib 3.1.0 → 3.2.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 (48) hide show
  1. package/CHANGES_NEXT_RELEASE +0 -2
  2. package/config.js +5 -5
  3. package/doc/api.md +1540 -298
  4. package/doc/deprecated.md +3 -1
  5. package/doc/development.md +120 -0
  6. package/doc/installationguide.md +3 -6
  7. package/lib/commonConfig.js +7 -10
  8. package/lib/fiware-iotagent-lib.js +0 -10
  9. package/lib/jexlTranformsMap.js +2 -1
  10. package/lib/plugins/bidirectionalData.js +8 -26
  11. package/lib/plugins/expressionPlugin.js +8 -40
  12. package/lib/plugins/jexlParser.js +28 -0
  13. package/lib/services/commands/commandService.js +1 -1
  14. package/lib/services/devices/deviceService.js +2 -1
  15. package/lib/services/ngsi/entities-NGSI-LD.js +13 -57
  16. package/lib/services/ngsi/entities-NGSI-v2.js +145 -108
  17. package/lib/services/northBound/deviceProvisioningServer.js +17 -14
  18. package/lib/templates/createDevice.json +5 -2
  19. package/lib/templates/createDeviceLax.json +7 -5
  20. package/lib/templates/updateDevice.json +5 -2
  21. package/lib/templates/updateDeviceLax.json +3 -5
  22. package/package.json +1 -1
  23. package/test/unit/examples/deviceProvisioningRequests/provisionBidirectionalDevice.json +5 -5
  24. package/test/unit/examples/deviceProvisioningRequests/provisionMinimumDevice4.json +1 -0
  25. package/test/unit/examples/groupProvisioningRequests/bidirectionalGroup.json +4 -4
  26. package/test/unit/ngsi-ld/expressions/jexlBasedTransformations-test.js +0 -2
  27. package/test/unit/ngsi-ld/plugins/bidirectional-plugin_test.js +8 -8
  28. package/test/unit/ngsi-ld/plugins/custom-plugin_test.js +152 -0
  29. package/test/unit/ngsi-ld/plugins/multientity-plugin_test.js +1 -1
  30. package/test/unit/ngsiv2/examples/contextRequests/createMinimumProvisionedDevice4.json +5 -1
  31. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin31.json +0 -8
  32. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin35.json +20 -0
  33. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin40.json +42 -0
  34. package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin41.json +32 -0
  35. package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin25.json +37 -0
  36. package/test/unit/ngsiv2/expressions/jexlBasedTransformations-test.js +228 -6
  37. package/test/unit/ngsiv2/lazyAndCommands/polling-commands-test.js +1 -2
  38. package/test/unit/ngsiv2/plugins/bidirectional-plugin_test.js +6 -6
  39. package/test/unit/ngsiv2/plugins/custom-plugin_test.js +151 -0
  40. package/test/unit/ngsiv2/plugins/multientity-plugin_test.js +85 -12
  41. package/test/unit/ngsiv2/provisioning/device-provisioning-api_test.js +11 -3
  42. package/doc/advanced-topics.md +0 -626
  43. package/doc/expressionLanguage.md +0 -762
  44. package/lib/plugins/expressionParser.js +0 -205
  45. package/test/unit/expressions/expression-test.js +0 -197
  46. package/test/unit/ngsi-ld/expressions/expressionBasedTransformations-test.js +0 -882
  47. package/test/unit/ngsiv2/expressions/expressionBasedTransformations-test.js +0 -951
  48. package/test/unit/ngsiv2/expressions/expressionCombinedTransformations-test.js +0 -296
@@ -1,951 +0,0 @@
1
- /*
2
- * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U
3
- *
4
- * This file is part of fiware-iotagent-lib
5
- *
6
- * fiware-iotagent-lib is free software: you can redistribute it and/or
7
- * modify it under the terms of the GNU Affero General Public License as
8
- * published by the Free Software Foundation, either version 3 of the License,
9
- * or (at your option) any later version.
10
- *
11
- * fiware-iotagent-lib is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
- * See the GNU Affero General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU Affero General Public
17
- * License along with fiware-iotagent-lib.
18
- * If not, see http://www.gnu.org/licenses/.
19
- *
20
- * For those usages not covered by the GNU Affero General Public License
21
- * please contact with::[contacto@tid.es]
22
- *
23
- * Modified by: Daniel Calvo - ATOS Research & Innovation
24
- */
25
-
26
- /* eslint-disable no-useless-concat */
27
-
28
- const iotAgentLib = require('../../../../lib/fiware-iotagent-lib');
29
- const utils = require('../../../tools/utils');
30
- const should = require('should');
31
- const logger = require('logops');
32
- const nock = require('nock');
33
- let contextBrokerMock;
34
- const iotAgentConfig = {
35
- logLevel: 'FATAL',
36
- contextBroker: {
37
- host: '192.168.1.1',
38
- port: '1026',
39
- ngsiVersion: 'v2'
40
- },
41
- server: {
42
- port: 4041,
43
- host: 'localhost'
44
- },
45
- types: {
46
- Light: {
47
- commands: [],
48
- type: 'Light',
49
- lazy: [],
50
- active: [
51
- {
52
- object_id: 'p',
53
- name: 'pressure',
54
- type: 'Number'
55
- },
56
- {
57
- object_id: 'e',
58
- name: 'consumption',
59
- type: 'Number'
60
- },
61
- {
62
- object_id: 'a',
63
- name: 'alive',
64
- type: 'None'
65
- },
66
- {
67
- object_id: 'u',
68
- name: 'updated',
69
- type: 'Boolean'
70
- },
71
- {
72
- object_id: 'm',
73
- name: 'manufacturer',
74
- type: 'Object'
75
- },
76
- {
77
- object_id: 'r',
78
- name: 'revisions',
79
- type: 'Array'
80
- },
81
- {
82
- object_id: 'x',
83
- name: 'consumption_x',
84
- type: 'Number',
85
- expression: '${@pressure * 20}'
86
- }
87
- ]
88
- },
89
- LightError: {
90
- commands: [],
91
- type: 'Light',
92
- lazy: [],
93
- active: [
94
- {
95
- object_id: 'p',
96
- name: 'pressure',
97
- type: 'Number',
98
- expression: '${@pressure * / 20}'
99
- }
100
- ]
101
- },
102
- WeatherStation: {
103
- commands: [],
104
- type: 'WeatherStation',
105
- lazy: [],
106
- active: [
107
- {
108
- object_id: 'p',
109
- name: 'pressure',
110
- type: 'Number',
111
- expression: '${@pressure * 20}'
112
- },
113
- {
114
- object_id: 'e',
115
- name: 'consumption',
116
- type: 'Number',
117
- expression: '${@consumption * 20}'
118
- },
119
- {
120
- object_id: 'h',
121
- name: 'humidity',
122
- type: 'Percentage'
123
- },
124
- {
125
- name: 'weather',
126
- type: 'Summary',
127
- expression: 'Humidity ${@humidity / 2} and pressure ${@pressure * 20}'
128
- },
129
- {
130
- object_id: 'a',
131
- name: 'alive',
132
- type: 'None',
133
- expression: '${@alive * 20}'
134
- },
135
- {
136
- object_id: 'u',
137
- name: 'updated',
138
- type: 'Boolean',
139
- expression: '${@updated * 20}'
140
- }
141
- ]
142
- },
143
- WeatherStationMultiple: {
144
- commands: [],
145
- type: 'WeatherStation',
146
- lazy: [],
147
- active: [
148
- {
149
- object_id: 'p',
150
- name: 'pressure',
151
- type: 'Number',
152
- expression: '${trim(@pressure)}'
153
- },
154
- {
155
- object_id: 'p_25',
156
- name: 'pressure_25',
157
- type: 'Number'
158
- },
159
- {
160
- object_id: 'e',
161
- name: 'consumption',
162
- type: 'Number',
163
- expression: '${trim(@consumption)}'
164
- },
165
- {
166
- object_id: 'h',
167
- name: 'humidity12',
168
- type: 'Percentage'
169
- },
170
- {
171
- name: 'weather',
172
- type: 'Summary',
173
- expression: 'Humidity ${@humidity12 / 2} and pressure ${@pressure_25 * 20}'
174
- },
175
- {
176
- object_id: 'a',
177
- name: 'alive',
178
- type: 'None',
179
- expression: '${trim(@alive)}'
180
- },
181
- {
182
- object_id: 'u',
183
- name: 'updated',
184
- type: 'Boolean',
185
- expression: '${trim(@updated)}'
186
- }
187
- ]
188
- }
189
- },
190
- service: 'smartgondor',
191
- subservice: 'gardens',
192
- providerUrl: 'http://smartgondor.com'
193
- };
194
-
195
- describe('NGSI-v2 - Expression-based transformations plugin', function () {
196
- beforeEach(function (done) {
197
- logger.setLevel('FATAL');
198
-
199
- iotAgentLib.activate(iotAgentConfig, function () {
200
- iotAgentLib.clearAll(function () {
201
- done();
202
- });
203
- });
204
- });
205
-
206
- afterEach(function (done) {
207
- iotAgentLib.clearAll(function () {
208
- iotAgentLib.deactivate(done);
209
- });
210
- });
211
-
212
- describe('When an update comes for expressions with syntax errors', function () {
213
- // Case: Update for an attribute with bad expression
214
- const values = [
215
- {
216
- name: 'p',
217
- type: 'centigrades',
218
- value: '52'
219
- }
220
- ];
221
-
222
- it('should apply the expression before sending the values', function (done) {
223
- iotAgentLib.update('light1', 'LightError', '', values, function (error) {
224
- should.exist(error);
225
- error.name.should.equal('INVALID_EXPRESSION');
226
- error.code.should.equal(400);
227
- done();
228
- });
229
- });
230
- });
231
-
232
- describe('When there are expression attributes that are just calculated (not sent by the device)', function () {
233
- // Case: Expression which results is sent as a new attribute
234
- const values = [
235
- {
236
- name: 'p',
237
- type: 'Number',
238
- value: 52
239
- },
240
- {
241
- name: 'h',
242
- type: 'Percentage',
243
- value: '12'
244
- }
245
- ];
246
-
247
- beforeEach(function () {
248
- nock.cleanAll();
249
-
250
- contextBrokerMock = nock('http://192.168.1.1:1026')
251
- .matchHeader('fiware-service', 'smartgondor')
252
- .matchHeader('fiware-servicepath', 'gardens')
253
- .patch(
254
- '/v2/entities/ws1/attrs',
255
- utils.readExampleFile(
256
- './test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin16.json'
257
- )
258
- )
259
- .query({ type: 'WeatherStation' })
260
- .reply(204);
261
- });
262
-
263
- it('should calculate them and add them to the payload', function (done) {
264
- iotAgentLib.update('ws1', 'WeatherStation', '', values, function (error) {
265
- should.not.exist(error);
266
- contextBrokerMock.done();
267
- done();
268
- });
269
- });
270
- });
271
-
272
- describe('When an expression with multiple variables with numbers arrive', function () {
273
- // Case: Update for integer and string attributes with expression
274
-
275
- const values = [
276
- {
277
- name: 'p_25',
278
- type: 'Number',
279
- value: 52
280
- },
281
- {
282
- name: 'h',
283
- type: 'percentage',
284
- value: '12'
285
- }
286
- ];
287
-
288
- beforeEach(function () {
289
- nock.cleanAll();
290
-
291
- contextBrokerMock = nock('http://192.168.1.1:1026')
292
- .matchHeader('fiware-service', 'smartgondor')
293
- .matchHeader('fiware-servicepath', 'gardens')
294
- .patch(
295
- '/v2/entities/ws1/attrs',
296
- utils.readExampleFile(
297
- './test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin17.json'
298
- )
299
- )
300
- .query({ type: 'WeatherStation' })
301
- .reply(204);
302
- });
303
-
304
- it('should calculate it and add it to the payload', function (done) {
305
- iotAgentLib.update('ws1', 'WeatherStationMultiple', '', values, function (error) {
306
- should.not.exist(error);
307
- contextBrokerMock.done();
308
- done();
309
- });
310
- });
311
- });
312
-
313
- describe('When an update comes for attributes without expressions and type integer', function () {
314
- // Case: Update for an integer attribute without expression
315
- const values = [
316
- {
317
- name: 'e',
318
- type: 'Number',
319
- value: 52
320
- }
321
- ];
322
-
323
- beforeEach(function () {
324
- nock.cleanAll();
325
-
326
- contextBrokerMock = nock('http://192.168.1.1:1026')
327
- .matchHeader('fiware-service', 'smartgondor')
328
- .matchHeader('fiware-servicepath', 'gardens')
329
- .patch(
330
- '/v2/entities/light1/attrs',
331
- utils.readExampleFile(
332
- './test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin18.json'
333
- )
334
- )
335
- .query({ type: 'Light' })
336
- .reply(204);
337
- });
338
-
339
- it('should apply the expression before sending the values', function (done) {
340
- iotAgentLib.update('light1', 'Light', '', values, function (error) {
341
- should.not.exist(error);
342
- contextBrokerMock.done();
343
- done();
344
- });
345
- });
346
- });
347
-
348
- describe('When an update comes for attributes with numeric expressions and type integer', function () {
349
- // Case: Update for an integer attribute with arithmetic expression
350
- const values = [
351
- {
352
- name: 'p',
353
- type: 'Number',
354
- value: 52
355
- }
356
- ];
357
-
358
- beforeEach(function () {
359
- nock.cleanAll();
360
-
361
- contextBrokerMock = nock('http://192.168.1.1:1026')
362
- .matchHeader('fiware-service', 'smartgondor')
363
- .matchHeader('fiware-servicepath', 'gardens')
364
- .patch(
365
- '/v2/entities/ws1/attrs',
366
- utils.readExampleFile(
367
- './test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin19.json'
368
- )
369
- )
370
- .query({ type: 'WeatherStation' })
371
- .reply(204);
372
- });
373
-
374
- it('should apply the expression before sending the values', function (done) {
375
- iotAgentLib.update('ws1', 'WeatherStation', '', values, function (error) {
376
- should.not.exist(error);
377
- contextBrokerMock.done();
378
- done();
379
- });
380
- });
381
- });
382
-
383
- describe('When an update comes for attributes with string expression and type integer', function () {
384
- // Case: Update for an integer attribute with string expression
385
- const values = [
386
- {
387
- name: 'e',
388
- type: 'Number',
389
- value: 52
390
- }
391
- ];
392
-
393
- beforeEach(function () {
394
- nock.cleanAll();
395
-
396
- contextBrokerMock = nock('http://192.168.1.1:1026')
397
- .matchHeader('fiware-service', 'smartgondor')
398
- .matchHeader('fiware-servicepath', 'gardens')
399
- .patch(
400
- '/v2/entities/ws1/attrs',
401
- utils.readExampleFile(
402
- './test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin20.json'
403
- )
404
- )
405
- .query({ type: 'WeatherStation' })
406
- .reply(204);
407
- });
408
-
409
- it('should apply the expression before sending the values', function (done) {
410
- iotAgentLib.update('ws1', 'WeatherStationMultiple', '', values, function (error) {
411
- should.not.exist(error);
412
- contextBrokerMock.done();
413
- done();
414
- });
415
- });
416
- });
417
-
418
- describe('When an update comes for attributes without expressions and type float', function () {
419
- // Case: Update for a Float attribute without expressions
420
-
421
- const values = [
422
- {
423
- name: 'e',
424
- type: 'Number',
425
- value: 0.44
426
- }
427
- ];
428
-
429
- beforeEach(function () {
430
- nock.cleanAll();
431
-
432
- contextBrokerMock = nock('http://192.168.1.1:1026')
433
- .matchHeader('fiware-service', 'smartgondor')
434
- .matchHeader('fiware-servicepath', 'gardens')
435
- .patch(
436
- '/v2/entities/light1/attrs',
437
- utils.readExampleFile(
438
- './test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin21.json'
439
- )
440
- )
441
- .query({ type: 'Light' })
442
- .reply(204);
443
- });
444
-
445
- it('should apply the expression before sending the values', function (done) {
446
- iotAgentLib.update('light1', 'Light', '', values, function (error) {
447
- should.not.exist(error);
448
- contextBrokerMock.done();
449
- done();
450
- });
451
- });
452
- });
453
-
454
- describe('When an update comes for attributes with numeric expressions and type float', function () {
455
- // Case: Update for a Float attribute with arithmetic expression
456
-
457
- const values = [
458
- {
459
- name: 'e',
460
- type: 'Number',
461
- value: 0.44
462
- }
463
- ];
464
-
465
- beforeEach(function () {
466
- nock.cleanAll();
467
-
468
- contextBrokerMock = nock('http://192.168.1.1:1026')
469
- .matchHeader('fiware-service', 'smartgondor')
470
- .matchHeader('fiware-servicepath', 'gardens')
471
- .patch(
472
- '/v2/entities/ws1/attrs',
473
- utils.readExampleFile(
474
- './test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin22.json'
475
- )
476
- )
477
- .query({ type: 'WeatherStation' })
478
- .reply(204);
479
- });
480
-
481
- it('should apply the expression before sending the values', function (done) {
482
- iotAgentLib.update('ws1', 'WeatherStation', '', values, function (error) {
483
- should.not.exist(error);
484
- contextBrokerMock.done();
485
- done();
486
- });
487
- });
488
- });
489
-
490
- describe('When an update comes for attributes with string expressions and type float', function () {
491
- // Case: Update for a Float attribute with string expression
492
-
493
- const values = [
494
- {
495
- name: 'e',
496
- type: 'Number',
497
- value: 0.44
498
- }
499
- ];
500
-
501
- beforeEach(function () {
502
- nock.cleanAll();
503
-
504
- contextBrokerMock = nock('http://192.168.1.1:1026')
505
- .matchHeader('fiware-service', 'smartgondor')
506
- .matchHeader('fiware-servicepath', 'gardens')
507
- .patch(
508
- '/v2/entities/ws1/attrs',
509
- utils.readExampleFile(
510
- './test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin23.json'
511
- )
512
- )
513
- .query({ type: 'WeatherStation' })
514
- .reply(204);
515
- });
516
-
517
- it('should apply the expression before sending the values', function (done) {
518
- iotAgentLib.update('ws1', 'WeatherStationMultiple', '', values, function (error) {
519
- should.not.exist(error);
520
- contextBrokerMock.done();
521
- done();
522
- });
523
- });
524
- });
525
-
526
- describe('When an update comes for attributes without expressions and NULL type', function () {
527
- // Case: Update for a Null attribute without expression
528
-
529
- const values = [
530
- {
531
- name: 'a',
532
- type: 'None',
533
- value: null
534
- }
535
- ];
536
-
537
- beforeEach(function () {
538
- nock.cleanAll();
539
-
540
- contextBrokerMock = nock('http://192.168.1.1:1026')
541
- .matchHeader('fiware-service', 'smartgondor')
542
- .matchHeader('fiware-servicepath', 'gardens')
543
- .patch(
544
- '/v2/entities/light1/attrs',
545
- utils.readExampleFile(
546
- './test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin24.json'
547
- )
548
- )
549
- .query({ type: 'Light' })
550
- .reply(204);
551
- });
552
-
553
- it('should apply the expression before sending the values', function (done) {
554
- iotAgentLib.update('light1', 'Light', '', values, function (error) {
555
- should.not.exist(error);
556
- contextBrokerMock.done();
557
- done();
558
- });
559
- });
560
- });
561
-
562
- describe('When an update comes for attributes with numeric expressions and NULL type', function () {
563
- // Case: Update for a Null attribute with arithmetic expression
564
-
565
- const values = [
566
- {
567
- name: 'a',
568
- type: 'None',
569
- value: null
570
- }
571
- ];
572
-
573
- beforeEach(function () {
574
- nock.cleanAll();
575
-
576
- contextBrokerMock = nock('http://192.168.1.1:1026')
577
- .matchHeader('fiware-service', 'smartgondor')
578
- .matchHeader('fiware-servicepath', 'gardens')
579
- .patch(
580
- '/v2/entities/ws1/attrs',
581
- utils.readExampleFile(
582
- './test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin24.json'
583
- )
584
- )
585
- .query({ type: 'WeatherStation' })
586
- .reply(204);
587
- });
588
-
589
- it('should apply the expression before sending the values', function (done) {
590
- iotAgentLib.update('ws1', 'WeatherStation', '', values, function (error) {
591
- should.not.exist(error);
592
- contextBrokerMock.done();
593
- done();
594
- });
595
- });
596
- });
597
-
598
- describe('When an update comes for attributes with string expressions and NULL type', function () {
599
- // Case: Update for a Null attribute with string expression
600
-
601
- const values = [
602
- {
603
- name: 'a',
604
- type: 'None',
605
- value: null
606
- }
607
- ];
608
-
609
- beforeEach(function () {
610
- nock.cleanAll();
611
-
612
- contextBrokerMock = nock('http://192.168.1.1:1026')
613
- .matchHeader('fiware-service', 'smartgondor')
614
- .matchHeader('fiware-servicepath', 'gardens')
615
- .patch(
616
- '/v2/entities/ws1/attrs',
617
- utils.readExampleFile(
618
- './test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin24.json'
619
- )
620
- )
621
- .query({ type: 'WeatherStation' })
622
- .reply(204);
623
- });
624
-
625
- it('should apply the expression before sending the values', function (done) {
626
- iotAgentLib.update('ws1', 'WeatherStationMultiple', '', values, function (error) {
627
- should.not.exist(error);
628
- contextBrokerMock.done();
629
- done();
630
- });
631
- });
632
- });
633
-
634
- describe('When an update comes for attributes without expressions and Boolean type', function () {
635
- // Case: Update for a Boolean attribute without expression
636
-
637
- const values = [
638
- {
639
- name: 'u',
640
- type: 'Boolean',
641
- value: true
642
- }
643
- ];
644
-
645
- beforeEach(function () {
646
- nock.cleanAll();
647
-
648
- contextBrokerMock = nock('http://192.168.1.1:1026')
649
- .matchHeader('fiware-service', 'smartgondor')
650
- .matchHeader('fiware-servicepath', 'gardens')
651
- .patch(
652
- '/v2/entities/light1/attrs',
653
- utils.readExampleFile(
654
- './test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin25.json'
655
- )
656
- )
657
- .query({ type: 'Light' })
658
- .reply(204);
659
- });
660
-
661
- it('should apply the expression before sending the values', function (done) {
662
- iotAgentLib.update('light1', 'Light', '', values, function (error) {
663
- should.not.exist(error);
664
- contextBrokerMock.done();
665
- done();
666
- });
667
- });
668
- });
669
-
670
- describe('When an update comes for attributes with numeric expressions and Boolean type', function () {
671
- // Case: Update for a Boolean attribute with arithmetic expression
672
-
673
- const values = [
674
- {
675
- name: 'u',
676
- type: 'Boolean',
677
- value: true
678
- }
679
- ];
680
-
681
- beforeEach(function () {
682
- nock.cleanAll();
683
-
684
- contextBrokerMock = nock('http://192.168.1.1:1026')
685
- .matchHeader('fiware-service', 'smartgondor')
686
- .matchHeader('fiware-servicepath', 'gardens')
687
- .patch(
688
- '/v2/entities/ws1/attrs',
689
- utils.readExampleFile(
690
- './test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin10.json'
691
- )
692
- )
693
- .query({ type: 'WeatherStation' })
694
- .reply(204);
695
- });
696
-
697
- it('should apply the expression before sending the values', function (done) {
698
- iotAgentLib.update('ws1', 'WeatherStation', '', values, function (error) {
699
- should.not.exist(error);
700
- contextBrokerMock.done();
701
- done();
702
- });
703
- });
704
- });
705
-
706
- describe('When an update comes for attributes with string expressions and Boolean type', function () {
707
- // Case: Update for a Boolean attribute with string expression
708
- const values = [
709
- {
710
- name: 'u',
711
- type: 'Boolean',
712
- value: true
713
- }
714
- ];
715
-
716
- beforeEach(function () {
717
- nock.cleanAll();
718
-
719
- contextBrokerMock = nock('http://192.168.1.1:1026')
720
- .matchHeader('fiware-service', 'smartgondor')
721
- .matchHeader('fiware-servicepath', 'gardens')
722
- .patch(
723
- '/v2/entities/ws1/attrs',
724
- utils.readExampleFile(
725
- './test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin25.json'
726
- )
727
- )
728
- .query({ type: 'WeatherStation' })
729
- .reply(204);
730
- });
731
-
732
- it('should apply the expression before sending the values', function (done) {
733
- iotAgentLib.update('ws1', 'WeatherStationMultiple', '', values, function (error) {
734
- should.not.exist(error);
735
- contextBrokerMock.done();
736
- done();
737
- });
738
- });
739
- });
740
-
741
- describe('When an update comes for attributes without expressions and Object type', function () {
742
- // Case: Update for a JSON document attribute without expression
743
- const values = [
744
- {
745
- name: 'm',
746
- type: 'Object',
747
- value: { name: 'Manufacturer1', VAT: 'U12345678' }
748
- }
749
- ];
750
-
751
- beforeEach(function () {
752
- nock.cleanAll();
753
-
754
- contextBrokerMock = nock('http://192.168.1.1:1026')
755
- .matchHeader('fiware-service', 'smartgondor')
756
- .matchHeader('fiware-servicepath', 'gardens')
757
- .patch(
758
- '/v2/entities/light1/attrs',
759
- utils.readExampleFile(
760
- './test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin26.json'
761
- )
762
- )
763
- .query({ type: 'Light' })
764
- .reply(204);
765
- });
766
-
767
- it('should apply the expression before sending the values', function (done) {
768
- iotAgentLib.update('light1', 'Light', '', values, function (error) {
769
- should.not.exist(error);
770
- contextBrokerMock.done();
771
- done();
772
- });
773
- });
774
- });
775
-
776
- describe('When an update comes for attributes without expressions and Object type', function () {
777
- // Case: Update for a JSON array attribute without expression
778
-
779
- const values = [
780
- {
781
- name: 'r',
782
- type: 'Object',
783
- value: ['v0.1', 'v0.2', 'v0.3']
784
- }
785
- ];
786
-
787
- beforeEach(function () {
788
- nock.cleanAll();
789
-
790
- contextBrokerMock = nock('http://192.168.1.1:1026')
791
- .matchHeader('fiware-service', 'smartgondor')
792
- .matchHeader('fiware-servicepath', 'gardens')
793
- .patch(
794
- '/v2/entities/light1/attrs',
795
- utils.readExampleFile(
796
- './test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin27.json'
797
- )
798
- )
799
- .query({ type: 'Light' })
800
- .reply(204);
801
- });
802
-
803
- it('should apply the expression before sending the values', function (done) {
804
- iotAgentLib.update('light1', 'Light', '', values, function (error) {
805
- should.not.exist(error);
806
- contextBrokerMock.done();
807
- done();
808
- });
809
- });
810
- });
811
-
812
- describe('When there are expressions including other attributes and they are not updated', function () {
813
- const values = [
814
- {
815
- name: 'x',
816
- type: 'Number',
817
- value: 0.44
818
- }
819
- ];
820
-
821
- beforeEach(function () {
822
- nock.cleanAll();
823
-
824
- contextBrokerMock = nock('http://192.168.1.1:1026')
825
- .matchHeader('fiware-service', 'smartgondor')
826
- .matchHeader('fiware-servicepath', 'gardens')
827
- .patch(
828
- '/v2/entities/light1/attrs',
829
- utils.readExampleFile(
830
- './test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin28.json'
831
- )
832
- )
833
- .query({ type: 'Light' })
834
- .reply(204);
835
- });
836
-
837
- it('should apply the expression before sending the values', function (done) {
838
- iotAgentLib.update('light1', 'Light', '', values, function (error) {
839
- should.not.exist(error);
840
- contextBrokerMock.done();
841
- done();
842
- });
843
- });
844
- });
845
-
846
- describe('When there are expressions including other attributes and they are updated', function () {
847
- const values = [
848
- {
849
- name: 'p',
850
- type: 'Number',
851
- value: 10
852
- }
853
- ];
854
-
855
- beforeEach(function () {
856
- nock.cleanAll();
857
-
858
- contextBrokerMock = nock('http://192.168.1.1:1026')
859
- .matchHeader('fiware-service', 'smartgondor')
860
- .matchHeader('fiware-servicepath', 'gardens')
861
- .patch(
862
- '/v2/entities/light1/attrs',
863
- utils.readExampleFile(
864
- './test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin13.json'
865
- )
866
- )
867
- .query({ type: 'Light' })
868
- .reply(204);
869
- });
870
-
871
- it('should apply the expression before sending the values', function (done) {
872
- iotAgentLib.update('light1', 'Light', '', values, function (error) {
873
- should.not.exist(error);
874
- contextBrokerMock.done();
875
- done();
876
- });
877
- });
878
- });
879
-
880
- describe('When there are expressions including other attributes and they are updated (overriding situation)', function () {
881
- const values = [
882
- {
883
- name: 'x',
884
- type: 'Number',
885
- value: 0.44
886
- },
887
- {
888
- name: 'p',
889
- type: 'Number',
890
- value: 10
891
- }
892
- ];
893
-
894
- beforeEach(function () {
895
- nock.cleanAll();
896
-
897
- contextBrokerMock = nock('http://192.168.1.1:1026')
898
- .matchHeader('fiware-service', 'smartgondor')
899
- .matchHeader('fiware-servicepath', 'gardens')
900
- .patch(
901
- '/v2/entities/light1/attrs',
902
- utils.readExampleFile(
903
- './test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin13.json'
904
- )
905
- )
906
- .query({ type: 'Light' })
907
- .reply(204);
908
- });
909
-
910
- it('should apply the expression before sending the values', function (done) {
911
- iotAgentLib.update('light1', 'Light', '', values, function (error) {
912
- should.not.exist(error);
913
- contextBrokerMock.done();
914
- done();
915
- });
916
- });
917
- });
918
- describe('When a measure arrives and there is not enough information to calculate an expression', function () {
919
- const values = [
920
- {
921
- name: 'p',
922
- type: 'centigrades',
923
- value: '52'
924
- }
925
- ];
926
-
927
- beforeEach(function () {
928
- nock.cleanAll();
929
-
930
- contextBrokerMock = nock('http://192.168.1.1:1026')
931
- .matchHeader('fiware-service', 'smartgondor')
932
- .matchHeader('fiware-servicepath', 'gardens')
933
- .patch(
934
- '/v2/entities/ws1/attrs',
935
- utils.readExampleFile(
936
- './test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin30.json'
937
- )
938
- )
939
- .query({ type: 'WeatherStation' })
940
- .reply(204);
941
- });
942
-
943
- it('should not calculate the expression', function (done) {
944
- iotAgentLib.update('ws1', 'WeatherStation', '', values, function (error) {
945
- should.not.exist(error);
946
- contextBrokerMock.done();
947
- done();
948
- });
949
- });
950
- });
951
- });