diff --git a/bin/oisp-cli.js b/bin/oisp-cli.js index c30cccb..60fcb23 100755 --- a/bin/oisp-cli.js +++ b/bin/oisp-cli.js @@ -26,18 +26,20 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; var admin= require('commander'), - pkgJson = require('../package.json'), + pkgJson = require('../package.json'); + +/* Modules */ +var accounts = require('../modules/accounts'), + alerts = require('../modules/alerts'), auth = require('../modules/auth'), - accounts = require('../modules/accounts'), - users = require('../modules/users'), + cmpcatalog = require('../modules/cmpcatalog'), data = require('../modules/data'), devices = require('../modules/devices'), local = require('../modules/local'), - alerts = require('../modules/alerts'); - + users = require('../modules/users'); var helpBase = function(apiBase) { - if ( ! apiBase.match(new RegExp("^regular$|^all$|^apionly$|^auth$|^users$|^accounts$|^rules$|^alerts$|^devices$|^data$|^components$|^control$|^invites$"))) { + if ( ! apiBase.match(new RegExp("^regular$|^all$|^apionly$|^auth$|^users$|^accounts$|^rules$|^alerts$|^devices$|^data$|^components$|^control$|^invites$|^cmpcatalog$"))) { console.log("Unknown apiBase command"); return; } @@ -73,7 +75,7 @@ var helpBase = function(apiBase) { admin.version(pkgJson.version) .command("help ") - .description("Filters help text by API base path, [all, apionly, auth, users, accounts, rules, alerts, devices, data, components, control, invites]") + .description("Filters help text by API base path, [all, apionly, auth, users, accounts, rules, alerts, devices, data, components, control, invites, cmpcatalog]") .action(helpBase); /* Error handling */ @@ -88,13 +90,14 @@ var errorHandler = function(error, code) { /* * Add commando as option */ -auth.addCommand(admin, errorHandler); -users.addCommand(admin, errorHandler); accounts.addCommand(admin, errorHandler); -devices.addCommand(admin, errorHandler); +alerts.addCommand(admin, errorHandler); +auth.addCommand(admin, errorHandler); +cmpcatalog.addCommand(admin, errorHandler); data.addCommand(admin, errorHandler); +devices.addCommand(admin, errorHandler); local.addCommand(admin, errorHandler); -alerts.addCommand(admin, errorHandler); +users.addCommand(admin, errorHandler); admin.command('*') .description('Error message for non valid command') diff --git a/modules/cmpcatalog.js b/modules/cmpcatalog.js new file mode 100644 index 0000000..a89871a --- /dev/null +++ b/modules/cmpcatalog.js @@ -0,0 +1,85 @@ +/* +Copyright (c) 2017, Intel Corporation + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +"use strict"; + +var config = require('../config'), + api = require("@open-iot-service-platform/oisp-sdk-js")(config).api.rest, + logger = require('../lib/logger').init(), + userAdminTools = require("../lib/cli-tools"), + userAdminData = require("../lib/cli-data"), + common = require("../lib/common"); +var errorHandler = {}; + +var getCatalog = function(accountId) { + logger.info("Starting getCatalog ..."); + var userAdminDataObj = userAdminData.loadUserAdminBaseData(); + var targetAccount = userAdminTools.findAccountId(accountId, userAdminDataObj.accounts); + if (! targetAccount) { + logger.error(common.errors["accountIdError"].message); + errorHandler(null, common.errors["accountIdError"].code); + } + userAdminDataObj.accountId = targetAccount.id; + api.cmpcatalog.getCatalog(userAdminDataObj, function(err, response) { + if (!err && response) { + logger.info("Info retrieved: ", response); + } else { + logger.error(common.errors["responseError"].message + ": " + err); + errorHandler(null, common.errors["responseError"].code); + } + }); +}; + +var getCatalogDetails = function(accountId, componentId) { + logger.info("Starting getCatalogDetails ..."); + var userAdminDataObj = userAdminData.loadUserAdminBaseData(); + var targetAccount = userAdminTools.findAccountId(accountId, userAdminDataObj.accounts); + if (! targetAccount) { + logger.error(common.errors["accountIdError"].message); + errorHandler(null, common.errors["accountIdError"].code); + } + userAdminDataObj.accountId = targetAccount.id; + userAdminDataObj.componentId = componentId; + api.cmpcatalog.getCatalogDetail(userAdminDataObj, function(err, response) { + if (!err && response) { + logger.info("Info retrieved: ", response); + } else { + logger.error(common.errors["responseError"].message + ": " + err); + errorHandler(null, common.errors["responseError"].code); + } + }); +}; + +module.exports = { + addCommand : function (program, errorHdl) { + errorHandler = errorHdl; + program + .command('cmpcatalog.get ') + .description('|List component types for account.|GET:/v1/api/accounts/{accountId}/cmpcatalog') + .action(getCatalog); + program + .command('cmpcatalog.get.component ') + .description('|Get component type details.|GET:/v1/api/accounts/{accountId}/cmpcatalog/{componentId}') + .action(getCatalogDetails); + } +}; diff --git a/test/commonTest.js b/test/commonTest.js index dbf2de2..079838c 100644 --- a/test/commonTest.js +++ b/test/commonTest.js @@ -185,12 +185,13 @@ global.fakeUserAdminData = { global.fakeApi = { - data: {}, - users: {}, - accounts: {}, - devices: {}, - auth: {}, - alerts: {} + accounts: {}, + alerts: {}, + auth: {}, + cmpcatalog: {}, + data: {}, + devices: {}, + users: {} } diff --git a/test/modules_cmpcatalogTests.js b/test/modules_cmpcatalogTests.js new file mode 100644 index 0000000..65cf8ee --- /dev/null +++ b/test/modules_cmpcatalogTests.js @@ -0,0 +1,115 @@ +/* + Copyright (c) 2017, Intel Corporation + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +var assert = require('chai').assert, + rewire = require('rewire'), + url = require('url'), + sinon = require('sinon'); +require("./commonTest.js"); + +var fileToTest = "../modules/cmpcatalog.js"; + +describe(fileToTest, function() { + var toTest = rewire(fileToTest); + toTest.__set__("logger", logger); + + it('Shall return response error (getCatalog) >', function(done) { + adminDataFile = {}; + + var test = function(object, callback) { + object.token = token; + callback(new Error("Error")); + } + + var fakeErrorHandler = function(error, code) { + assert.equal(code, fakeCommon.errors["responseError"].code); + done(); + } + + toTest.__set__("userAdminData", fakeUserAdminData); + toTest.__set__("userAdminTools", fakeLibTools); + fakeApi.cmpcatalog.getCatalog = test; + toTest.__set__("api", fakeApi); + toTest.__set__("errorHandler", fakeErrorHandler); + toTest.__set__("common", fakeCommon); + toTest.__get__("getCatalog")(jsonString); + }); + + + it('Shall fail to get accountId (getCatalog) >', function(done) { + + var fakeErrorHandler = function(error, code) { + assert.equal(code, fakeCommon.errors["accountIdError"].code); + done(); + } + + toTest.__set__("userAdminData", fakeUserAdminData); + toTest.__set__("userAdminTools", fakeLibToolsError); + fakeApi.cmpcatalog.getCatalog = function() {}; + toTest.__set__("api", fakeApi); + toTest.__set__("errorHandler", fakeErrorHandler); + toTest.__set__("common", fakeCommon); + toTest.__get__("getCatalog")(account.id); + }); + + + it('Shall return response error (getCatalogDetails) >', function(done) { + adminDataFile = {}; + + var test = function(object, callback) { + object.token = token; + callback(new Error("Error")); + } + + var fakeErrorHandler = function(error, code) { + assert.equal(code, fakeCommon.errors["responseError"].code); + done(); + } + + toTest.__set__("userAdminData", fakeUserAdminData); + toTest.__set__("userAdminTools", fakeLibTools); + fakeApi.cmpcatalog.getCatalogDetail = test; + toTest.__set__("api", fakeApi); + toTest.__set__("errorHandler", fakeErrorHandler); + toTest.__set__("common", fakeCommon); + toTest.__get__("getCatalogDetails")(jsonString); + }); + + + it('Shall fail to get accountId (getCatalogDetails) >', function(done) { + + var fakeErrorHandler = function(error, code) { + assert.equal(code, fakeCommon.errors["accountIdError"].code); + done(); + } + + toTest.__set__("userAdminData", fakeUserAdminData); + toTest.__set__("userAdminTools", fakeLibToolsError); + fakeApi.cmpcatalog.getCatalogDetail = function() {}; + toTest.__set__("api", fakeApi); + toTest.__set__("errorHandler", fakeErrorHandler); + toTest.__set__("common", fakeCommon); + toTest.__get__("getCatalogDetails")(account.id); + }); +});