UNPKG

iotagent-node-lib

Version:

IoT Agent library to interface with NGSI Context Broker

120 lines (107 loc) 4.15 kB
/* * 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] * * Modified by: Daniel Calvo - ATOS Research & Innovation */ /* eslint-disable no-unused-vars */ const iotAgentLib = require('../../../../lib/fiware-iotagent-lib'); const utils = require('../../../tools/utils'); const request = utils.request; const should = require('should'); const nock = require('nock'); let contextBrokerMock; const iotAgentConfig = { logLevel: 'FATAL', contextBroker: { host: '192.168.1.1', port: '1026', ngsiVersion: 'v2' }, server: { port: 4041, host: 'localhost', baseRoot: '/' }, types: {}, service: 'smartgondor', subservice: 'gardens', providerUrl: 'http://smartgondor.com' }; describe('NGSI-v2 - Device provisioning API: Provision devices', function () { beforeEach(function (done) { nock.cleanAll(); iotAgentLib.activate(iotAgentConfig, function () { iotAgentLib.clearAll(done); }); }); afterEach(function (done) { nock.cleanAll(); iotAgentLib.setProvisioningHandler(); iotAgentLib.deactivate(done); }); describe('When a device provisioning request with all the required data arrives to the IoT Agent', function () { beforeEach(function () { nock.cleanAll(); contextBrokerMock = nock('http://192.168.1.1:1026') .matchHeader('fiware-service', 'smartgondor') .matchHeader('fiware-servicepath', '/gardens') .post( '/v2/registrations', utils.readExampleFile( './test/unit/ngsiv2/examples/contextAvailabilityRequests/registerProvisionedDevice.json' ) ) .reply(201, null, { Location: '/v2/registrations/6319a7f5254b05844116584d' }); contextBrokerMock .matchHeader('fiware-service', 'smartgondor') .matchHeader('fiware-servicepath', '/gardens') .post( '/v2/entities?options=upsert,flowControl', utils.readExampleFile( './test/unit/ngsiv2/examples/contextRequests/createProvisionedDeviceMultientity.json' ) ) .reply(204); }); const options = { url: 'http://localhost:' + iotAgentConfig.server.port + '/iot/devices', method: 'POST', json: utils.readExampleFile( './test/unit/examples/deviceProvisioningRequests/provisionNewDeviceMultientity.json' ), headers: { 'fiware-service': 'smartgondor', 'fiware-servicepath': '/gardens' } }; it('should add the device to the devices list', function (done) { request(options, function (error, response, body) { should.not.exist(error); response.statusCode.should.equal(201); iotAgentLib.listDevices('smartgondor', '/gardens', function (error, results) { results.devices.length.should.equal(1); done(); }); }); }); }); });