Skip to content

Commit 65fed2d

Browse files
author
Pascal Dal Farra
committed
tests: refactoring + add yaml endpoint testing
1 parent 3d7aa93 commit 65fed2d

File tree

8 files changed

+316
-25
lines changed

8 files changed

+316
-25
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ springwolf-bindings/springwolf-sqs-binding/build/
2424
node_modules/
2525

2626
asyncapi.actual.json
27+
asyncapi.actual.yaml
2728

2829
# Eclipse IDE
2930
.classpath
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
package io.github.springwolf.examples.amqp;
3+
4+
public class AmqpConstants {
5+
6+
public static final String EXAMPLE_TOPIC_EXCHANGE = "example-topic-exchange";
7+
8+
public static final String EXAMPLE_TOPIC_ROUTING_KEY = "example-topic-routing-key";
9+
10+
public static final String EXAMPLE_QUEUE = "example-queue";
11+
public static final String ANOTHER_QUEUE = "another-queue";
12+
public static final String MULTI_PAYLOAD_QUEUE = "multi-payload-queue";
13+
14+
public static final String EXAMPLE_BINDINGS_QUEUE = "example-bindings-queue";
15+
}

springwolf-examples/springwolf-amqp-example/src/main/java/io/github/springwolf/examples/amqp/configuration/RabbitConfiguration.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
package io.github.springwolf.examples.amqp.configuration;
33

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

2425
@Bean
2526
public Queue exampleQueue() {
26-
return new Queue("example-queue", false);
27+
return new Queue(AmqpConstants.EXAMPLE_QUEUE, false);
2728
}
2829

2930
@Bean
3031
public Queue anotherQueue() {
31-
return new Queue("another-queue", false);
32+
return new Queue(AmqpConstants.ANOTHER_QUEUE, false);
3233
}
3334

3435
@Bean
3536
public Queue exampleBindingsQueue() {
36-
return new Queue("example-bindings-queue", false, true, true);
37+
return new Queue(AmqpConstants.EXAMPLE_BINDINGS_QUEUE, false, true, true);
3738
}
3839

3940
@Bean
4041
public Exchange exampleTopicExchange() {
41-
return new TopicExchange("example-topic-exchange");
42+
return new TopicExchange(AmqpConstants.EXAMPLE_TOPIC_EXCHANGE);
4243
}
4344

4445
@Bean
4546
public Queue multiPayloadQueue() {
46-
return new Queue("multi-payload-queue");
47+
return new Queue(AmqpConstants.MULTI_PAYLOAD_QUEUE);
4748
}
4849

4950
@Bean
5051
public Binding exampleTopicBinding(Queue exampleBindingsQueue, Exchange exampleTopicExchange) {
5152
return BindingBuilder.bind(exampleBindingsQueue)
5253
.to(exampleTopicExchange)
53-
.with("example-topic-routing-key")
54+
.with(AmqpConstants.EXAMPLE_TOPIC_ROUTING_KEY)
5455
.noargs();
5556
}
5657
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
package io.github.springwolf.examples.amqp.consumers;
33

4+
import io.github.springwolf.examples.amqp.AmqpConstants;
45
import io.github.springwolf.examples.amqp.dtos.AnotherPayloadDto;
56
import io.github.springwolf.examples.amqp.dtos.ExamplePayloadDto;
67
import io.github.springwolf.examples.amqp.producers.AnotherProducer;
@@ -20,9 +21,9 @@ public class ExampleConsumer {
2021

2122
private final AnotherProducer anotherProducer;
2223

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

2728
AnotherPayloadDto example = new AnotherPayloadDto();
2829
example.setExample(payload);
@@ -31,37 +32,45 @@ public void receiveExamplePayload(ExamplePayloadDto payload) {
3132
anotherProducer.sendMessage(example);
3233
}
3334

34-
@RabbitListener(queues = "another-queue")
35+
@RabbitListener(queues = AmqpConstants.ANOTHER_QUEUE)
3536
public void receiveAnotherPayload(AnotherPayloadDto payload) {
36-
log.info("Received new message in another-queue: {}", payload.toString());
37+
log.info("Received new message in {}: {}", AmqpConstants.ANOTHER_QUEUE, payload.toString());
3738
}
3839

3940
@RabbitListener(
4041
bindings = {
4142
@QueueBinding(
42-
exchange = @Exchange(name = "example-topic-exchange", type = ExchangeTypes.TOPIC),
43+
exchange = @Exchange(name = AmqpConstants.EXAMPLE_TOPIC_EXCHANGE, type = ExchangeTypes.TOPIC),
4344
value =
4445
@Queue(
45-
name = "example-bindings-queue",
46+
name = AmqpConstants.EXAMPLE_BINDINGS_QUEUE,
4647
durable = "false",
4748
exclusive = "true",
4849
autoDelete = "true"),
49-
key = "example-topic-routing-key")
50+
key = AmqpConstants.EXAMPLE_TOPIC_ROUTING_KEY)
5051
})
5152
public void bindingsExample(AnotherPayloadDto payload) {
5253
log.info(
53-
"Received new message in example-bindings-queue"
54-
+ " through exchange example-topic-exchange using routing key example-topic-routing-key: {}",
54+
"Received new message in {}" + " through exchange {}" + " using routing key {}: {}",
55+
AmqpConstants.EXAMPLE_BINDINGS_QUEUE,
56+
AmqpConstants.EXAMPLE_TOPIC_EXCHANGE,
57+
AmqpConstants.EXAMPLE_TOPIC_ROUTING_KEY,
5558
payload.toString());
5659
}
5760

