Skip to content

Commit cac5934

Browse files
committed
test(amqp-sender): add tests for queue, topic, binding and publish
1 parent 8bfb9a4 commit cac5934

File tree

4 files changed

+94
-9
lines changed

4 files changed

+94
-9
lines changed

src/adapter/amqp/amqp-sender.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ConfirmChannel } from 'amqplib'
44
import {
55
BrokerSender,
66
QueueSpecitication,
7-
ExchangeSpecification,
7+
TopicSpecification,
88
BindingSpecification
99
} from '../../domain/model/broker.model'
1010

@@ -15,7 +15,7 @@ export class AmqpSender implements BrokerSender {
1515
return this.channel.addSetup((channel: ConfirmChannel) => channel.assertQueue(name, rest))
1616
}
1717

18-
declareTopic({ name }: ExchangeSpecification): Promise<void> {
18+
declareTopic({ name }: TopicSpecification): Promise<void> {
1919
return this.channel.addSetup((channel: ConfirmChannel) => channel.assertExchange(name, 'topic'))
2020
}
2121

src/domain/model/broker.model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export interface QueueSpecitication {
33
exclusive: boolean
44
}
55

6-
export interface ExchangeSpecification {
6+
export interface TopicSpecification {
77
name: string
88
}
99

@@ -23,7 +23,7 @@ export type MessageConsumer = (messageContent: object) => Promise<void>
2323
export interface BrokerSender {
2424
declareQueue(specification: QueueSpecitication): Promise<void>
2525

26-
declareTopic(specification: ExchangeSpecification): Promise<void>
26+
declareTopic(specification: TopicSpecification): Promise<void>
2727

2828
bind(specification: BindingSpecification): Promise<void>
2929
}

test/adapter/amqp-receiver.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { ChannelWrapper } from 'amqp-connection-manager'
33
import { AmqpReceiver } from '../../src/adapter/amqp/amqp-receiver'
44
import { ConfirmChannel, ConsumeMessage, Message } from 'amqplib'
55

6+
const queueName = 'myQueue'
7+
const messageContent = Buffer.from('{"foo": "bar"}', 'utf8')
8+
69
describe(`AMQP receiver`, () => {
7-
const queueName = 'myQueue'
8-
const messageContent = Buffer.from('{"foo": "bar"}', 'utf8')
910
let addSetupMock: jest.MockedFunction<
1011
(func: (channel: ConfirmChannel) => Promise<void>) => Promise<void>
1112
>

test/adapter/amqp-sender.test.ts

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,91 @@
1+
import { AmqpSender } from '../../src/adapter/amqp/amqp-sender'
2+
3+
import { ConfirmChannel } from 'amqplib'
4+
import { ChannelWrapper } from 'amqp-connection-manager'
5+
import {
6+
QueueSpecitication,
7+
TopicSpecification,
8+
BindingSpecification
9+
} from '../../src/domain/model/broker.model'
10+
11+
const queueName = 'myQueue'
12+
const topicName = 'mytopic'
13+
const routingKey = 'myRoutingKey'
14+
115
describe(`AMQP sender`, () => {
2-
it(`declares a queue`, () => {})
16+
let addSetupMock: jest.MockedFunction<
17+
(func: (channel: ConfirmChannel) => Promise<void>) => Promise<void>
18+
>
19+
let publishMock: jest.MockedFunction<
20+
(exchange: string, routingKey: string, content: Buffer) => Promise<void>
21+
>
22+
let channelWrapper: ChannelWrapper
23+
let sender: AmqpSender
24+
25+
beforeEach(() => {
26+
jest.clearAllMocks()
27+
addSetupMock = jest.fn(func => Promise.resolve())
28+
publishMock = jest.fn((exchange, routingKey, content) => Promise.resolve())
29+
channelWrapper = ({ addSetup: addSetupMock, publish: publishMock } as any) as ChannelWrapper
30+
sender = new AmqpSender(channelWrapper)
31+
})
32+
33+
it(`declares a queue`, async () => {
34+
const queue: QueueSpecitication = {
35+
name: queueName,
36+
exclusive: true
37+
}
38+
39+
const assertQueueMock = jest.fn((name, options) => undefined)
40+
const channel = ({ assertQueue: assertQueueMock } as any) as ConfirmChannel
41+
42+
await sender.declareQueue(queue)
43+
await addSetupMock.mock.calls[0][0](channel)
44+
45+
expect(assertQueueMock.mock.calls[0][0]).toBe(queueName)
46+
expect(assertQueueMock.mock.calls[0][1]).toStrictEqual({
47+
exclusive: true
48+
})
49+
})
50+
51+
it(`declares a topic`, async () => {
52+
const topic: TopicSpecification = {
53+
name: topicName
54+
}
55+
56+
const assertExchangeMock = jest.fn((name, type) => undefined)
57+
const channel = ({ assertExchange: assertExchangeMock } as any) as ConfirmChannel
58+
59+
await sender.declareTopic(topic)
60+
await addSetupMock.mock.calls[0][0](channel)
61+
62+
expect(assertExchangeMock.mock.calls[0][0]).toBe(topicName)
63+
expect(assertExchangeMock.mock.calls[0][1]).toEqual('topic')
64+
})
65+
66+
it(`do a binding`, async () => {
67+
const binding: BindingSpecification = {
68+
queue: queueName,
69+
topic: topicName,
70+
routingKey: routingKey
71+
}
72+
73+
const bindQueueMock = jest.fn((queue, topic, routingKey) => undefined)
74+
const channel = ({ bindQueue: bindQueueMock } as any) as ConfirmChannel
75+
76+
await sender.bind(binding)
77+
await addSetupMock.mock.calls[0][0](channel)
78+
79+
expect(bindQueueMock.mock.calls[0][0]).toBe(queueName)
80+
expect(bindQueueMock.mock.calls[0][1]).toEqual(topicName)
81+
expect(bindQueueMock.mock.calls[0][2]).toEqual(routingKey)
82+
})
383

4-
it(`declares a topic`, () => {})
84+
it(`publish messages`, async () => {
85+
await sender.publish(topicName, routingKey, { foo: 'bar' })
586

6-
it(`do a binding`, () => {})
87+
expect(publishMock.mock.calls[0][0]).toBe(topicName)
88+
expect(publishMock.mock.calls[0][1]).toBe(routingKey)
89+
expect(publishMock.mock.calls[0][2]).toStrictEqual({ foo: 'bar' })
90+
})
791
})

0 commit comments

Comments
 (0)