node message consumer example

This commit is contained in:
Tom Hombergs
2018-11-11 21:55:18 +01:00
parent 382c307ae9
commit a4c3f21391
5 changed files with 2698 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
class HeroCreatedMessage {
constructor(name, superpower, universe, id) {
this.id = id;
this.name = name;
this.superpower = superpower;
this.universe = universe;
}
static validateUniverse(message) {
if (typeof message.universe !== 'string') {
throw new Error(`Hero universe must be a string! Invalid value: ${message.universe}`)
}
}
static validateSuperpower(message) {
if (typeof message.superpower !== 'string') {
throw new Error(`Hero superpower must be a string! Invalid value: ${message.superpower}`)
}
}
static validateName(message) {
if (typeof message.name !== 'string') {
throw new Error(`Hero name must be a string! Invalid value: ${message.name}`);
}
}
static validateId(message) {
if (typeof message.id !== 'number') {
throw new Error(`Hero id must be a number! Invalid value: ${message.id}`)
}
}
}
module.exports = HeroCreatedMessage;

View File

@@ -0,0 +1,15 @@
const HeroCreatedMessage = require('../common/hero-created-message');
function handleHeroCreatedEvent(message) {
HeroCreatedMessage.validateId(message);
HeroCreatedMessage.validateName(message);
HeroCreatedMessage.validateSuperpower(message);
HeroCreatedMessage.validateUniverse(message);
// pass message into business logic
// note that the business logic should be mocked away for the contract test
}
module.exports = handleHeroCreatedEvent;

View File

@@ -0,0 +1,35 @@
const {MessageConsumerPact, Matchers, synchronousBodyHandler} = require('@pact-foundation/pact');
const handleHeroCreatedEvent = require('./hero-event-handler');
const path = require('path');
describe("message consumer", () => {
const messagePact = new MessageConsumerPact({
consumer: "node-message-consumer",
provider: "node-message-provider",
dir: path.resolve(process.cwd(), "pacts"),
pactfileWriteMode: "update",
logLevel: "info",
});
describe("'hero created' message Handler", () => {
it("should accept a valid 'hero created' message", (done) => {
return messagePact
.expectsToReceive("a 'hero created' message")
.withContent({
id: Matchers.like(42),
name: Matchers.like("Superman"),
superpower: Matchers.like("flying"),
universe: Matchers.term({generate: "DC", matcher: "^(DC|Marvel)$"})
})
.withMetadata({
"content-type": "application/json",
})
.verify(synchronousBodyHandler(handleHeroCreatedEvent))
.then(done());
});
});
});

2596
pact/pact-node-messages/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,17 @@
{
"name": "pact-node-messages",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha consumer/*.spec.js",
"pact-message": "pact-message update {}"
},
"author": "Tom Hombergs",
"license": "MIT",
"devDependencies": {
"@pact-foundation/pact": "^7.0.3",
"chai": "^4.2.0",
"mocha": "^5.2.0"
}
}