Skip to content

Commit

Permalink
changing expected message serialization (#295)
Browse files Browse the repository at this point in the history
Co-authored-by: Michal Krzywanski <[email protected]>
  • Loading branch information
mkrzywanski and mkrzywanski authored Aug 9, 2021
1 parent a7159bc commit ebb0333
Showing 1 changed file with 12 additions and 11 deletions.
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

0 comments on commit ebb0333

Please sign in to comment.