Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

producer-rabbit-middleware - changing expected message serialization #295

Merged
merged 1 commit into from
Aug 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
package com.example;

// remove::start[]

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.annotation.Nullable;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.assertj.core.api.BDDAssertions;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
Expand All @@ -51,15 +51,16 @@
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = { TestConfig.class, Application.class }, properties = "stubrunner.amqp.mockConnection=false")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = {TestConfig.class, Application.class}, properties = "stubrunner.amqp.mockConnection=false")
@AutoConfigureStubRunner(ids = "com.example:beer-api-producer-rabbit-middleware", stubsMode = StubRunnerProperties.StubsMode.LOCAL)
@Testcontainers
@ActiveProfiles("test")
// remove::end[]
public class ApplicationTests {

// remove::start[]
@Container static RabbitMQContainer rabbit = new RabbitMQContainer();
@Container
static RabbitMQContainer rabbit = new RabbitMQContainer();

@DynamicPropertySource
static void rabbitProperties(DynamicPropertyRegistry registry) {
Expand All @@ -78,7 +79,7 @@ public void contextLoads() {

Awaitility.await().untilAsserted(() -> {
BDDAssertions.then(this.application.storedFoo).isNotNull();
BDDAssertions.then(this.application.storedFoo.getFoo()).contains("example");
BDDAssertions.then(this.application.storedFoo.getFoo()).isEqualTo("example");
});
}
// remove::end[]
Expand All @@ -105,7 +106,7 @@ public Message receive(String destination, YamlContract contract) {

@Override
public void send(Message message, String destination, @Nullable YamlContract contract) {
rabbitTemplate.send(destination, message);
rabbitTemplate.send(destination, message);
}

@Override
Expand All @@ -114,12 +115,12 @@ public <T> void send(T payload, Map<String, Object> headers, String destination,
MessageProperties messageProperties = new MessageProperties();
newHeaders.forEach(messageProperties::setHeader);
log.info("Sending a message to destination [{}] with routing key", destination);
try {
Message message = MessageBuilder.withBody(new ObjectMapper().writeValueAsBytes(payload)).andProperties(messageProperties).build();
if (payload instanceof String) {
String json = (String) payload;
Message message = MessageBuilder.withBody(json.getBytes(StandardCharsets.UTF_8)).andProperties(messageProperties).build();
send(message, destination, contract);
}
catch (JsonProcessingException e) {
throw new IllegalStateException(e);
} else {
throw new IllegalStateException("Payload is not a String");
}
}
};
Expand Down