Skip to content
This repository was archived by the owner on Jun 2, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Helpers/testcase-helpers/js/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict';

var test = require('ava');
var OpenT2T = require('opent2t').OpenT2T;
var OpenT2TConstants = require('opent2t').OpenT2TConstants;
var OpenT2TLogger = require('opent2t').Logger;

function runTest(settings, t, testMethod) {
let expectedException = settings.expectedExceptions === undefined ? undefined : settings.expectedExceptions[t.title];
Expand Down Expand Up @@ -40,5 +43,26 @@ function verifyModesData(t, response) {
t.is(Object.prototype.toString.call(response.modes), '[object Array]', 'Verify modes data is object array');
}

function createLogger() {
return new OpenT2TLogger("info");
}

function createOpenT2T() {
return new OpenT2T(createLogger());
}

function updateSettings(settings) {
if (!settings.opent2t) {
settings.opent2t = createOpenT2T();
}

if (!settings.test) {
settings.test = test;
}
}

module.exports.runTest = runTest;
module.exports.verifyModesData = verifyModesData;
module.exports.createLogger = createLogger;
module.exports.createOpenT2T = createOpenT2T;
module.exports.updateSettings = updateSettings;
3 changes: 2 additions & 1 deletion Helpers/testcase-helpers/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"main": "helpers.js",
"license": "MIT",
"dependencies": {
"opent2t": "^1.1.0"
"opent2t": "^1.1.0",
"ava": "^0.18.2"
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
var test = require('ava');
var translatorPath = require('path').join(__dirname, '..');
var runBinarySwitchTests = require('opent2t-device-binaryswitch/binarySwitchTests');
var deviceData = require('./devicedata');
var OpenT2TLogger = require('opent2t').Logger;
var logger = new OpenT2TLogger("info");
var MockHub = require('opent2t-device-mockcontosothingshub/mockContosoThingsHub');
var mockHub = new MockHub(logger, deviceData);
var mockHub = new MockHub(deviceData);

var settings = {
logger,
test,
createTranslator: mockHub.createTranslator(translatorPath, deviceData.base_state.Id),
setTestData: mockHub.setTestData
translatorPath,
getDeviceInfo: mockHub.getDeviceInfo.bind(mockHub),
setTestData: mockHub.setTestData.bind(mockHub),
};

// Run standard binary switch unit tests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
var test = require('ava');
var translatorPath = require('path').join(__dirname, '..');
var runBinarySwitchTests = require('opent2t-device-binaryswitch/binarySwitchTests');
var deviceData = require('./devicedata');
var OpenT2TLogger = require('opent2t').Logger;
var logger = new OpenT2TLogger("info");
var MockHub = require('opent2t-device-insteonhub/mockInsteonHub');
var mockHub = new MockHub(logger, deviceData);
var mockHub = new MockHub(deviceData);

var settings = {
logger,
createTranslator: mockHub.createTranslator(translatorPath, deviceData.base_state.DeviceID),
test,
setTestData: mockHub.setTestData
translatorPath,
getDeviceInfo: mockHub.getDeviceInfo.bind(mockHub),
setTestData: mockHub.setTestData.bind(mockHub),
};

// Run standard binary switch unit tests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
var test = require('ava');
var translatorPath = require('path').join(__dirname, '..');
var runBinarySwitchTests = require('opent2t-device-binaryswitch/binarySwitchTests');
var deviceData = require('./devicedata');
var OpenT2TLogger = require('opent2t').Logger;
var logger = new OpenT2TLogger("info");
var MockHub = require('opent2t-device-smartthingshub/mockSmartthingsHub');
var mockHub = new MockHub(logger, deviceData);
var mockHub = new MockHub(deviceData);

var settings = {
logger,
createTranslator: mockHub.createTranslator(translatorPath, deviceData.base_state.id),
test: test,
setTestData: mockHub.setTestData
translatorPath,
getDeviceInfo: mockHub.getDeviceInfo.bind(mockHub),
setTestData: mockHub.setTestData.bind(mockHub),
};

// Run standard binary switch unit tests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
var test = require('ava');
var translatorPath = require('path').join(__dirname, '..');
var runBinarySwitchTests = require('opent2t-device-binaryswitch/binarySwitchTests');
var deviceData = require('./devicedata');
var OpenT2TLogger = require('opent2t').Logger;
var logger = new OpenT2TLogger("info");
var MockHub = require('opent2t-device-winkhub/mockWinkHub');
var mockHub = new MockHub(logger, deviceData);
var mockHub = new MockHub(deviceData);

var settings = {
logger,
test,
createTranslator: mockHub.createTranslator(translatorPath, deviceData.base_state.data.object_id),
setTestData: mockHub.setTestData
translatorPath,
getDeviceInfo: mockHub.getDeviceInfo.bind(mockHub),
setTestData: mockHub.setTestData.bind(mockHub),
};

// Run standard binary switch unit tests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
'use strict';

var OpenT2T = require('opent2t').OpenT2T;
const SchemaName = 'org.opent2t.sample.binaryswitch.superpopular';
var runAllPlatformTests = require('opent2t-device-all/tests');
var helpers = require('opent2t-testcase-helpers');
const SchemaName = 'org.opent2t.sample.binaryswitch.superpopular';
var translator = undefined;

function runBinarySwitchTests(settings) {
var opent2t = new OpenT2T(settings.logger);
var test = settings.test;
helpers.updateSettings(settings);
var test = settings.test;
var opent2t = settings.opent2t;
var deviceId = settings.deviceId;
var translator;
settings.schemaName = SchemaName;

function setData(t) {
if(settings.setTestData) {
settings.setTestData(t.title, t);
}
}

runAllPlatformTests(settings);

test.before(() => {
return settings.createTranslator().then(trans => {
return opent2t.createTranslatorAsync(settings.translatorPath, 'thingTranslator', settings.getDeviceInfo()).then(trans => {
translator = trans;
return opent2t.invokeMethodAsync(translator, SchemaName, 'get', []).then((response) => {
if(deviceId === undefined) {
Expand All @@ -41,7 +35,6 @@ function runBinarySwitchTests(settings) {
});

test.serial('SetPower', t => {
setData(t);
return helpers.runTest(settings, t, () => {
return opent2t.invokeMethodAsync(translator, SchemaName, 'postDevicesPower', [deviceId, { 'value': true }])
.then((response) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"main": "binarySwitchTests.js",
"license": "MIT",
"dependencies": {
"opent2t": "^1.1.0",
"opent2t-testcase-helpers": "file:../../Helpers/testcase-helpers/js",
"opent2t-device-all": "file:../../unittests"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,20 @@

var MockHub = require('opent2t-device-hub/mockHub');

// given a deviceState, applies the modification to it
function modifyDeviceState(deviceState, modifications) {
if(deviceState && modifications) {
for(var modification in modifications) {
deviceState[modification] = modifications[modification];
}
class MockContosoThingsHub extends MockHub {
constructor(initialState) {
super(initialState.base_state.Id, initialState);
}
}

/// ensures the payload matches the modification
function verifyPayload(modification, t, args) {
var payload = args[2];
var key = Object.keys(modification)[0];
return payload.propertyName == key && payload.value == modification[key];
}
modifyDeviceState(modifications, args) {
for(var modification in modifications) {
this.deviceState[modification] = modifications[modification];
}

class MockContosoThingsHub extends MockHub {
constructor(logger, initialState) {
super(logger, initialState, modifyDeviceState, verifyPayload);
let payload = args[2];
let key = Object.keys(modifications)[0];

this.test.true(payload.propertyName == key && payload.value == modifications[key], 'Verify payload');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"main": "mockContosoThingsHub.js",
"license": "MIT",
"dependencies": {
"opent2t": "*",
"opent2t-device-hub": "file:../../test"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,40 @@

var MockHub = require('opent2t-device-hub/mockHub');

function modifyDeviceState(deviceState, modifications) {
if(deviceState && modifications) {
for(var modification in modifications) {
deviceState[modification] = modifications[modification];
}
class MockInsteonHub extends MockHub {
constructor(initialState) {
super(initialState.base_state.DeviceID, initialState);
}
}

function verifyPayload(modification, t, args) {
let expectedPayload = undefined;
if(modification.Power !== undefined) {
expectedPayload = {command: modification.Power};
}
else if(modification.Level !== undefined) {
expectedPayload = {command: 'on', level: modification.Level};
}
else if(modification.cool_point !== undefined) {
expectedPayload = {command: 'set_cool_to', temp: modification.cool_point};
}
else if(modification.heat_point !== undefined) {
expectedPayload = {command: 'set_heat_to', temp: modification.heat_point};
}
else if(modification.fan !== undefined) {
expectedPayload = {command: 'fan_' + modification.fan};
}
else if(modification.mode !== undefined) {
expectedPayload = {command: modification.mode};
modifyDeviceState(modifications, args) {
for(var modification in modifications) {
this.deviceState[modification] = modifications[modification];
}

this.verifyPayload(modifications, args);
}
return JSON.stringify(args[1]) === JSON.stringify(expectedPayload);
}

class MockInsteonHub extends MockHub {
constructor(logger, initialState) {
super(logger, initialState, modifyDeviceState, verifyPayload);
verifyPayload(modification, args) {
let expectedPayload = undefined;
if(modification.Power !== undefined) {
expectedPayload = {command: modification.Power};
}
else if(modification.Level !== undefined) {
expectedPayload = {command: 'on', level: modification.Level};
}
else if(modification.cool_point !== undefined) {
expectedPayload = {command: 'set_cool_to', temp: modification.cool_point};
}
else if(modification.heat_point !== undefined) {
expectedPayload = {command: 'set_heat_to', temp: modification.heat_point};
}
else if(modification.fan !== undefined) {
expectedPayload = {command: 'fan_' + modification.fan};
}
else if(modification.mode !== undefined) {
expectedPayload = {command: modification.mode};
}
this.test.true(JSON.stringify(args[1]) === JSON.stringify(expectedPayload), 'Verify payload');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"main": "mockInsteonHub.js",
"license": "MIT",
"dependencies": {
"opent2t": "*",
"opent2t-device-hub": "file:../../test"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,23 @@
var q = require('q');
var MockHub = require('opent2t-device-hub/mockHub');

function modifyDeviceState(deviceState, modifications) {
if(deviceState && modifications) {
class MockNestHub extends MockHub {
constructor(initialState) {
super(initialState.base_state.device_id, initialState);
}

modifyDeviceState(modifications, args) {
for(var modification in modifications) {
deviceState[modification] = modifications[modification];
this.deviceState[modification] = modifications[modification];
}

this.test.true(JSON.stringify(args[2]) === JSON.stringify(modifications), 'Verify payload');
}
}

function verifyPayload(modification, t, args) {
return JSON.stringify(args[2]) === JSON.stringify(modification);
}

class MockNestHub extends MockHub {
constructor(logger, initialState) {
super(logger, initialState, modifyDeviceState, verifyPayload);
this.hub.setAwayMode = function(structureId, deviceId, mode) {
return q.fcall(function () {
return {device_id: deviceId, awayMode: mode};
});
}
setAwayMode(structureId, deviceId, mode) {
return q.fcall(function () {
return {device_id: deviceId, awayMode: mode};
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"main": "mockNestHub.js",
"license": "MIT",
"dependencies": {
"opent2t": "*",
"q": "^1.4.1",
"opent2t-device-hub": "file:../../test"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@

var MockHub = require('opent2t-device-hub/mockHub');

function modifyDeviceState(deviceState, modifications) {
if(deviceState && modifications) {
for(var modification in modifications) {
deviceState.attributes[modification] = modifications[modification];
}
class MockSmartthingsHub extends MockHub {
constructor(initialState) {
super(initialState.base_state.id, initialState);
}
}

function verifyPayload(modification, t, args) {
return JSON.stringify(args[2]) === JSON.stringify(modification);
}

class MockSmartthingsHub extends MockHub {
constructor(logger, initialState) {
super(logger, initialState, modifyDeviceState, verifyPayload);
modifyDeviceState(modifications, args) {
for(var modification in modifications) {
this.deviceState.attributes[modification] = modifications[modification];
}

this.test.true(JSON.stringify(args[2]) === JSON.stringify(modifications), 'Verify payload');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"main": "mockSmartthingsHub.js",
"license": "MIT",
"dependencies": {
"opent2t": "*",
"opent2t-device-hub": "file:../../test"
}
}
Loading