Skip to content

Commit

Permalink
Add code + services tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mapedraza committed Aug 27, 2024
1 parent 9ff3655 commit 605a7bd
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 11 deletions.
15 changes: 13 additions & 2 deletions lib/services/configurations.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,25 @@ function handleListRequest(req, res, next) {
if (error) {
next(error);
} else {
res.status(200).json(translateToApi(logger, configurations));
res.status(200).json(modifyPayload(req, translateToApi(logger, configurations))); // #FIXME341 - Remove modifyPayload() usage when dropping /iot/services endpoint
}
}
);
}
// #FIXME341 - Remove this function when dropping /iot/services endpoint
function modifyPayload(req, payload) {
if (req._parsedUrl.pathname === '/iot/groups') {
if (payload.services) {
payload.groups = payload.services;
delete payload.services;
}
}
return payload;
}

function loadContextRoutes(router) {
router.get('/iot/services', [validateListParameters, handleListRequest]);
router.get('/iot/services', [validateListParameters, handleListRequest]); // #FIXME341 - Remove this function when dropping /iot/services endpoint
router.get('/iot/groups', [validateListParameters, handleListRequest]);
}

exports.loadContextRoutes = loadContextRoutes;
25 changes: 21 additions & 4 deletions lib/services/iotaRedirector.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ function guessCollection(body) {
if (body.services) {
return 'services';
} else if (body.devices) {
// #FIXME341 - Remove this part of the code when dropping /iot/services endpoint
return 'devices';
} else if (body.groups) {
return 'groups';
}
return null;
}
Expand Down Expand Up @@ -214,18 +217,27 @@ function createRequest(req, protocol, body) {

options.uri = protocolAddress + req.path;

// #FIXME341 - Remove this part of the code when dropping /iot/services endpoint
if (body && body.services) {
body.services = body.services.map(function cleanProtocol(item) {
delete item.protocol;
return item;
});
// Translate body.services

if (req.method === 'PUT') {
body = body.services[0];
}
}

if (body && body.groups) {
body.groups = body.groups.map(function cleanProtocol(item) {
delete item.protocol;
return item;
});
if (req.method === 'PUT') {
body = body.groups[0];
}
}

if (req.method === 'PUT' || req.method === 'POST') {
options.body = JSON.stringify(body);
}
Expand Down Expand Up @@ -333,7 +345,10 @@ function processRequests(req, res, next) {
if (results[0][1].devices || results[0][1].device_id) {
collectionName = 'devices';
} else if (results[0][1].services) {
// #FIXME341 - Remove this part of the code when dropping /iot/services endpoint
collectionName = 'services';
} else if (results[0][1].groups) {
collectionName = 'groups';
} else {
return null;
}
Expand Down Expand Up @@ -413,8 +428,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.put('/iot/groups', middlewareList);
router.delete('/iot/groups', middlewareList);
router.post('/iot/services', middlewareList); // #FIXME341 - Remove this part of the code when dropping /iot/services endpoint
router.post('/iot/devices', middlewareList);
router.put('/iot/services', middlewareList);
router.delete('/iot/services', middlewareList);
Expand Down
15 changes: 14 additions & 1 deletion lib/services/protocols.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,24 @@ function deleteProtocol(req, res, next) {
});
}

// #FIXME341 - Remove this function of the code when dropping /iot/services endpoint
function multipleValidation(req, res, next) {
const logger = req.logger;
const body = req.body;

if (body.groups) {
logger.debug('Validating protocol with groups');
body.services = body.groups;
delete body.groups;
}
middleware.validateJson(protocolTemplate)(req, res, next);
}

