Skip to content

Commit a4c3f21

Browse files
committed
node message consumer example
1 parent 382c307 commit a4c3f21

5 files changed

Lines changed: 2698 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class HeroCreatedMessage {
2+
3+
constructor(name, superpower, universe, id) {
4+
this.id = id;
5+
this.name = name;
6+
this.superpower = superpower;
7+
this.universe = universe;
8+
}
9+
10+
static validateUniverse(message) {
11+
if (typeof message.universe !== 'string') {
12+
throw new Error(`Hero universe must be a string! Invalid value: ${message.universe}`)
13+
}
14+
}
15+
16+
static validateSuperpower(message) {
17+
if (typeof message.superpower !== 'string') {
18+
throw new Error(`Hero superpower must be a string! Invalid value: ${message.superpower}`)
19+
}
20+
}
21+
22+
static validateName(message) {
23+
if (typeof message.name !== 'string') {
24+
throw new Error(`Hero name must be a string! Invalid value: ${message.name}`);
25+
}
26+
}
27+
28+
static validateId(message) {
29+
if (typeof message.id !== 'number') {
30+
throw new Error(`Hero id must be a number! Invalid value: ${message.id}`)
31+
}
32+
}
33+
}
34+
35+
module.exports = HeroCreatedMessage;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const HeroCreatedMessage = require('../common/hero-created-message');
2+
3+
function handleHeroCreatedEvent(message) {
4+
5+
HeroCreatedMessage.validateId(message);
6+
HeroCreatedMessage.validateName(message);
7+
HeroCreatedMessage.validateSuperpower(message);
8+
HeroCreatedMessage.validateUniverse(message);
9+
10+
// pass message into business logic
11+
// note that the business logic should be mocked away for the contract test
12+
13+
}
14+
15+
module.exports = handleHeroCreatedEvent;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const {MessageConsumerPact, Matchers, synchronousBodyHandler} = require('@pact-foundation/pact');
2+
const handleHeroCreatedEvent = require('./hero-event-handler');
3+
const path = require('path');
4+
5+
describe("message consumer", () => {
6+
7+
const messagePact = new MessageConsumerPact({
8+
consumer: "node-message-consumer",
9+
provider: "node-message-provider",
10+
dir: path.resolve(process.cwd(), "pacts"),
11+
pactfileWriteMode: "update",
12+
logLevel: "info",
13+
});
14+
15+
describe("'hero created' message Handler", () => {
16+
17+
it("should accept a valid 'hero created' message", (done) => {
18+
return messagePact
19+
.expectsToReceive("a 'hero created' message")
20+
.withContent({
21+
id: Matchers.like(42),
22+
name: Matchers.like("Superman"),
23+
superpower: Matchers.like("flying"),
24+
universe: Matchers.term({generate: "DC", matcher: "^(DC|Marvel)$"})
25+
})
26+
.withMetadata({
27+
"content-type": "application/json",
28+
})
29+
.verify(synchronousBodyHandler(handleHeroCreatedEvent))
30+
.then(done());
31+
});
32+
33+
});
34+
35+
});

0 commit comments

Comments
 (0)