diff --git a/test/unit/HTTP_commands_test.js b/test/unit/HTTP_commands_test.js deleted file mode 100644 index a1ab86a5..00000000 --- a/test/unit/HTTP_commands_test.js +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright 2016 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] - */ - -/* eslint-disable no-unused-vars */ - -const iotagentMqtt = require('../../'); -const config = require('../config-test.js'); -const nock = require('nock'); -const should = require('should'); -const iotAgentLib = require('iotagent-node-lib'); -const async = require('async'); -const request = require('request'); -const utils = require('../utils'); -let mockedClientServer; -let contextBrokerMock; - -describe('HTTP: Commands', function() { - beforeEach(function(done) { - const provisionOptions = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices', - method: 'POST', - json: utils.readExampleFile('./test/deviceProvisioning/provisionCommandHTTP.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - config.logLevel = 'INFO'; - - nock.cleanAll(); - - contextBrokerMock = nock('http://192.168.1.1:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/NGSI9/registerContext') - .reply(200, utils.readExampleFile('./test/contextAvailabilityResponses/registerIoTAgent1Success.json')); - - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/updateStatus1Success.json')); - - iotagentMqtt.start(config, function() { - request(provisionOptions, function(error, response, body) { - done(); - }); - }); - }); - - afterEach(function(done) { - nock.cleanAll(); - async.series([iotAgentLib.clearAll, iotagentMqtt.stop], done); - }); - - describe('When a command arrive to the Agent for a device with the HTTP protocol', function() { - const commandOptions = { - url: 'http://localhost:' + config.iota.server.port + '/v1/updateContext', - method: 'POST', - json: utils.readExampleFile('./test/contextRequests/updateCommand1.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - beforeEach(function() { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/updateStatus1.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/updateStatus1Success.json')); - - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/updateStatus6.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/updateStatus1Success.json')); - - mockedClientServer = nock('http://localhost:9876') - .post('/command', function(body) { - return body.PING || body.PING.data || body.PING.data === 22; - }) - .reply(200, '{"PING":{"data":"22"}}'); - }); - - it('should return a 200 OK without errors', function(done) { - request(commandOptions, function(error, response, body) { - should.not.exist(error); - response.statusCode.should.equal(200); - done(); - }); - }); - it('should reply with the appropriate command information', function(done) { - request(commandOptions, function(error, response, body) { - should.exist(body); - body.contextResponses['0'].statusCode.code.should.equal(200); - done(); - }); - }); - it('should update the status in the Context Broker', function(done) { - request(commandOptions, function(error, response, body) { - contextBrokerMock.done(); - done(); - }); - }); - it('should publish the command information in the MQTT topic', function(done) { - request(commandOptions, function(error, response, body) { - setTimeout(function() { - mockedClientServer.done(); - done(); - }, 100); - }); - }); - }); -}); diff --git a/test/unit/HTTP_reveice_measures-test.js b/test/unit/HTTP_reveice_measures-test.js deleted file mode 100644 index 978abd3e..00000000 --- a/test/unit/HTTP_reveice_measures-test.js +++ /dev/null @@ -1,339 +0,0 @@ -/* - * Copyright 2016 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] - */ - -/* eslint-disable no-unused-vars */ - -const iotagentMqtt = require('../../'); -const config = require('../config-test.js'); -const nock = require('nock'); -const iotAgentLib = require('iotagent-node-lib'); -const should = require('should'); -const async = require('async'); -const request = require('request'); -const utils = require('../utils'); -const groupCreation = { - url: 'http://localhost:' + config.iota.server.port + '/iot/services', - method: 'POST', - json: { - services: [ - { - resource: '/iot/json', - apikey: 'KL223HHV8732SFL1', - entity_type: 'TheLightType', - trust: '8970A9078A803H3BL98PINEQRW8342HBAMS', - cbHost: 'http://unexistentHost:1026', - commands: [], - lazy: [], - attributes: [ - { - name: 'status', - type: 'Boolean' - } - ], - static_attributes: [] - } - ] - }, - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } -}; -let contextBrokerMock; -let contextBrokerUnprovMock; - -describe('HTTP: Measure reception ', function() { - beforeEach(function(done) { - const provisionOptions = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices', - method: 'POST', - json: utils.readExampleFile('./test/deviceProvisioning/provisionDeviceHTTP.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - nock.cleanAll(); - - contextBrokerMock = nock('http://192.168.1.1:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - iotagentMqtt.start(config, function() { - request(provisionOptions, function(error, response, body) { - done(); - }); - }); - }); - - afterEach(function(done) { - nock.cleanAll(); - - async.series([iotAgentLib.clearAll, iotagentMqtt.stop], done); - }); - - describe('When a POST measure arrives for the HTTP binding', function() { - const optionsMeasure = { - url: 'http://localhost:' + config.http.port + '/iot/json', - method: 'POST', - json: { - humidity: '32', - temperature: '87' - }, - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - }, - qs: { - i: 'MQTT_2', - k: '1234' - } - }; - - beforeEach(function() { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/multipleMeasures.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - }); - it('should return a 200 OK with no error', function(done) { - request(optionsMeasure, function(error, result, body) { - should.not.exist(error); - result.statusCode.should.equal(200); - done(); - }); - }); - it('should send its value to the Context Broker', function(done) { - request(optionsMeasure, function(error, result, body) { - contextBrokerMock.done(); - done(); - }); - }); - }); - - describe('When a POST measure arrives with a TimeInstant attribute in the body', function() { - const optionsMeasure = { - url: 'http://localhost:' + config.http.port + '/iot/json', - method: 'POST', - json: { - humidity: '111222', - TimeInstant: '20200222T222222' - }, - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - }, - qs: { - i: 'dev0130101', - k: '1234' - } - }; - const provisionOptions = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices', - method: 'POST', - json: utils.readExampleFile('./test/deviceProvisioning/provisionDeviceTimeinstant.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - beforeEach(function(done) { - nock.cleanAll(); - - contextBrokerMock = nock('http://192.168.1.1:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/timeInstantMeasures.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/timeInstantMeasuresSuccess.json')); - - iotagentMqtt.stop(function() { - config.iota.timestamp = true; - config.compressTimestamp = false; - iotagentMqtt.start(config, function() { - request(provisionOptions, function(error, response, body) { - done(); - }); - }); - }); - }); - - afterEach(function() { - config.iota.timestamp = false; - config.compressTimestamp = true; - }); - - it('should send its value to the Context Broker', function(done) { - request(optionsMeasure, function(error, result, body) { - contextBrokerMock.done(); - done(); - }); - }); - }); - - describe('When a POST measure arrives with a TimeInstant query parameter in the body', function() { - const optionsMeasure = { - url: 'http://localhost:' + config.http.port + '/iot/json', - method: 'POST', - json: { - humidity: '111222' - }, - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - }, - qs: { - i: 'dev0130101', - k: '1234', - t: '20200222T222222' - } - }; - const provisionOptions = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices', - method: 'POST', - json: utils.readExampleFile('./test/deviceProvisioning/provisionDeviceTimeinstant.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - beforeEach(function(done) { - nock.cleanAll(); - - contextBrokerMock = nock('http://192.168.1.1:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/timeInstantMeasures.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/timeInstantMeasuresSuccess.json')); - - iotagentMqtt.stop(function() { - config.iota.timestamp = true; - config.compressTimestamp = false; - iotagentMqtt.start(config, function() { - request(provisionOptions, function(error, response, body) { - done(); - }); - }); - }); - }); - - afterEach(function() { - config.iota.timestamp = false; - config.compressTimestamp = true; - }); - - it('should send its value to the Context Broker', function(done) { - request(optionsMeasure, function(error, result, body) { - contextBrokerMock.done(); - done(); - }); - }); - }); - - describe('When a POST measure arrives for an unprovisioned device', function() { - const optionsMeasure = { - url: 'http://localhost:' + config.http.port + '/iot/json', - method: 'POST', - json: { - humidity: '32', - temperature: '87' - }, - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - }, - qs: { - i: 'MQTT_UNPROVISIONED', - k: 'KL223HHV8732SFL1' - } - }; - - beforeEach(function(done) { - contextBrokerUnprovMock = nock('http://unexistentHost:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - contextBrokerUnprovMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/unprovisionedDevice.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - request(groupCreation, function(error, response, body) { - done(); - }); - }); - - it('should send its value to the Context Broker', function(done) { - request(optionsMeasure, function(error, result, body) { - contextBrokerUnprovMock.done(); - done(); - }); - }); - it('should add a transport to the registered devices', function(done) { - const getDeviceOptions = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices', - method: 'GET', - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - }, - qs: { - i: 'MQTT_UNPROVISIONED', - k: 'KL223HHV8732SFL1' - } - }; - - request(optionsMeasure, function(error, response, body) { - request(getDeviceOptions, function(error, response, body) { - should.not.exist(error); - const parsedBody = JSON.parse(body); - - response.statusCode.should.equal(200); - should.exist(parsedBody.devices[0].transport); - parsedBody.devices[0].transport.should.equal('HTTP'); - done(); - }); - }); - }); - }); -}); diff --git a/test/unit/HTTP_reveice_measures-test2.js b/test/unit/HTTP_reveice_measures-test2.js deleted file mode 100755 index 8ace137f..00000000 --- a/test/unit/HTTP_reveice_measures-test2.js +++ /dev/null @@ -1,385 +0,0 @@ -/* - * Copyright 2016 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] - */ - -/* eslint-disable no-unused-vars */ - -const iotagentMqtt = require('../../'); -const config = require('../config-test.js'); -const nock = require('nock'); -const iotAgentLib = require('iotagent-node-lib'); -const should = require('should'); -const async = require('async'); -const request = require('request'); -const utils = require('../utils'); -const groupCreation = { - url: 'http://localhost:' + config.iota.server.port + '/iot/services', - method: 'POST', - json: { - services: [ - { - resource: '/iot/json', - apikey: 'KL223HHV8732SFL1', - entity_type: 'TheLightType', - trust: '8970A9078A803H3BL98PINEQRW8342HBAMS', - cbHost: 'http://unexistentHost:1026', - commands: [], - lazy: [], - attributes: [ - { - name: 'status', - type: 'Boolean' - } - ], - static_attributes: [] - } - ] - }, - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } -}; -let contextBrokerMock; -let contextBrokerUnprovMock; - -describe('HTTP: Measure reception ', function() { - beforeEach(function(done) { - const provisionOptions = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices', - method: 'POST', - json: utils.readExampleFile('./test/deviceProvisioning/provisionDeviceHTTP.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - nock.cleanAll(); - - contextBrokerMock = nock('http://192.168.1.1:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - iotagentMqtt.start(config, function() { - request(provisionOptions, function(error, response, body) { - done(); - }); - }); - }); - - afterEach(function(done) { - nock.cleanAll(); - - async.series([iotAgentLib.clearAll, iotagentMqtt.stop], done); - }); - - describe('When a POST multimeasure arrives for the HTTP binding', function() { - const optionsMeasure = { - url: 'http://localhost:' + config.http.port + '/iot/json', - method: 'POST', - json: [ - { - humidity: '33', - temperature: '89' - }, - { - humidity: '32', - temperature: '87' - } - ], - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - }, - qs: { - i: 'MQTT_2', - k: '1234' - } - }; - - beforeEach(function() { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/multipleMeasures2.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/multipleMeasures.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - }); - it('should return a 200 OK with no error', function(done) { - request(optionsMeasure, function(error, result, body) { - should.not.exist(error); - result.statusCode.should.equal(200); - done(); - }); - }); - it('should send its value to the Context Broker', function(done) { - request(optionsMeasure, function(error, result, body) { - contextBrokerMock.done(); - done(); - }); - }); - }); - - describe('When a POST multimeasure arrives with a TimeInstant attribute in the body', function() { - const optionsMeasure = { - url: 'http://localhost:' + config.http.port + '/iot/json', - method: 'POST', - json: [ - { - humidity: '111222', - TimeInstant: '20200222T222222' - }, - { - humidity: '111333', - TimeInstant: '20200222T222224' - } - ], - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - }, - qs: { - i: 'dev0130101', - k: '1234' - } - }; - const provisionOptions = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices', - method: 'POST', - json: utils.readExampleFile('./test/deviceProvisioning/provisionDeviceTimeinstant.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - beforeEach(function(done) { - nock.cleanAll(); - - contextBrokerMock = nock('http://192.168.1.1:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/timeInstantMeasures.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/timeInstantMeasuresSuccess.json')); - - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/timeInstantMeasures2.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/timeInstantMeasuresSuccess.json')); - - iotagentMqtt.stop(function() { - config.iota.timestamp = true; - config.compressTimestamp = false; - iotagentMqtt.start(config, function() { - request(provisionOptions, function(error, response, body) { - done(); - }); - }); - }); - }); - - afterEach(function() { - config.iota.timestamp = false; - config.compressTimestamp = true; - }); - - it('should send its value to the Context Broker', function(done) { - request(optionsMeasure, function(error, result, body) { - contextBrokerMock.done(); - done(); - }); - }); - }); - - describe('When a POST multimeasure arrives with a TimeInstant query parameter in the body', function() { - const optionsMeasure = { - url: 'http://localhost:' + config.http.port + '/iot/json', - method: 'POST', - json: [ - { - humidity: '111222' - }, - { - humidity: '111333' - } - ], - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - }, - qs: { - i: 'dev0130101', - k: '1234', - t: '20200222T222222' - } - }; - const provisionOptions = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices', - method: 'POST', - json: utils.readExampleFile('./test/deviceProvisioning/provisionDeviceTimeinstant.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - beforeEach(function(done) { - nock.cleanAll(); - - contextBrokerMock = nock('http://192.168.1.1:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/timeInstantMeasures.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/timeInstantMeasuresSuccess.json')); - - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/timeInstantMeasures3.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/timeInstantMeasuresSuccess.json')); - - iotagentMqtt.stop(function() { - config.iota.timestamp = true; - config.compressTimestamp = false; - iotagentMqtt.start(config, function() { - request(provisionOptions, function(error, response, body) { - done(); - }); - }); - }); - }); - - afterEach(function() { - config.iota.timestamp = false; - config.compressTimestamp = true; - }); - - it('should send its value to the Context Broker', function(done) { - request(optionsMeasure, function(error, result, body) { - contextBrokerMock.done(); - done(); - }); - }); - }); - - describe('When a POST multimeasure arrives for an unprovisioned device', function() { - const optionsMeasure = { - url: 'http://localhost:' + config.http.port + '/iot/json', - method: 'POST', - json: [ - { - humidity: '33', - temperature: '89' - }, - { - humidity: '32', - temperature: '87' - } - ], - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - }, - qs: { - i: 'MQTT_UNPROVISIONED', - k: 'KL223HHV8732SFL1' - } - }; - - beforeEach(function(done) { - contextBrokerUnprovMock = nock('http://unexistentHost:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - contextBrokerUnprovMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/unprovisionedDevice2.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - contextBrokerUnprovMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/unprovisionedDevice.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - request(groupCreation, function(error, response, body) { - done(); - }); - }); - - it('should send its value to the Context Broker', function(done) { - request(optionsMeasure, function(error, result, body) { - contextBrokerUnprovMock.done(); - done(); - }); - }); - it('should add a transport to the registered devices', function(done) { - const getDeviceOptions = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices', - method: 'GET', - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - }, - qs: { - i: 'MQTT_UNPROVISIONED', - k: 'KL223HHV8732SFL1' - } - }; - - request(optionsMeasure, function(error, response, body) { - request(getDeviceOptions, function(error, response, body) { - should.not.exist(error); - const parsedBody = JSON.parse(body); - - response.statusCode.should.equal(200); - should.exist(parsedBody.devices[0].transport); - parsedBody.devices[0].transport.should.equal('HTTP'); - done(); - }); - }); - }); - }); -}); diff --git a/test/unit/MQTT_commands_test.js b/test/unit/MQTT_commands_test.js deleted file mode 100644 index 68b14dbd..00000000 --- a/test/unit/MQTT_commands_test.js +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright 2016 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] - */ - -/* eslint-disable no-unused-vars */ - -const iotagentMqtt = require('../../'); -const mqtt = require('mqtt'); -const config = require('../config-test.js'); -const nock = require('nock'); -const should = require('should'); -const iotAgentLib = require('iotagent-node-lib'); -const async = require('async'); -const request = require('request'); -const utils = require('../utils'); -let contextBrokerMock; -let mqttClient; - -describe('MQTT: Commands', function() { - beforeEach(function(done) { - const provisionOptions = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices', - method: 'POST', - json: utils.readExampleFile('./test/deviceProvisioning/provisionCommand1.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - config.logLevel = 'INFO'; - - nock.cleanAll(); - - mqttClient = mqtt.connect( - 'mqtt://' + config.mqtt.host, - { - keepalive: 0, - connectTimeout: 60 * 60 * 1000 - } - ); - - mqttClient.subscribe('/1234/MQTT_2/cmd', null); - - contextBrokerMock = nock('http://192.168.1.1:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/NGSI9/registerContext') - .reply(200, utils.readExampleFile('./test/contextAvailabilityResponses/registerIoTAgent1Success.json')); - - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/updateStatus1Success.json')); - - iotagentMqtt.start(config, function() { - request(provisionOptions, function(error, response, body) { - done(); - }); - }); - }); - - afterEach(function(done) { - nock.cleanAll(); - mqttClient.unsubscribe('/1234/MQTT_2/cmd', null); - mqttClient.end(); - - async.series([iotAgentLib.clearAll, iotagentMqtt.stop], done); - }); - - describe('When a command arrive to the Agent for a device with the MQTT_UL protocol', function() { - const commandOptions = { - url: 'http://localhost:' + config.iota.server.port + '/v1/updateContext', - method: 'POST', - json: utils.readExampleFile('./test/contextRequests/updateCommand1.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - beforeEach(function() { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/updateStatus1.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/updateStatus1Success.json')); - }); - - it('should return a 200 OK without errors', function(done) { - request(commandOptions, function(error, response, body) { - should.not.exist(error); - response.statusCode.should.equal(200); - done(); - }); - }); - it('should reply with the appropriate command information', function(done) { - request(commandOptions, function(error, response, body) { - should.exist(body); - body.contextResponses['0'].statusCode.code.should.equal(200); - done(); - }); - }); - it('should update the status in the Context Broker', function(done) { - request(commandOptions, function(error, response, body) { - contextBrokerMock.done(); - done(); - }); - }); - it('should publish the command information in the MQTT topic', function(done) { - const commandMsg = '{"PING":{"data":"22"}}'; - let payload; - - mqttClient.on('message', function(topic, data) { - payload = data.toString(); - }); - - request(commandOptions, function(error, response, body) { - setTimeout(function() { - should.exist(payload); - payload.should.equal(commandMsg); - done(); - }, 100); - }); - }); - }); - - describe('When a command update arrives to the MQTT command topic', function() { - beforeEach(function() { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/updateStatus2.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/updateStatus2Success.json')); - }); - - it('should send an update request to the Context Broker', function(done) { - mqttClient.publish('/1234/MQTT_2/cmdexe', '{ "PING": "1234567890" }', null, function(error) { - setTimeout(function() { - contextBrokerMock.done(); - done(); - }, 200); - }); - }); - }); -}); diff --git a/test/unit/MQTT_receive_measures-test.js b/test/unit/MQTT_receive_measures-test.js deleted file mode 100644 index cf65e353..00000000 --- a/test/unit/MQTT_receive_measures-test.js +++ /dev/null @@ -1,306 +0,0 @@ -/* - * Copyright 2015 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] - */ - -/* eslint-disable no-unused-vars */ - -const iotagentMqtt = require('../../'); -const mqtt = require('mqtt'); -const config = require('../config-test.js'); -const nock = require('nock'); -const iotAgentLib = require('iotagent-node-lib'); -const async = require('async'); -const request = require('request'); -const utils = require('../utils'); -const groupCreation = { - url: 'http://localhost:' + config.iota.server.port + '/iot/services', - method: 'POST', - json: { - services: [ - { - resource: '/iot/json', - apikey: 'KL223HHV8732SFL1', - entity_type: 'TheLightType', - trust: '8970A9078A803H3BL98PINEQRW8342HBAMS', - cbHost: 'http://unexistentHost:1026', - commands: [], - lazy: [], - attributes: [ - { - name: 'status', - type: 'Boolean' - } - ], - static_attributes: [] - } - ] - }, - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } -}; -let contextBrokerMock; -let contextBrokerUnprovMock; -let mqttClient; - -describe('MQTT: Measure reception ', function() { - beforeEach(function(done) { - const provisionOptions = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices', - method: 'POST', - json: utils.readExampleFile('./test/deviceProvisioning/provisionDevice1.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - nock.cleanAll(); - - mqttClient = mqtt.connect( - 'mqtt://' + config.mqtt.host, - { - keepalive: 0, - connectTimeout: 60 * 60 * 1000 - } - ); - - contextBrokerMock = nock('http://192.168.1.1:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - iotagentMqtt.start(config, function() { - request(provisionOptions, function(error, response, body) { - done(); - }); - }); - }); - - afterEach(function(done) { - nock.cleanAll(); - mqttClient.end(); - - async.series([iotAgentLib.clearAll, iotagentMqtt.stop], done); - }); - - describe('When a new multiple measure arrives to the MQTT Topic', function() { - beforeEach(function() { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/multipleMeasures.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - }); - it('should send its value to the Context Broker', function(done) { - const values = { - humidity: '32', - temperature: '87' - }; - - mqttClient.publish('/json/1234/MQTT_2/attrs', JSON.stringify(values), null, function(error) { - setTimeout(function() { - contextBrokerMock.done(); - done(); - }, 100); - }); - }); - it('should send its value to the Context Broker (without leading slash)', function(done) { - const values = { - humidity: '32', - temperature: '87' - }; - - mqttClient.publish('json/1234/MQTT_2/attrs', JSON.stringify(values), null, function(error) { - setTimeout(function() { - contextBrokerMock.done(); - done(); - }, 100); - }); - }); - }); - - describe('When a new multiple measure arrives for an unprovisioned device', function() { - beforeEach(function(done) { - contextBrokerUnprovMock = nock('http://unexistentHost:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - contextBrokerUnprovMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/unprovisionedDevice.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - request(groupCreation, function(error, response, body) { - done(); - }); - }); - it('should send its value to the Context Broker', function(done) { - const values = { - humidity: '32', - temperature: '87' - }; - - mqttClient.publish( - '/json/KL223HHV8732SFL1/MQTT_UNPROVISIONED/attrs', - JSON.stringify(values), - null, - function(error) { - setTimeout(function() { - contextBrokerUnprovMock.done(); - done(); - }, 100); - } - ); - }); - it('should send its value to the Context Broker (without leading slash)', function(done) { - const values = { - humidity: '32', - temperature: '87' - }; - - mqttClient.publish('json/KL223HHV8732SFL1/MQTT_UNPROVISIONED/attrs', JSON.stringify(values), null, function( - error - ) { - setTimeout(function() { - contextBrokerUnprovMock.done(); - done(); - }, 100); - }); - }); - }); - - describe('When a new multiple measure arrives to the MQTT Topic with unknown attributes', function() { - beforeEach(function() { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/unknownMeasures.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/unknownMeasuresSuccess.json')); - }); - it('should send its value to the Context Broker', function(done) { - const values = { - humidity: '32', - weight: '87' - }; - - mqttClient.publish('/json/1234/MQTT_2/attrs', JSON.stringify(values), null, function(error) { - setTimeout(function() { - contextBrokerMock.done(); - done(); - }, 100); - }); - }); - it('should send its value to the Context Broker (without leading slash)', function(done) { - const values = { - humidity: '32', - weight: '87' - }; - - mqttClient.publish('json/1234/MQTT_2/attrs', JSON.stringify(values), null, function(error) { - setTimeout(function() { - contextBrokerMock.done(); - done(); - }, 100); - }); - }); - }); - - describe('When a new multiple measure arrives with a timestamp to the MQTT Topic', function() { - beforeEach(function() { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/timestampMeasure.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/timestampMeasureSuccess.json')); - }); - it('should send its value to the Context Broker', function(done) { - const values = { - humidity: '32', - temperature: '87', - TimeInstant: '20071103T131805' - }; - - mqttClient.publish('/json/1234/MQTT_2/attrs', JSON.stringify(values), null, function(error) { - setTimeout(function() { - contextBrokerMock.done(); - done(); - }, 100); - }); - }); - it('should send its value to the Context Broker (without leading slash)', function(done) { - const values = { - humidity: '32', - temperature: '87', - TimeInstant: '20071103T131805' - }; - - mqttClient.publish('json/1234/MQTT_2/attrs', JSON.stringify(values), null, function(error) { - setTimeout(function() { - contextBrokerMock.done(); - done(); - }, 100); - }); - }); - }); - - describe('When a new single measure arrives to the MQTT Topic', function() { - beforeEach(function() { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/singleMeasure.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/singleMeasureSuccess.json')); - }); - it('should send its values to the Context Broker', function(done) { - mqttClient.publish('/json/1234/MQTT_2/attrs/temperature', '87', null, function(error) { - setTimeout(function() { - contextBrokerMock.done(); - done(); - }, 100); - }); - }); - it('should send its values to the Context Broker (without leading slash)', function(done) { - mqttClient.publish('json/1234/MQTT_2/attrs/temperature', '87', null, function(error) { - setTimeout(function() { - contextBrokerMock.done(); - done(); - }, 100); - }); - }); - }); - - describe('When a malformed multiple measure arrives to the MQTT Topic', function() { - it('should not crash', function(done) { - mqttClient.publish('/json/MQTT_2/attrs', '{"humidity": " }(}', null, function(error) { - setTimeout(function() { - done(); - }, 100); - }); - }); - }); -}); diff --git a/test/unit/MQTT_receive_measures-test2.js b/test/unit/MQTT_receive_measures-test2.js deleted file mode 100644 index 394b685d..00000000 --- a/test/unit/MQTT_receive_measures-test2.js +++ /dev/null @@ -1,332 +0,0 @@ -/* - * Copyright 2015 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] - */ - -/* eslint-disable no-unused-vars */ - -const iotagentMqtt = require('../../lib/iotagent-json'); -const mqtt = require('mqtt'); -const config = require('../config-test.js'); -const nock = require('nock'); -const iotAgentLib = require('iotagent-node-lib'); -const async = require('async'); -const request = require('request'); -const utils = require('../utils'); -const groupCreation = { - url: 'http://localhost:' + config.iota.server.port + '/iot/services', - method: 'POST', - json: { - services: [ - { - resource: '/iot/json', - apikey: 'KL223HHV8732SFL1', - entity_type: 'TheLightType', - trust: '8970A9078A803H3BL98PINEQRW8342HBAMS', - cbHost: 'http://unexistentHost:1026', - commands: [], - lazy: [], - attributes: [ - { - name: 'status', - type: 'Boolean' - } - ], - static_attributes: [] - } - ] - }, - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } -}; -let contextBrokerMock; -let contextBrokerUnprovMock; -let mqttClient; - -describe('MQTT: Measure reception ', function () { - beforeEach(function (done) { - const provisionOptions = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices', - method: 'POST', - json: utils.readExampleFile('./test/deviceProvisioning/provisionDevice1.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - nock.cleanAll(); - - mqttClient = mqtt.connect('mqtt://' + config.mqtt.host, { - keepalive: 0, - connectTimeout: 60 * 60 * 1000 - }); - - contextBrokerMock = nock('http://192.168.1.1:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - iotagentMqtt.start(config, function () { - request(provisionOptions, function (error, response, body) { - done(); - }); - }); - }); - - afterEach(function (done) { - nock.cleanAll(); - mqttClient.end(); - - async.series([iotAgentLib.clearAll, iotagentMqtt.stop], done); - }); - - describe('When a new multiple measure arrives to the MQTT Topic', function () { - beforeEach(function () { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/multipleMeasures.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - }); - it('should send its value to the Context Broker', function (done) { - const values = [ - { - humidity: '33', - temperature: '89' - }, - { - humidity: '32', - temperature: '87' - } - ]; - - mqttClient.publish('/json/1234/MQTT_2/attrs', JSON.stringify(values), null, function (error) { - setTimeout(function () { - contextBrokerMock.done(); - done(); - }, 100); - }); - }); - it('should send its value to the Context Broker (without leading slash)', function (done) { - const values = [ - { - humidity: '33', - temperature: '89' - }, - { - humidity: '32', - temperature: '87' - } - ]; - - mqttClient.publish('json/1234/MQTT_2/attrs', JSON.stringify(values), null, function (error) { - setTimeout(function () { - contextBrokerMock.done(); - done(); - }, 100); - }); - }); - }); - - describe('When a new multiple measure arrives for an unprovisioned device', function () { - beforeEach(function (done) { - contextBrokerUnprovMock = nock('http://unexistentHost:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - contextBrokerUnprovMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/unprovisionedDevice.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - request(groupCreation, function (error, response, body) { - done(); - }); - }); - it('should send its value to the Context Broker', function (done) { - const values = [ - { - humidity: '33', - temperature: '89' - }, - { - humidity: '32', - temperature: '87' - } - ]; - - mqttClient.publish( - '/json/KL223HHV8732SFL1/MQTT_UNPROVISIONED/attrs', - JSON.stringify(values), - null, - function (error) { - setTimeout(function () { - contextBrokerUnprovMock.done(); - done(); - }, 100); - } - ); - }); - it('should send its value to the Context Broker (without leading slash)', function (done) { - const values = [ - { - humidity: '33', - temperature: '89' - }, - { - humidity: '32', - temperature: '87' - } - ]; - - mqttClient.publish( - 'json/KL223HHV8732SFL1/MQTT_UNPROVISIONED/attrs', - JSON.stringify(values), - null, - function (error) { - setTimeout(function () { - contextBrokerUnprovMock.done(); - done(); - }, 100); - } - ); - }); - }); - - describe('When a new multiple measure arrives to the MQTT Topic with unknown attributes', function () { - beforeEach(function () { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/unknownMeasures2.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/unknownMeasuresSuccess.json')); - - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/unknownMeasures.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/unknownMeasuresSuccess.json')); - }); - it('should send its value to the Context Broker', function (done) { - const values = [ - { - humidity: '32', - weight: '87' - }, - { - humidity: '33', - weight: '89' - } - ]; - - mqttClient.publish('/json/1234/MQTT_2/attrs', JSON.stringify(values), null, function (error) { - setTimeout(function () { - contextBrokerMock.done(); - done(); - }, 100); - }); - }); - it('should send its value to the Context Broker (without leading slash)', function (done) { - const values = [ - { - humidity: '32', - weight: '87' - }, - { - humidity: '33', - weight: '89' - } - ]; - - mqttClient.publish('json/1234/MQTT_2/attrs', JSON.stringify(values), null, function (error) { - setTimeout(function () { - contextBrokerMock.done(); - done(); - }, 100); - }); - }); - }); - - describe('When a new multiple measure arrives with a timestamp to the MQTT Topic', function () { - beforeEach(function () { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/timestampMeasure2.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/timestampMeasureSuccess.json')); - - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/timestampMeasure.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/timestampMeasureSuccess.json')); - }); - it('should send its value to the Context Broker', function (done) { - const values = [ - { - humidity: '33', - temperature: '89', - TimeInstant: '20071103T131805' - }, - { - humidity: '32', - temperature: '87', - TimeInstant: '20071103T131805' - } - ]; - - mqttClient.publish('/json/1234/MQTT_2/attrs', JSON.stringify(values), null, function (error) { - setTimeout(function () { - contextBrokerMock.done(); - done(); - }, 100); - }); - }); - it('should send its value to the Context Broker (without leading slash)', function (done) { - const values = [ - { - humidity: '33', - temperature: '89', - TimeInstant: '20071103T131805' - }, - { - humidity: '32', - temperature: '87', - TimeInstant: '20071103T131805' - } - ]; - - mqttClient.publish('json/1234/MQTT_2/attrs', JSON.stringify(values), null, function (error) { - setTimeout(function () { - contextBrokerMock.done(); - done(); - }, 100); - }); - }); - }); -}); diff --git a/test/unit/amqpBinding-test.js b/test/unit/amqpBinding-test.js deleted file mode 100644 index 12fd71f9..00000000 --- a/test/unit/amqpBinding-test.js +++ /dev/null @@ -1,221 +0,0 @@ -/* - * Copyright 2017 Telefonica Investigación y Desarrollo, S.A.U - * - * This file is part of iotagent-ul - * - * iotagent-ul 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-ul 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-ul. - * If not, seehttp://www.gnu.org/licenses/. - * - * For those usages not covered by the GNU Affero General Public License - * please contact with::[iot_support@tid.es] - */ - -/* eslint-disable no-unused-vars */ - -const iotagentMqtt = require('../../'); -const config = require('../config-test.js'); -const nock = require('nock'); -const async = require('async'); -const request = require('request'); -const utils = require('../utils'); -const iotAgentLib = require('iotagent-node-lib'); -const amqp = require('amqplib/callback_api'); -const apply = async.apply; -let contextBrokerMock; -let contextBrokerUnprovMock; -let amqpConn; -let oldResource; -let channel; - -function startConnection(exchange, callback) { - amqp.connect( - 'amqp://localhost', - function(err, conn) { - amqpConn = conn; - - conn.createChannel(function(err, ch) { - ch.assertExchange(exchange, 'topic', {}); - - channel = ch; - callback(err); - }); - } - ); -} - -describe('AMQP Transport binding: measures', function() { - beforeEach(function(done) { - const provisionOptions = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices', - method: 'POST', - json: utils.readExampleFile('./test/deviceProvisioning/provisionDeviceAMQP1.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - nock.cleanAll(); - - oldResource = config.iota.defaultResource; - config.iota.defaultResource = '/iot/json'; - - contextBrokerMock = nock('http://192.168.1.1:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - async.series( - [ - apply(iotagentMqtt.start, config), - apply(request, provisionOptions), - apply(startConnection, config.amqp.exchange) - ], - done - ); - }); - - afterEach(function(done) { - nock.cleanAll(); - - amqpConn.close(); - config.iota.defaultResource = oldResource; - - async.series([iotAgentLib.clearAll, iotagentMqtt.stop], done); - }); - - describe('When a new single measure arrives to a Device routing key', function() { - beforeEach(function() { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/singleMeasureAMQP.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/singleMeasureSuccess.json')); - }); - - it('should send a new update context request to the Context Broker with just that attribute', function(done) { - channel.publish(config.amqp.exchange, '.1234.MQTT_2.attrs.a', Buffer.from('23')); - - setTimeout(function() { - contextBrokerMock.done(); - done(); - }, 100); - }); - }); - - describe('When a new measure arrives for an unprovisioned Device', function() { - const groupCreation = { - url: 'http://localhost:4041/iot/services', - method: 'POST', - json: utils.readExampleFile('./test/groupProvisioning/provisionFullGroupAMQP.json'), - headers: { - 'fiware-service': 'TestService', - 'fiware-servicepath': '/testingPath' - } - }; - - beforeEach(function(done) { - contextBrokerUnprovMock = nock('http://unexistentHost:1026') - .matchHeader('fiware-service', 'TestService') - .matchHeader('fiware-servicepath', '/testingPath') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - contextBrokerUnprovMock - .matchHeader('fiware-service', 'TestService') - .matchHeader('fiware-servicepath', '/testingPath') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/unprovisionedMeasure.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/unprovisionedSuccess.json')); - - request(groupCreation, function(error, response, body) { - done(); - }); - }); - - it('should send a new update context request to the Context Broker with just that attribute', function(done) { - channel.publish(config.amqp.exchange, '.80K09H324HV8732.MQTT_UNPROVISIONED.attrs.a', Buffer.from('23')); - - setTimeout(function() { - contextBrokerUnprovMock.done(); - done(); - }, 100); - }); - }); - - describe('When a new multiple measure arrives to a Device routing key with one measure', function() { - beforeEach(function() { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/singleMeasureAMQP.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/singleMeasureSuccess.json')); - }); - - it('should send a single update context request with all the attributes', function(done) { - channel.publish(config.amqp.exchange, '.1234.MQTT_2.attrs', Buffer.from(JSON.stringify({ a: '23' }))); - - setTimeout(function() { - contextBrokerMock.done(); - done(); - }, 100); - }); - }); - - describe('When a new multiple measure arrives to a Device routing key with a faulty payload', function() { - beforeEach(function() { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/singleMeasureAMQP.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/singleMeasureSuccess.json')); - }); - - it('should silently ignore the error (without crashing)', function(done) { - channel.publish(config.amqp.exchange, '.1234.MQTT_2.attrs', Buffer.from('notAULPayload ')); - - setTimeout(function() { - done(); - }, 100); - }); - }); - - describe('When single message with multiple measures arrive to a Device routing key', function() { - beforeEach(function() { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/multipleMeasure.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - }); - - it('should send one update context per measure group to the Contet Broker', function(done) { - channel.publish( - config.amqp.exchange, - '.1234.MQTT_2.attrs', - Buffer.from( - JSON.stringify({ - a: '23', - b: '98' - }) - ) - ); - - setTimeout(function() { - contextBrokerMock.done(); - done(); - }, 100); - }); - }); -}); diff --git a/test/unit/amqpBinding-test2.js b/test/unit/amqpBinding-test2.js deleted file mode 100644 index 597ac00f..00000000 --- a/test/unit/amqpBinding-test2.js +++ /dev/null @@ -1,164 +0,0 @@ -/* - * Copyright 2017 Telefonica Investigación y Desarrollo, S.A.U - * - * This file is part of iotagent-ul - * - * iotagent-ul 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-ul 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-ul. - * If not, seehttp://www.gnu.org/licenses/. - * - * For those usages not covered by the GNU Affero General Public License - * please contact with::[iot_support@tid.es] - */ - -/* eslint-disable no-unused-vars */ - -const iotagentMqtt = require('../../lib/iotagent-json'); -const config = require('../config-test.js'); -const nock = require('nock'); -const async = require('async'); -const request = require('request'); -const utils = require('../utils'); -const iotAgentLib = require('iotagent-node-lib'); -const amqp = require('amqplib/callback_api'); -const apply = async.apply; -let contextBrokerMock; -let contextBrokerUnprovMock; -let amqpConn; -let oldResource; -let channel; - -function startConnection(exchange, callback) { - amqp.connect('amqp://localhost', function (err, conn) { - amqpConn = conn; - - conn.createChannel(function (err, ch) { - ch.assertExchange(exchange, 'topic', {}); - - channel = ch; - callback(err); - }); - }); -} - -describe('AMQP Transport binding: multiple measures', function () { - beforeEach(function (done) { - const provisionOptions = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices', - method: 'POST', - json: utils.readExampleFile('./test/deviceProvisioning/provisionDeviceAMQP1.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - nock.cleanAll(); - - oldResource = config.iota.defaultResource; - config.iota.defaultResource = '/iot/json'; - - contextBrokerMock = nock('http://192.168.1.1:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - async.series( - [ - apply(iotagentMqtt.start, config), - apply(request, provisionOptions), - apply(startConnection, config.amqp.exchange) - ], - done - ); - }); - - afterEach(function (done) { - nock.cleanAll(); - - amqpConn.close(); - config.iota.defaultResource = oldResource; - - async.series([iotAgentLib.clearAll, iotagentMqtt.stop], done); - }); - - describe('When a new multiple message arrives to a Device routing key with one measure', function () { - beforeEach(function () { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/singleMeasureAMQP.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/singleMeasureSuccess.json')); - - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/singleMeasureAMQP2.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/singleMeasureSuccess.json')); - }); - - it('should send a single update context request with all the attributes', function (done) { - channel.publish( - config.amqp.exchange, - '.1234.MQTT_2.attrs', - Buffer.from(JSON.stringify([{ a: '23' }, { a: '25' }])) - ); - - setTimeout(function () { - contextBrokerMock.done(); - done(); - }, 100); - }); - }); - - describe('When multiple message with multiple measures arrive to a Device routing key', function () { - beforeEach(function () { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/multipleMeasure.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/multipleMeasure2.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - }); - - it('should send one update context per measure group to the Contet Broker', function (done) { - channel.publish( - config.amqp.exchange, - '.1234.MQTT_2.attrs', - Buffer.from( - JSON.stringify([ - { - a: '23', - b: '98' - }, - { - a: '25', - b: '100' - } - ]) - ) - ); - - setTimeout(function () { - contextBrokerMock.done(); - done(); - }, 100); - }); - }); -}); diff --git a/test/unit/attribute-alias_test.js b/test/unit/attribute-alias_test.js deleted file mode 100644 index 1e88bae6..00000000 --- a/test/unit/attribute-alias_test.js +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright 2016 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] - */ - -/* eslint-disable no-unused-vars */ - -const iotagentMqtt = require('../../'); -const mqtt = require('mqtt'); -const config = require('../config-test.js'); -const nock = require('nock'); -const iotAgentLib = require('iotagent-node-lib'); -const async = require('async'); -const request = require('request'); -const utils = require('../utils'); -let contextBrokerMock; -let mqttClient; - -describe('Attribute alias', function() { - beforeEach(function(done) { - const provisionOptions = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices', - method: 'POST', - json: utils.readExampleFile('./test/deviceProvisioning/provisionDevice2.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - nock.cleanAll(); - - mqttClient = mqtt.connect( - 'mqtt://' + config.mqtt.host, - { - keepalive: 0, - connectTimeout: 60 * 60 * 1000 - } - ); - - contextBrokerMock = nock('http://192.168.1.1:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - iotagentMqtt.start(config, function() { - request(provisionOptions, function(error, response, body) { - done(); - }); - }); - }); - - afterEach(function(done) { - nock.cleanAll(); - mqttClient.end(); - - async.series([iotAgentLib.clearAll, iotagentMqtt.stop], done); - }); - - describe('When a new multiple measure arrives with a timestamp in an attribute alias', function() { - beforeEach(function() { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/timestampAliasMeasure.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/timestampMeasureSuccess.json')); - }); - it('should send its value to the Context Broker', function(done) { - const values = { - humidity: '32', - temperature: '87', - tt: '20071103T131805' - }; - - mqttClient.publish('/json/1234/MQTT_2/attrs', JSON.stringify(values), null, function(error) { - setTimeout(function() { - contextBrokerMock.done(); - done(); - }, 200); - }); - }); - }); -}); diff --git a/test/unit/bidirectionalityHttp-test.js b/test/unit/bidirectionalityHttp-test.js deleted file mode 100644 index 9d1e3677..00000000 --- a/test/unit/bidirectionalityHttp-test.js +++ /dev/null @@ -1,238 +0,0 @@ -/* - * Copyright 2016 Telefonica Investigación y Desarrollo, S.A.U - * - * This file is part of iotagent-ul - * - * iotagent-ul 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-ul 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-ul. - * If not, seehttp://www.gnu.org/licenses/. - * - * For those usages not covered by the GNU Affero General Public License - * please contact with::[iot_support@tid.es] - */ - -/* eslint-disable no-unused-vars */ - -const iotagentJson = require('../../'); -const config = require('../config-test.js'); -const nock = require('nock'); -const iotAgentLib = require('iotagent-node-lib'); -const should = require('should'); -const request = require('request'); -const utils = require('../utils'); -let mockedClientServer; -let contextBrokerMock; - -describe('Data Bidirectionality: HTTP', function() { - const notificationOptions = { - url: 'http://localhost:' + config.iota.server.port + '/notify', - method: 'POST', - json: utils.readExampleFile('./test/subscriptions/bidirectionalNotification.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - afterEach(function(done) { - nock.cleanAll(); - - iotAgentLib.clearAll(function() { - iotagentJson.stop(done); - }); - }); - - describe('When a bidirectional attribute is set and a new value arrives to a device without endpoint', function() { - beforeEach(function(done) { - const provisionOptions = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices', - method: 'POST', - json: utils.readExampleFile('./test/deviceProvisioning/provisionCommandBidirectional.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - nock.cleanAll(); - - contextBrokerMock = nock('http://192.168.1.1:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post( - '/v1/subscribeContext', - utils.readExampleFile('./test//subscriptions/bidirectionalSubscriptionRequest.json') - ) - .reply( - 200, - utils.readExampleFile('./test/subscriptionResponses/bidirectionalSubscriptionSuccess.json') - ); - - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post( - '/v1/updateContext', - utils.readExampleFile('./test/contextRequests/createBidirectionalDevice.json') - ) - .reply(200, utils.readExampleFile('./test/contextResponses/createBidirectionalDeviceSuccess.json')); - - contextBrokerMock = nock('http://192.168.1.1:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post( - '/v1/unsubscribeContext', - utils.readExampleFile('./test/subscriptions/simpleSubscriptionRemove.json') - ) - .reply( - 200, - utils.readExampleFile('./test/subscriptionResponses/bidirectionalSubscriptionSuccess.json') - ); - - iotagentJson.start(config, function(error) { - request(provisionOptions, function(error, response, body) { - done(); - }); - }); - }); - - it('should return a 200 OK', function(done) { - request(notificationOptions, function(error, response, body) { - should.not.exist(error); - response.statusCode.should.equal(200); - done(); - }); - }); - - it('should leave the data in the polling queue', function(done) { - request(notificationOptions, function(error, response, body) { - iotAgentLib.commandQueue('smartGondor', '/gardens', 'MQTT_2', function(error, list) { - should.not.exist(error); - - list.commands.length.should.equal(3); - done(); - }); - }); - }); - - it('should send all the data from the notification in command syntax', function(done) { - request(notificationOptions, function(error, response, body) { - iotAgentLib.commandQueue('smartGondor', '/gardens', 'MQTT_2', function(error, list) { - let latitudeFound = false; - let longitudeFound = false; - - for (let i = 0; i < list.commands.length; i++) { - if ( - list.commands[i].name === 'latitude' && - list.commands[i].type === 'string' && - list.commands[i].value === '-9.6' - ) { - latitudeFound = true; - } - - if ( - list.commands[i].name === 'longitude' && - list.commands[i].type === 'string' && - list.commands[i].value === '12.4' - ) { - longitudeFound = true; - } - } - - latitudeFound.should.equal(true); - longitudeFound.should.equal(true); - - done(); - }); - }); - }); - }); - - describe('When a bidirectional attribute is set and a new value arrives to a device with endpoint', function() { - beforeEach(function(done) { - const provisionOptions = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices', - method: 'POST', - json: utils.readExampleFile('./test/deviceProvisioning/provisionCommandBidirectionalWithUrl.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - nock.cleanAll(); - - contextBrokerMock = nock('http://192.168.1.1:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post( - '/v1/subscribeContext', - utils.readExampleFile('./test//subscriptions/bidirectionalSubscriptionRequest.json') - ) - .reply( - 200, - utils.readExampleFile('./test/subscriptionResponses/bidirectionalSubscriptionSuccess.json') - ); - - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post( - '/v1/updateContext', - utils.readExampleFile('./test/contextRequests/createBidirectionalDevice.json') - ) - .reply(200, utils.readExampleFile('./test/contextResponses/createBidirectionalDeviceSuccess.json')); - - contextBrokerMock = nock('http://192.168.1.1:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post( - '/v1/unsubscribeContext', - utils.readExampleFile('./test/subscriptions/simpleSubscriptionRemove.json') - ) - .reply( - 200, - utils.readExampleFile('./test/subscriptionResponses/bidirectionalSubscriptionSuccess.json') - ); - - mockedClientServer = nock('http://localhost:9876') - .post('/command', '{"location":"12.4, -9.6"}') - .reply(200, '') - .post('/command', '{"latitude":"-9.6"}') - .reply(200, '') - .post('/command', '{"longitude":"12.4"}') - .reply(200, ''); - - iotagentJson.start(config, function(error) { - request(provisionOptions, function(error, response, body) { - done(); - }); - }); - }); - - it('should return a 200 OK', function(done) { - request(notificationOptions, function(error, response, body) { - should.not.exist(error); - response.statusCode.should.equal(200); - done(); - }); - }); - - it('should send all the data from the notification in command syntax', function(done) { - request(notificationOptions, function(error, response, body) { - mockedClientServer.done(); - done(); - }); - }); - }); -}); diff --git a/test/unit/commandsAmqp-test.js b/test/unit/commandsAmqp-test.js deleted file mode 100644 index 0ac8327b..00000000 --- a/test/unit/commandsAmqp-test.js +++ /dev/null @@ -1,278 +0,0 @@ -/* - * Copyright 2017 Telefonica Investigación y Desarrollo, S.A.U - * - * This file is part of iotagent-ul - * - * iotagent-ul 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-ul 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-ul. - * If not, seehttp://www.gnu.org/licenses/. - * - * For those usages not covered by the GNU Affero General Public License - * please contact with::[iot_support@tid.es] - */ - -/* eslint-disable no-unused-vars */ - -const iotagentMqtt = require('../../'); -const config = require('../config-test.js'); -const nock = require('nock'); -const async = require('async'); -const request = require('request'); -const utils = require('../utils'); -const should = require('should'); -const iotAgentLib = require('iotagent-node-lib'); -const amqp = require('amqplib/callback_api'); -const apply = async.apply; -let contextBrokerMock; -let oldTransport; -let amqpConn; -let channel; - -function startConnection(exchange, callback) { - amqp.connect( - 'amqp://localhost', - function(err, conn) { - amqpConn = conn; - - conn.createChannel(function(err, ch) { - ch.assertExchange(exchange, 'topic', {}); - - channel = ch; - callback(err); - }); - } - ); -} - -describe('AMQP Transport binding: commands', function() { - beforeEach(function(done) { - const provisionOptions = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices', - method: 'POST', - json: utils.readExampleFile('./test/deviceProvisioning/provisionCommand5.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - config.logLevel = 'INFO'; - - nock.cleanAll(); - - contextBrokerMock = nock('http://192.168.1.1:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/NGSI9/registerContext') - .reply(200, utils.readExampleFile('./test/contextAvailabilityResponses/registerIoTAgent1Success.json')); - - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/updateStatus1Success.json')); - - oldTransport = config.defaultTransport; - config.defaultTransport = 'AMQP'; - - async.series( - [ - apply(iotagentMqtt.start, config), - apply(request, provisionOptions), - apply(startConnection, config.amqp.exchange) - ], - done - ); - }); - - afterEach(function(done) { - nock.cleanAll(); - - amqpConn.close(); - - config.defaultTransport = oldTransport; - - async.series([iotAgentLib.clearAll, iotagentMqtt.stop], done); - }); - - describe('When a command arrive to the Agent for a device with the AMQP protocol', function() { - const commandOptions = { - url: 'http://localhost:' + config.iota.server.port + '/v1/updateContext', - method: 'POST', - json: utils.readExampleFile('./test/contextRequests/updateCommand1.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - beforeEach(function() { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/updateStatus1.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/updateStatus1Success.json')); - }); - - it('should return a 200 OK without errors', function(done) { - request(commandOptions, function(error, response, body) { - should.not.exist(error); - response.statusCode.should.equal(200); - done(); - }); - }); - it('should reply with the appropriate command information', function(done) { - request(commandOptions, function(error, response, body) { - should.exist(body); - done(); - }); - }); - it('should update the status in the Context Broker', function(done) { - request(commandOptions, function(error, response, body) { - contextBrokerMock.done(); - done(); - }); - }); - it('should publish the command information in the AMQP topic', function(done) { - const commandMsg = '{"PING":{"data":"22"}}'; - let payload; - - channel.assertExchange(config.amqp.exchange, 'topic', config.amqp.options); - - channel.assertQueue('client-queue', { exclusive: false }, function(err, q) { - channel.bindQueue(q.queue, config.amqp.exchange, '.' + config.defaultKey + '.MQTT_2.cmd'); - - channel.consume( - q.queue, - function(msg) { - payload = msg.content.toString(); - }, - { noAck: true } - ); - - request(commandOptions, function(error, response, body) { - setTimeout(function() { - should.exist(payload); - payload.should.equal(commandMsg); - done(); - }, 1000); - }); - }); - }); - }); - - describe('When a command update arrives to the AMQP command topic', function() { - beforeEach(function() { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/updateStatus2.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/updateStatus2Success.json')); - }); - - it('should send an update request to the Context Broker', function(done) { - channel.assertExchange(config.amqp.exchange, 'topic', config.amqp.options); - channel.publish(config.amqp.exchange, '.1234.MQTT_2.cmdexe', Buffer.from('{"PING":"1234567890"}')); - - setTimeout(function() { - contextBrokerMock.done(); - done(); - }, 1000); - }); - }); - - describe('When a command update arrives with a single text value', function() { - const provisionOptionsAlt = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices', - method: 'POST', - json: utils.readExampleFile('./test/deviceProvisioning/provisionCommand6.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - const configurationOptions = { - url: 'http://localhost:' + config.iota.server.port + '/iot/services', - method: 'POST', - json: utils.readExampleFile('./test/deviceProvisioning/provisionGroup1.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - const commandOptions = { - url: 'http://localhost:' + config.iota.server.port + '/v1/updateContext', - method: 'POST', - json: utils.readExampleFile('./test/contextRequests/updateCommand3.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - beforeEach(function(done) { - nock.cleanAll(); - - contextBrokerMock = nock('http://192.168.1.1:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/NGSI9/registerContext') - .reply(200, utils.readExampleFile('./test/contextAvailabilityResponses/registerIoTAgent1Success.json')); - - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/updateStatus1Success.json')); - - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/updateStatus3.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/updateStatus1Success.json')); - - request(configurationOptions, function(error, response, body) { - request(provisionOptionsAlt, function(error, response, body) { - done(); - }); - }); - }); - - it('should publish the command information in the AMQP topic', function(done) { - const commandMsg = '{"PING":"22"}'; - let payload; - - channel.assertExchange(config.amqp.exchange, 'topic', config.amqp.options); - - channel.assertQueue('client-queue', { exclusive: false }, function(err, q) { - channel.bindQueue(q.queue, config.amqp.exchange, '.ALTERNATIVE.MQTT_4.cmd'); - - channel.consume( - q.queue, - function(msg) { - payload = msg.content.toString(); - }, - { noAck: true } - ); - - request(commandOptions, function(error, response, body) { - setTimeout(function() { - should.exist(payload); - payload.should.equal(commandMsg); - done(); - }, 1000); - }); - }); - }); - }); -}); diff --git a/test/unit/commandsPolling-test.js b/test/unit/commandsPolling-test.js deleted file mode 100644 index 49165949..00000000 --- a/test/unit/commandsPolling-test.js +++ /dev/null @@ -1,293 +0,0 @@ -/* - * Copyright 2016 Telefonica Investigación y Desarrollo, S.A.U - * - * This file is part of iotagent-ul - * - * iotagent-ul 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-ul 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-ul. - * If not, seehttp://www.gnu.org/licenses/. - * - * For those usages not covered by the GNU Affero General Public License - * please contact with::[iot_support@tid.es] - */ - -/* eslint-disable no-unused-vars */ - -const iotagentUl = require('../../'); -const config = require('../config-test.js'); -const nock = require('nock'); -const iotAgentLib = require('iotagent-node-lib'); -const should = require('should'); -const request = require('request'); -const utils = require('../utils'); -let mockedClientServer; -let contextBrokerMock; - -describe('HTTP Transport binding: polling commands', function() { - const commandOptions = { - url: 'http://localhost:' + config.iota.server.port + '/v1/updateContext', - method: 'POST', - json: utils.readExampleFile('./test/contextRequests/updateCommand1.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - beforeEach(function(done) { - const provisionOptions = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices', - method: 'POST', - json: utils.readExampleFile('./test/deviceProvisioning/provisionCommand4.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - nock.cleanAll(); - - contextBrokerMock = nock('http://192.168.1.1:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/NGSI9/registerContext') - .reply(200, utils.readExampleFile('./test/contextAvailabilityResponses/registerIoTAgent1Success.json')); - - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/updateStatus1Success.json')); - - mockedClientServer = nock('http://localhost:9876') - .post('/command', 'MQTT_2@PING|data=22') - .reply(200, 'MQTT_2@PING|1234567890'); - - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/updateStatus2Success.json')); - - iotagentUl.start(config, function(error) { - request(provisionOptions, function(error, response, body) { - done(); - }); - }); - }); - - afterEach(function(done) { - nock.cleanAll(); - - iotAgentLib.clearAll(function() { - iotagentUl.stop(done); - }); - }); - - describe('When a command arrives to the IoTA with HTTP transport and no endpoint', function() { - it('should return a 200 OK without errors', function(done) { - request(commandOptions, function(error, response, body) { - should.not.exist(error); - response.statusCode.should.equal(200); - done(); - }); - }); - - it('should be stored in the commands collection', function(done) { - request(commandOptions, function(error, response, body) { - iotAgentLib.commandQueue('smartGondor', '/gardens', 'MQTT_2', function(error, list) { - should.not.exist(error); - list.count.should.equal(1); - list.commands[0].name.should.equal('PING'); - done(); - }); - }); - }); - }); - - describe('When a device asks for the pending commands', function() { - const deviceRequest = { - url: 'http://localhost:' + config.http.port + '/iot/json', - method: 'POST', - json: { - a: 23 - }, - qs: { - i: 'MQTT_2', - k: '1234', - getCmd: 1 - } - }; - - beforeEach(function(done) { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/pollingMeasure.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/pollingMeasureSuccess.json')); - - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/updateStatus4.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/updateStatus4Success.json')); - - request(commandOptions, done); - }); - - it('should return a list of the pending commands', function(done) { - request(deviceRequest, function(error, response, body) { - should.not.exist(error); - response.statusCode.should.equal(200); - should.exist(body.PING); - should.exist(body.PING.data); - body.PING.data.should.equal('22'); - done(); - }); - }); - - it('should be marked as delivered in the Context Broker', function(done) { - request(deviceRequest, function(error, response, body) { - setTimeout(function() { - contextBrokerMock.done(); - done(); - }, 50); - }); - }); - - it('should remove them from the IoTAgent', function(done) { - request(deviceRequest, function(error, response, body) { - iotAgentLib.commandQueue('smartGondor', '/gardens', 'MQTT_2', function(error, list) { - should.not.exist(error); - list.count.should.equal(0); - done(); - }); - }); - }); - }); - - describe('When a device asks for the pending commands without body', function() { - const deviceRequest = { - url: 'http://localhost:' + config.http.port + '/iot/json', - method: 'POST', - json: { - a: 23 - }, - qs: { - i: 'MQTT_2', - k: '1234', - getCmd: 1 - } - }; - - const deviceRequestWithoutPayload = { - url: 'http://localhost:' + config.http.port + '/iot/json', - method: 'GET', - json: true, - qs: { - i: 'MQTT_2', - k: '1234', - getCmd: 1 - } - }; - - beforeEach(function(done) { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/pollingMeasure.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/pollingMeasureSuccess.json')); - - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/updateStatus4.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/updateStatus4Success.json')); - - request(commandOptions, done); - }); - - it('should return a list of the pending commands', function(done) { - request(deviceRequestWithoutPayload, function(error, response, body) { - should.not.exist(error); - response.statusCode.should.equal(200); - should.exist(body.PING); - should.exist(body.PING.data); - body.PING.data.should.equal('22'); - done(); - }); - }); - - it('should be marked as delivered in the Context Broker', function(done) { - request(deviceRequest, function(error, response, body) { - setTimeout(function() { - contextBrokerMock.done(); - done(); - }, 50); - }); - }); - - it('should remove them from the IoTAgent', function(done) { - request(deviceRequest, function(error, response, body) { - iotAgentLib.commandQueue('smartGondor', '/gardens', 'MQTT_2', function(error, list) { - should.not.exist(error); - list.count.should.equal(0); - done(); - }); - }); - }); - }); - - describe('When a device asks for the list of commands and there is more than one command', function() { - it('should retrieve the list sepparated by the "#" character'); - }); - - describe('When a device sends the result for a pending command', function() { - const commandResponse = { - uri: 'http://localhost:' + config.http.port + '/iot/json/commands', - method: 'POST', - json: { - PING: 'MADE_OK' - }, - qs: { - i: 'MQTT_2', - k: '1234' - } - }; - - beforeEach(function(done) { - contextBrokerMock - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/updateStatus5.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/updateStatus4Success.json')); - - request(commandOptions, done); - }); - - it('should update the entity in the Context Broker with the OK status and the result', function(done) { - request(commandResponse, function(error, response, body) { - contextBrokerMock.done(); - done(); - }); - }); - }); - - describe('When the device sends the result for multiple pending commands', function() { - it('should make all the updates in the Context Broker'); - }); - - describe('When a device sends measures and command responses mixed in a message', function() { - it('should send both the command updates and the measures to the Context Broker'); - }); -}); diff --git a/test/unit/configuration-api_test.js b/test/unit/configuration-api_test.js deleted file mode 100644 index 42803b29..00000000 --- a/test/unit/configuration-api_test.js +++ /dev/null @@ -1,193 +0,0 @@ -/* - * Copyright 2015 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] - */ - -/* eslint-disable no-unused-vars */ - -const iotagentMqtt = require('../../'); -const iotAgentLib = require('iotagent-node-lib'); -const mqtt = require('mqtt'); -const config = require('../config-test.js'); -const nock = require('nock'); -const should = require('should'); -const request = require('request'); -const utils = require('../utils'); -let contextBrokerMock; -let contextBrokerUnprovMock; -let iotamMock; -let mqttClient; -let originalResource; - -describe('Configuration API support', function () { - const provisionOptions = { - url: 'http://localhost:' + config.iota.server.port + '/iot/devices', - method: 'POST', - json: utils.readExampleFile('./test/deviceProvisioning/provisionDevice1.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - const configurationOptions = { - url: 'http://localhost:' + config.iota.server.port + '/iot/services', - method: 'POST', - json: utils.readExampleFile('./test/deviceProvisioning/provisionConfiguration1.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - const configurationOptionsWithResource = { - url: 'http://localhost:' + config.iota.server.port + '/iot/services', - method: 'POST', - json: utils.readExampleFile('./test/deviceProvisioning/provisionConfiguration2.json'), - headers: { - 'fiware-service': 'smartGondor', - 'fiware-servicepath': '/gardens' - } - }; - - beforeEach(function (done) { - nock.cleanAll(); - originalResource = config.iota.defaultResource; - mqttClient = mqtt.connect('mqtt://' + config.mqtt.host, { - keepalive: 0, - connectTimeout: 60 * 60 * 1000 - }); - - config.iota.iotManager = { - host: '127.0.0.1', - port: 8081, - path: '/iot/protocols', - protocol: 'TT_MQTT-JSON', - description: 'MQTT-JSON protocol for TT' - }; - - config.iota.defaultResource = '/iotamqtt'; - - iotamMock = nock('http://127.0.0.1:8081') - .post('/iot/protocols', { - protocol: 'TT_MQTT-JSON', - description: 'MQTT-JSON protocol for TT', - iotagent: 'http://localhost:4041', - resource: '/iotamqtt', - services: [] - }) - .reply(200, {}); - - contextBrokerMock = nock('http://unexistentHost:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext') - .reply(200, utils.readExampleFile('./test/contextResponses/multipleMeasuresSuccess.json')); - - iotagentMqtt.start(config, done); - }); - - afterEach(function (done) { - delete config.iota.iotManager; - delete config.iota.defaultResource; - config.iota.defaultResource = originalResource; - iotAgentLib.clearAll(); - nock.cleanAll(); - mqttClient.end(); - iotagentMqtt.stop(done); - }); - - describe('When a configuration is provisioned for a service', function () { - beforeEach(function () { - iotamMock - .post('/iot/protocols', { - protocol: 'TT_MQTT-JSON', - description: 'MQTT-JSON protocol for TT', - iotagent: 'http://localhost:4041', - resource: '/iotamqtt', - services: [ - { - apikey: '728289', - token: '8970A9078A803H3BL98PINEQRW8342HBAMS', - cbHost: 'http://unexistentHost:1026', - entity_type: 'Light', - resource: '', - service: 'smartGondor', - service_path: '/gardens' - } - ] - }) - .reply(200, {}); - - contextBrokerUnprovMock = nock('http://unexistentHost:1026') - .matchHeader('fiware-service', 'smartGondor') - .matchHeader('fiware-servicepath', '/gardens') - .post('/v1/updateContext', utils.readExampleFile('./test/contextRequests/singleMeasure.json')) - .reply(200, utils.readExampleFile('./test/contextResponses/singleMeasureSuccess.json')); - }); - - it('should use the API Key of that configuration in device topics', function (done) { - request(configurationOptions, function (error, response, body) { - request(provisionOptions, function (error, response, body) { - mqttClient.publish('/json/728289/MQTT_2/attrs/temperature', '87', null, function (error) { - setTimeout(function () { - contextBrokerUnprovMock.done(); - done(); - }, 100); - }); - }); - }); - }); - }); - - describe('When a configuration is provisioned with a Resource set', function () { - beforeEach(function () { - /*jshint camelcase:false */ - - const configurationProvision = { - protocol: 'TT_MQTT-JSON', - description: 'MQTT-JSON protocol for TT', - iotagent: 'http://localhost:4041', - resource: '/iotamqtt', - services: [ - { - apikey: '728289', - token: '8970A9078A803H3BL98PINEQRW8342HBAMS', - entity_type: 'Light', - cbHost: 'http://unexistentHost:1026', - resource: '/AnotherValue', - service: 'smartGondor', - service_path: '/gardens' - } - ] - }; - - iotamMock.post('/iot/protocols', configurationProvision).reply(200, {}); - }); - - it('should reject the configuration provisioning with a BAD FORMAT error', function (done) { - request(configurationOptionsWithResource, function (error, response, body) { - should.not.exist(error); - - response.statusCode.should.equal(400); - done(); - }); - }); - }); -}); diff --git a/test/unit/ngsiv2/HTTP_reveice_measures-test.js b/test/unit/ngsiv2/HTTP_reveice_measures-test.js index ae337597..bbad53a4 100644 --- a/test/unit/ngsiv2/HTTP_reveice_measures-test.js +++ b/test/unit/ngsiv2/HTTP_reveice_measures-test.js @@ -493,5 +493,32 @@ describe('HTTP: Measure reception ', function() { done(); }); }); + + it('should add a transport to the registered devices', function(done) { + const getDeviceOptions = { + url: 'http://localhost:' + config.iota.server.port + '/iot/devices', + method: 'GET', + headers: { + 'fiware-service': 'smartGondor', + 'fiware-servicepath': '/gardens' + }, + qs: { + i: 'JSON_UNPROVISIONED', + k: 'KL223HHV8732SFL1' + } + }; + + request(optionsMeasure, function(error, response, body) { + request(getDeviceOptions, function(error, response, body) { + should.not.exist(error); + const parsedBody = JSON.parse(body); + + response.statusCode.should.equal(200); + should.exist(parsedBody.devices[0].transport); + parsedBody.devices[0].transport.should.equal('HTTP'); + done(); + }); + }); + }); }); }); diff --git a/test/unit/ngsiv2/HTTP_reveice_measures-test2.js b/test/unit/ngsiv2/HTTP_reveice_measures-test2.js index c4b9f396..258bb34e 100755 --- a/test/unit/ngsiv2/HTTP_reveice_measures-test2.js +++ b/test/unit/ngsiv2/HTTP_reveice_measures-test2.js @@ -415,5 +415,32 @@ describe('HTTP: Measure reception ', function() { done(); }); }); + + it('should add a transport to the registered devices', function(done) { + const getDeviceOptions = { + url: 'http://localhost:' + config.iota.server.port + '/iot/devices', + method: 'GET', + headers: { + 'fiware-service': 'smartGondor', + 'fiware-servicepath': '/gardens' + }, + qs: { + i: 'JSON_UNPROVISIONED', + k: 'KL223HHV8732SFL1' + } + }; + + request(optionsMeasure, function(error, response, body) { + request(getDeviceOptions, function(error, response, body) { + should.not.exist(error); + const parsedBody = JSON.parse(body); + + response.statusCode.should.equal(200); + should.exist(parsedBody.devices[0].transport); + parsedBody.devices[0].transport.should.equal('HTTP'); + done(); + }); + }); + }); }); });