-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathtestUtils.js
More file actions
299 lines (282 loc) · 11.5 KB
/
testUtils.js
File metadata and controls
299 lines (282 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* Copyright 2023 Telefonica Investigación y Desarrollo, S.A.U
*
* This file is part of iotagent-json
*
* iotagent-json is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* iotagent-json is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with iotagent-json.
* If not, seehttp://www.gnu.org/licenses/.
*
* For those usages not covered by the GNU Affero General Public License
* please contact with::[contacto@tid.es]
*
* Modified by: Miguel Angel Pedraza
*/
/* eslint-disable no-unused-vars*/
/* eslint-disable no-unused-expressions*/
const nock = require('nock');
const utils = require('../tools/utils');
const request = utils.request;
const async = require('async');
const chai = require('chai');
const MQTT = require('async-mqtt');
const iotAgentLib = require('../../lib/fiware-iotagent-lib');
var chaiMatchPattern = require('chai-match-pattern');
chai.use(chaiMatchPattern);
var _ = chaiMatchPattern.getLodashModule();
var expect = chai.expect;
chai.config.truncateThreshold = 0;
// Error messages
const ERR_CB_EXPECTATION_DIFFER = 'Assertion Error - Context Broker received payload differs from expectation';
const ERR_MEAS_BODY = 'Assertion Error - Measure response is not empty';
const ERR_MEAS_CODE = 'Assertion Error - Measure response status code differs from 200';
const ERR_MQTT = 'Error with MQTT: ';
const ERR_CB_NOT_EMPTY = 'Assertion Error - unexpected Context Broker request received (no request expected)';
const DEF_TYPE = 'TestType';
/**
* @brief Sends a measure to the IoT Agent and returns a promise with the response
*
* @param {Object} measure Measure to be sent to the IoT Agent
*/
function sendMeasureHttp(measure) {
return new Promise((resolve, reject) => {
request(measure, function (error, result, body) {
error ? reject(error) : resolve(result);
});
});
}
/**
* @brief Sends a measure to the IoT Agent and returns a promise with the response
*
* @param {Object} measure Measure to be sent to the IoT Agent
*/
function sendMeasureIotaLib(measure, provision) {
return new Promise((resolve, reject) => {
/**
* WARNING: This is kind of a hack, only required for the tests using Lib, since the method iotAgentLib.update
* requires a type and does not check the type of the group. For this purpose, this function uses the
* provision type, setting the measure type to the same type as the provision.
* This is not a problem for the tests using other transports than Lib, in that case, the type will be retrieved
* from the real provision.
*/
let typeInformation = {
service: provision.headers['fiware-service'],
subservice: provision.headers['fiware-servicepath']
};
let type;
let staticAttrs;
if (Array.isArray(provision.json.services) && provision.json.services.length > 0) {
type = provision.json.services[0].entity_type;
staticAttrs = provision.json.services[0].static_attributes;
} else {
type = DEF_TYPE;
}
typeInformation.type = type;
if (staticAttrs) {
typeInformation.staticAttributes = staticAttrs;
}
typeInformation.id = measure.qs.i;
iotAgentLib.update(
type + ':' + measure.qs.i,
type,
typeInformation,
jsonToIotaMeasures(measure.json),
function (error, result, body) {
error ? reject(error) : resolve(result);
}
);
});
}
/**
* @brief Converts a IOTA JSON object to an array of measures as expected by the IOTA Lib
*
* @param {Object} json
* @returns {Array} measures
*/
function jsonToIotaMeasures(originJson) {
// FIXME: maybe this could be refactored to use less code
if (originJson && originJson[0]) {
// multimeasure case
let finalMeasures = [];
for (let json of originJson) {
let measures = [];
for (let key in json) {
/* eslint-disable-next-line no-prototype-builtins */
if (json.hasOwnProperty(key)) {
let measure = {
name: key,
value: json[key]
};
// A bit of Magic. If the key is TimeInstant, we set the type to DateTime.
// When sending the data through iot
if (key === 'TimeInstant') {
measure.type = 'DateTime';
} else {
// Although the type is not meaningfull and we could have picked any string for this,
// we have aligned with DEFAULT_ATTRIBUTE_TYPE constant in IOTA-JSON and IOTA-UL repositories
measure.type = 'Text';
}
measures.push(measure);
}
}
finalMeasures.push(measures);
}
return finalMeasures;
} else {
let json = originJson;
let measures = [];
for (let key in json) {
/* eslint-disable-next-line no-prototype-builtins */
if (json.hasOwnProperty(key)) {
let measure = {
name: key,
value: json[key]
};
// A bit of Magic. If the key is TimeInstant, we set the type to DateTime.
// When sending the data through iot
if (key === 'TimeInstant') {
measure.type = 'DateTime';
} else {
// Although the type is not meaningfull and we could have picked any string for this,
// we have aligned with DEFAULT_ATTRIBUTE_TYPE constant in IOTA-JSON and IOTA-UL repositories
measure.type = 'Text';
}
measures.push(measure);
}
}
return measures;
}
}
/**
* @brief Delays the execution of the code for the specified time in ms
*
* @param {Number} time in ms
* @returns
*/
const delay = (time) => new Promise((res) => setTimeout(res, time));
function groupToIoTAConfigType(group, service, subservice) {
let type = {};
for (var key in group) {
/* eslint-disable-next-line no-prototype-builtins */
if (group.hasOwnProperty(key)) {
if (key === 'attributes') {
type.active = group.attributes;
} else if (key === 'entity_type') {
type.type = group.entity_type;
} else if (key === 'static_attributes') {
type.staticAttributes = group.static_attributes;
} else if (key === 'commands') {
type.commands = group.commands;
} else if (key !== 'resource') {
type[key] = group[key];
}
}
}
type.service = service;
type.subservice = subservice;
return { name: group.entity_type, type: type };
}
/**
* Test Case function
* @brief Sends a measure to the IoT Agent and validates the response
* and validates the Context Broker expectation
*
* @param {Object} measure Measure to be sent to the IoT Agent
* @param {Object} expectation Expectation for the Context Broker
* @param {Object} env Environment variables
* @param {Object} config IoTA Configuration object
* @param {String} type Type of test (multientity or multimeasure)
* @param {String} transport Transport to be used (Lib, HTTP or MQTT). If not specified, Lib is used.
* @param {Boolean} regex If true, the expectation is treated as a regex
*/
async function testCase(measure, expectation, provision, env, config, type, transport, regex) {
let receivedContext = [];
let cbMockRoute = '';
// Set the correct route depending if the test is multientity or not
if (type === 'multientity' || type === 'multimeasure') {
cbMockRoute = '/v2/op/update?options=flowControl';
} else {
cbMockRoute = '/v2/entities?options=upsert,flowControl';
}
// Set the correct mock times depending if the test is multimeasure or not
// based on the length of the expectation array
let mockTimes = 1;
if (expectation.length > 1) {
mockTimes = expectation.length;
}
let contextBrokerMock = nock('http://192.168.1.1:1026')
.matchHeader('fiware-service', env.service)
.matchHeader('fiware-servicepath', env.servicePath)
.post(cbMockRoute, function (body) {
mockTimes === 1 ? (receivedContext = body) : receivedContext.push(body); // Save the received body for later comparison
return true;
})
.times(mockTimes)
.reply(204);
// Send a measure to the IoT Agent and wait for the response
if (transport === 'MQTT') {
try {
let client = await MQTT.connectAsync('mqtt://' + config.mqtt.host);
await client.publish('/' + measure.qs.k + '/' + measure.qs.i + '/attrs', JSON.stringify(measure.json));
await client.end();
} catch (error) {
expect.fail(ERR_MQTT + error);
}
} else if (transport === 'HTTP') {
// HTTP
const response = await sendMeasureHttp(measure);
// Validate the response status code and the response body
expect(response.statusCode, ERR_MEAS_CODE).to.equal(200);
expect(response.body, ERR_MEAS_BODY).to.be.empty;
} else {
const response = await sendMeasureIotaLib(measure, provision);
}
// Validate Context Broker Expectation
if ((Array.isArray(expectation) && expectation.length > 0) || !Array.isArray(expectation)) {
// Filter empty expectations
regex && regex === true
? expect(receivedContext, ERR_CB_EXPECTATION_DIFFER).to.matchPattern(expectation)
: expect(receivedContext, ERR_CB_EXPECTATION_DIFFER).to.deep.equal(expectation);
contextBrokerMock.done(); // Ensure the request was made, no matter the body content
} else {
// If empty expectation, ensure no request was made
expect(contextBrokerMock.isDone(), ERR_CB_NOT_EMPTY).to.be.false;
expect(receivedContext, ERR_CB_NOT_EMPTY).to.be.empty;
}
}
/**
*
* @param {*} skip skip string from test case. I.E: "lib, !json"
* @param {*} matchPattern skip pattern to check. I.E: "lib"
* @returns true if the test should be skipped. False otherwise
*/
function checkSkip(skip, matchPattern) {
var isMatch = false;
// Separate tokens by comma or space, and remove empty tokens
var tokens = skip.split(/[ , ]+/).filter(function (value, index, arr) {
return value !== '' && !value.match(/[* ]+/);
});
// Check if the skip pattern is in the tokens array, or there is a token starting with ! without the pattern (negative match -!b)
tokens.forEach((element) => {
if (element === matchPattern || (element[0] === '!' && element.substr(1) !== matchPattern)) {
isMatch = true;
}
});
return isMatch;
}
exports.checkSkip = checkSkip;
exports.sendMeasureHttp = sendMeasureHttp;
exports.sendMeasureIotaLib = sendMeasureIotaLib;
exports.delayMs = delay;
exports.testCase = testCase;
exports.groupToIoTAConfigType = groupToIoTAConfigType;