iotagent-node-lib
Version:
IoT Agent library to interface with NGSI Context Broker
203 lines (188 loc) • 6.54 kB
JavaScript
/*
* Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U
*
* This file is part of fiware-iotagent-lib
*
* fiware-iotagent-lib 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.
*
* fiware-iotagent-lib 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 fiware-iotagent-lib.
* If not, see http://www.gnu.org/licenses/.
*
* For those usages not covered by the GNU Affero General Public License
* please contact with::[contacto@tid.es]
*/
const iotAgentLib = require('../../../../lib/fiware-iotagent-lib');
const should = require('should');
const logger = require('logops');
const nock = require('nock');
let contextBrokerMock;
const iotAgentConfig = {
contextBroker: {
host: '192.168.1.1',
port: '1026',
ngsiVersion: 'v2'
},
server: {
port: 4041,
host: 'localhost'
},
types: {
Light: {
commands: [],
type: 'Light',
lazy: [
{
name: 'temperature',
type: 'centigrades'
}
],
active: [
{
name: 'pressure',
type: 'Hgmm'
}
]
},
BrokenLight: {
commands: [],
lazy: [
{
name: 'temperature',
type: 'centigrades'
}
],
active: [
{
name: 'pressure',
type: 'Hgmm'
}
]
},
Termometer: {
type: 'Termometer',
commands: [],
lazy: [
{
name: 'temp',
type: 'kelvin'
}
],
active: []
},
Humidity: {
type: 'Humidity',
cbHost: 'http://192.168.1.1:3024',
commands: [],
lazy: [],
active: [
{
name: 'humidity',
type: 'percentage'
}
]
},
Motion: {
type: 'Motion',
commands: [],
lazy: [],
staticAttributes: [
{
name: 'location',
type: 'Vector',
value: '(123,523)'
}
],
active: [
{
name: 'humidity',
type: 'percentage'
}
]
}
},
service: 'smartgondor',
subservice: 'gardens',
providerUrl: 'http://smartgondor.com',
deviceRegistrationDuration: 'P1M'
};
describe('NGSI-v2 - Query device information in the Context Broker', function () {
const attributes = ['state', 'dimming'];
beforeEach(function (done) {
logger.setLevel('FATAL');
iotAgentLib.activate(iotAgentConfig, done);
});
afterEach(function (done) {
iotAgentLib.deactivate(done);
});
describe('When the user requests information about a registered device', function () {
beforeEach(function () {
nock.cleanAll();
contextBrokerMock = nock('http://192.168.1.1:1026')
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', 'gardens')
.get('/v2/entities/light1/attrs?attrs=state,dimming&type=Light')
.reply(200, {});
});
it('should return the information about the desired attributes', function (done) {
iotAgentLib.query('light1', 'Light', '', attributes, function (error) {
should.not.exist(error);
contextBrokerMock.done();
done();
});
});
});
describe("When the user requests information about a device that it's not in the CB", function () {
beforeEach(function () {
nock.cleanAll();
contextBrokerMock = nock('http://192.168.1.1:1026')
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', 'gardens')
.get('/v2/entities/light3/attrs?attrs=state,dimming&type=Light')
.reply(404, {
error: 'NotFound',
description: 'The requested entity has not been found. Check type and id'
});
});
it('should return a DEVICE_NOT_FOUND_ERROR', function (done) {
iotAgentLib.query('light3', 'Light', '', attributes, function (error) {
should.exist(error);
error.name.should.equal('DEVICE_NOT_FOUND');
done();
});
});
});
describe('When the user requests information and there is an unknown errorCode in the response', function () {
beforeEach(function () {
nock.cleanAll();
contextBrokerMock = nock('http://192.168.1.1:1026')
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', 'gardens')
.get('/v2/entities/light3/attrs?attrs=state,dimming&type=Light')
.reply(503, {
error: 'Service Unavailable',
description: 'The context broker is current down for maintenence'
});
});
it('should return a ENTITY_GENERIC_ERROR', function (done) {
iotAgentLib.query('light3', 'Light', '', attributes, function (error) {
should.exist(error);
should.exist(error.name);
should.exist(error.code);
should.exist(error.details.description);
error.name.should.equal('ENTITY_GENERIC_ERROR');
error.code.should.equal(503);
error.details.description.should.equal('The context broker is current down for maintenence');
done();
});
});
});
});