Skip to content

Commit

Permalink
add unit tests + change EventValidatorInterface to return promise whi…
Browse files Browse the repository at this point in the history
…ch resolves to boolean (#2)
  • Loading branch information
matthewgoslett authored May 9, 2017
1 parent d9d8d46 commit 5a274d6
Show file tree
Hide file tree
Showing 15 changed files with 1,310 additions and 35 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: node_js

node_js:
- "node"
- "7"
- "6"
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,14 @@ class CustomValidator {
* Validates an event.
*
* @param {EventInterface} event
* @return {Promise}
* @return {Promise<boolean>}
* @example
* validator.validates(event).then(() => {
* // event validates!
* }).catch((reason) => {
* // event failed validation
* console.log(reason);
* validator.validates(event).then((success) => {
* if (success) {
* console.log('event validates!');
* } else {
* console.log('event failed validation');
* }
* });
*/
validates(event) {
Expand Down
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 1.0.0 - ?

* Add unit tests
* Bump @superbalist/js-pubsub to ^1.0.0
* Change `EventValidatorInterface` to return a promise which resolves to a boolean

## 0.0.1 - 2017-05-03

* Initial release
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "An event protocol and implementation over pub/sub",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha test",
"coverage": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- --ui bdd -R spec -t 5000"
},
"repository": {
"type": "git",
Expand All @@ -24,15 +25,20 @@
},
"homepage": "https://github.com/Superbalist/js-event-pubsub#readme",
"dependencies": {
"@superbalist/js-pubsub": "0.0.1",
"@superbalist/js-pubsub": "^1.0.0",
"ajv": "^5.0.1",
"request": "^2.81.0",
"request-promise-native": "^1.0.3",
"semver": "^5.3.0",
"uuid": "^3.0.1"
},
"devDependencies": {
"chai": "^3.5.0",
"chai-as-promised": "^6.0.0",
"eslint": "^3.19.0",
"eslint-config-google": "^0.7.1"
"eslint-config-google": "^0.7.1",
"istanbul": "^0.4.5",
"mocha": "^3.3.0",
"sinon": "^2.2.0"
}
}
8 changes: 5 additions & 3 deletions src/EventManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ class EventManager {
// not using a validator
handler(event);
} else {
this.validator.validates(event).then(() => {
// event passed validation
handler(event);
this.validator.validates(event).then((success) => {
if (success) {
// event passed validation
handler(event);
}
});
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/EventValidatorInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ class EventValidatorInterface {
* Validates an event.
*
* @param {EventInterface} event
* @return {Promise}
* @return {Promise<boolean>}
* @example
* validator.validates(event).then(() => {
* // event validates!
* }).catch((reason) => {
* // event failed validation
* console.log(reason);
* validator.validates(event).then((success) => {
* if (success) {
* console.log('event validates!');
* } else {
* console.log('event failed validation');
* }
* });
*/
validates(event) {
Expand Down
2 changes: 1 addition & 1 deletion src/translators/TopicEventMessageTranslator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TopicEventMessageTranslator {
return null;
}

// we must have an event property
// we must have all required properties
for (let key of ['topic', 'event', 'version']) {
if (!message.hasOwnProperty(key)) {
return null;
Expand Down
27 changes: 11 additions & 16 deletions src/validators/JSONSchemaEventValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class JSONSchemaEventValidator {
/**
* Construct a JSONSchemaEventValidator
*
* @param {?ajv} [ajv=null]
* @param {?Ajv} [ajv=null]
*/
constructor(ajv = null) {
/**
* @type {ajv}
* @type {Ajv}
*/
this.ajv = ajv || JSONSchemaEventValidator.makeDefaultAjv();
}
Expand All @@ -25,33 +25,28 @@ class JSONSchemaEventValidator {
* Validates an event.
*
* @param {SchemaEvent} event
* @return {Promise}
* @return {Promise<boolean>}
* @example
* validator.validates(event).then(() => {
* // event validates!
* }).catch((reason) => {
* // event failed validation
* console.log(reason);
* validator.validates(event).then((success) => {
* if (success) {
* console.log('event validates!');
* } else {
* console.log('event failed validation');
* }
* });
*/
validates(event) {
return this.ajv.compileAsync({$ref: event.schema})
.then(function(validate) {
return new Promise(function(resolve, reject) {
if (validate(event.toMessage())) {
resolve();
} else {
reject(validate.errors);
}
});
return validate(event.toMessage());
}
);
}

/**
* Factory a default instance of ajv.
*
* @return {ajv}
* @return {Ajv}
*/
static makeDefaultAjv() {
let ajv = new Ajv({
Expand Down
73 changes: 73 additions & 0 deletions test/AttributeInjectorsTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict';

let chai = require('chai');
let expect = chai.expect;
let dateAttributeInjector = require('../src/attribute_injectors/date_attribute_injector');
let hostnameAttributeInjector = require('../src/attribute_injectors/hostname_attribute_injector');
let uuid4AttributeInjector = require('../src/attribute_injectors/uuid4_attribute_injector');
let os = require('os');

describe('Attribute Injectors', () => {
describe('date_attribute_injector', () => {
it('should be a function ', () => {
expect(dateAttributeInjector).to.be.a('function');
});

it('when called, should return an object with "key" and "value" properties', () => {
let resolved = dateAttributeInjector();
expect(resolved).to.have.all.keys(['key', 'value']);
});

it('when called, should return an object with a "key" property set to "date"', () => {
let resolved = dateAttributeInjector();
expect(resolved.key).to.equal('date');
});

it('when called, should return an object with a "value" property set to the current ISO 8601 date"', () => {
let resolved = dateAttributeInjector();
expect(resolved.value).to.equal((new Date()).toISOString());
});
});

describe('hostname_attribute_injector', () => {
it('should be a function ', () => {
expect(hostnameAttributeInjector).to.be.a('function');
});

it('when called, should return an object with "key" and "value" properties', () => {
let resolved = hostnameAttributeInjector();
expect(resolved).to.have.all.keys(['key', 'value']);
});

it('when called, should return an object with a "key" property set to "hostname"', () => {
let resolved = hostnameAttributeInjector();
expect(resolved.key).to.equal('hostname');
});

it('when called, should return an object with a "value" property set to the hostname"', () => {
let resolved = hostnameAttributeInjector();
expect(resolved.value).to.equal(os.hostname());
});
});

describe('uuid4_attribute_injector', () => {
it('should be a function ', () => {
expect(uuid4AttributeInjector).to.be.a('function');
});

it('when called, should return an object with "key" and "value" properties', () => {
let resolved = uuid4AttributeInjector();
expect(resolved).to.have.all.keys(['key', 'value']);
});

it('when called, should return an object with a "key" property set to "uuid"', () => {
let resolved = uuid4AttributeInjector();
expect(resolved.key).to.equal('uuid');
});

it('when called, should return an object with a "value" property matching a uui4 regex"', () => {
let resolved = uuid4AttributeInjector();
expect(resolved.value).to.match(/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i);
});
});
});
Loading

0 comments on commit 5a274d6

Please sign in to comment.