iotagent-node-lib 3.1.0 → 3.3.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.
- package/.github/ISSUE_TEMPLATE/bug_report.yml +134 -0
- package/.github/ISSUE_TEMPLATE/config.yml +16 -0
- package/.github/ISSUE_TEMPLATE/feature_request.yml +55 -0
- package/.github/advanced-issue-labeler.yml +30 -0
- package/.github/workflows/issue-labeler.yml +43 -0
- package/CHANGES_NEXT_RELEASE +0 -2
- package/config.js +5 -5
- package/doc/api.md +1483 -298
- package/doc/deprecated.md +7 -1
- package/doc/development.md +120 -0
- package/doc/howto.md +58 -62
- package/doc/installationguide.md +3 -11
- package/doc/requirements.txt +1 -1
- package/docker/Mosquitto/Dockerfile +1 -1
- package/lib/commonConfig.js +7 -10
- package/lib/fiware-iotagent-lib.js +0 -10
- package/lib/jexlTranformsMap.js +2 -1
- package/lib/model/Device.js +0 -1
- package/lib/model/Group.js +0 -1
- package/lib/model/dbConn.js +1 -7
- package/lib/plugins/bidirectionalData.js +8 -26
- package/lib/plugins/expressionPlugin.js +8 -40
- package/lib/plugins/jexlParser.js +28 -0
- package/lib/services/commands/commandService.js +1 -1
- package/lib/services/common/iotManagerService.js +0 -1
- package/lib/services/devices/deviceRegistryMongoDB.js +10 -10
- package/lib/services/devices/deviceService.js +16 -20
- package/lib/services/devices/devices-NGSI-v2.js +2 -5
- package/lib/services/ngsi/entities-NGSI-LD.js +16 -60
- package/lib/services/ngsi/entities-NGSI-v2.js +179 -119
- package/lib/services/northBound/deviceProvisioningServer.js +17 -17
- package/lib/templates/createDevice.json +5 -6
- package/lib/templates/createDeviceLax.json +6 -8
- package/lib/templates/deviceGroup.json +1 -5
- package/lib/templates/updateDevice.json +9 -2
- package/lib/templates/updateDeviceLax.json +14 -5
- package/package.json +1 -1
- package/scripts/legacy_expression_tool/README.md +262 -0
- package/scripts/legacy_expression_tool/legacy_expression_tool.py +423 -0
- package/scripts/legacy_expression_tool/requirements.txt +3 -0
- package/test/unit/examples/deviceProvisioningRequests/provisionBidirectionalDevice.json +5 -5
- package/test/unit/examples/groupProvisioningRequests/bidirectionalGroup.json +4 -4
- package/test/unit/general/contextBrokerKeystoneSecurityAccess-test.js +3 -13
- package/test/unit/ngsi-ld/expressions/jexlBasedTransformations-test.js +0 -2
- package/test/unit/ngsi-ld/lazyAndCommands/merge-patch-test.js +31 -30
- package/test/unit/ngsi-ld/plugins/bidirectional-plugin_test.js +8 -8
- package/test/unit/ngsi-ld/plugins/custom-plugin_test.js +152 -0
- package/test/unit/ngsi-ld/plugins/multientity-plugin_test.js +1 -1
- package/test/unit/ngsi-mixed/provisioning/ngsi-versioning-test.js +33 -37
- package/test/unit/ngsiv2/examples/contextRequests/createMinimumProvisionedDevice4.json +5 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContext.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContext1.json +3 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContext3WithStatic.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContext4.json +4 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContext5.json +12 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContext6.json +10 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextAliasPlugin1.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextAliasPlugin2.json +3 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextAliasPlugin3.json +3 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextAliasPlugin4.json +3 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextAliasPlugin5.json +3 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextAliasPlugin6.json +3 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextAliasPlugin7.json +3 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextAliasPlugin8.json +3 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextAliasPlugin9.json +3 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextAutocast1.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextAutocast2.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextAutocast3.json +3 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextAutocast4.json +3 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextAutocast5.json +3 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextAutocast6.json +3 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextAutocast7.json +3 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextCommandError.json +3 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextCommandExpired.json +3 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextCommandFinish.json +3 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextCommandStatus.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextCommandStatus2.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextCompressTimestamp1.json +3 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextCompressTimestamp2.json +3 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin1.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin11.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin12.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin13.json +3 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin2.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin29.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin3.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin30.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin31.json +2 -8
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin32.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin33.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin34.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin35.json +22 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin36.json +1 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin4.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin40.json +42 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin41.json +33 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin5.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin6.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin7.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin8.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionPlugin9.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextExpressionSkip.json +12 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityJexlExpressionPlugin1.json +1 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin1.json +1 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin10.json +1 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin11.json +1 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin12.json +1 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin13.json +1 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin14.json +1 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin15.json +1 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin16.json +1 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin17.json +1 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin2.json +1 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin25.json +37 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin3.json +1 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin4.json +1 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin5.json +1 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin6.json +1 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin7.json +1 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin8.json +1 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityPlugin9.json +1 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityTimestampPlugin1.json +1 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityTimestampPlugin2.json +1 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityTimestampPlugin3.json +1 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextMultientityTimestampPlugin4.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextProcessTimestamp.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextStaticAttributes.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextStaticAttributesMetadata.json +3 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextTimestamp.json +3 -1
- package/test/unit/ngsiv2/examples/contextRequests/updateContextTimestampFalse.json +12 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextTimestampFalseTimeInstant.json +12 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextTimestampOverride.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextTimestampOverrideWithoutMilis.json +2 -0
- package/test/unit/ngsiv2/examples/contextRequests/updateContextTimestampTimezone.json +3 -1
- package/test/unit/ngsiv2/expressions/jexlBasedTransformations-test.js +355 -75
- package/test/unit/ngsiv2/general/contextBrokerOAuthSecurityAccess-test.js +18 -51
- package/test/unit/ngsiv2/general/https-support-test.js +1 -5
- package/test/unit/ngsiv2/lazyAndCommands/command-test.js +4 -10
- package/test/unit/ngsiv2/lazyAndCommands/polling-commands-test.js +9 -26
- package/test/unit/ngsiv2/ngsiService/active-devices-test.js +143 -57
- package/test/unit/ngsiv2/ngsiService/autocast-test.js +14 -21
- package/test/unit/ngsiv2/ngsiService/staticAttributes-test.js +3 -5
- package/test/unit/ngsiv2/ngsiService/subscriptions-test.js +1 -10
- package/test/unit/ngsiv2/plugins/alias-plugin_test.js +20 -30
- package/test/unit/ngsiv2/plugins/bidirectional-plugin_test.js +6 -69
- package/test/unit/ngsiv2/plugins/compress-timestamp-plugin_test.js +4 -6
- package/test/unit/ngsiv2/plugins/custom-plugin_test.js +150 -0
- package/test/unit/ngsiv2/plugins/multientity-plugin_test.js +87 -16
- package/test/unit/ngsiv2/plugins/timestamp-processing-plugin_test.js +2 -3
- package/test/unit/ngsiv2/provisioning/device-group-api-test.js +2 -3
- package/test/unit/ngsiv2/provisioning/device-provisioning-api_test.js +15 -55
- package/test/unit/ngsiv2/provisioning/device-registration_test.js +1 -7
- package/test/unit/ngsiv2/provisioning/device-update-registration_test.js +2 -9
- package/test/unit/ngsiv2/provisioning/singleConfigurationMode-test.js +0 -11
- package/test/unit/ngsiv2/provisioning/updateProvisionedDevices-test.js +0 -7
- package/test/unit/plugins/capture-provision-inPlugins_test.js +0 -6
- package/doc/advanced-topics.md +0 -626
- package/doc/expressionLanguage.md +0 -762
- package/lib/plugins/expressionParser.js +0 -205
- package/test/unit/expressions/expression-test.js +0 -197
- package/test/unit/ngsi-ld/expressions/expressionBasedTransformations-test.js +0 -882
- package/test/unit/ngsiv2/expressions/expressionBasedTransformations-test.js +0 -951
- package/test/unit/ngsiv2/expressions/expressionCombinedTransformations-test.js +0 -296
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2016 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::daniel.moranjimenez@telefonica.com
|
|
22
|
-
*
|
|
23
|
-
* Modified by: Daniel Calvo - ATOS Research & Innovation
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
/* eslint-disable consistent-return */
|
|
27
|
-
|
|
28
|
-
const Parser = require('jison').Parser;
|
|
29
|
-
const errors = require('../errors');
|
|
30
|
-
const logger = require('logops');
|
|
31
|
-
const fillService = require('../services/common/domain').fillService;
|
|
32
|
-
const _ = require('underscore');
|
|
33
|
-
let logContext = {
|
|
34
|
-
op: 'IoTAgentNGSI.Expression'
|
|
35
|
-
};
|
|
36
|
-
const grammar = {
|
|
37
|
-
lex: {
|
|
38
|
-
rules: [
|
|
39
|
-
['\\s+', '/* skip whitespace */'],
|
|
40
|
-
['@[a-zA-Z0-9_]+\\b', 'return "VARIABLE";'],
|
|
41
|
-
['[0-9]+(?:\\.[0-9]+)?\\b', 'return "NUMBER";'],
|
|
42
|
-
['\\*', 'return "*";'],
|
|
43
|
-
['\\/', 'return "/";'],
|
|
44
|
-
['-', 'return "-";'],
|
|
45
|
-
['\\+', 'return "+";'],
|
|
46
|
-
['\\^', 'return "^";'],
|
|
47
|
-
['\\(', 'return "(";'],
|
|
48
|
-
['\\)', 'return ")";'],
|
|
49
|
-
[',', 'return ",";'],
|
|
50
|
-
['#', 'return "#";'],
|
|
51
|
-
['indexOf', 'return "INDEX";'],
|
|
52
|
-
['length', 'return "LENGTH";'],
|
|
53
|
-
['substr', 'return "SUBSTR";'],
|
|
54
|
-
['trim', 'return "TRIM";'],
|
|
55
|
-
['"[a-zA-Z0-9\\s,]+"', 'return "STRING";'],
|
|
56
|
-
["'[a-zA-Z0-9\\s,]+'", 'return "STRING";'],
|
|
57
|
-
['$', 'return "EOF";']
|
|
58
|
-
]
|
|
59
|
-
},
|
|
60
|
-
|
|
61
|
-
operators: [
|
|
62
|
-
['left', '#', '+', '-'],
|
|
63
|
-
['left', '*', '/'],
|
|
64
|
-
['left', '^'],
|
|
65
|
-
['left', 'UMINUS']
|
|
66
|
-
],
|
|
67
|
-
|
|
68
|
-
bnf: {
|
|
69
|
-
expressions: [['e EOF', 'return $1;']],
|
|
70
|
-
|
|
71
|
-
e: [
|
|
72
|
-
['e + e', '$$ = Number($1) + Number($3);'],
|
|
73
|
-
['e - e', '$$ = $1 - $3;'],
|
|
74
|
-
['e * e', '$$ = $1 * $3;'],
|
|
75
|
-
['e / e', '$$ = $1 / $3;'],
|
|
76
|
-
['e ^ e', '$$ = Math.pow($1, $3);'],
|
|
77
|
-
['e # e', '$$ = String($1) + String($3);'],
|
|
78
|
-
['- e', '$$ = -$2;', { prec: 'UMINUS' }],
|
|
79
|
-
['INDEX ( e , e )', '$$ = String($3).indexOf($5)'],
|
|
80
|
-
['SUBSTR ( e , e , e )', '$$ = String($3).substr($5, $7)'],
|
|
81
|
-
['LENGTH ( e )', '$$ = String($3).length'],
|
|
82
|
-
['TRIM ( e )', '$$ = String($3).trim()'],
|
|
83
|
-
['( e )', '$$ = $2;'],
|
|
84
|
-
['NUMBER', '$$ = Number(yytext);'],
|
|
85
|
-
['VARIABLE', '$$ = yy[yytext.substr(1)];'],
|
|
86
|
-
['STRING', '$$ = yytext.substr(1, yytext.length -2);'],
|
|
87
|
-
['E', '$$ = Math.E;'],
|
|
88
|
-
['PI', '$$ = Math.PI;']
|
|
89
|
-
]
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
const parser = new Parser(grammar);
|
|
93
|
-
|
|
94
|
-
function parse(expression, context, type, callback) {
|
|
95
|
-
let result;
|
|
96
|
-
let error;
|
|
97
|
-
|
|
98
|
-
if (type !== 'String' && type !== 'Number') {
|
|
99
|
-
error = new errors.WrongExpressionType(type);
|
|
100
|
-
if (callback) {
|
|
101
|
-
callback(error);
|
|
102
|
-
} else {
|
|
103
|
-
throw error;
|
|
104
|
-
}
|
|
105
|
-
} else {
|
|
106
|
-
parser.yy = context;
|
|
107
|
-
|
|
108
|
-
try {
|
|
109
|
-
result = parser.parse(expression);
|
|
110
|
-
logger.debug(logContext, 'parse expression "[%j]" over "[%j]" result "[%j]" ', expression, context, result);
|
|
111
|
-
} catch (e) {
|
|
112
|
-
error = new errors.InvalidExpression(expression);
|
|
113
|
-
|
|
114
|
-
if (callback) {
|
|
115
|
-
return callback(error);
|
|
116
|
-
} else {
|
|
117
|
-
throw error;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
if (callback) {
|
|
122
|
-
callback(null, result);
|
|
123
|
-
} else {
|
|
124
|
-
return result;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function extractContext(attributeList) {
|
|
130
|
-
const context = {};
|
|
131
|
-
|
|
132
|
-
for (let i = 0; i < attributeList.length; i++) {
|
|
133
|
-
if (attributeList[i].name) {
|
|
134
|
-
context[attributeList[i].name] = attributeList[i].value;
|
|
135
|
-
}
|
|
136
|
-
if (attributeList[i].object_id) {
|
|
137
|
-
context[attributeList[i].object_id] = attributeList[i].value;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
return context;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
function processExpression(context) {
|
|
145
|
-
return function (expression) {
|
|
146
|
-
const cleanedExpression = expression.substr(2, expression.length - 3);
|
|
147
|
-
// Note that parse() function allows both String way processing and Number way processing
|
|
148
|
-
// (have a look to expression-test.js). However, here we only use one of the possibilities, i.e. String
|
|
149
|
-
const result = parse(cleanedExpression, context, 'String');
|
|
150
|
-
|
|
151
|
-
return {
|
|
152
|
-
original: expression,
|
|
153
|
-
value: result
|
|
154
|
-
};
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/* eslint-disable-next-line no-unused-vars */
|
|
159
|
-
function applyExpression(expression, context, typeInformation) {
|
|
160
|
-
logContext = fillService(logContext, typeInformation);
|
|
161
|
-
const expressionList = expression.match(/\$\{.*?\}/g) || [];
|
|
162
|
-
const substitutions = expressionList.map(processExpression(context));
|
|
163
|
-
let expressionResult = expression;
|
|
164
|
-
|
|
165
|
-
for (let i = 0; i < substitutions.length; i++) {
|
|
166
|
-
expressionResult = expressionResult.replace(substitutions[i].original, substitutions[i].value);
|
|
167
|
-
}
|
|
168
|
-
logger.debug(
|
|
169
|
-
logContext,
|
|
170
|
-
'applyExpression "[%j]" over "[%j]" result "[%j]" ',
|
|
171
|
-
expression,
|
|
172
|
-
context,
|
|
173
|
-
expressionResult
|
|
174
|
-
);
|
|
175
|
-
return expressionResult;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
function contextAvailable(expression, context) {
|
|
179
|
-
let error;
|
|
180
|
-
try {
|
|
181
|
-
const variablesList = expression.match(/@[a-zA-Z0-9_]+/g) || [];
|
|
182
|
-
const variables = variablesList.map(function (item) {
|
|
183
|
-
return item.substr(1);
|
|
184
|
-
});
|
|
185
|
-
const keys = Object.keys(context);
|
|
186
|
-
let validContext = _.difference(variables, keys).length === 0;
|
|
187
|
-
if (!validContext) {
|
|
188
|
-
logger.info(
|
|
189
|
-
logContext,
|
|
190
|
-
'For expression "[%s]" context "[%j]" does not have element to match',
|
|
191
|
-
expression,
|
|
192
|
-
context
|
|
193
|
-
);
|
|
194
|
-
}
|
|
195
|
-
return validContext;
|
|
196
|
-
} catch (e) {
|
|
197
|
-
error = new errors.InvalidExpression(expression);
|
|
198
|
-
throw error;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
exports.parse = parse;
|
|
203
|
-
exports.extractContext = extractContext;
|
|
204
|
-
exports.contextAvailable = contextAvailable;
|
|
205
|
-
exports.applyExpression = applyExpression;
|
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2016 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
|
-
|
|
24
|
-
/* eslint-disable no-unused-vars */
|
|
25
|
-
|
|
26
|
-
const should = require('should');
|
|
27
|
-
const expressionParser = require('../../../lib/plugins/expressionParser');
|
|
28
|
-
|
|
29
|
-
describe('Expression interpreter', function () {
|
|
30
|
-
const scope = {
|
|
31
|
-
value: 6,
|
|
32
|
-
other: 3,
|
|
33
|
-
theString: '12.6, -19.4',
|
|
34
|
-
spaces: '5 a b c d 5',
|
|
35
|
-
big: 2000,
|
|
36
|
-
number: 145,
|
|
37
|
-
number2: 155,
|
|
38
|
-
number3inside: 200
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
describe('When a expression with a single value is parsed', function () {
|
|
42
|
-
it('should return that value', function (done) {
|
|
43
|
-
expressionParser.parse('5 * @value', scope, 'Number', function (error, result) {
|
|
44
|
-
should.not.exist(error);
|
|
45
|
-
result.should.equal(30);
|
|
46
|
-
done();
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
const arithmetic = [
|
|
52
|
-
['5 * @value', 30],
|
|
53
|
-
['(6 + @value) * 3', 36],
|
|
54
|
-
['@value / 12 + 1', 1.5],
|
|
55
|
-
['(5 + 2) * (@value + 7)', 91],
|
|
56
|
-
['(5 - @other) * (@value + 7)', 26],
|
|
57
|
-
['3 * 5.2', 15.6],
|
|
58
|
-
['@value * 5.2', 31.2]
|
|
59
|
-
];
|
|
60
|
-
|
|
61
|
-
function arithmeticUseCase(arithmeticExpr) {
|
|
62
|
-
describe('When an expression with the arithmetic operation [' + arithmeticExpr[0] + '] is parsed', function () {
|
|
63
|
-
it('should be interpreted appropriately', function (done) {
|
|
64
|
-
expressionParser.parse(arithmeticExpr[0], scope, 'Number', function (error, result) {
|
|
65
|
-
should.not.exist(error);
|
|
66
|
-
result.should.approximately(arithmeticExpr[1], 0.000001);
|
|
67
|
-
done();
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
for (let i = 0; i < arithmetic.length; i++) {
|
|
74
|
-
arithmeticUseCase(arithmetic[i]);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
describe('When an expression with two strings is concatenated', function () {
|
|
78
|
-
it('should return the concatenation of both strings', function (done) {
|
|
79
|
-
expressionParser.parse('"Pruebas" # "DeStrings"', scope, 'String', function (error, result) {
|
|
80
|
-
should.not.exist(error);
|
|
81
|
-
result.should.equal('PruebasDeStrings');
|
|
82
|
-
done();
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
describe('When string transformation functions are executed', function () {
|
|
88
|
-
it('should return the appropriate piece of the string', function (done) {
|
|
89
|
-
expressionParser.parse(
|
|
90
|
-
'trim(substr(@theString, indexOf(@theString, ",") + 1, length(@theString)))',
|
|
91
|
-
scope,
|
|
92
|
-
'String',
|
|
93
|
-
function (error, result) {
|
|
94
|
-
should.not.exist(error);
|
|
95
|
-
result.should.equal('-19.4');
|
|
96
|
-
done();
|
|
97
|
-
}
|
|
98
|
-
);
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
describe('When an expression contains variables with numbers', function () {
|
|
103
|
-
it('should return the appropriate result', function (done) {
|
|
104
|
-
expressionParser.parse('@number + @number2 + @number3inside', scope, 'String', function (error, result) {
|
|
105
|
-
should.not.exist(error);
|
|
106
|
-
result.should.equal(500);
|
|
107
|
-
done();
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
describe('When an expression contains multiple parenthesis', function () {
|
|
113
|
-
it('should return the appropriate result', function (done) {
|
|
114
|
-
expressionParser.parse('((@number) * (@number2))', scope, 'String', function (error, result) {
|
|
115
|
-
should.not.exist(error);
|
|
116
|
-
result.should.equal(22475);
|
|
117
|
-
done();
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
describe('When trim() function is executed', function () {
|
|
123
|
-
it('should return the appropriate piece of the string', function (done) {
|
|
124
|
-
expressionParser.parse('trim(@spaces)', scope, 'String', function (error, result) {
|
|
125
|
-
should.not.exist(error);
|
|
126
|
-
result.should.equal('5 a b c d 5');
|
|
127
|
-
done();
|
|
128
|
-
});
|
|
129
|
-
});
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
describe('When an expression with strings containing spaces is concatenated', function () {
|
|
133
|
-
it('should honour the whitespaces', function (done) {
|
|
134
|
-
expressionParser.parse('"Pruebas " # "De Strings"', scope, 'String', function (error, result) {
|
|
135
|
-
should.not.exist(error);
|
|
136
|
-
result.should.equal('Pruebas De Strings');
|
|
137
|
-
done();
|
|
138
|
-
});
|
|
139
|
-
});
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
describe('When an expression with strings with single quotation marks is parsed', function () {
|
|
143
|
-
it('should accept the strings', function (done) {
|
|
144
|
-
expressionParser.parse(
|
|
145
|
-
"'Pruebas ' # 'De Strings'",
|
|
146
|
-
|
|
147
|
-
scope,
|
|
148
|
-
'String',
|
|
149
|
-
function (error, result) {
|
|
150
|
-
should.not.exist(error);
|
|
151
|
-
result.should.equal('Pruebas De Strings');
|
|
152
|
-
done();
|
|
153
|
-
}
|
|
154
|
-
);
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
describe('When a string is concatenated with a number', function () {
|
|
159
|
-
it('should result in a string concatenation', function (done) {
|
|
160
|
-
expressionParser.parse('"number " # 5', scope, 'String', function (error, result) {
|
|
161
|
-
should.not.exist(error);
|
|
162
|
-
result.should.equal('number 5');
|
|
163
|
-
done();
|
|
164
|
-
});
|
|
165
|
-
});
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
describe('When an expression with a wrong type is parsed', function () {
|
|
169
|
-
it('should raise a WRONG_EXPRESSION_TYPE error', function (done) {
|
|
170
|
-
expressionParser.parse('"number " # 5', scope, 'Device', function (error, result) {
|
|
171
|
-
should.exist(error);
|
|
172
|
-
error.name.should.equal('WRONG_EXPRESSION_TYPE');
|
|
173
|
-
done();
|
|
174
|
-
});
|
|
175
|
-
});
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
describe('When an expression with a parse error is parsed', function () {
|
|
179
|
-
it('should raise an INVALID_EXPRESSION error', function (done) {
|
|
180
|
-
expressionParser.parse('"numb+sd ññ ((', scope, 'Number', function (error, result) {
|
|
181
|
-
should.exist(error);
|
|
182
|
-
error.name.should.equal('INVALID_EXPRESSION');
|
|
183
|
-
done();
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
describe('When an string function is used with an expression', function () {
|
|
189
|
-
it('should work on the expression value', function (done) {
|
|
190
|
-
expressionParser.parse('indexOf(24 * @big, "80")', scope, 'String', function (error, result) {
|
|
191
|
-
should.not.exist(error);
|
|
192
|
-
result.should.equal(1);
|
|
193
|
-
done();
|
|
194
|
-
});
|
|
195
|
-
});
|
|
196
|
-
});
|
|
197
|
-
});
|