58-
@RabbitListener(queues = "multi-payload-queue")
61+
@RabbitListener(queues = AmqpConstants.MULTI_PAYLOAD_QUEUE)
5962
public void bindingsBeanExample(AnotherPayloadDto payload) {
60-
log.info("Received new message in multi-payload-queue (AnotherPayloadDto): {}", payload.toString());
63+
log.info(
64+
"Received new message in {} (AnotherPayloadDto): {}",
65+
AmqpConstants.MULTI_PAYLOAD_QUEUE,
66+
payload.toString());
6167
}
6268

63-
@RabbitListener(queues = "multi-payload-queue")
69+
@RabbitListener(queues = AmqpConstants.MULTI_PAYLOAD_QUEUE)
6470
public void bindingsBeanExample(ExamplePayloadDto payload) {
65-
log.info("Received new message in multi-payload-queue (ExamplePayloadDto): {}", payload.toString());
71+
log.info(
72+
"Received new message in {} (ExamplePayloadDto): {}",
73+
AmqpConstants.MULTI_PAYLOAD_QUEUE,
74+
payload.toString());
6675
}
6776
}

springwolf-examples/springwolf-amqp-example/src/main/java/io/github/springwolf/examples/amqp/producers/AnotherProducer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import io.github.springwolf.core.asyncapi.annotations.AsyncOperation;
55
import io.github.springwolf.core.asyncapi.annotations.AsyncPublisher;
6+
import io.github.springwolf.examples.amqp.AmqpConstants;
67
import io.github.springwolf.examples.amqp.dtos.AnotherPayloadDto;
78
import io.github.springwolf.plugins.amqp.asyncapi.annotations.AmqpAsyncOperationBinding;
89
import lombok.RequiredArgsConstructor;
@@ -17,11 +18,12 @@ public class AnotherProducer {
1718
@AsyncPublisher(
1819
operation =
1920
@AsyncOperation(
20-
channelName = "example-topic-exchange",
21+
channelName = AmqpConstants.EXAMPLE_TOPIC_EXCHANGE,
2122
description = "Custom, optional description defined in the AsyncPublisher annotation"))
2223
@AmqpAsyncOperationBinding()
2324
public void sendMessage(AnotherPayloadDto msg) {
2425
// send
25-
rabbitTemplate.convertAndSend("example-topic-exchange", "example-topic-routing-key", msg);
26+
rabbitTemplate.convertAndSend(
27+
AmqpConstants.EXAMPLE_TOPIC_EXCHANGE, AmqpConstants.EXAMPLE_TOPIC_ROUTING_KEY, msg);
2628
}
2729
}

springwolf-examples/springwolf-amqp-example/src/test/java/io/github/springwolf/examples/amqp/ApiIntegrationTest.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,36 @@ class ApiIntegrationTest {
2525
private TestRestTemplate restTemplate;
2626

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

33-
InputStream s = this.getClass().getResourceAsStream("/asyncapi.json");
34-
String expected = new String(s.readAllBytes(), StandardCharsets.UTF_8).trim();
33+
String expected;
34+
try (InputStream s = this.getClass().getResourceAsStream("/asyncapi.json")) {
35+
assert s != null;
36+
expected = new String(s.readAllBytes(), StandardCharsets.UTF_8).trim();
37+
}
38+
39+
assertEquals(expected, actual);
40+
}
41+
42+
@Test
43+
void asyncApiResourceYamlArtifactTest() throws IOException {
44+
String url = "/springwolf/docs.yaml";
45+
String actual = restTemplate
46+
.getForObject(url, String.class)
47+
.replaceAll("\r", "")
48+
.trim();
49+
Files.writeString(Path.of("src", "test", "resources", "asyncapi.actual.yaml"), actual);
50+
51+
String expected;
52+
try (InputStream s = this.getClass().getResourceAsStream("/asyncapi.yaml")) {
53+
assert s != null;
54+
expected = new String(s.readAllBytes(), StandardCharsets.UTF_8)
55+
.replaceAll("\r", "")
56+
.trim();
57+
}
3558

3659
assertEquals(expected, actual);
3760
}

springwolf-examples/springwolf-amqp-example/src/test/java/io/github/springwolf/examples/amqp/ProducerSystemTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void producerCanUseSpringwolfConfigurationToSendMessage() {
7575
payload.setSomeEnum(FOO1);
7676

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

8080
// then
8181
verify(exampleConsumer, timeout(10000)).receiveExamplePayload(payload);

0 commit comments

Comments
 (0)