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

Units test to investigate AMQP generation #790

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add YAML to ease analysis / investigation


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

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class AmqpConstants {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add constant to ease navigation in code


// Exchanges

public static final String EXCHANGE_EXAMPLE_TOPIC_EXCHANGE = "example-topic-exchange";
public static final String EXCHANGE_CRUD_TOPIC_EXCHANGE_1 = "CRUD-topic-exchange-1";
public static final String EXCHANGE_CRUD_TOPIC_EXCHANGE_2 = "CRUD-topic-exchange-2";
/**
* Direct Exchange - Routing is based on 'Routing key'.
* Messages are 'routed' towards queue which name is equals to 'routing key' <BR/>
* Note:<BR/>
* The 'Default exchange' is a 'Direct exchange' with an empty name ( name= "")
*/
public static final String EXCHANGE_DEFAULT_EXCHANGE = "";

// Routing keys

/**
* When a queue is bound with "#" (hash) binding key,
* it will receive all the messages, regardless of the routing key - like in fanout exchange.
*/
public static final String ROUTING_KEY_ALL_MESSAGES = "#";

public static final String ROUTING_KEY_CRUD = "crud-routing-key";
public static final String ROUTING_KEY_EXAMPLE_TOPIC_ROUTING_KEY = "example-topic-routing-key";

// Queues

public static final String QUEUE_EXAMPLE_QUEUE = "example-queue";
public static final String QUEUE_ANOTHER_QUEUE = "another-queue";
public static final String QUEUE_MULTI_PAYLOAD_QUEUE = "multi-payload-queue";
public static final String QUEUE_EXAMPLE_BINDINGS_QUEUE = "example-bindings-queue";

public static final String QUEUE_CREATE = "queue-create";
public static final String QUEUE_READ = "queue-read";
public static final String QUEUE_DELETE = "queue-delete";
public static final String QUEUE_UPDATE = "queue-update";
}
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,39 @@ public Jackson2JsonMessageConverter converter() {

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

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

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

@Bean
public Queue queueRead() {
return new Queue(AmqpConstants.QUEUE_READ, false);
}

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

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

@Bean
public Binding exampleTopicBinding(Queue exampleBindingsQueue, Exchange exampleTopicExchange) {
return BindingBuilder.bind(exampleBindingsQueue)
.to(exampleTopicExchange)
.with("example-topic-routing-key")
.with(AmqpConstants.ROUTING_KEY_EXAMPLE_TOPIC_ROUTING_KEY)
.noargs();
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
// 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.dtos.GenericPayloadDto;
import io.github.springwolf.examples.amqp.producers.AnotherProducer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

@Component
Expand All @@ -20,9 +24,9 @@ public class ExampleConsumer {

private final AnotherProducer anotherProducer;

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

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

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

@RabbitListener(
bindings = {
@QueueBinding(
exchange = @Exchange(name = "example-topic-exchange", type = ExchangeTypes.TOPIC),
exchange =
@Exchange(
name = AmqpConstants.EXCHANGE_EXAMPLE_TOPIC_EXCHANGE,
type = ExchangeTypes.TOPIC),
value =
@Queue(
name = "example-bindings-queue",
name = AmqpConstants.QUEUE_EXAMPLE_BINDINGS_QUEUE,
durable = "false",
exclusive = "true",
autoDelete = "true"),
key = "example-topic-routing-key")
key = AmqpConstants.ROUTING_KEY_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.QUEUE_EXAMPLE_BINDINGS_QUEUE,
AmqpConstants.EXCHANGE_EXAMPLE_TOPIC_EXCHANGE,
AmqpConstants.ROUTING_KEY_EXAMPLE_TOPIC_ROUTING_KEY,
payload.toString());
}

@RabbitListener(queues = "multi-payload-queue")
@RabbitListener(queues = AmqpConstants.QUEUE_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.QUEUE_MULTI_PAYLOAD_QUEUE,
payload.toString());
}

@RabbitListener(queues = "multi-payload-queue")
@RabbitListener(queues = AmqpConstants.QUEUE_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.QUEUE_MULTI_PAYLOAD_QUEUE,
payload.toString());
}

@RabbitListener(
autoStartup = "false",
queuesToDeclare = @Queue(name = AmqpConstants.QUEUE_CREATE, autoDelete = "false", durable = "true"))
public void queuesToDeclareCreate(Message message, @Payload GenericPayloadDto<String> payload) {
log.info(
"Received new message {} in {} (GenericPayloadDto<String>): {}",
message,
AmqpConstants.QUEUE_CREATE,
payload.toString());
}

@RabbitListener(
autoStartup = "false",
queuesToDeclare = @Queue(name = AmqpConstants.QUEUE_DELETE, autoDelete = "false", durable = "true"))
public void queuesToDeclareDelete(Message message, @Payload GenericPayloadDto<Long> payload) {
log.info(
"Received new message {} in {} (GenericPayloadDto<Long>): {}",
message,
AmqpConstants.QUEUE_DELETE,
payload.toString());
}

@RabbitListener(
autoStartup = "false",
bindings =
@QueueBinding(
exchange =
@Exchange(
name = AmqpConstants.EXCHANGE_CRUD_TOPIC_EXCHANGE_1,
type = ExchangeTypes.TOPIC),
key = AmqpConstants.ROUTING_KEY_ALL_MESSAGES,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using "#" routing key

value = @Queue(name = AmqpConstants.QUEUE_UPDATE, durable = "true", autoDelete = "false")))
public void bindingsUpdate(Message message, @Payload GenericPayloadDto<ExamplePayloadDto> payload) {
log.info(
"Received new message {} in {} (GenericPayloadDto<ExamplePayloadDto>): {}",
message,
AmqpConstants.QUEUE_UPDATE,
payload.toString());
}

@RabbitListener(
autoStartup = "false",
bindings =
@QueueBinding(
exchange =
@Exchange(
name = AmqpConstants.EXCHANGE_CRUD_TOPIC_EXCHANGE_2,
type = ExchangeTypes.TOPIC),
key = AmqpConstants.ROUTING_KEY_ALL_MESSAGES,
value = @Queue(name = AmqpConstants.QUEUE_READ, durable = "true", autoDelete = "false")))
public void bindingsRead(Message message, @Payload ExamplePayloadDto payload) {
log.info(
"Received new message {} in {} (ExamplePayloadDto): {}",
message,
AmqpConstants.QUEUE_UPDATE,
payload.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.springwolf.examples.amqp.dtos;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;

@Schema(description = "Generic payload model")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GenericPayloadDto<T> {

@Schema(description = "Generic Payload field", requiredMode = REQUIRED)
private T genericValue;
}
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,14 @@ public class AnotherProducer {
@AsyncPublisher(
operation =
@AsyncOperation(
channelName = "example-topic-exchange",
channelName = AmqpConstants.EXCHANGE_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.EXCHANGE_EXAMPLE_TOPIC_EXCHANGE,
AmqpConstants.ROUTING_KEY_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.QUEUE_EXAMPLE_QUEUE, payload);

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