From a5c168da27004c366d709c00f489e9f8340a4376 Mon Sep 17 00:00:00 2001 From: mapedraza Date: Mon, 26 Aug 2024 16:19:12 +0200 Subject: [PATCH 1/2] Fix tests using `groups` --- .../examples/protocols/registrationEmpty.json | 2 +- .../protocols/registrationWithGroups.json | 2 +- .../registrationWithGroupsUpdate.json | 2 +- .../registrationWithMissingAttrs.json | 2 +- .../protocols/registrationWithNewGroups.json | 2 +- .../protocols/registrationWithWrongAttrs.json | 2 +- .../provisioning/postCleanGroup1.json | 2 +- .../provisioning/postCleanGroup2.json | 2 +- test/examples/provisioning/postGroup.json | 2 +- .../examples/provisioning/postGroupArray.json | 2 +- test/unit/configuration-retrieval-test.js | 36 +++++++++---------- test/unit/iotaRedirections-test.js | 12 +++---- 12 files changed, 34 insertions(+), 34 deletions(-) diff --git a/test/examples/protocols/registrationEmpty.json b/test/examples/protocols/registrationEmpty.json index 27a241d..f92e4d0 100644 --- a/test/examples/protocols/registrationEmpty.json +++ b/test/examples/protocols/registrationEmpty.json @@ -3,5 +3,5 @@ "description": "A generic protocol", "iotagent": "http://smartGondor.com/", "resource": "/iot/d", - "services": [] + "groups": [] } diff --git a/test/examples/protocols/registrationWithGroups.json b/test/examples/protocols/registrationWithGroups.json index c2343a5..aecf163 100644 --- a/test/examples/protocols/registrationWithGroups.json +++ b/test/examples/protocols/registrationWithGroups.json @@ -3,7 +3,7 @@ "description": "A generic protocol", "iotagent": "http://smartGondor.com/", "resource": "/iot/d", - "services": [ + "groups": [ { "apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732", "token": "8970A9078A803H3BL98PINEQRW8342HBAMS", diff --git a/test/examples/protocols/registrationWithGroupsUpdate.json b/test/examples/protocols/registrationWithGroupsUpdate.json index 2b8a07b..38166ba 100644 --- a/test/examples/protocols/registrationWithGroupsUpdate.json +++ b/test/examples/protocols/registrationWithGroupsUpdate.json @@ -3,7 +3,7 @@ "description": "A generic protocol updated with new information", "iotagent": "http://smartGondor.com/New", "resource": "/iot/d", - "services": [ + "groups": [ { "apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732", "token": "8970A9078A803H3BL98PINEQRW8342HBAMS", diff --git a/test/examples/protocols/registrationWithMissingAttrs.json b/test/examples/protocols/registrationWithMissingAttrs.json index 2cafca2..484e7fd 100644 --- a/test/examples/protocols/registrationWithMissingAttrs.json +++ b/test/examples/protocols/registrationWithMissingAttrs.json @@ -2,7 +2,7 @@ "description": "A generic protocol", "iotagent": "http://smartGondor.com/", "resource": "/iot/d", - "services": [ + "groups": [ { "apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732", "token": "8970A9078A803H3BL98PINEQRW8342HBAMS", diff --git a/test/examples/protocols/registrationWithNewGroups.json b/test/examples/protocols/registrationWithNewGroups.json index 493ca11..f933409 100644 --- a/test/examples/protocols/registrationWithNewGroups.json +++ b/test/examples/protocols/registrationWithNewGroups.json @@ -3,7 +3,7 @@ "description": "A generic protocol", "iotagent": "http://smartGondor.com/", "resource": "/iot/d", - "services": [ + "groups": [ { "apikey": "L23123HJ01230BJ4HV87K0BMSA807898PI9H2", "token": "90DSFLK3Y9032NEQL8970A92HBARW83403H3", diff --git a/test/examples/protocols/registrationWithWrongAttrs.json b/test/examples/protocols/registrationWithWrongAttrs.json index 1c13f45..33a62c0 100644 --- a/test/examples/protocols/registrationWithWrongAttrs.json +++ b/test/examples/protocols/registrationWithWrongAttrs.json @@ -4,7 +4,7 @@ "iotagent": "http://smartGondor.com/", "APIKey": "567586gyh rtygooytrdytfcy7567", "service": "SmartGondor", - "services": [ + "groups": [ { "apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732", "token": "8970A9078A803H3BL98PINEQRW8342HBAMS", diff --git a/test/examples/provisioning/postCleanGroup1.json b/test/examples/provisioning/postCleanGroup1.json index dc9528b..86d5f95 100644 --- a/test/examples/provisioning/postCleanGroup1.json +++ b/test/examples/provisioning/postCleanGroup1.json @@ -1,5 +1,5 @@ { - "services": [ + "groups": [ { "resource": "/iot/d", "apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732", diff --git a/test/examples/provisioning/postCleanGroup2.json b/test/examples/provisioning/postCleanGroup2.json index de29e85..9b65b04 100644 --- a/test/examples/provisioning/postCleanGroup2.json +++ b/test/examples/provisioning/postCleanGroup2.json @@ -1,5 +1,5 @@ { - "services": [ + "groups": [ { "resource": "/iot/a", "apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732", diff --git a/test/examples/provisioning/postGroup.json b/test/examples/provisioning/postGroup.json index cd2a111..5764460 100644 --- a/test/examples/provisioning/postGroup.json +++ b/test/examples/provisioning/postGroup.json @@ -1,5 +1,5 @@ { - "services": [ + "groups": [ { "resource": "/deviceTest", "apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732", diff --git a/test/examples/provisioning/postGroupArray.json b/test/examples/provisioning/postGroupArray.json index f27d63e..92cfb8f 100644 --- a/test/examples/provisioning/postGroupArray.json +++ b/test/examples/provisioning/postGroupArray.json @@ -1,5 +1,5 @@ { - "services": [ + "groups": [ { "apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732", "entity_type": "SensorMachine", diff --git a/test/unit/configuration-retrieval-test.js b/test/unit/configuration-retrieval-test.js index 2ee1b48..e22652a 100644 --- a/test/unit/configuration-retrieval-test.js +++ b/test/unit/configuration-retrieval-test.js @@ -73,7 +73,7 @@ describe('Configuration list', function () { protocolRequest.headers['fiware-service'] = services[service]; protocolRequest.headers['fiware-servicepath'] = newConfiguration.service_path; - protocolRequest.json.services.push(newConfiguration); + protocolRequest.json.groups.push(newConfiguration); } } @@ -105,7 +105,7 @@ describe('Configuration list', function () { describe('When a new configuration list request arrives to the IoTAM', function () { const options = { - url: 'http://localhost:' + iotConfig.server.port + '/iot/services', + url: 'http://localhost:' + iotConfig.server.port + '/iot/groups', headers: { 'fiware-service': 'smartGondor', 'fiware-servicepath': '/gardens' @@ -124,20 +124,20 @@ describe('Configuration list', function () { it('should return all the available configurations for its service', function (done) { request(options, function (error, response, body) { // It should be greather than 7 but due to some mongodb-travis isses was fixed to 2 - body.services.length.should.greaterThan(2); + body.groups.length.should.greaterThan(2); done(); }); }); it('should map the attributes for the configurations appropriately', function (done) { request(options, function (error, response, body) { - for (let i = 0; i < body.services.length; i++) { - should.exist(body.services[i].entity_type); - should.not.exist(body.services[i].type); - should.exist(body.services[i].service_path); - should.not.exist(body.services[i].subservice); - should.exist(body.services[i].internal_attributes); - should.not.exist(body.services[i].internalAttributes); + for (let i = 0; i < body.groups.length; i++) { + should.exist(body.groups[i].entity_type); + should.not.exist(body.groups[i].type); + should.exist(body.groups[i].service_path); + should.not.exist(body.groups[i].subservice); + should.exist(body.groups[i].internal_attributes); + should.not.exist(body.groups[i].internalAttributes); } done(); @@ -148,8 +148,8 @@ describe('Configuration list', function () { request(options, function (error, response, body) { let otherServiceFound = false; - for (let i = 0; i < body.services.length; i++) { - if (body.services[i].service !== 'smartGondor') { + for (let i = 0; i < body.groups.length; i++) { + if (body.groups[i].service !== 'smartGondor') { otherServiceFound = true; } } @@ -162,7 +162,7 @@ describe('Configuration list', function () { describe('When a configuration list request with a limit 3 arrives to the IoTAM', function () { const options = { - url: 'http://localhost:' + iotConfig.server.port + '/iot/services', + url: 'http://localhost:' + iotConfig.server.port + '/iot/groups', headers: { 'fiware-service': 'smartGondor', 'fiware-servicepath': '/gardens' @@ -175,7 +175,7 @@ describe('Configuration list', function () { it('should return just 3 results', function (done) { request(options, function (error, response, body) { - body.services.length.should.equal(3); + body.groups.length.should.equal(3); done(); }); }); @@ -183,7 +183,7 @@ describe('Configuration list', function () { describe('When a configuration list request with a offset 3 arrives to the IoTAM', function () { const options = { - url: 'http://localhost:' + iotConfig.server.port + '/iot/services', + url: 'http://localhost:' + iotConfig.server.port + '/iot/groups', headers: { 'fiware-service': 'smartGondor', 'fiware-servicepath': '/gardens' @@ -197,7 +197,7 @@ describe('Configuration list', function () { it('should skip the first 3 results', function (done) { request(options, function (error, response, body) { // It should be greather than 3 but due to some mongodb-travis isses was fixed to 0 - body.services.length.should.greaterThan(-1); + body.groups.length.should.greaterThan(-1); done(); }); }); @@ -205,7 +205,7 @@ describe('Configuration list', function () { describe('When a configuration list request arrives with a wrong limit', function () { const options = { - url: 'http://localhost:' + iotConfig.server.port + '/iot/services', + url: 'http://localhost:' + iotConfig.server.port + '/iot/groups', headers: { 'fiware-service': 'smartGondor', 'fiware-servicepath': '/gardens' @@ -226,7 +226,7 @@ describe('Configuration list', function () { describe('When a configuration list request arrives with a wrong offset', function () { const options = { - url: 'http://localhost:' + iotConfig.server.port + '/iot/services', + url: 'http://localhost:' + iotConfig.server.port + '/iot/groups', headers: { 'fiware-service': 'smartGondor', 'fiware-servicepath': '/gardens' diff --git a/test/unit/iotaRedirections-test.js b/test/unit/iotaRedirections-test.js index d1d9b5b..77bef01 100644 --- a/test/unit/iotaRedirections-test.js +++ b/test/unit/iotaRedirections-test.js @@ -55,9 +55,9 @@ describe('IoTA Redirections', function () { ['DELETE Device', null, 'DELETE', '/iot/devices/devId'], ['POST Device', './test/examples/provisioning/postDevice.json', 'POST', '/iot/devices'], - ['POST Configuration', './test/examples/provisioning/postGroup.json', 'POST', '/iot/services'], - ['PUT Configuration', './test/examples/provisioning/putGroup.json', 'PUT', '/iot/services'], - ['DELETE Configuration', null, 'DELETE', '/iot/services'] + ['POST Configuration', './test/examples/provisioning/postGroup.json', 'POST', '/iot/groups'], + ['PUT Configuration', './test/examples/provisioning/putGroup.json', 'PUT', '/iot/groups'], + ['DELETE Configuration', null, 'DELETE', '/iot/groups'] ]; const protocolRequest = { url: 'http://localhost:' + iotConfig.server.port + '/iot/protocols', @@ -193,7 +193,7 @@ describe('IoTA Redirections', function () { describe('When a request arrives to the manager with an array of protocols', function () { const options = { - url: 'http://localhost:' + iotConfig.server.port + '/iot/services', + url: 'http://localhost:' + iotConfig.server.port + '/iot/groups', method: 'POST', headers: { 'fiware-service': 'smartGondor', @@ -209,7 +209,7 @@ describe('IoTA Redirections', function () { .matchHeader('fiware-servicepath', '/gardens'); agentMock - .post('/iot/services', utils.readExampleFile('./test/examples/provisioning/postCleanGroup1.json')) + .post('/iot/groups', utils.readExampleFile('./test/examples/provisioning/postCleanGroup1.json')) .query({ resource: '/iot/d', protocol: 'GENERIC_PROTOCOL' }) .reply(200, {}); @@ -218,7 +218,7 @@ describe('IoTA Redirections', function () { .matchHeader('fiware-servicepath', '/gardens'); secondAgentMock - .post('/iot/services', utils.readExampleFile('./test/examples/provisioning/postCleanGroup2.json')) + .post('/iot/groups', utils.readExampleFile('./test/examples/provisioning/postCleanGroup2.json')) .query({ resource: '/iot/a', protocol: 'ANOTHER_PROTOCOL' }) .reply(200, {}); From 838d7fd053c345389a73757ce4c5df072b378dff Mon Sep 17 00:00:00 2001 From: mapedraza Date: Mon, 26 Aug 2024 17:10:12 +0200 Subject: [PATCH 2/2] Add code fix --- lib/services/configurationData.js | 2 +- lib/services/configurations.js | 18 ++++++++-------- lib/services/iotaRedirector.js | 22 ++++++++++---------- lib/services/protocolData.js | 8 +++---- lib/templates/protocol.json | 4 ++-- test/examples/provisioning/getGroupList.json | 2 +- 6 files changed, 28 insertions(+), 28 deletions(-) diff --git a/lib/services/configurationData.js b/lib/services/configurationData.js index d9173f3..39a53b9 100644 --- a/lib/services/configurationData.js +++ b/lib/services/configurationData.js @@ -84,7 +84,7 @@ function createGetWithFields(fields) { function (error, results) { if (!error && results && results.length === 2) { callback(null, { - services: results[0], + groups: results[0], count: results[1] }); } else if (error) { diff --git a/lib/services/configurations.js b/lib/services/configurations.js index 8dcbe1f..1764adb 100644 --- a/lib/services/configurations.js +++ b/lib/services/configurations.js @@ -55,7 +55,7 @@ function validateListParameters(req, res, next) { } function translateToApi(logger, configurations) { - const services = []; + const groups = []; const attributeList = [ '_id', '__v', @@ -87,19 +87,19 @@ function translateToApi(logger, configurations) { ]; logger.debug('configurations %j', configurations); - for (let j = 0; j < configurations.services.length; j++) { - const service = {}; + for (let j = 0; j < configurations.groups.length; j++) { + const group = {}; for (let i = 0; i < attributeList.length; i++) { - service[retrievingAPITranslation[attributeList[i]] || attributeList[i]] = - configurations.services[j][attributeList[i]]; + group[retrievingAPITranslation[attributeList[i]] || attributeList[i]] = + configurations.groups[j][attributeList[i]]; } - logger.debug('translated to %j', service); - services.push(service); + logger.debug('translated to %j', group); + groups.push(group); } return { - services, + groups, count: configurations.count }; } @@ -125,7 +125,7 @@ function handleListRequest(req, res, next) { } function loadContextRoutes(router) { - router.get('/iot/services', [validateListParameters, handleListRequest]); + router.get('/iot/groups', [validateListParameters, handleListRequest]); } exports.loadContextRoutes = loadContextRoutes; diff --git a/lib/services/iotaRedirector.js b/lib/services/iotaRedirector.js index af3b003..7e59540 100644 --- a/lib/services/iotaRedirector.js +++ b/lib/services/iotaRedirector.js @@ -28,8 +28,8 @@ const _ = require('underscore'); const async = require('async'); function guessCollection(body) { - if (body.services) { - return 'services'; + if (body.groups) { + return 'groups'; } else if (body.devices) { return 'devices'; } @@ -214,15 +214,15 @@ function createRequest(req, protocol, body) { options.uri = protocolAddress + req.path; - if (body && body.services) { - body.services = body.services.map(function cleanProtocol(item) { + if (body && body.groups) { + body.groups = body.groups.map(function cleanProtocol(item) { delete item.protocol; return item; }); - // Translate body.services + // Translate body.groups if (req.method === 'PUT') { - body = body.services[0]; + body = body.groups[0]; } } @@ -332,8 +332,8 @@ function processRequests(req, res, next) { if (results[0] && results[0][1]) { if (results[0][1].devices || results[0][1].device_id) { collectionName = 'devices'; - } else if (results[0][1].services) { - collectionName = 'services'; + } else if (results[0][1].groups) { + collectionName = 'groups'; } else { return null; } @@ -414,10 +414,10 @@ function processRequests(req, res, next) { function loadContextRoutes(router) { const middlewareList = [queryParamExtractor, getProtocols, createRequests, processRequests]; - router.post('/iot/services', middlewareList); + router.post('/iot/groups', middlewareList); router.post('/iot/devices', middlewareList); - router.put('/iot/services', middlewareList); - router.delete('/iot/services', middlewareList); + router.put('/iot/groups', middlewareList); + router.delete('/iot/groups', middlewareList); router.get('/iot/devices', middlewareList); router.get('/iot/devices/:id', middlewareList); router.put('/iot/devices/:id', middlewareList); diff --git a/lib/services/protocolData.js b/lib/services/protocolData.js index 0c69742..89f4beb 100644 --- a/lib/services/protocolData.js +++ b/lib/services/protocolData.js @@ -33,7 +33,7 @@ function processConfiguration(logger, protocol, description, iotagent, resource, configurations.get(configuration.apikey, resource, protocol, function (error, oldConfiguration) { if (error) { callback(error); - } else if (oldConfiguration.services.length === 0) { + } else if (oldConfiguration.groups.length === 0) { configurations.save(logger, protocol, description, iotagent, resource, configuration, null, callback); } else { configurations.save( @@ -43,7 +43,7 @@ function processConfiguration(logger, protocol, description, iotagent, resource, iotagent, resource, configuration, - oldConfiguration.services[0], + oldConfiguration.groups[0], callback ); } @@ -131,11 +131,11 @@ function save(logger, newProtocol, callback) { apply(cleanConfigurations, logger, newProtocol.protocol, newProtocol.iotagent, newProtocol.resource) ); - if (newProtocol.services) { + if (newProtocol.groups) { actions.push( apply( async.map, - newProtocol.services, + newProtocol.groups, apply( processConfiguration, logger, diff --git a/lib/templates/protocol.json b/lib/templates/protocol.json index cc62ae8..ac36fca 100644 --- a/lib/templates/protocol.json +++ b/lib/templates/protocol.json @@ -29,9 +29,9 @@ "type": "string", "required": true }, - "services": { + "groups": { "type": "array", - "id": "services", + "id": "groups", "required": false, "items": { "type": "object", diff --git a/test/examples/provisioning/getGroupList.json b/test/examples/provisioning/getGroupList.json index 58445d6..f788864 100644 --- a/test/examples/provisioning/getGroupList.json +++ b/test/examples/provisioning/getGroupList.json @@ -1,5 +1,5 @@ { - "services": [ + "groups": [ { "resource": "/deviceTest", "apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",