Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO NOT MERGE] Replace /iot/services to /iot/groups #340

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/services/configurationData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 9 additions & 9 deletions lib/services/configurations.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function validateListParameters(req, res, next) {
}

function translateToApi(logger, configurations) {
const services = [];
const groups = [];
const attributeList = [
'_id',
'__v',
Expand Down Expand Up @@ -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
};
}
Expand All @@ -125,7 +125,7 @@ function handleListRequest(req, res, next) {
}

function loadContextRoutes(router) {
router.get('/iot/services', [validateListParameters, handleListRequest]);
router.get('/iot/groups', [validateListParameters, handleListRequest]);
Copy link
Member

@AlvaroVega AlvaroVega Aug 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this PR adding a new route or replacing current route? Related PR telefonicaid/iotagent-node-lib#1648 was only adding a new route and deprecating (but not removing) previous one.

}

exports.loadContextRoutes = loadContextRoutes;
22 changes: 11 additions & 11 deletions lib/services/iotaRedirector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down Expand Up @@ -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];
}
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions lib/services/protocolData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -43,7 +43,7 @@ function processConfiguration(logger, protocol, description, iotagent, resource,
iotagent,
resource,
configuration,
oldConfiguration.services[0],
oldConfiguration.groups[0],
callback
);
}
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions lib/templates/protocol.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
"type": "string",
"required": true
},
"services": {
"groups": {
"type": "array",
"id": "services",
"id": "groups",
"required": false,
"items": {
"type": "object",
Expand Down
2 changes: 1 addition & 1 deletion test/examples/protocols/registrationEmpty.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"description": "A generic protocol",
"iotagent": "http://smartGondor.com/",
"resource": "/iot/d",
"services": []
"groups": []
}
2 changes: 1 addition & 1 deletion test/examples/protocols/registrationWithGroups.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A generic protocol",
"iotagent": "http://smartGondor.com/",
"resource": "/iot/d",
"services": [
"groups": [
{
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
"token": "8970A9078A803H3BL98PINEQRW8342HBAMS",
Expand Down
2 changes: 1 addition & 1 deletion test/examples/protocols/registrationWithGroupsUpdate.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion test/examples/protocols/registrationWithMissingAttrs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"description": "A generic protocol",
"iotagent": "http://smartGondor.com/",
"resource": "/iot/d",
"services": [
"groups": [
{
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
"token": "8970A9078A803H3BL98PINEQRW8342HBAMS",
Expand Down
2 changes: 1 addition & 1 deletion test/examples/protocols/registrationWithNewGroups.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A generic protocol",
"iotagent": "http://smartGondor.com/",
"resource": "/iot/d",
"services": [
"groups": [
{
"apikey": "L23123HJ01230BJ4HV87K0BMSA807898PI9H2",
"token": "90DSFLK3Y9032NEQL8970A92HBARW83403H3",
Expand Down
2 changes: 1 addition & 1 deletion test/examples/protocols/registrationWithWrongAttrs.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"iotagent": "http://smartGondor.com/",
"APIKey": "567586gyh rtygooytrdytfcy7567",
"service": "SmartGondor",
"services": [
"groups": [
{
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
"token": "8970A9078A803H3BL98PINEQRW8342HBAMS",
Expand Down
2 changes: 1 addition & 1 deletion test/examples/provisioning/getGroupList.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"services": [
"groups": [
{
"resource": "/deviceTest",
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
Expand Down
2 changes: 1 addition & 1 deletion test/examples/provisioning/postCleanGroup1.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"services": [
"groups": [
{
"resource": "/iot/d",
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
Expand Down
2 changes: 1 addition & 1 deletion test/examples/provisioning/postCleanGroup2.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"services": [
"groups": [
{
"resource": "/iot/a",
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
Expand Down
2 changes: 1 addition & 1 deletion test/examples/provisioning/postGroup.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"services": [
"groups": [
{
"resource": "/deviceTest",
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
Expand Down
2 changes: 1 addition & 1 deletion test/examples/provisioning/postGroupArray.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"services": [
"groups": [
{
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
"entity_type": "SensorMachine",
Expand Down
36 changes: 18 additions & 18 deletions test/unit/configuration-retrieval-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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'
Expand All @@ -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();
Expand All @@ -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;
}
}
Expand All @@ -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'
Expand All @@ -175,15 +175,15 @@ 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();
});
});
});

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'
Expand All @@ -197,15 +197,15 @@ 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();
});
});
});

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'
Expand All @@ -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'
Expand Down
12 changes: 6 additions & 6 deletions test/unit/iotaRedirections-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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, {});

Expand All @@ -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, {});

Expand Down
Loading