function loadContextRoutes(router) {
router.get('/iot/protocols', [readProtocolList, handleProtocolList]);

router.post('/iot/protocols', [
middleware.validateJson(protocolTemplate),
multipleValidation, // #FIXME341 - Remove calling this function when dropping /iot/services endpoint
saveProtocol,
returnProtocolCreationResponse
]);
Expand Down
36 changes: 36 additions & 0 deletions test/examples/provisioning/postGroupServices.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"services": [
{
"resource": "/deviceTest",
"apikey": "801230BJKL23Y9090DSFL123HJK09H324HV8732",
"entity_type": "SensorMachine",
"trust": "8970A9078A803H3BL98PINEQRW8342HBAMS",
"cbHost": "http://unexistentHost:1026",
"commands": [
{
"name": "wheel1",
"type": "Wheel"
}
],
"lazy": [
{
"name": "luminescence",
"type": "Lumens"
}
],
"attributes": [
{
"name": "status",
"type": "Boolean"
}
],
"static_attributes": [
{
"name": "bootstrapServer",
"type": "Address",
"value": "127.0.0.1"
}
]
}
]
}
62 changes: 61 additions & 1 deletion test/unit/configuration-retrieval-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('Configuration list', function () {
}

request(protocolRequest, function () {
setTimeout(callback, 200);
callback();
});
}

Expand Down Expand Up @@ -122,6 +122,7 @@ describe('Configuration list', function () {
});

it('should return all the available configurations for its service', function (done) {
this.retries(3); //Add retries to avoid mongodb-travis issues
request(options, function (error, response, body) {
// It should be greather than 7 but due to some mongodb-travis isses was fixed to 2
body.groups.length.should.greaterThan(2);
Expand Down Expand Up @@ -160,6 +161,65 @@ describe('Configuration list', function () {
});
});

// #FIXME341 - Remove this test when dropping /iot/services endpoint
describe('When a new configuration list request arrives to the IoTAM (SERVICES)', function () {
const options = {
url: 'http://localhost:' + iotConfig.server.port + '/iot/services',
headers: {
'fiware-service': 'smartGondor',
'fiware-servicepath': '/gardens'
},
method: 'GET'
};

it('should return a 200 OK', function (done) {
request(options, function (error, response, body) {
should.not.exist(error);
response.statusCode.should.equal(200);
done();
});
});

it('should return all the available configurations for its service', function (done) {
this.retries(3); //Add retries to avoid mongodb-travis issues
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);
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);
}

done();
});
});

it('should not return any configurations for other services', function (done) {
request(options, function (error, response, body) {
let otherServiceFound = false;

for (let i = 0; i < body.services.length; i++) {
if (body.services[i].service !== 'smartGondor') {
otherServiceFound = true;
}
}

otherServiceFound.should.equal(false);
done();
});
});
});

describe('When a configuration list request with a limit 3 arrives to the IoTAM', function () {
const options = {
url: 'http://localhost:' + iotConfig.server.port + '/iot/groups',
Expand Down
15 changes: 12 additions & 3 deletions test/unit/iotaRedirections-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,18 @@ 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/groups'],
['PUT Configuration', './test/examples/provisioning/putGroup.json', 'PUT', '/iot/groups'],
['DELETE Configuration', null, 'DELETE', '/iot/groups']
['POST Configuration (/iot/groups)', './test/examples/provisioning/postGroup.json', 'POST', '/iot/groups'],
['PUT Configuration (/iot/groups)', './test/examples/provisioning/putGroup.json', 'PUT', '/iot/groups'],
['DELETE Configuration (/iot/groups)', null, 'DELETE', '/iot/groups'],
// #FIXME341 - Remove those test cases when dropping /iot/services endpoint
[
'POST Configuration (/iot/services)',
'./test/examples/provisioning/postGroupServices.json',
'POST',
'/iot/services'
],
['PUT Configuration (/iot/services)', './test/examples/provisioning/putGroup.json', 'PUT', '/iot/services'],
['DELETE Configuration (/iot/services)', null, 'DELETE', '/iot/services']
];
const protocolRequest = {
url: 'http://localhost:' + iotConfig.server.port + '/iot/protocols',
Expand Down

0 comments on commit 605a7bd

Please sign in to comment.