Skip to content

Commit 94ea44f

Browse files
author
akuksin
committed
Add integration test for RPC with Spring Boot AMQP.
1 parent 3c12cfb commit 94ea44f

File tree

5 files changed

+79
-12
lines changed

5 files changed

+79
-12
lines changed

spring-boot/request-response/client/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ dependencies {
3030
implementation 'com.fasterxml.jackson.core:jackson-core:2.9.6'
3131
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.9.6'
3232
implementation 'com.fasterxml.jackson.core:jackson-databind:2.9.6'
33+
34+
testCompile "org.testcontainers:rabbitmq:1.14.3"
3335
}
3436

3537
test {

spring-boot/request-response/client/src/main/java/io/reflectoring/client/rpc/EventPublisher.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public class EventPublisher {
2525

2626
private final DirectExchange directExchange;
2727

28-
public EventPublisher(DirectExchange directExchange, RabbitTemplate template, AsyncRabbitTemplate asyncRabbitTemplate) {
28+
public EventPublisher(DirectExchange directExchange, RabbitTemplate template,
29+
AsyncRabbitTemplate asyncRabbitTemplate) {
2930
this.directExchange = directExchange;
3031
this.template = template;
3132
this.asyncRabbitTemplate = asyncRabbitTemplate;
@@ -36,38 +37,41 @@ public void send() {
3637
String key = "vw";
3738
Car car = Car.builder()
3839
.id(UUID.randomUUID())
39-
// TODO get random color and name.
4040
.color("white")
4141
.name("vw")
4242
.build();
4343
LOGGER.info("Sending message with routing key {} and id {}", key, car.getId());
4444

45-
ParameterizedTypeReference<Registration> responseType = new ParameterizedTypeReference<>() {
45+
ParameterizedTypeReference<Registration> responseType
46+
= new ParameterizedTypeReference<>() {
4647
};
47-
Registration registration = template.convertSendAndReceiveAsType(directExchange.getName(), key, car, responseType);
48+
Registration registration = template.convertSendAndReceiveAsType(
49+
directExchange.getName(), key, car, responseType);
4850
LOGGER.info("Message received: {}", registration);
4951
}
5052

53+
5154
@Scheduled(fixedDelay = 3000, initialDelay = 1500)
5255
public void sendAsynchronously() {
5356
String key = "vw";
5457
Car car = Car.builder()
5558
.id(UUID.randomUUID())
56-
// TODO get random color and name.
57-
.color("white")
58-
.name("vw")
59+
.color("black")
60+
.name("bmw")
5961
.build();
6062
LOGGER.info("Sending message with routing key {} and id {}", key, car.getId());
6163

62-
ParameterizedTypeReference<Registration> responseType = new ParameterizedTypeReference<>() {
64+
ParameterizedTypeReference<Registration> responseType
65+
= new ParameterizedTypeReference<>() {
6366
};
6467
AsyncRabbitTemplate.RabbitConverterFuture<Registration> future =
65-
asyncRabbitTemplate.convertSendAndReceiveAsType(directExchange.getName(), key, car, responseType);
68+
asyncRabbitTemplate.convertSendAndReceiveAsType(
69+
directExchange.getName(), key, car, responseType);
6670
try {
6771
Registration registration = future.get();
6872
LOGGER.info("Asynchronous message received: {}", registration);
6973
} catch (InterruptedException | ExecutionException e) {
70-
LOGGER.error("Cannot get response.");
74+
LOGGER.error("Cannot get response.", e);
7175
}
7276
}
7377
}

spring-boot/request-response/client/src/main/java/io/reflectoring/client/rpc/PublisherConfiguration.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
import org.springframework.amqp.support.converter.MessageConverter;
88
import org.springframework.context.annotation.Bean;
99
import org.springframework.context.annotation.Configuration;
10-
import org.springframework.scheduling.annotation.EnableScheduling;
1110

1211
@Configuration
13-
@EnableScheduling
1412
public class PublisherConfiguration {
1513

1614

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.reflectoring.client.rpc;
2+
3+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.scheduling.annotation.EnableScheduling;
6+
7+
@ConditionalOnProperty(
8+
value = "scheduling.enable",
9+
havingValue = "true",
10+
matchIfMissing = true
11+
)
12+
@Configuration
13+
@EnableScheduling
14+
public class SchedulingConfiguration {
15+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package io.reflectoring.client.rpc;
2+
3+
import org.assertj.core.api.ThrowableAssert;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.test.context.TestPropertySource;
9+
import org.testcontainers.containers.RabbitMQContainer;
10+
11+
import static org.assertj.core.api.Assertions.assertThatCode;
12+
13+
@SpringBootTest
14+
@TestPropertySource(properties = "scheduling.enable=false")
15+
class EventPublisherTest {
16+
17+
private final RabbitMQContainer rabbitMQContainer = new RabbitMQContainer();
18+
19+
@Autowired
20+
private EventPublisher eventPublisher;
21+
22+
@BeforeEach
23+
void setUp() {
24+
rabbitMQContainer.start();
25+
}
26+
27+
@Test
28+
void sendMessageSynchronously() {
29+
// given
30+
31+
// when
32+
ThrowableAssert.ThrowingCallable send = () -> eventPublisher.send();
33+
34+
// then
35+
assertThatCode(send).doesNotThrowAnyException();
36+
}
37+
38+
@Test
39+
void sendMessageAsynchronously() {
40+
// given
41+
42+
// when
43+
ThrowableAssert.ThrowingCallable send = () -> eventPublisher.sendAsynchronously();
44+
45+
// then
46+
assertThatCode(send).doesNotThrowAnyException();
47+
}
48+
}

0 commit comments

Comments
 (0)