From f6a03bda0aa3dcee742d5779e0ce593cd3965508 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 19 Dec 2023 16:44:51 +0100 Subject: [PATCH] add test with group endpoint --- test/unit/ngsiv2/HTTP_commands_test.js | 163 ++++++++++++++++++ .../contextRequests/unprovisionedDevice3.json | 8 + .../contextRequests/updateCommand2.json | 15 ++ .../contextRequests/updateStatus10.json | 14 ++ .../ngsiv2/contextRequests/updateStatus9.json | 8 + 5 files changed, 208 insertions(+) create mode 100644 test/unit/ngsiv2/contextRequests/unprovisionedDevice3.json create mode 100644 test/unit/ngsiv2/contextRequests/updateCommand2.json create mode 100644 test/unit/ngsiv2/contextRequests/updateStatus10.json create mode 100644 test/unit/ngsiv2/contextRequests/updateStatus9.json diff --git a/test/unit/ngsiv2/HTTP_commands_test.js b/test/unit/ngsiv2/HTTP_commands_test.js index 6eca5f85..6df8b8b2 100644 --- a/test/unit/ngsiv2/HTTP_commands_test.js +++ b/test/unit/ngsiv2/HTTP_commands_test.js @@ -36,6 +36,35 @@ const request = utils.request; let mockedClientServer; let contextBrokerMock; +const groupCreation = { + url: 'http://localhost:' + config.iota.server.port + '/iot/services', + method: 'POST', + json: { + services: [ + { + resource: '/iot/json', + apikey: 'KL223HHV8732SFL1', + entity_type: 'TheLightType', + endpoint: 'http://localhost:9876/command', + transport: 'HTTP', + commands: [ + { + name: 'cmd1', + type: 'command' + } + ], + lazy: [], + attributes: [], + static_attributes: [] + } + ] + }, + headers: { + 'fiware-service': 'smartgondor', + 'fiware-servicepath': '/gardens' + } +}; + describe('HTTP: Commands', function () { beforeEach(function (done) { const provisionOptions = { @@ -225,3 +254,137 @@ describe('HTTP: Commands with expressions', function () { }); }); }); + +describe('HTTP: Commands from groups', function () { + beforeEach(function (done) { + config.logLevel = 'INFO'; + + nock.cleanAll(); + + contextBrokerMock = nock('http://192.168.1.1:1026') + .matchHeader('fiware-service', 'smartgondor') + .matchHeader('fiware-servicepath', '/gardens') + .post('/v2/registrations') + .reply(201, null, { Location: '/v2/registrations/6319a7f5254b05844116584d' }); + + iotagentMqtt.start(config, function () { + done(); + }); + }); + + afterEach(function (done) { + nock.cleanAll(); + async.series([iotAgentLib.clearAll, iotagentMqtt.stop], done); + }); + describe('When a POST measure arrives for an unprovisioned device in a command group', function () { + const optionsMeasure = { + url: 'http://localhost:' + config.http.port + '/iot/json', + method: 'POST', + json: { + h: '33' + }, + headers: { + 'fiware-service': 'smartgondor', + 'fiware-servicepath': '/gardens' + }, + qs: { + i: 'JSON_UNPROVISIONED', + k: 'KL223HHV8732SFL1' + } + }; + // This mock does not check the payload since the aim of the test is not to verify + // device provisioning functionality. Appropriate verification is done in tests under + // provisioning folder of iotagent-node-lib + beforeEach(function (done) { + //contextBrokerUnprovMock = nock('http://192.168.1.1:1026'); + + contextBrokerMock + .matchHeader('fiware-service', 'smartgondor') + .matchHeader('fiware-servicepath', '/gardens') + .post( + '/v2/entities?options=upsert', + utils.readExampleFile('./test/unit/ngsiv2/contextRequests/unprovisionedDevice3.json') + ) + .reply(204); + + request(groupCreation, function (error, response, body) { + done(); + }); + }); + + it('should send its value to the Context Broker', function (done) { + request(optionsMeasure, function (error, result, body) { + contextBrokerMock.done(); + done(); + }); + }); + + it('should not 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, result, body) { + request(getDeviceOptions, function (error, response, body) { + should.not.exist(error); + response.statusCode.should.equal(200); + should.not.exist(body.devices[0].transport); + 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 + '/v2/op/update', + method: 'POST', + json: utils.readExampleFile('./test/unit/ngsiv2/contextRequests/updateCommand2.json'), + headers: { + 'fiware-service': 'smartgondor', + 'fiware-servicepath': '/gardens' + } + }; + beforeEach(function () { + contextBrokerMock + .matchHeader('fiware-service', 'smartgondor') + .matchHeader('fiware-servicepath', '/gardens') + .post( + '/v2/entities?options=upsert', + utils.readExampleFile('./test/unit/ngsiv2/contextRequests/updateStatus9.json') + ) + .reply(204); + contextBrokerMock + .matchHeader('fiware-service', 'smartgondor') + .matchHeader('fiware-servicepath', '/gardens') + .post( + '/v2/entities?options=upsert', + utils.readExampleFile('./test/unit/ngsiv2/contextRequests/updateStatus10.json') + ) + .reply(204); + mockedClientServer = nock('http://localhost:9876') + .post('/command', function (body) { + return body.cmd1 || body.cmd1.data || body.cmd1.data === 22; + }) + .reply(200, '{"cmd1":{"data":"22"}}'); + }); + it('should return a 204 OK without errors', function (done) { + request(optionsMeasure, function (error, result, body) { + request(commandOptions, function (error, response, body) { + should.not.exist(error); + response.statusCode.should.equal(204); + contextBrokerMock.done(); + done(); + }); + }); + }); + }); + }); +}); diff --git a/test/unit/ngsiv2/contextRequests/unprovisionedDevice3.json b/test/unit/ngsiv2/contextRequests/unprovisionedDevice3.json new file mode 100644 index 00000000..a5493ad1 --- /dev/null +++ b/test/unit/ngsiv2/contextRequests/unprovisionedDevice3.json @@ -0,0 +1,8 @@ +{ + "id": "TheLightType:JSON_UNPROVISIONED", + "type": "TheLightType", + "h":{ + "type": "string", + "value": "33" + } +} diff --git a/test/unit/ngsiv2/contextRequests/updateCommand2.json b/test/unit/ngsiv2/contextRequests/updateCommand2.json new file mode 100644 index 00000000..4e0192c4 --- /dev/null +++ b/test/unit/ngsiv2/contextRequests/updateCommand2.json @@ -0,0 +1,15 @@ +{ + "actionType": "update", + "entities": [ + { + "id": "TheLightType:JSON_UNPROVISIONED", + "type": "TheLightType", + "cmd1": { + "type": "command", + "value":{ + "data": "22" + } + } + } + ] +} diff --git a/test/unit/ngsiv2/contextRequests/updateStatus10.json b/test/unit/ngsiv2/contextRequests/updateStatus10.json new file mode 100644 index 00000000..55de81e1 --- /dev/null +++ b/test/unit/ngsiv2/contextRequests/updateStatus10.json @@ -0,0 +1,14 @@ +{ + "id":"TheLightType:JSON_UNPROVISIONED", + "type":"TheLightType", + "cmd1_status": { + "type": "commandStatus", + "value": "OK" + }, + "cmd1_info": { + "type": "commandResult", + "value": { + "data": "22" + } + } +} diff --git a/test/unit/ngsiv2/contextRequests/updateStatus9.json b/test/unit/ngsiv2/contextRequests/updateStatus9.json new file mode 100644 index 00000000..a146e003 --- /dev/null +++ b/test/unit/ngsiv2/contextRequests/updateStatus9.json @@ -0,0 +1,8 @@ +{ + "id":"TheLightType:JSON_UNPROVISIONED", + "type":"TheLightType", + "cmd1_status": { + "type": "commandStatus", + "value": "PENDING" + } +}