Skip to content

Commit

Permalink
Merge pull request #134 from ibm-watson-iot/codeCoverageJonah
Browse files Browse the repository at this point in the history
DeviceConfig code coverage & minor error fixes
  • Loading branch information
JonahLuckett authored Sep 20, 2019
2 parents d4d25eb + e00c4a9 commit 972d362
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/device/DeviceConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class DeviceConfig extends BaseConfig{
if (!("orgId" in this.identity) || this.identity.orgId == null) {
throw new Error("Missing identity.orgId from configuration");
}
if (!("typeId" in this.identity) || this.identity == null) {
if (!("typeId" in this.identity) || this.identity.typeId == null) {
throw new Error("Missing identity.typeId from configuration");
}
if (!("deviceId" in this.identity) || this.identity.deviceId == null) {
Expand Down Expand Up @@ -163,7 +163,7 @@ export default class DeviceConfig extends BaseConfig{
var validLevels = ["error", "warning", "info", "debug"];
if (!(validLevels.includes(data["options"]["logLevel"])))
{
throw new Error("Optional setting options.logLevel must be one of error, warning, info, debug" + data["options"]["logLevel"])
throw new Error("Optional setting options.logLevel (Currently: " + data["options"]["logLevel"] + ") must be one of error, warning, info, debug")
}
}
else
Expand Down
64 changes: 63 additions & 1 deletion test/DeviceConfig.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('WIoTP Device Configuration', () => {

it('Initialise with the minimum required configuration', () => {
let identity = {orgId: "orgid", typeId: "mytypeid", deviceId: "mydeviceid"};
let auth = {token: "test"};
let auth = {token: "myToken"};
let options = null;

let config = new DeviceConfig(identity, auth, options);
Expand Down Expand Up @@ -58,4 +58,66 @@ describe('WIoTP Device Configuration', () => {
expect(config.options.mqtt.caFile).to.equal("myPath");
});

it('Missing identity throws error', () => {
let identity = null;
let auth = null;
let options = null;
var deviceConfigTest = function(){new DeviceConfig(identity, auth, options)};
expect(deviceConfigTest).to.throw('Missing identity from configuration');
});

it('Missing orgID throws error', () => {
let identity = {orgId: null};
let auth = null;
let options = null;
var deviceConfigTest = function(){new DeviceConfig(identity, auth, options)};
expect(deviceConfigTest).to.throw('Missing identity.orgId from configuration');
});

it('Missing typeId throws error', () => {
let identity = {orgId: "myOrg", typeId: null};
let auth = null;
let options = null;
var deviceConfigTest = function(){new DeviceConfig(identity, auth, options)};
expect(deviceConfigTest).to.throw('Missing identity.typeId from configuration');
});

it('Missing deviceId throws error', () => {
let identity = {orgId: "myOrg", typeId: "myType", deviceId: null};
let auth = null;
let options = null;
var deviceConfigTest = function(){new DeviceConfig(identity, auth, options)};
expect(deviceConfigTest).to.throw('Missing identity.deviceId from configuration');
});

it('Quickstart with an auth throws error', () => {
let identity = {orgId: "quickstart", typeId: "myType", deviceId: "MyDevice"};
let auth = {token: "myToken"};
let options = null;
var deviceConfigTest = function(){new DeviceConfig(identity, auth, options)};
expect(deviceConfigTest).to.throw('Quickstart service does not support device authentication');
});

it('Missing auth throws error', () => {
let identity = {orgId: "myOrg", typeId: "myType", deviceId: "MyDevice"};
let auth = null;
let options = null;
var deviceConfigTest = function(){new DeviceConfig(identity, auth, options)};
expect(deviceConfigTest).to.throw('Missing auth from configuration');
});

it('Missing auth token throws error', () => {
let identity = {orgId: "myOrg", typeId: "myType", deviceId: "MyDevice"};
let auth = {token: null};
let options = null;
var deviceConfigTest = function(){new DeviceConfig(identity, auth, options)};
expect(deviceConfigTest).to.throw('Missing auth.token from configuration');
});

it('Missing config file throws error', () => {
var deviceConfigTest = function(){new DeviceConfig.parseConfigFile("./test/DeviceConfigFileLogLevelTest.spec.yaml")};
expect(deviceConfigTest).to.throw('Optional setting options.logLevel (Currently: notALogLevel) must be one of error, warning, info, debug');
});


});
17 changes: 17 additions & 0 deletions test/DeviceConfigFileLogLevelTest.spec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

identity:
orgId: myOrg
typeId: myType
deviceId: myDevice
auth:
token: myToken
options:
domain: internetofthings.ibmcloud.com
logLevel: notALogLevel
mqtt:
port: 8883
transport: tcp
cleanStart: true
sessionExpiry: 3600
keepAlive: 60
caFile: myPath

0 comments on commit 972d362

Please sign in to comment.