Skip to content

Commit bc54c39

Browse files
committed
Support NGSI-LD QueryEntities endpoint for lazy attributes
1 parent 6bf855f commit bc54c39

File tree

3 files changed

+76
-13
lines changed

3 files changed

+76
-13
lines changed

lib/services/northBound/contextServer-NGSI-LD.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,9 @@ function handleMergePatchNgsiLD(req, res, next) {
506506
* @param {Object} res Response that will be sent.
507507
*/
508508
function handleQueryNgsiLD(req, res, next) {
509+
const returnAsArray = req.query.id;
510+
req.params.entity = req.params.entity || req.query.id || '';
511+
509512
function getName(element) {
510513
return element.name;
511514
}
@@ -641,7 +644,7 @@ function handleQueryNgsiLD(req, res, next) {
641644
next(error);
642645
} else {
643646
logger.debug(context, 'Query from [%s] handled successfully.', req.get('host'));
644-
res.status(200).json(result);
647+
res.status(200).json(returnAsArray ? [result] : result);
645648
}
646649
}
647650

@@ -793,7 +796,6 @@ function loadUnsupportedEndpointsNGSILD(router) {
793796
const unsupportedEndpoint = function (req, res) {
794797
return res.status(501).send(new errors.MethodNotSupported(req.method, req.path));
795798
};
796-
router.get('/ngsi-ld/v1/entities', unsupportedEndpoint);
797799
router.post('/ngsi-ld/v1/entities', unsupportedEndpoint);
798800
router.delete('/ngsi-ld/v1/entities/:entity', unsupportedEndpoint);
799801
router.delete('/ngsi-ld/v1/entities/:entity/attrs/:attr', unsupportedEndpoint);
@@ -846,6 +848,7 @@ function loadContextRoutesNGSILD(router) {
846848
for (i = 0; i < queryPaths.length; i++) {
847849
router.get(queryPaths[i], [handleQueryNgsiLD, ErrorHandlingNgsiLD('Query')]);
848850
}
851+
router.get('/ngsi-ld/v1/entities', [handleQueryNgsiLD, ErrorHandlingNgsiLD('Query')]);
849852
router.post('/notify', [
850853
middlewares.ensureType,
851854
middlewares.validateJson(notificationTemplateNgsiLD),

test/unit/ngsi-ld/lazyAndCommands/lazy-devices-test.js

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ describe('NGSI-LD - IoT Agent Lazy Devices', function () {
226226
it('should call the device handler for each of the contexts');
227227
});
228228

229-
describe('When a context query arrives to the IoT Agent', function () {
229+
describe('When a context query arrives to the IoT Agent using retrieveEntity', function () {
230230
const options = {
231231
url:
232232
'http://localhost:' +
@@ -294,6 +294,76 @@ describe('NGSI-LD - IoT Agent Lazy Devices', function () {
294294
});
295295
});
296296

297+
describe('When a context query arrives to the IoT Agent using queryEntities', function () {
298+
const options = {
299+
url:
300+
'http://localhost:' +
301+
iotAgentConfig.server.port +
302+
'/ngsi-ld/v1/entities/?id=urn:ngsi-ld:Light:light1&attrs=dimming',
303+
method: 'GET',
304+
headers: {
305+
'fiware-service': 'smartgondor',
306+
'fiware-servicepath': 'gardens'
307+
}
308+
};
309+
const sensorData = [
310+
{
311+
id: 'Light:light1',
312+
type: 'Light',
313+
dimming: {
314+
type: 'Percentage',
315+
value: 19
316+
}
317+
}
318+
];
319+
320+
beforeEach(function (done) {
321+
nock.cleanAll();
322+
323+
contextBrokerMock = nock('http://192.168.1.1:1026')
324+
.matchHeader('fiware-service', 'smartgondor')
325+
.post(
326+
'/ngsi-ld/v1/csourceRegistrations/',
327+
utils.readExampleFile(
328+
'./test/unit/ngsi-ld/examples/contextAvailabilityRequests/registerIoTAgent1.json'
329+
)
330+
)
331+
.reply(201, null, { Location: '/ngsi-ld/v1/csourceRegistrations/6319a7f5254b05844116584d' });
332+
333+
contextBrokerMock
334+
.matchHeader('fiware-service', 'smartgondor')
335+
.post('/ngsi-ld/v1/entityOperations/upsert/')
336+
.reply(204);
337+
338+
async.series([apply(iotAgentLib.activate, iotAgentConfig), apply(iotAgentLib.register, device1)], done);
339+
});
340+
341+
it('should return the information querying the underlying devices', function (done) {
342+
const expectedResponse = [
343+
utils.readExampleFile(
344+
'./test/unit/ngsi-ld/examples/contextProviderResponses/queryInformationResponse.json'
345+
)
346+
];
347+
348+
iotAgentLib.setDataUpdateHandler(function (id, type, service, subservice, attributes, callback) {
349+
callback(null);
350+
});
351+
352+
iotAgentLib.setDataQueryHandler(function (id, type, service, subservice, attributes, callback) {
353+
id.should.equal('urn:ngsi-ld:' + device1.type + ':' + device1.id);
354+
type.should.equal(device1.type);
355+
attributes[0].should.equal('dimming');
356+
callback(null, sensorData[0]);
357+
});
358+
359+
request(options, function (error, response, body) {
360+
should.not.exist(error);
361+
body.should.eql(expectedResponse);
362+
done();
363+
});
364+
});
365+
});
366+
297367
describe('When a context query arrives to the IoT Agent and no handler is set', function () {
298368
const options = {
299369
url:

test/unit/ngsi-ld/ngsiService/unsupported-endpoints-test.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,6 @@ describe('NGSI-LD - Unsupported Endpoints', function () {
108108
});
109109

110110
describe('When accessing an Unsupported Endpoint', function () {
111-
it('GET /entities should return a valid NSGI-LD error message', function (done) {
112-
const options = {
113-
url: 'http://localhost:' + iotAgentConfig.server.port + '/ngsi-ld/v1/entities',
114-
method: 'GET'
115-
};
116-
request(options, function (error, response, body) {
117-
response.statusCode.should.equal(501);
118-
done();
119-
});
120-
});
121111
it('POST /entities should return a valid NSGI-LD error message', function (done) {
122112
const options = {
123113
url: 'http://localhost:' + iotAgentConfig.server.port + '/ngsi-ld/v1/entities',

0 commit comments

Comments
 (0)