Add integration test for RPC with Spring Boot AMQP.
This commit is contained in:
@@ -30,6 +30,8 @@ dependencies {
|
||||
implementation 'com.fasterxml.jackson.core:jackson-core:2.9.6'
|
||||
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.9.6'
|
||||
implementation 'com.fasterxml.jackson.core:jackson-databind:2.9.6'
|
||||
|
||||
testCompile "org.testcontainers:rabbitmq:1.14.3"
|
||||
}
|
||||
|
||||
test {
|
||||
|
||||
@@ -25,7 +25,8 @@ public class EventPublisher {
|
||||
|
||||
private final DirectExchange directExchange;
|
||||
|
||||
public EventPublisher(DirectExchange directExchange, RabbitTemplate template, AsyncRabbitTemplate asyncRabbitTemplate) {
|
||||
public EventPublisher(DirectExchange directExchange, RabbitTemplate template,
|
||||
AsyncRabbitTemplate asyncRabbitTemplate) {
|
||||
this.directExchange = directExchange;
|
||||
this.template = template;
|
||||
this.asyncRabbitTemplate = asyncRabbitTemplate;
|
||||
@@ -36,38 +37,41 @@ public class EventPublisher {
|
||||
String key = "vw";
|
||||
Car car = Car.builder()
|
||||
.id(UUID.randomUUID())
|
||||
// TODO get random color and name.
|
||||
.color("white")
|
||||
.name("vw")
|
||||
.build();
|
||||
LOGGER.info("Sending message with routing key {} and id {}", key, car.getId());
|
||||
|
||||
ParameterizedTypeReference<Registration> responseType = new ParameterizedTypeReference<>() {
|
||||
ParameterizedTypeReference<Registration> responseType
|
||||
= new ParameterizedTypeReference<>() {
|
||||
};
|
||||
Registration registration = template.convertSendAndReceiveAsType(directExchange.getName(), key, car, responseType);
|
||||
Registration registration = template.convertSendAndReceiveAsType(
|
||||
directExchange.getName(), key, car, responseType);
|
||||
LOGGER.info("Message received: {}", registration);
|
||||
}
|
||||
|
||||
|
||||
@Scheduled(fixedDelay = 3000, initialDelay = 1500)
|
||||
public void sendAsynchronously() {
|
||||
String key = "vw";
|
||||
Car car = Car.builder()
|
||||
.id(UUID.randomUUID())
|
||||
// TODO get random color and name.
|
||||
.color("white")
|
||||
.name("vw")
|
||||
.color("black")
|
||||
.name("bmw")
|
||||
.build();
|
||||
LOGGER.info("Sending message with routing key {} and id {}", key, car.getId());
|
||||
|
||||
ParameterizedTypeReference<Registration> responseType = new ParameterizedTypeReference<>() {
|
||||
ParameterizedTypeReference<Registration> responseType
|
||||
= new ParameterizedTypeReference<>() {
|
||||
};
|
||||
AsyncRabbitTemplate.RabbitConverterFuture<Registration> future =
|
||||
asyncRabbitTemplate.convertSendAndReceiveAsType(directExchange.getName(), key, car, responseType);
|
||||
asyncRabbitTemplate.convertSendAndReceiveAsType(
|
||||
directExchange.getName(), key, car, responseType);
|
||||
try {
|
||||
Registration registration = future.get();
|
||||
LOGGER.info("Asynchronous message received: {}", registration);
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
LOGGER.error("Cannot get response.");
|
||||
LOGGER.error("Cannot get response.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,8 @@ import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
|
||||
import org.springframework.amqp.support.converter.MessageConverter;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@Configuration
|
||||
@EnableScheduling
|
||||
public class PublisherConfiguration {
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package io.reflectoring.client.rpc;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@ConditionalOnProperty(
|
||||
value = "scheduling.enable",
|
||||
havingValue = "true",
|
||||
matchIfMissing = true
|
||||
)
|
||||
@Configuration
|
||||
@EnableScheduling
|
||||
public class SchedulingConfiguration {
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package io.reflectoring.client.rpc;
|
||||
|
||||
import org.assertj.core.api.ThrowableAssert;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.testcontainers.containers.RabbitMQContainer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
|
||||
@SpringBootTest
|
||||
@TestPropertySource(properties = "scheduling.enable=false")
|
||||
class EventPublisherTest {
|
||||
|
||||
private final RabbitMQContainer rabbitMQContainer = new RabbitMQContainer();
|
||||
|
||||
@Autowired
|
||||
private EventPublisher eventPublisher;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
rabbitMQContainer.start();
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendMessageSynchronously() {
|
||||
// given
|
||||
|
||||
// when
|
||||
ThrowableAssert.ThrowingCallable send = () -> eventPublisher.send();
|
||||
|
||||
// then
|
||||
assertThatCode(send).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendMessageAsynchronously() {
|
||||
// given
|
||||
|
||||
// when
|
||||
ThrowableAssert.ThrowingCallable send = () -> eventPublisher.sendAsynchronously();
|
||||
|
||||
// then
|
||||
assertThatCode(send).doesNotThrowAnyException();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user