diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 6d74e84c8..0d2bfb0b0 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1,3 +1,4 @@ -- Fix: ensure device apikey in already provisioned device (#1430) -- Upgrade mongodb dev dep from 4.7.0 to 4.17.0 -- Upgrade mongoose dep from 5.13.14 to 5.13.20 +- Fix: try to use apikey from measure/group to find, update, remove device in first attempt (#1426, #1435) +- Fix: ensure device apikey in already provisioned device (#1430) +- Upgrade mongodb dev dep from 4.7.0 to 4.17.0 +- Upgrade mongoose dep from 5.13.14 to 5.13.20 diff --git a/lib/fiware-iotagent-lib.js b/lib/fiware-iotagent-lib.js index e4862feaf..28603583f 100644 --- a/lib/fiware-iotagent-lib.js +++ b/lib/fiware-iotagent-lib.js @@ -302,6 +302,7 @@ exports.update = ngsi.update; exports.setCommandResult = ngsi.setCommandResult; exports.listDevices = deviceService.listDevices; exports.getDevice = deviceService.getDevice; +exports.updateDevice = deviceService.updateDevice; exports.getDeviceSilently = deviceService.getDeviceSilently; exports.getDeviceByName = deviceService.getDeviceByName; exports.getDeviceByNameAndType = deviceService.getDeviceByNameAndType; diff --git a/lib/services/commands/commandService.js b/lib/services/commands/commandService.js index b6452579e..757e4d2ba 100644 --- a/lib/services/commands/commandService.js +++ b/lib/services/commands/commandService.js @@ -120,7 +120,7 @@ function markAsExpired(command) { async.waterfall( [ - apply(deviceService.getDevice, command.deviceId, command.service, command.subservice), + apply(deviceService.getDevice, command.deviceId, null, command.service, command.subservice), getGroup, calculateTypeInformation, updateExpiredCommand diff --git a/lib/services/common/genericMiddleware.js b/lib/services/common/genericMiddleware.js index 5f632faea..2f9c0cbd9 100644 --- a/lib/services/common/genericMiddleware.js +++ b/lib/services/common/genericMiddleware.js @@ -56,7 +56,7 @@ function handleError(error, req, res, next) { * Express middleware for tracing the complete request arriving to the IoTA in debug mode. */ function traceRequest(req, res, next) { - logger.debug(context, 'Request for path [%s] from [%s]', req.path, req.get('host')); + logger.debug(context, 'Request for path [%s] query [%j] from [%s]', req.path, req.query, req.get('host')); if (req.is('json') || req.is('application/ld+json')) { logger.debug(context, 'Body:\n\n%s\n\n', JSON.stringify(req.body, null, 4)); diff --git a/lib/services/devices/deviceRegistryMemory.js b/lib/services/devices/deviceRegistryMemory.js index 8227620fe..7f7408dd0 100644 --- a/lib/services/devices/deviceRegistryMemory.js +++ b/lib/services/devices/deviceRegistryMemory.js @@ -71,7 +71,7 @@ function storeDevice(newDevice, callback) { * @param {String} service Service of the device to remove. * @param {String} subservice Subservice inside the service for the removed device. */ -function removeDevice(id, service, subservice, callback) { +function removeDevice(id, apikey, service, subservice, callback) { const services = Object.keys(registeredDevices); for (let i = 0; i < services.length; i++) { @@ -141,7 +141,7 @@ function listDevices(type, service, subservice, limit, offset, callback) { }); } -function getDevice(id, service, subservice, callback) { +function getDevice(id, apikey, service, subservice, callback) { if (registeredDevices[service] && registeredDevices[service][id]) { callback(null, registeredDevices[service][id]); } else { diff --git a/lib/services/devices/deviceRegistryMongoDB.js b/lib/services/devices/deviceRegistryMongoDB.js index 8e6164904..be92335bb 100644 --- a/lib/services/devices/deviceRegistryMongoDB.js +++ b/lib/services/devices/deviceRegistryMongoDB.js @@ -119,13 +119,15 @@ function storeDevice(newDevice, callback) { * @param {String} service Service of the device to remove. * @param {String} subservice Subservice inside the service for the removed device. */ -function removeDevice(id, service, subservice, callback) { +function removeDevice(id, apikey, service, subservice, callback) { const condition = { id, service, subservice }; - + if (apikey) { + condition.apikey = apikey; + } logger.debug(context, 'Removing device with id [%s]', id); Device.model.deleteOne(condition, function (error) { @@ -211,17 +213,21 @@ function findOneInMongoDB(queryParams, id, callback) { * Internal function used to find a device in the DB. * * @param {String} id ID of the Device to find. + * @param {String} Apikey Apikey of the Device to find. * @param {String} service Service the device belongs to (optional). * @param {String} subservice Division inside the service (optional). */ -function getDeviceById(id, service, subservice, callback) { - const queryParams = { +function getDeviceById(id, apikey, service, subservice, callback) { + let queryParams = { id, service, subservice }; + if (apikey) { + queryParams.apikey = apikey; + } context = fillService(context, queryParams); - logger.debug(context, 'Looking for device with id [%s].', id); + logger.debug(context, 'Looking for device with id [%s] and queryParams [%j].', id, queryParams); findOneInMongoDB(queryParams, id, callback); } @@ -232,10 +238,17 @@ function getDeviceById(id, service, subservice, callback) { * @param {String} service Service the device belongs to. * @param {String} subservice Division inside the service. */ -function getDevice(id, service, subservice, callback) { - getDeviceById(id, service, subservice, function (error, data) { +function getDevice(id, apikey, service, subservice, callback) { + getDeviceById(id, apikey, service, subservice, function (error, data) { if (error) { - callback(error); + // Try without apikey: apikey will be added to device later + getDeviceById(id, null, service, subservice, function (error, data) { + if (error) { + callback(error); + } else { + callback(null, data); + } + }); } else { callback(null, data); } @@ -284,7 +297,7 @@ function getByName(name, service, servicepath, callback) { */ function update(device, callback) { logger.debug(context, 'Storing updated values for device [%s]:\n%s', device.id, JSON.stringify(device, null, 4)); - getDeviceById(device.id, device.service, device.subservice, function (error, data) { + getDevice(device.id, device.apikey, device.service, device.subservice, function (error, data) { if (error) { callback(error); } else { diff --git a/lib/services/devices/deviceService.js b/lib/services/devices/deviceService.js index d8fdaac1f..f6768944e 100644 --- a/lib/services/devices/deviceService.js +++ b/lib/services/devices/deviceService.js @@ -253,6 +253,7 @@ function registerDevice(deviceObj, callback) { function checkDuplicates(deviceObj, innerCb) { config.getRegistry().getSilently( deviceObj.id, + deviceObj.apikey, deviceObj.service, deviceObj.subservice, /* eslint-disable-next-line no-unused-vars */ @@ -419,7 +420,7 @@ function removeAllSubscriptions(device, callback) { * @param {String} service Service of the device to unregister. * @param {String} subservice Subservice inside the service for the unregisterd device. */ -function unregisterDevice(id, service, subservice, callback) { +function unregisterDevice(id, apikey, service, subservice, callback) { function processContextUnregister(body, innerCallback) { innerCallback(null); } @@ -428,9 +429,9 @@ function unregisterDevice(id, service, subservice, callback) { innerCallback(null); } - logger.debug(context, 'Removing device register in Device Service'); + logger.debug(context, 'Removing device %j %j %j %j register in Device Service', id, apikey, service, subservice); - config.getRegistry().get(id, service, subservice, function (error, device) { + config.getRegistry().get(id, apikey, service, subservice, function (error, device) { if (error) { callback(error); } else { @@ -454,7 +455,7 @@ function unregisterDevice(id, service, subservice, callback) { processUnsubscribes, apply(registrationUtils.sendRegistrations, true, mergedDevice), processContextUnregister, - apply(config.getRegistry().remove, id, service, subservice) + apply(config.getRegistry().remove, id, apikey, service, subservice) ], callback ); @@ -529,8 +530,17 @@ function listDevices(service, subservice, limit, offset, callback) { * @param {String} service Service for which the requested device. * @param {String} subservice Subservice inside the service for which the device is requested. */ -function getDevice(deviceId, service, subservice, callback) { - config.getRegistry().get(deviceId, service, subservice, callback); +function getDevice(deviceId, apikey, service, subservice, callback) { + config.getRegistry().get(deviceId, apikey, service, subservice, callback); +} + +/** + * Update a device from the device registry. + * + * @param {String} device JSON object contain the device to update. + */ +function updateDevice(device, callback) { + config.getRegistry().update(device, callback); } /** @@ -540,8 +550,8 @@ function getDevice(deviceId, service, subservice, callback) { * @param {String} service Service for which the requested device. * @param {String} subservice Subservice inside the service for which the device is requested. */ -function getDeviceSilently(deviceId, service, subservice, callback) { - config.getRegistry().getSilently(deviceId, service, subservice, callback); +function getDeviceSilently(deviceId, apikey, service, subservice, callback) { + config.getRegistry().getSilently(deviceId, apikey, service, subservice, callback); } /** @@ -608,8 +618,8 @@ function checkRegistry(fn) { }; } -function findOrCreate(deviceId, group, callback) { - getDeviceSilently(deviceId, group.service, group.subservice, function (error, device) { +function findOrCreate(deviceId, apikey, group, callback) { + getDeviceSilently(deviceId, apikey, group.service, group.subservice, function (error, device) { if (!error && device) { if ( (!('apikey' in device) || device.apikey === undefined) && @@ -618,11 +628,12 @@ function findOrCreate(deviceId, group, callback) { ) { logger.info(context, 'Update provisioned device %j with measure/group apikey %j', device, group.apikey); device.apikey = group.apikey; // group apikey is the same of current measure apikey - updateRegisterDevice(device, function (error, device) { + updateDevice(device, function (error) { callback(error, device, group); }); + } else { + callback(null, device, group); } - callback(null, device, group); } else if (error.name === 'DEVICE_NOT_FOUND') { const newDevice = { id: deviceId, @@ -689,7 +700,7 @@ function retrieveDevice(deviceId, apiKey, callback) { async.waterfall( [ apply(groupService.get, config.getConfig().defaultResource || '', apiKey), - apply(findOrCreate, deviceId), + apply(findOrCreate, deviceId, apiKey), apply( mergeDeviceWithConfiguration, ['lazy', 'active', 'staticAttributes', 'commands', 'subscriptions'], @@ -704,6 +715,7 @@ function retrieveDevice(deviceId, apiKey, callback) { exports.listDevices = intoTrans(context, checkRegistry)(listDevices); exports.listDevicesWithType = intoTrans(context, checkRegistry)(listDevicesWithType); exports.getDevice = intoTrans(context, checkRegistry)(getDevice); +exports.updateDevice = intoTrans(context, checkRegistry)(updateDevice); exports.getDeviceSilently = intoTrans(context, checkRegistry)(getDeviceSilently); exports.getDevicesByAttribute = intoTrans(context, checkRegistry)(getDevicesByAttribute); exports.getDeviceByName = intoTrans(context, checkRegistry)(getDeviceByName); diff --git a/lib/services/devices/devices-NGSI-LD.js b/lib/services/devices/devices-NGSI-LD.js index dd962f188..36f131601 100644 --- a/lib/services/devices/devices-NGSI-LD.js +++ b/lib/services/devices/devices-NGSI-LD.js @@ -369,7 +369,13 @@ function updateRegisterDeviceNgsiLD(deviceObj, entityInfoUpdated, callback) { if (entityInfoUpdated) { async.waterfall( [ - apply(config.getRegistry().get, deviceObj.id, deviceObj.service, deviceObj.subservice), + apply( + config.getRegistry().get, + deviceObj.id, + deviceObj.apikey, + deviceObj.service, + deviceObj.subservice + ), apply(extractDeviceDifference, deviceObj), createInitialEntityNgsiLD, apply(combineWithNewDevice, deviceObj), @@ -382,7 +388,13 @@ function updateRegisterDeviceNgsiLD(deviceObj, entityInfoUpdated, callback) { } else { async.waterfall( [ - apply(config.getRegistry().get, deviceObj.id, deviceObj.service, deviceObj.subservice), + apply( + config.getRegistry().get, + deviceObj.id, + deviceObj.apikey, + deviceObj.service, + deviceObj.subservice + ), apply(extractDeviceDifference, deviceObj), updateEntityNgsiLD, apply(combineWithNewDevice, deviceObj), diff --git a/lib/services/devices/devices-NGSI-v2.js b/lib/services/devices/devices-NGSI-v2.js index 1ebc7f4cd..90e47ed35 100644 --- a/lib/services/devices/devices-NGSI-v2.js +++ b/lib/services/devices/devices-NGSI-v2.js @@ -424,7 +424,13 @@ function updateRegisterDeviceNgsi2(deviceObj, entityInfoUpdated, callback) { if (entityInfoUpdated) { async.waterfall( [ - apply(config.getRegistry().get, deviceObj.id, deviceObj.service, deviceObj.subservice), + apply( + config.getRegistry().get, + deviceObj.id, + deviceObj.apikey, + deviceObj.service, + deviceObj.subservice + ), apply(extractDeviceDifference, deviceObj), createInitialEntityNgsi2, apply(combineWithNewDevice, deviceObj), @@ -437,7 +443,13 @@ function updateRegisterDeviceNgsi2(deviceObj, entityInfoUpdated, callback) { } else { async.waterfall( [ - apply(config.getRegistry().get, deviceObj.id, deviceObj.service, deviceObj.subservice), + apply( + config.getRegistry().get, + deviceObj.id, + deviceObj.apikey, + deviceObj.service, + deviceObj.subservice + ), apply(extractDeviceDifference, deviceObj), updateEntityNgsi2, apply(combineWithNewDevice, deviceObj), diff --git a/lib/services/groups/groupService.js b/lib/services/groups/groupService.js index 89a1db5f4..adea091cc 100644 --- a/lib/services/groups/groupService.js +++ b/lib/services/groups/groupService.js @@ -172,7 +172,7 @@ function remove(service, subservice, resource, apikey, device, callback) { } function unregisterDevice(device, cb) { - deviceService.unregister(device.id, service, subservice, function (error) { + deviceService.unregister(device.id, device.apikey, service, subservice, function (error) { if (error) { cb(error); } diff --git a/lib/services/northBound/deviceProvisioningServer.js b/lib/services/northBound/deviceProvisioningServer.js index 4003d6dd8..9a179d6b5 100644 --- a/lib/services/northBound/deviceProvisioningServer.js +++ b/lib/services/northBound/deviceProvisioningServer.js @@ -251,6 +251,7 @@ function handleListDevices(req, res, next) { function handleGetDevice(req, res, next) { deviceService.getDevice( req.params.deviceId, + req.query.apikey, req.headers['fiware-service'], req.headers['fiware-servicepath'], function (error, device) { @@ -269,8 +270,8 @@ function handleGetDevice(req, res, next) { * This middleware handles the removal of a particular device specified with the deviceId. */ function handleRemoveDevice(req, res, next) { - function getDevice(deviceId, service, subservice, callback) { - deviceService.getDevice(deviceId, service, subservice, function (error, device) { + function getDevice(deviceId, apikey, service, subservice, callback) { + deviceService.getDevice(deviceId, apikey, service, subservice, function (error, device) { if (error) { callback(error); } else if (device) { @@ -289,18 +290,25 @@ function handleRemoveDevice(req, res, next) { } } - function unregisterDevice(deviceId, service, subservice, device, callback) { - return deviceService.unregister(deviceId, service, subservice, callback); + function unregisterDevice(deviceId, apikey, service, subservice, device, callback) { + return deviceService.unregister(deviceId, apikey, service, subservice, callback); } async.waterfall( [ apply(statsRegistry.add, 'deviceRemovalRequests', 1), - apply(getDevice, req.params.deviceId, req.headers['fiware-service'], req.headers['fiware-servicepath']), + apply( + getDevice, + req.params.deviceId, + req.query.apikey, + req.headers['fiware-service'], + req.headers['fiware-servicepath'] + ), applyRemoveDeviceHandler, apply( unregisterDevice, req.params.deviceId, + req.query.apikey, req.headers['fiware-service'], req.headers['fiware-servicepath'] ) @@ -334,6 +342,7 @@ function handleUpdateDevice(req, res, next) { } else { deviceService.getDevice( req.params.deviceId, + req.query.apikey, req.headers['fiware-service'], req.headers['fiware-servicepath'], function (error, device) { diff --git a/test/unit/general/contextBrokerKeystoneSecurityAccess-test.js b/test/unit/general/contextBrokerKeystoneSecurityAccess-test.js index af0641c19..165333f74 100644 --- a/test/unit/general/contextBrokerKeystoneSecurityAccess-test.js +++ b/test/unit/general/contextBrokerKeystoneSecurityAccess-test.js @@ -293,7 +293,7 @@ describe('NGSI-v2 - Secured access to the Context Broker with Keystone', functio }); it('subscribe requests use auth header', function (done) { - iotAgentLib.getDevice('Light1', 'smartgondor', 'electricity', function (error, device) { + iotAgentLib.getDevice('Light1', null, 'smartgondor', 'electricity', function (error, device) { iotAgentLib.subscribe(device, ['dimming'], null, function (error) { should.not.exist(error); @@ -314,7 +314,7 @@ describe('NGSI-v2 - Secured access to the Context Broker with Keystone', functio 'X-Subject-Token': '12345679ABCDEF' }); - iotAgentLib.getDevice('Light1', 'smartgondor', 'electricity', function (error, device) { + iotAgentLib.getDevice('Light1', null, 'smartgondor', 'electricity', function (error, device) { iotAgentLib.subscribe(device, ['dimming'], null, function (error) { iotAgentLib.unsubscribe(device, '51c0ac9ed714fb3b37d7d5a8', function (error) { contextBrokerMock.done(); diff --git a/test/unit/mongodb/mongodb-registry-test.js b/test/unit/mongodb/mongodb-registry-test.js index cc2f2a992..01b64f6a1 100644 --- a/test/unit/mongodb/mongodb-registry-test.js +++ b/test/unit/mongodb/mongodb-registry-test.js @@ -387,7 +387,7 @@ describe('NGSI-v2 - MongoDB Device Registry', function () { }); it('should be removed from MongoDB', function (done) { - iotAgentLib.unregister(device1.id, 'smartgondor', 'gardens', function (error) { + iotAgentLib.unregister(device1.id, null, 'smartgondor', 'gardens', function (error) { iotAgentDb .db() .collection('devices') diff --git a/test/unit/ngsi-ld/general/contextBrokerOAuthSecurityAccess-test.js b/test/unit/ngsi-ld/general/contextBrokerOAuthSecurityAccess-test.js index 8e248ced3..43be63c8f 100644 --- a/test/unit/ngsi-ld/general/contextBrokerOAuthSecurityAccess-test.js +++ b/test/unit/ngsi-ld/general/contextBrokerOAuthSecurityAccess-test.js @@ -318,7 +318,7 @@ describe('NGSI-LD - Secured access to the Context Broker with OAuth2 provider', }); it('subscribe requests use auth header', function (done) { - iotAgentLib.getDevice('Light1', 'smartgondor', 'electricity', function (error, device) { + iotAgentLib.getDevice('Light1', null, 'smartgondor', 'electricity', function (error, device) { iotAgentLib.subscribe(device, ['dimming'], null, function (error) { should.not.exist(error); @@ -339,7 +339,7 @@ describe('NGSI-LD - Secured access to the Context Broker with OAuth2 provider', contextBrokerMock.delete('/ngsi-ld/v1/subscriptions/51c0ac9ed714fb3b37d7d5a8', '').reply(204); - iotAgentLib.getDevice('Light1', 'smartgondor', 'electricity', function (error, device) { + iotAgentLib.getDevice('Light1', null, 'smartgondor', 'electricity', function (error, device) { iotAgentLib.subscribe(device, ['dimming'], null, function (error) { iotAgentLib.unsubscribe(device, '51c0ac9ed714fb3b37d7d5a8', function (error) { contextBrokerMock.done(); diff --git a/test/unit/ngsi-ld/general/https-support-test.js b/test/unit/ngsi-ld/general/https-support-test.js index fb6221091..a48da7d37 100644 --- a/test/unit/ngsi-ld/general/https-support-test.js +++ b/test/unit/ngsi-ld/general/https-support-test.js @@ -213,7 +213,7 @@ describe('NGSI-LD - HTTPS support tests', function () { }); it('should send the appropriate request to the Context Broker', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { should.not.exist(error); diff --git a/test/unit/ngsi-ld/ngsiService/subscriptions-test.js b/test/unit/ngsi-ld/ngsiService/subscriptions-test.js index 25c8baceb..069457254 100644 --- a/test/unit/ngsi-ld/ngsiService/subscriptions-test.js +++ b/test/unit/ngsi-ld/ngsiService/subscriptions-test.js @@ -103,7 +103,7 @@ describe('NGSI-LD - Subscription tests', function () { describe('When a client invokes the subscribe() function for device', function () { it('should send the appropriate request to the Context Broker', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { should.not.exist(error); @@ -114,9 +114,9 @@ describe('NGSI-LD - Subscription tests', function () { }); }); it('should store the subscription ID in the Device Registry', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { should.not.exist(error); should.exist(device); should.exist(device.subscriptions); @@ -139,10 +139,10 @@ describe('NGSI-LD - Subscription tests', function () { done(); }); it('should delete the subscription from the CB', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { iotAgentLib.unsubscribe(device, '51c0ac9ed714fb3b37d7d5a8', function (error) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { contextBrokerMock.done(); done(); }); @@ -151,10 +151,10 @@ describe('NGSI-LD - Subscription tests', function () { }); }); it('should remove the id from the subscriptions array', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { iotAgentLib.unsubscribe(device, '51c0ac9ed714fb3b37d7d5a8', function (error) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { should.not.exist(error); should.exist(device); should.exist(device.subscriptions); @@ -177,9 +177,9 @@ describe('NGSI-LD - Subscription tests', function () { }); it('should delete the subscription from the CB', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { - iotAgentLib.unregister(device.id, 'smartgondor', '/gardens', function (error) { + iotAgentLib.unregister(device.id, null, 'smartgondor', '/gardens', function (error) { contextBrokerMock.done(); done(); }); @@ -189,7 +189,7 @@ describe('NGSI-LD - Subscription tests', function () { }); describe('When a new notification comes to the IoTAgent', function () { beforeEach(function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { done(); }); diff --git a/test/unit/ngsi-ld/provisioning/device-registration_test.js b/test/unit/ngsi-ld/provisioning/device-registration_test.js index d93e78517..6c979ebfd 100644 --- a/test/unit/ngsi-ld/provisioning/device-registration_test.js +++ b/test/unit/ngsi-ld/provisioning/device-registration_test.js @@ -222,7 +222,7 @@ describe('NGSI-LD - IoT Agent Device Registration', function () { it("should return all the device's information", function (done) { iotAgentLib.register(device1, function (error) { - iotAgentLib.getDevice('light1', 'smartgondor', 'gardens', function (error, data) { + iotAgentLib.getDevice('light1', null, 'smartgondor', 'gardens', function (error, data) { should.not.exist(error); should.exist(data); data.type.should.equal('Light'); @@ -252,7 +252,7 @@ describe('NGSI-LD - IoT Agent Device Registration', function () { it('should return a ENTITY_NOT_FOUND error', function (done) { iotAgentLib.register(device1, function (error) { - iotAgentLib.getDevice('lightUnexistent', 'smartgondor', 'gardens', function (error, data) { + iotAgentLib.getDevice('lightUnexistent', null, 'smartgondor', 'gardens', function (error, data) { should.exist(error); should.not.exist(data); error.code.should.equal(404); @@ -301,7 +301,7 @@ describe('NGSI-LD - IoT Agent Device Registration', function () { }); it('should update the devices information in Context Broker', function (done) { - iotAgentLib.unregister(device1.id, 'smartgondor', 'gardens', function (error) { + iotAgentLib.unregister(device1.id, null, 'smartgondor', 'gardens', function (error) { should.not.exist(error); contextBrokerMock.done(); done(); @@ -346,7 +346,7 @@ describe('NGSI-LD - IoT Agent Device Registration', function () { it('should not remove the device from the internal registry'); it('should return a UNREGISTRATION_ERROR error to the caller', function (done) { - iotAgentLib.unregister(device1.id, 'smartgondor', 'gardens', function (error) { + iotAgentLib.unregister(device1.id, null, 'smartgondor', 'gardens', function (error) { should.exist(error); should.exist(error.name); error.name.should.equal('UNREGISTRATION_ERROR'); diff --git a/test/unit/ngsi-ld/provisioning/device-update-registration_test.js b/test/unit/ngsi-ld/provisioning/device-update-registration_test.js index ff2ede4bf..306dad6b2 100644 --- a/test/unit/ngsi-ld/provisioning/device-update-registration_test.js +++ b/test/unit/ngsi-ld/provisioning/device-update-registration_test.js @@ -202,7 +202,7 @@ describe('NGSI-LD - IoT Agent Device Update Registration', function () { }); it('should store the new values in the registry', function (done) { iotAgentLib.updateRegister(deviceUpdated, false, function (error, data) { - iotAgentLib.getDevice(deviceUpdated.id, 'smartgondor', 'gardens', function (error, deviceResult) { + iotAgentLib.getDevice(deviceUpdated.id, null, 'smartgondor', 'gardens', function (error, deviceResult) { should.not.exist(error); should.exist(deviceResult); deviceResult.internalId.should.equal(deviceUpdated.internalId); @@ -247,6 +247,7 @@ describe('NGSI-LD - IoT Agent Device Update Registration', function () { iotAgentLib.updateRegister(deviceCommandUpdated, false, function (error, data) { iotAgentLib.getDevice( deviceCommandUpdated.id, + null, 'smartgondor', 'gardens', function (error, deviceResult) { diff --git a/test/unit/ngsiv2/general/contextBrokerOAuthSecurityAccess-test.js b/test/unit/ngsiv2/general/contextBrokerOAuthSecurityAccess-test.js index 14388b028..87f492189 100644 --- a/test/unit/ngsiv2/general/contextBrokerOAuthSecurityAccess-test.js +++ b/test/unit/ngsiv2/general/contextBrokerOAuthSecurityAccess-test.js @@ -310,7 +310,7 @@ describe('NGSI-v2 - Secured access to the Context Broker with OAuth2 provider', }); it('subscribe requests use auth header', function (done) { - iotAgentLib.getDevice('Light1', 'smartgondor', 'electricity', function (error, device) { + iotAgentLib.getDevice('Light1', null, 'smartgondor', 'electricity', function (error, device) { iotAgentLib.subscribe(device, ['dimming'], null, function (error) { should.not.exist(error); @@ -331,7 +331,7 @@ describe('NGSI-v2 - Secured access to the Context Broker with OAuth2 provider', contextBrokerMock.delete('/v2/subscriptions/51c0ac9ed714fb3b37d7d5a8', '').reply(204); - iotAgentLib.getDevice('Light1', 'smartgondor', 'electricity', function (error, device) { + iotAgentLib.getDevice('Light1', null, 'smartgondor', 'electricity', function (error, device) { iotAgentLib.subscribe(device, ['dimming'], null, function (error) { iotAgentLib.unsubscribe(device, '51c0ac9ed714fb3b37d7d5a8', function (error) { contextBrokerMock.done(); diff --git a/test/unit/ngsiv2/general/https-support-test.js b/test/unit/ngsiv2/general/https-support-test.js index 3f3ac93be..d92b9daa9 100644 --- a/test/unit/ngsiv2/general/https-support-test.js +++ b/test/unit/ngsiv2/general/https-support-test.js @@ -210,7 +210,7 @@ describe('NGSI-v2 - HTTPS support tests', function () { }); it('should send the appropriate request to the Context Broker', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { should.not.exist(error); diff --git a/test/unit/ngsiv2/ngsiService/subscriptions-test.js b/test/unit/ngsiv2/ngsiService/subscriptions-test.js index d8d1fd3a7..c971a95d8 100644 --- a/test/unit/ngsiv2/ngsiService/subscriptions-test.js +++ b/test/unit/ngsiv2/ngsiService/subscriptions-test.js @@ -95,7 +95,7 @@ describe('NGSI-v2 - Subscription tests', function () { describe('When a client invokes the subscribe() function for device', function () { it('should send the appropriate request to the Context Broker', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { should.not.exist(error); @@ -106,9 +106,9 @@ describe('NGSI-v2 - Subscription tests', function () { }); }); it('should store the subscription ID in the Device Registry', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { should.not.exist(error); should.exist(device); should.exist(device.subscriptions); @@ -132,10 +132,10 @@ describe('NGSI-v2 - Subscription tests', function () { done(); }); it('should delete the subscription from the CB', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { iotAgentLib.unsubscribe(device, '51c0ac9ed714fb3b37d7d5a8', function (error) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { contextBrokerMock.done(); done(); }); @@ -144,10 +144,10 @@ describe('NGSI-v2 - Subscription tests', function () { }); }); it('should remove the id from the subscriptions array', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { iotAgentLib.unsubscribe(device, '51c0ac9ed714fb3b37d7d5a8', function (error) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { should.not.exist(error); should.exist(device); should.exist(device.subscriptions); @@ -171,9 +171,9 @@ describe('NGSI-v2 - Subscription tests', function () { }); it('should delete the subscription from the CB', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { - iotAgentLib.unregister(device.id, 'smartgondor', '/gardens', function (error) { + iotAgentLib.unregister(device.id, null, 'smartgondor', '/gardens', function (error) { contextBrokerMock.done(); done(); }); @@ -183,7 +183,7 @@ describe('NGSI-v2 - Subscription tests', function () { }); describe('When a new notification comes to the IoTAgent', function () { beforeEach(function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { done(); }); diff --git a/test/unit/ngsiv2/provisioning/device-registration_test.js b/test/unit/ngsiv2/provisioning/device-registration_test.js index 532357382..ba881d4ed 100644 --- a/test/unit/ngsiv2/provisioning/device-registration_test.js +++ b/test/unit/ngsiv2/provisioning/device-registration_test.js @@ -82,13 +82,15 @@ const device1 = { id: 'light1', type: 'Light', service: 'smartgondor', - subservice: 'gardens' + subservice: 'gardens', + apikey: null }; const device2 = { id: 'term2', type: 'Termometer', service: 'smartgondor', - subservice: 'gardens' + subservice: 'gardens', + apikey: null }; describe('NGSI-v2 - IoT Agent Device Registration', function () { @@ -223,7 +225,7 @@ describe('NGSI-v2 - IoT Agent Device Registration', function () { it("should return all the device's information", function (done) { iotAgentLib.register(device1, function (error) { - iotAgentLib.getDevice('light1', 'smartgondor', 'gardens', function (error, data) { + iotAgentLib.getDevice('light1', null, 'smartgondor', 'gardens', function (error, data) { should.not.exist(error); should.exist(data); data.type.should.equal('Light'); @@ -254,7 +256,7 @@ describe('NGSI-v2 - IoT Agent Device Registration', function () { it('should return a ENTITY_NOT_FOUND error', function (done) { iotAgentLib.register(device1, function (error) { - iotAgentLib.getDevice('lightUnexistent', 'smartgondor', 'gardens', function (error, data) { + iotAgentLib.getDevice('lightUnexistent', null, 'smartgondor', 'gardens', function (error, data) { should.exist(error); should.not.exist(data); error.code.should.equal(404); @@ -301,7 +303,7 @@ describe('NGSI-v2 - IoT Agent Device Registration', function () { }); it('should update the devices information in Context Broker', function (done) { - iotAgentLib.unregister(device1.id, 'smartgondor', 'gardens', function (error) { + iotAgentLib.unregister(device1.id, null, 'smartgondor', 'gardens', function (error) { should.not.exist(error); contextBrokerMock.done(); done(); @@ -346,7 +348,7 @@ describe('NGSI-v2 - IoT Agent Device Registration', function () { it('should not remove the device from the internal registry'); it('should return a UNREGISTRATION_ERROR error to the caller', function (done) { - iotAgentLib.unregister(device1.id, 'smartgondor', 'gardens', function (error) { + iotAgentLib.unregister(device1.id, null, 'smartgondor', 'gardens', function (error) { should.exist(error); should.exist(error.name); error.name.should.equal('UNREGISTRATION_ERROR'); diff --git a/test/unit/ngsiv2/provisioning/device-update-registration_test.js b/test/unit/ngsiv2/provisioning/device-update-registration_test.js index 6326e755a..5b1576ac2 100644 --- a/test/unit/ngsiv2/provisioning/device-update-registration_test.js +++ b/test/unit/ngsiv2/provisioning/device-update-registration_test.js @@ -206,7 +206,7 @@ describe('NGSI-v2 - IoT Agent Device Update Registration', function () { it('should store the new values in the registry', function (done) { iotAgentLib.updateRegister(deviceUpdated, false, function (error, data) { - iotAgentLib.getDevice(deviceUpdated.id, 'smartgondor', 'gardens', function (error, deviceResult) { + iotAgentLib.getDevice(deviceUpdated.id, null, 'smartgondor', 'gardens', function (error, deviceResult) { should.not.exist(error); should.exist(deviceResult); deviceResult.internalId.should.equal(deviceUpdated.internalId); @@ -264,6 +264,7 @@ describe('NGSI-v2 - IoT Agent Device Update Registration', function () { iotAgentLib.updateRegister(deviceCommandUpdated, false, function (error, data) { iotAgentLib.getDevice( deviceCommandUpdated.id, + null, 'smartgondor', 'gardens', function (error, deviceResult) {