Skip to content

Commit 7222442

Browse files
committed
Merge pull request #66 from telefonicaid/bug/emptyArrayInQueryContextRetrievesAnEmptyEntity
FIX Empty attributes array treated as unexistent
2 parents a818d06 + 8f4b503 commit 7222442

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

lib/services/contextServer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ function handleQuery(req, res, next) {
223223
}
224224

225225
function completeAttributes(attributes, device, callback) {
226-
if (attributes) {
226+
if (attributes && attributes.length !== 0) {
227227
logger.debug(context, 'Handling received set of attributes: %j', attributes);
228228
callback(null, attributes);
229229
} else if (device.lazy) {

test/unit/lazy-devices-test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,72 @@ describe('IoT Agent Lazy Devices', function() {
450450
});
451451
});
452452

453+
describe('When a query arrives to the IoT Agent with an empty attributes array', function() {
454+
var options = {
455+
url: 'http://localhost:' + iotAgentConfig.server.port + '/v1/queryContext',
456+
method: 'POST',
457+
json: {
458+
entities: [
459+
{
460+
type: 'Light',
461+
isPattern: 'false',
462+
id: 'light1'
463+
}
464+
],
465+
attributes: []
466+
}
467+
},
468+
sensorData = [
469+
{
470+
id: 'light1',
471+
isPattern: false,
472+
type: 'Light',
473+
attributes: [
474+
{
475+
name: 'temperature',
476+
type: 'centigrades',
477+
value: 19
478+
}
479+
]
480+
}
481+
];
482+
483+
beforeEach(function(done) {
484+
nock.cleanAll();
485+
486+
contextBrokerMock = nock('http://10.11.128.16:1026')
487+
.matchHeader('fiware-service', 'smartGondor')
488+
.matchHeader('fiware-servicepath', 'gardens')
489+
.post('/NGSI9/registerContext',
490+
utils.readExampleFile('./test/unit/contextAvailabilityRequests/registerIoTAgent1.json'))
491+
.reply(200,
492+
utils.readExampleFile('./test/unit/contextAvailabilityResponses/registerIoTAgent1Success.json'));
493+
494+
async.series([
495+
apply(iotAgentLib.activate, iotAgentConfig),
496+
apply(iotAgentLib.register, device1)
497+
], done);
498+
});
499+
500+
it('should return the information of all the attributes', function(done) {
501+
var expectedResponse = utils
502+
.readExampleFile('./test/unit/contextProviderResponses/queryInformationResponseEmptyAttributes.json');
503+
504+
iotAgentLib.setDataQueryHandler(function(id, type, attributes, callback) {
505+
should.exist(attributes);
506+
attributes.length.should.equal(1);
507+
attributes[0].should.equal('temperature');
508+
callback(null, sensorData[0]);
509+
});
510+
511+
request(options, function(error, response, body) {
512+
should.not.exist(error);
513+
body.should.eql(expectedResponse);
514+
done();
515+
});
516+
});
517+
});
518+
453519
describe('When a context query arrives to the IoT Agent for a type with static attributes', function() {
454520
var options = {
455521
url: 'http://localhost:' + iotAgentConfig.server.port + '/v1/queryContext',

0 commit comments

Comments
 (0)