forked from fridujo/rabbitmq-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpringIntegrationTest.java
156 lines (128 loc) · 5.94 KB
/
SpringIntegrationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.github.fridujo.rabbitmq.mock.spring;
import static com.github.fridujo.rabbitmq.mock.exchange.MockExchangeCreator.creatorWithExchangeType;
import static java.time.Duration.ofMillis;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import com.github.fridujo.rabbitmq.mock.compatibility.MockConnectionFactoryFactory;
import com.github.fridujo.rabbitmq.mock.exchange.FixDelayExchange;
class SpringIntegrationTest {
private static final String QUEUE_NAME = UUID.randomUUID().toString();
private static final String EXCHANGE_NAME = UUID.randomUUID().toString();
@Test
void basic_get_case() {
String messageBody = "Hello world!";
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AmqpConfiguration.class)) {
RabbitTemplate rabbitTemplate = queueAndExchangeSetup(context);
rabbitTemplate.convertAndSend(EXCHANGE_NAME, "test.key1", messageBody);
Message message = rabbitTemplate.receive(QUEUE_NAME);
assertThat(message).isNotNull();
assertThat(message.getBody()).isEqualTo(messageBody.getBytes());
}
}
@Test
void basic_consume_case() {
String messageBody = "Hello world!";
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AmqpConfiguration.class)) {
RabbitTemplate rabbitTemplate = queueAndExchangeSetup(context);
Receiver receiver = new Receiver();
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(context.getBean(ConnectionFactory.class));
container.setQueueNames(QUEUE_NAME);
container.setMessageListener(new MessageListenerAdapter(receiver, "receiveMessage"));
try {
container.start();
rabbitTemplate.convertAndSend(EXCHANGE_NAME, "test.key2", messageBody);
List<String> receivedMessages = new ArrayList<>();
assertTimeoutPreemptively(ofMillis(500L), () -> {
while (receivedMessages.isEmpty()) {
receivedMessages.addAll(receiver.getMessages());
TimeUnit.MILLISECONDS.sleep(100L);
}
}
);
assertThat(receivedMessages).containsExactly(messageBody);
} finally {
container.stop();
}
}
}
private RabbitTemplate queueAndExchangeSetup(BeanFactory context) {
RabbitAdmin rabbitAdmin = context.getBean(RabbitAdmin.class);
Queue queue = new Queue(QUEUE_NAME, false);
rabbitAdmin.declareQueue(queue);
TopicExchange exchange = new TopicExchange(EXCHANGE_NAME);
rabbitAdmin.declareExchange(exchange);
rabbitAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("test.*"));
return context.getBean(RabbitTemplate.class);
}
@Configuration
static class AmqpConfiguration {
@Bean
ConnectionFactory connectionFactory() {
return new CachingConnectionFactory(
MockConnectionFactoryFactory
.build()
.enableConsistentHashPlugin()
.withAdditionalExchange(creatorWithExchangeType("x-fix-delayed-message", FixDelayExchange::new))
);
}
@Bean
RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory);
}
@Bean
RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
return new RabbitTemplate(connectionFactory);
}
}
@Configuration
@Import(AmqpConfiguration.class)
static class AmqpConsumerConfiguration {
@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(QUEUE_NAME);
container.setMessageListener(listenerAdapter);
return container;
}
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}
@Bean
Receiver receiver() {
return new Receiver();
}
}
static class Receiver {
private final List<String> messages = new ArrayList<>();
public void receiveMessage(String message) {
this.messages.add(message);
}
List<String> getMessages() {
return messages;
}
}
}