Skip to content

Commit

Permalink
tests: refactoring + add yaml endpoint testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Pascal Dal Farra committed Jun 5, 2024
1 parent 3d7aa93 commit 65fed2d
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ springwolf-bindings/springwolf-sqs-binding/build/
node_modules/

asyncapi.actual.json
asyncapi.actual.yaml

# Eclipse IDE
.classpath
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.springwolf.examples.amqp;

public class AmqpConstants {

public static final String EXAMPLE_TOPIC_EXCHANGE = "example-topic-exchange";

public static final String EXAMPLE_TOPIC_ROUTING_KEY = "example-topic-routing-key";

public static final String EXAMPLE_QUEUE = "example-queue";
public static final String ANOTHER_QUEUE = "another-queue";
public static final String MULTI_PAYLOAD_QUEUE = "multi-payload-queue";

public static final String EXAMPLE_BINDINGS_QUEUE = "example-bindings-queue";
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.springwolf.examples.amqp.configuration;

import io.github.springwolf.examples.amqp.AmqpConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Exchange;
Expand All @@ -23,34 +24,34 @@ public Jackson2JsonMessageConverter converter() {

@Bean
public Queue exampleQueue() {
return new Queue("example-queue", false);
return new Queue(AmqpConstants.EXAMPLE_QUEUE, false);
}

@Bean
public Queue anotherQueue() {
return new Queue("another-queue", false);
return new Queue(AmqpConstants.ANOTHER_QUEUE, false);
}

@Bean
public Queue exampleBindingsQueue() {
return new Queue("example-bindings-queue", false, true, true);
return new Queue(AmqpConstants.EXAMPLE_BINDINGS_QUEUE, false, true, true);
}

@Bean
public Exchange exampleTopicExchange() {
return new TopicExchange("example-topic-exchange");
return new TopicExchange(AmqpConstants.EXAMPLE_TOPIC_EXCHANGE);
}

@Bean
public Queue multiPayloadQueue() {
return new Queue("multi-payload-queue");
return new Queue(AmqpConstants.MULTI_PAYLOAD_QUEUE);
}

@Bean
public Binding exampleTopicBinding(Queue exampleBindingsQueue, Exchange exampleTopicExchange) {
return BindingBuilder.bind(exampleBindingsQueue)
.to(exampleTopicExchange)
.with("example-topic-routing-key")
.with(AmqpConstants.EXAMPLE_TOPIC_ROUTING_KEY)
.noargs();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.springwolf.examples.amqp.consumers;

import io.github.springwolf.examples.amqp.AmqpConstants;
import io.github.springwolf.examples.amqp.dtos.AnotherPayloadDto;
import io.github.springwolf.examples.amqp.dtos.ExamplePayloadDto;
import io.github.springwolf.examples.amqp.producers.AnotherProducer;
Expand All @@ -20,9 +21,9 @@ public class ExampleConsumer {

private final AnotherProducer anotherProducer;

@RabbitListener(queues = "example-queue")
@RabbitListener(queues = AmqpConstants.EXAMPLE_QUEUE)
public void receiveExamplePayload(ExamplePayloadDto payload) {
log.info("Received new message in example-queue: {}", payload.toString());
log.info("Received new message in {}: {}", AmqpConstants.EXAMPLE_QUEUE, payload.toString());

AnotherPayloadDto example = new AnotherPayloadDto();
example.setExample(payload);
Expand All @@ -31,37 +32,45 @@ public void receiveExamplePayload(ExamplePayloadDto payload) {
anotherProducer.sendMessage(example);
}

@RabbitListener(queues = "another-queue")
@RabbitListener(queues = AmqpConstants.ANOTHER_QUEUE)
public void receiveAnotherPayload(AnotherPayloadDto payload) {
log.info("Received new message in another-queue: {}", payload.toString());
log.info("Received new message in {}: {}", AmqpConstants.ANOTHER_QUEUE, payload.toString());
}

@RabbitListener(
bindings = {
@QueueBinding(
exchange = @Exchange(name = "example-topic-exchange", type = ExchangeTypes.TOPIC),
exchange = @Exchange(name = AmqpConstants.EXAMPLE_TOPIC_EXCHANGE, type = ExchangeTypes.TOPIC),
value =
@Queue(
name = "example-bindings-queue",
name = AmqpConstants.EXAMPLE_BINDINGS_QUEUE,
durable = "false",
exclusive = "true",
autoDelete = "true"),
key = "example-topic-routing-key")
key = AmqpConstants.EXAMPLE_TOPIC_ROUTING_KEY)
})
public void bindingsExample(AnotherPayloadDto payload) {
log.info(
"Received new message in example-bindings-queue"
+ " through exchange example-topic-exchange using routing key example-topic-routing-key: {}",
"Received new message in {}" + " through exchange {}" + " using routing key {}: {}",
AmqpConstants.EXAMPLE_BINDINGS_QUEUE,
AmqpConstants.EXAMPLE_TOPIC_EXCHANGE,
AmqpConstants.EXAMPLE_TOPIC_ROUTING_KEY,
payload.toString());
}

@RabbitListener(queues = "multi-payload-queue")
@RabbitListener(queues = AmqpConstants.MULTI_PAYLOAD_QUEUE)
public void bindingsBeanExample(AnotherPayloadDto payload) {
log.info("Received new message in multi-payload-queue (AnotherPayloadDto): {}", payload.toString());
log.info(
"Received new message in {} (AnotherPayloadDto): {}",
AmqpConstants.MULTI_PAYLOAD_QUEUE,
payload.toString());
}

@RabbitListener(queues = "multi-payload-queue")
@RabbitListener(queues = AmqpConstants.MULTI_PAYLOAD_QUEUE)
public void bindingsBeanExample(ExamplePayloadDto payload) {
log.info("Received new message in multi-payload-queue (ExamplePayloadDto): {}", payload.toString());
log.info(
"Received new message in {} (ExamplePayloadDto): {}",
AmqpConstants.MULTI_PAYLOAD_QUEUE,
payload.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import io.github.springwolf.core.asyncapi.annotations.AsyncOperation;
import io.github.springwolf.core.asyncapi.annotations.AsyncPublisher;
import io.github.springwolf.examples.amqp.AmqpConstants;
import io.github.springwolf.examples.amqp.dtos.AnotherPayloadDto;
import io.github.springwolf.plugins.amqp.asyncapi.annotations.AmqpAsyncOperationBinding;
import lombok.RequiredArgsConstructor;
Expand All @@ -17,11 +18,12 @@ public class AnotherProducer {
@AsyncPublisher(
operation =
@AsyncOperation(
channelName = "example-topic-exchange",
channelName = AmqpConstants.EXAMPLE_TOPIC_EXCHANGE,
description = "Custom, optional description defined in the AsyncPublisher annotation"))
@AmqpAsyncOperationBinding()
public void sendMessage(AnotherPayloadDto msg) {
// send
rabbitTemplate.convertAndSend("example-topic-exchange", "example-topic-routing-key", msg);
rabbitTemplate.convertAndSend(
AmqpConstants.EXAMPLE_TOPIC_EXCHANGE, AmqpConstants.EXAMPLE_TOPIC_ROUTING_KEY, msg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,36 @@ class ApiIntegrationTest {
private TestRestTemplate restTemplate;

@Test
void asyncApiResourceArtifactTest() throws IOException {
void asyncApiResourceJsonArtifactTest() throws IOException {
String url = "/springwolf/docs";
String actual = restTemplate.getForObject(url, String.class);
Files.writeString(Path.of("src", "test", "resources", "asyncapi.actual.json"), actual);

InputStream s = this.getClass().getResourceAsStream("/asyncapi.json");
String expected = new String(s.readAllBytes(), StandardCharsets.UTF_8).trim();
String expected;
try (InputStream s = this.getClass().getResourceAsStream("/asyncapi.json")) {
assert s != null;
expected = new String(s.readAllBytes(), StandardCharsets.UTF_8).trim();
}

assertEquals(expected, actual);
}

@Test
void asyncApiResourceYamlArtifactTest() throws IOException {
String url = "/springwolf/docs.yaml";
String actual = restTemplate
.getForObject(url, String.class)
.replaceAll("\r", "")
.trim();
Files.writeString(Path.of("src", "test", "resources", "asyncapi.actual.yaml"), actual);

String expected;
try (InputStream s = this.getClass().getResourceAsStream("/asyncapi.yaml")) {
assert s != null;
expected = new String(s.readAllBytes(), StandardCharsets.UTF_8)
.replaceAll("\r", "")
.trim();
}

assertEquals(expected, actual);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void producerCanUseSpringwolfConfigurationToSendMessage() {
payload.setSomeEnum(FOO1);

// when
springwolfAmqpProducer.send("example-queue", payload);
springwolfAmqpProducer.send(AmqpConstants.EXAMPLE_QUEUE, payload);

// then
verify(exampleConsumer, timeout(10000)).receiveExamplePayload(payload);
Expand Down
Loading

0 comments on commit 65fed2d

Please sign in to comment.