Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
walterwootz committed Mar 21, 2024
1 parent fb6a7be commit ea25197
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/bindings/OPCUABinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ async function readValueTypeFromOPCUANode(opcuaNodeId) {
* @returns results array
*/
async function doesOPCUANodeExist(opcuaNodeId) {
/* istanbul ignore if */
if (!the_session) {
config.getLogger().error(context, 'No Connection to the OPC UA Server');
return null;
Expand Down Expand Up @@ -142,6 +141,7 @@ async function connectToClient() {
/**
* Create OPCUA Session, throws exception if it fails
*/
/* istanbul ignore next */
async function createSession() {
try {
const username = config.getConfig().opcua.username;
Expand Down Expand Up @@ -190,6 +190,7 @@ async function createSubscription() {
priority: config.getConfig().opcua.subscription.priority
};

/* istanbul ignore next */
try {
const subscription = await the_session.createSubscription2(subscriptionOptions);
the_subscription = subscription;
Expand All @@ -213,6 +214,7 @@ async function createSubscription() {
/**
* Start monitoring OPCUA attributes
*/
/* istanbul ignore next */
async function startMonitoring() {
config.getConfig().iota.contexts.forEach(async function (opcuaServerConfigContext) {
opcuaServerConfigContext.mappings.forEach(async function (mapping) {
Expand Down Expand Up @@ -348,6 +350,7 @@ function prepareInputArguments(attribute) {
inputs.push(attribute.value);
config.getLogger().info(context, 'Command input not an array. Considering it as a single value');
}
/* istanbul ignore next */
} catch (e) {
inputs.push(attribute.value);
config.getLogger().warn(context, 'Unable to determine the type of the command input, considering it as a single value');
Expand Down Expand Up @@ -495,6 +498,7 @@ async function executeQuery(apiKey, device, attribute, callback) {
let attributeType = 'string';
let lazySet = [];

/* istanbul ignore else */
if (device.lazy) {
lazySet = device.lazy;
} else if (config.getConfig().iota.types[type] && config.getConfig().iota.types[type].lazy) {
Expand Down Expand Up @@ -552,6 +556,7 @@ function deviceUpdatingHandler(device, callback) {
* Prepare certificates and start OPCUA binding
*/
async function start() {
/* istanbul ignore if */
if (!config.getConfig().opcua) {
config.getLogger().fatal(context, 'GLOBAL-002: Configuration error. Configuration object [config.opcua] is missing');
throw new Error('GLOBAL-002: Configuration error. Configuration object [config.opcua] is missing');
Expand Down Expand Up @@ -595,6 +600,7 @@ async function start() {
await certificateManager.initialize();

const privateKeyFile = certificateManager.privateKey;
/* istanbul ignore if */
if (!fs.existsSync(certificateFile)) {
await certificateManager.createSelfSignedCertificate({
subject: '/CN=localhost/O=Engineering Ingegneria Informatica S.p.A./L=Palermo',
Expand Down Expand Up @@ -654,7 +660,9 @@ exports.executeUpdate = executeUpdate;
exports.executeQuery = executeQuery;
exports.readValueFromOPCUANode = readValueFromOPCUANode;
exports.readValueTypeFromOPCUANode = readValueTypeFromOPCUANode;
exports.doesOPCUANodeExist = doesOPCUANodeExist;
exports.start = start;
exports.stop = stop;
exports.startMonitoring = startMonitoring;
exports.createSubscription = createSubscription;
exports.protocol = 'OPCUA';
2 changes: 2 additions & 0 deletions lib/commonBindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ function messageHandler(topic, message, protocol) {
const parsedMessage = parseMessage(message);

function processMessageForDevice(device, apiKey, topicInformation) {
/* istanbul ignore if */
if (topicInformation[3] === 'configuration' && topicInformation[4] === 'commands' && parsedMessage) {
manageConfigurationRequest(apiKey, deviceId, device, parsedMessage);
} else if (topicInformation[4]) {
Expand All @@ -238,6 +239,7 @@ function messageHandler(topic, message, protocol) {
// it must be an array of object
multipleMeasures(apiKey, deviceId, device, parsedMessage);
} else {
/* istanbul ignore next */
config.getLogger().error(
context,
/*jshint quotmark: double */
Expand Down
2 changes: 2 additions & 0 deletions lib/metaBindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ function createDevices() {
if (err.message.includes('statusCode=409')) {
resolve(true);
} else {
/* istanbul ignore next */
reject(err);
}
});
Expand All @@ -138,6 +139,7 @@ function httpRequest(options, postData) {
try {
body = JSON.parse(Buffer.concat(body).toString());
} catch (e) {
/* istanbul ignore next */
reject(e);
}
resolve(body);
Expand Down
177 changes: 177 additions & 0 deletions test/unit/lib/bindings/OPCUABinding.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,115 @@ describe('OPCUABinding handling', () => {
const opcuaBinding = rewire('../../../../lib/bindings/OPCUABinding.js');
const mockConfig = require('../../../../conf/config-v2.example');

describe('createSubscription', () => {
describe('When the_session is not null', () => {
beforeEach(() => {
opcuaBinding.__set__('config.getConfig', () => {
return mockConfig;
});
opcuaBinding.__set__('the_session', {
createSubscription2: () => {
var subscription = {
on: (type, on_callback) => {
on_callback();
return subscription;
},
monitor: (itemToMonitor, monitoringParamaters, timestampToReturn) => {
return {
on: (type, on_callback) => {
on_callback({
statusCode: 0,
value: {
value: 0
}
});
}
};
}
};
return subscription;
}
});
});

it('Should create the subscription', async () => {
const result = await opcuaBinding.createSubscription();
expect(result).not.to.equal(null);
});
});

describe('When the_session is null', () => {
beforeEach(() => {
opcuaBinding.__set__('config.getLogger', () => {
return {
error: (context, text) => {}
};
});
opcuaBinding.__set__('the_session', null);
});

it('Should return null', async () => {
const opcuaNodeId = 'ns=3;s=Speed';
const result = await opcuaBinding.doesOPCUANodeExist(opcuaNodeId);
expect(result).to.equal(null);
});
});
});

describe('doesOPCUANodeExist', () => {
describe('When the_session is not null', () => {
beforeEach(() => {
opcuaBinding.__set__('the_session', {
readVariableValue: () => {
return {
value: {
dataType: 1
}
};
},
browse: (b) => {
return [];
}
});
});

it('Should check if OPCUA node exist and return results array', async () => {
const opcuaNodeId = 'ns=3;s=Speed';
const result = await opcuaBinding.doesOPCUANodeExist(opcuaNodeId);
expect(result).not.to.equal(null);
});
});

describe('When the_session is null', () => {
beforeEach(() => {
opcuaBinding.__set__('config.getLogger', () => {
return {
error: (context, text) => {}
};
});
opcuaBinding.__set__('the_session', null);
});

it('Should return null', async () => {
const opcuaNodeId = 'ns=3;s=Speed';
const result = await opcuaBinding.doesOPCUANodeExist(opcuaNodeId);
expect(result).to.equal(null);
});
});
});

describe('readValueFromOPCUANode', () => {
describe('When the_session is not null', () => {
beforeEach(() => {
opcuaBinding.__set__('config.getLogger', () => {
return {
error: (context, text) => {},
warn: (context, text) => {},
info: (context, text) => {},
fatal: (context, text) => {},
debug: (context, text) => {}
};
});
opcuaBinding.__set__('config.getConfig', () => {
return mockConfig;
});
Expand Down Expand Up @@ -72,6 +178,15 @@ describe('OPCUABinding handling', () => {
describe('readValueTypeFromOPCUANode', () => {
describe('When the_session is not null', () => {
beforeEach(() => {
opcuaBinding.__set__('config.getLogger', () => {
return {
error: (context, text) => {},
warn: (context, text) => {},
info: (context, text) => {},
fatal: (context, text) => {},
debug: (context, text) => {}
};
});
opcuaBinding.__set__('config.getConfig', () => {
return mockConfig;
});
Expand All @@ -95,6 +210,15 @@ describe('OPCUABinding handling', () => {

describe('When dataValue is null', () => {
beforeEach(() => {
opcuaBinding.__set__('config.getLogger', () => {
return {
error: (context, text) => {},
warn: (context, text) => {},
info: (context, text) => {},
fatal: (context, text) => {},
debug: (context, text) => {}
};
});
opcuaBinding.__set__('config.getConfig', () => {
return mockConfig;
});
Expand All @@ -116,6 +240,15 @@ describe('OPCUABinding handling', () => {

describe('When the_session is null', () => {
beforeEach(() => {
opcuaBinding.__set__('config.getLogger', () => {
return {
error: (context, text) => {},
warn: (context, text) => {},
info: (context, text) => {},
fatal: (context, text) => {},
debug: (context, text) => {}
};
});
opcuaBinding.__set__('config.getConfig', () => {
return mockConfig;
});
Expand All @@ -135,6 +268,15 @@ describe('OPCUABinding handling', () => {

describe('When the_session is not null', () => {
beforeEach(() => {
opcuaBinding.__set__('config.getLogger', () => {
return {
error: (context, text) => {},
warn: (context, text) => {},
info: (context, text) => {},
fatal: (context, text) => {},
debug: (context, text) => {}
};
});
opcuaBinding.__set__('config.getConfig', () => {
return mockConfig;
});
Expand Down Expand Up @@ -164,9 +306,26 @@ describe('OPCUABinding handling', () => {

describe('When the_session is not null', () => {
beforeEach(() => {
opcuaBinding.__set__('config.getLogger', () => {
return {
error: (context, text) => {},
warn: (context, text) => {},
info: (context, text) => {},
fatal: (context, text) => {},
debug: (context, text) => {}
};
});
opcuaBinding.__set__('config.getConfig', () => {
return mockConfig;
});
opcuaBinding.__set__('config.getLogger', () => {
return {
error: (context, text) => {},
warn: (context, text) => {},
info: (context, text) => {},
fatal: (context, text) => {}
};
});
opcuaBinding.__set__('the_session', {
call: () => {
return [
Expand Down Expand Up @@ -233,6 +392,15 @@ describe('OPCUABinding handling', () => {

describe('When the_session is not null', () => {
beforeEach(() => {
opcuaBinding.__set__('config.getLogger', () => {
return {
error: (context, text) => {},
warn: (context, text) => {},
info: (context, text) => {},
fatal: (context, text) => {},
debug: (context, text) => {}
};
});
opcuaBinding.__set__('config.getConfig', () => {
return mockConfig;
});
Expand Down Expand Up @@ -306,6 +474,15 @@ describe('OPCUABinding handling', () => {
describe('When the_session is not null', () => {
const opcuaNodeId = 'ns=3;s=Acceleration';
beforeEach(() => {
opcuaBinding.__set__('config.getLogger', () => {
return {
error: (context, text) => {},
warn: (context, text) => {},
info: (context, text) => {},
fatal: (context, text) => {},
debug: (context, text) => {}
};
});
opcuaBinding.__set__('config.getConfig', () => {
return mockConfig;
});
Expand Down
23 changes: 23 additions & 0 deletions test/unit/lib/configService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ describe('Configuration startup tests', () => {

describe('When environmental variables are set', () => {
beforeEach(() => {
process.env.IOTA_LOGLEVEL = 'test';
process.env.IOTA_TIMESTAMP = 'test';
process.env.IOTA_CB_HOST = 'test';
process.env.IOTA_CB_PORT = 'test';
process.env.IOTA_CB_NGSIVERSION = 'test';
process.env.IOTA_CB_NGSILDCONTEXT = 'test';
process.env.IOTA_CB_SERVICE = 'test';
process.env.IOTA_CB_SUBSERVICE = 'test';
process.env.IOTA_NORTH_PORT = 'test';
process.env.IOTA_REGISTRY_TYPE = 'test';
process.env.IOTA_MONGO_HOST = 'test';
process.env.IOTA_MONGO_PORT = 'test';
process.env.IOTA_MONGO_DB = 'test';
process.env.IOTA_SERVICE = 'test';
process.env.IOTA_SUBSERVICE = 'test';
process.env.IOTA_PROVIDER_URL = 'test';
process.env.IOTA_DEVICEREGDURATION = 'test';
process.env.IOTA_DEFAULTTYPE = 'test';
process.env.IOTA_DEFAULTRESOURCE = 'test';
process.env.IOTA_EXPLICITATTRS = 'test';
process.env.IOTA_EXTENDED_FORBIDDEN_CHARACTERS = 'test';
process.env.IOTA_AUTOPROVISION = 'test';
process.env.IOTA_CONFIG_RETRIEVAL = 'test';
process.env.IOTA_DEFAULT_KEY = 'test';
process.env.IOTA_DEFAULT_TRANSPORT = 'test';
Expand All @@ -32,6 +54,7 @@ describe('Configuration startup tests', () => {
process.env.IOTA_OPCUA_SECURITY_USERNAME = 'test';
process.env.IOTA_OPCUA_SECURITY_PASSWORD = 'test';
process.env.IOTA_OPCUA_UNIQUE_SUBSCRIPTION = 'test';
process.env.IOTA_OPCUA_MT_ENABLED = 'test';
process.env.IOTA_OPCUA_MT_POLLING = 'test';
process.env.IOTA_OPCUA_MT_AGENT_ID = 'test';
process.env.IOTA_OPCUA_MT_ENTITY_ID = 'test';
Expand Down
Loading

0 comments on commit ea25197

Please sign in to comment.