Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
Add basic cmpcatalog support
Browse files Browse the repository at this point in the history
Signed-off-by: Scott Ware <[email protected]>
  • Loading branch information
srware committed Jul 10, 2019
1 parent 1e3cdb2 commit 1727c9f
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 17 deletions.
25 changes: 14 additions & 11 deletions bin/oisp-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -73,7 +75,7 @@ var helpBase = function(apiBase) {

admin.version(pkgJson.version)
.command("help <apiBase>")
.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 */
Expand All @@ -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')
Expand Down
85 changes: 85 additions & 0 deletions modules/cmpcatalog.js
Original file line number Diff line number Diff line change
@@ -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 <accountId>')
.description('|List component types for account.|GET:/v1/api/accounts/{accountId}/cmpcatalog')
.action(getCatalog);
program
.command('cmpcatalog.get.component <accountId> <componentId>')
.description('|Get component type details.|GET:/v1/api/accounts/{accountId}/cmpcatalog/{componentId}')
.action(getCatalogDetails);
}
};
13 changes: 7 additions & 6 deletions test/commonTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,13 @@ global.fakeUserAdminData = {


global.fakeApi = {
data: {},
users: {},
accounts: {},
devices: {},
auth: {},
alerts: {}
accounts: {},
alerts: {},
auth: {},
cmpcatalog: {},
data: {},
devices: {},
users: {}
}


Expand Down
115 changes: 115 additions & 0 deletions test/modules_cmpcatalogTests.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 1727c9f

Please sign in to comment.