Skip to content

Commit

Permalink
Improving coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Reed committed Nov 14, 2017
1 parent f9024c5 commit a973daf
Show file tree
Hide file tree
Showing 14 changed files with 1,147 additions and 269 deletions.
12 changes: 8 additions & 4 deletions src/listener/alerts-listener.js
Expand Up @@ -26,14 +26,18 @@ class AlertsListener extends Listener {
constructor(robot, client, transform) {
super(robot, client, transform);

this.switchBoard = new Conversation(robot);

this.title = "Alerts";
this.capabilities = [];
this.respond(/(?:get|list|show) last (:<count>[0-9]*?) alerts\.$/i, this.ListNumberOfAlerts.bind(this));

this.LIST_COUNT = /(?:get|list|show) last (:<count>[0-9]*?) alerts\.$/i;
this.LIST_STATUS = /(?:get|list|show) all (:<state>active|locked|cleared){0,1}(?: ?)(:<status>critical|warning|ok|disabled){0,1}(?: ?)alerts(?: from ){0,1}(:<time>today|last 7 days|last 30 days){0,1}\.$/i;

this.switchBoard = new Conversation(robot);

this.respond(this.LIST_COUNT, this.ListNumberOfAlerts.bind(this));
this.capabilities.push(this.BULLET + "List last [insert #] alerts (e.g. list last 10 alerts)");

this.respond(/(?:get|list|show) all (:<state>active|locked|cleared){0,1}(?: ?)(:<status>critical|warning|ok|disabled){0,1}(?: ?)alerts(?: from ){0,1}(:<time>today|last 7 days|last 30 days){0,1}\.$/i, this.ListFilteredAlerts.bind(this));
this.respond(this.LIST_STATUS, this.ListFilteredAlerts.bind(this));
this.capabilities.push(this.BULLET + "List all active/locked/cleared critical/warning/ok/disabled alerts from today/last 7 days/last 30 days (e.g. list all (active) (critical) alerts (from today)) ");
}

Expand Down
10 changes: 7 additions & 3 deletions src/listener/bot.js
Expand Up @@ -33,11 +33,15 @@ class BotListener extends Listener {
this.title = "bot";
this.capabilities = [];

this.respond(/help\.$/i, this.ListActions.bind(this));
this.respond(/What can you do(?: for me){0,1}\.$/i, this.ListActions.bind(this));
this.LIST_HELP = /help\.$/i;
this.LIST_ACTIONS = /What can you do(?: for me){0,1}\.$/i
this.LIST_ACTION = /(:<text>[a-zA-Z][a-z A-Z]*?) help\.$/i

this.respond(this.LIST_HELP, this.ListActions.bind(this));
this.respond(this.LIST_ACTIONS, this.ListActions.bind(this));
this.capabilities.push(this.BULLET + "Help (Show list of areas of help).");

this.respond(/(:<text>[a-zA-Z][a-z A-Z]*?) help\.$/i, this.ListActionsFor.bind(this));
this.respond(this.LIST_ACTION, this.ListActionsFor.bind(this));
this.capabilities.push(this.BULLET + "<text> Help (Show help in a specific area).");
}

Expand Down
4 changes: 3 additions & 1 deletion src/listener/dashboard-listener.js
Expand Up @@ -30,7 +30,9 @@ class DashboardListener extends Listener {
this.capabilities = [];
this.room = client.chatRoom;

this.respond(/show (?:oneview ){0,1}(?:dashboard|status).$/i, this.ShowOneViewDashboard.bind(this));
this.SHOW = /show (?:oneview ){0,1}(?:dashboard|status).$/i;

this.respond(this.SHOW, this.ShowOneViewDashboard.bind(this));
this.capabilities.push(this.BULLET + "show (OneView) dashboard");
}

Expand Down
4 changes: 2 additions & 2 deletions src/listener/server-profile-templates.js
Expand Up @@ -44,11 +44,11 @@ class ServerProfileTemplateListener extends Listener {

this.LIST_DEPLOYED_PROFILES = /(?:get|list|show) profile[s]{0,1} (?:using|deployed from|deployed by) (:<host>.*?)(?:\/rest\/server-profile-templates\/)(:<templateId>[a-zA-Z0-9_-]*?)\.$/i;
this.respond(this.LIST_DEPLOYED_PROFILES, this.GetDeployedProfiles.bind(this));
this.capabilities.push(this.BULLET + "Show profile(s) using a server profile template (e.g. show profile using docker swarm).");
this.capabilities.push(this.BULLET + "Show profile(s) using a server profile template (e.g. show profiles using docker swarm).");

this.CREATE_PROFILES_FROM_TEMPATE = /(?:deploy|create) (:<count>\d+) profile[s]{0,1} (?:from|for|using) (:<host>.*?)(?:\/rest\/server-profile-templates\/)(:<templateId>[a-zA-Z0-9_-]*?)\.$/i;
this.respond(this.CREATE_PROFILES_FROM_TEMPATE, this.DeployProfiles.bind(this));
this.capabilities.push(this.BULLET + "Create profile(s) using a server profile template (e.g. create profile for docker swarm).");
this.capabilities.push(this.BULLET + "Create profile(s) using a server profile template (e.g. create 1 profile from docker swarm).");

this.GROW_TEMPLATE = /(?:flex|grow)(?: the)? (:<host>.*?)(?:\/rest\/server-profile-templates\/)(:<templateId>[a-zA-Z0-9_-]*?) by (:<count>\d+)(?: profile| profiles| hardware| servers)?\.$/i;
this.respond(this.GROW_TEMPLATE, this.DeployProfiles.bind(this));
Expand Down
147 changes: 135 additions & 12 deletions test/listener/alerts-listener.js
Expand Up @@ -23,6 +23,7 @@ const AlertsListener = require('../../src/listener/alerts-listener');
const OneViewBrain = require('../../src/middleware/ov-brain');
const OVClient = require('../../src/oneview-sdk/ov-client');
const ResourceTransforms = require('../../src/listener/utils/resource-transforms');
const NamedRegExp = require('../../src/listener/named-regexp');

const chai = require('chai');
const sinon = require('sinon');
Expand All @@ -33,18 +34,24 @@ const assert = chai.assert;

describe('AlertsListener', () => {

const robot = {adapterName: 'shell', on: function () { }, listen: function () {}, respond: function () {}, listenerMiddleware: function() {}, logger: {debug: function () {}, error: function () {}, info: function () {}}};
const robot = {
name: 'bot',
adapterName: 'shell',
receive: function () {},
on: function () { },
listen: function (matcher, options) {listeners.push(matcher, options)},
respond: function () {},
listenerMiddleware: function() {},
logger: {debug: function () {}, error: function () {}, info: function () {}}
};

// capture dialog liseners
let listeners = [];

const oneviewConfig = {
hosts: []
};
const oVClient = new OVClient(oneviewConfig, robot);

sinon.stub(oVClient.ServerHardware, 'getAllServerHardware').returns(Bluebird.resolve([]));
sinon.stub(oVClient.ServerProfiles, 'getAllServerProfiles').returns(Bluebird.resolve([]));
sinon.stub(oVClient.ServerProfileTemplates, 'getAllServerProfileTemplates').returns(Bluebird.resolve([]));
sinon.stub(oVClient.LogicalInterconnects, 'getAllLogicalInterconnects').returns(Bluebird.resolve([]));

const brain = new OneViewBrain(oVClient, robot, {});
const transform = new ResourceTransforms(robot, brain);

Expand Down Expand Up @@ -82,11 +89,49 @@ describe('AlertsListener', () => {
};

const err = {
error: {
errorCode: 'OOPS'
}
error: {errorCode: 'OOPS'}
};

it('constructor', (done) => {
let spy = sinon.spy(robot, "respond");
const alertsListener = new AlertsListener(robot, oVClient, transform);

let rgx0 = new NamedRegExp(alertsListener.LIST_COUNT);
let rgx1 = new NamedRegExp(alertsListener.LIST_STATUS);

let constructorArgs = robot.respond.getCalls();

rgx0.should.deep.equal(constructorArgs[0].args[0]);
assert(typeof constructorArgs[0].args[2] === 'function');
'bound ListNumberOfAlerts'.should.equal(constructorArgs[0].args[2].name);
assert.isFalse(constructorArgs[0].args[2].hasOwnProperty('prototype'));

rgx1.should.deep.equal(constructorArgs[1].args[0]);
assert(typeof constructorArgs[1].args[2] === 'function');
'bound ListFilteredAlerts'.should.equal(constructorArgs[1].args[2].name);
assert.isFalse(constructorArgs[1].args[2].hasOwnProperty('prototype'));

assert(constructorArgs.length === 2);

spy.restore();
done();
});

it('test regexps', (done) => {
const alertsListener = new AlertsListener(robot, oVClient, transform);

let rgx0 = new NamedRegExp(alertsListener.LIST_COUNT);
let rgx1 = new NamedRegExp(alertsListener.LIST_STATUS);

assert.isTrue(rgx0.test('@bot show last 7 alerts.'));
assert.isTrue(rgx1.test('@bot show all active alerts from today.'));
assert.isTrue(rgx1.test('@bot show all critical alerts from last 7 days.'));
assert.isTrue(rgx1.test('@bot show all disabled alerts from last 30 days.'));
assert.isTrue(rgx1.test('@bot show all locked ok alerts from last 30 days.'));

done();
});

it('list alerts by number', (done) => {
let stub = sinon.stub(oVClient.Alerts, 'getNumberOfAlerts').returns(Bluebird.resolve(alertResponse));
const alertsListener = new AlertsListener(robot, oVClient, transform);
Expand Down Expand Up @@ -140,6 +185,84 @@ describe('AlertsListener', () => {
}, 100);
});

it('list alerts by filter, no match today', (done) => {
let stub = sinon.stub(oVClient.Alerts, 'getFilteredAlerts').returns(Bluebird.resolve({members: []}));
const alertsListener = new AlertsListener(robot, oVClient, transform);

let msg = {
robot: {},
message: {TextMessage: {user: '', text: '@bot show all active critical alerts from today.'}},
count: 1,
status: 'warning',
state: 'active',
time: 'today',
send: function () {}
};
sinon.spy(msg, "send");

alertsListener.ListFilteredAlerts(msg);

//sleep a momemt to wait for results
setTimeout(() => {
assert(msg.send.callCount === 1);
"I didn't find any warning active alerts from today.".should.equal(msg.send.args[0][0]);
stub.restore();
done();
}, 100);
});

it('list alerts by filter, no match', (done) => {
let stub = sinon.stub(oVClient.Alerts, 'getFilteredAlerts').returns(Bluebird.resolve({members: []}));
const alertsListener = new AlertsListener(robot, oVClient, transform);

let msg = {
robot: {},
message: {TextMessage: {user: '', text: '@bot show all active critical alerts from last 7 days.'}},
count: 1,
status: 'critical',
state: 'active',
time: '7 days',
send: function () {}
};
sinon.spy(msg, "send");

alertsListener.ListFilteredAlerts(msg);

//sleep a momemt to wait for results
setTimeout(() => {
assert(msg.send.callCount === 1);
"I didn't find any critical active alerts from the 7 days.".should.equal(msg.send.args[0][0]);
stub.restore();
done();
}, 100);
});

it('list alerts by filter, no match undef', (done) => {
let stub = sinon.stub(oVClient.Alerts, 'getFilteredAlerts').returns(Bluebird.resolve({members: []}));
const alertsListener = new AlertsListener(robot, oVClient, transform);

let msg = {
robot: {},
message: {TextMessage: {user: '', text: '@bot show all alerts from .'}},
count: 1,
status: undefined,
state: undefined,
time: undefined,
send: function () {}
};
sinon.spy(msg, "send");

alertsListener.ListFilteredAlerts(msg);

//sleep a momemt to wait for results
setTimeout(() => {
assert(msg.send.callCount === 1);
"I didn't find any alerts.".should.equal(msg.send.args[0][0]);
stub.restore();
done();
}, 100);
});

it('list alerts by number error', (done) => {
let stub = sinon.stub(oVClient.Alerts, 'getNumberOfAlerts').returns(Bluebird.reject(err));
const alertsListener = new AlertsListener(robot, oVClient, transform);
Expand All @@ -157,7 +280,7 @@ describe('AlertsListener', () => {
//sleep a momemt to wait for results
setTimeout(() => {
assert(msg.send.callCount === 1);
'Oops there was a problem.\n\nOneView error code: OOPS\n'.should.equal(msg.send.args[0][0]);
"Oops there was a problem.\n\nOneView error code: OOPS\n".should.equal(msg.send.args[0][0]);
stub.restore();
done();
}, 100);
Expand All @@ -183,7 +306,7 @@ describe('AlertsListener', () => {
//sleep a momemt to wait for results
setTimeout(() => {
assert(msg.send.callCount === 1);
'Oops there was a problem.\n\nOneView error code: OOPS\n'.should.equal(msg.send.args[0][0]);
"Oops there was a problem.\n\nOneView error code: OOPS\n".should.equal(msg.send.args[0][0]);
stub.restore();
done();
}, 100);
Expand Down

0 comments on commit a973daf

Please sign in to comment.