Skip to content

Commit 3887fae

Browse files
committed
chore: #268 Refactor test classes for better maintainability and coverage
Signed-off-by: Laurent Broudoux <[email protected]>
1 parent 9715428 commit 3887fae

7 files changed

+1164
-906
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright The Microcks Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.microcks.testcontainers;
17+
18+
import io.github.microcks.testcontainers.connection.GenericConnection;
19+
20+
import com.rabbitmq.client.AMQP;
21+
import com.rabbitmq.client.Channel;
22+
import com.rabbitmq.client.Connection;
23+
import com.rabbitmq.client.ConnectionFactory;
24+
import com.rabbitmq.client.DefaultConsumer;
25+
import com.rabbitmq.client.Envelope;
26+
import org.junit.jupiter.params.ParameterizedTest;
27+
import org.junit.jupiter.params.provider.CsvSource;
28+
import org.testcontainers.containers.RabbitMQContainer;
29+
import org.testcontainers.utility.DockerImageName;
30+
31+
import java.io.IOException;
32+
import java.nio.charset.StandardCharsets;
33+
import java.util.concurrent.atomic.AtomicReference;
34+
35+
import static org.junit.jupiter.api.Assertions.*;
36+
import static org.junit.jupiter.api.Assertions.assertEquals;
37+
38+
/**
39+
* This is an integration test case using <a href="https://testcontainers.com/">Testcontainers</a> to test
40+
* Microcks AMQP async mocking functionality.
41+
* @author laurent
42+
*/
43+
class MicrocksContainersEnsembleAMQPTest {
44+
45+
@ParameterizedTest(name = "Mocking with {0} and {1})")
46+
@CsvSource({
47+
TestConstants.LATEST_IMAGE + "," + TestConstants.ASYNC_LATEST_IMAGE,
48+
TestConstants.LATEST_NATIVE_IMAGE + "," + TestConstants.ASYNC_LATEST_NATIVE_IMAGE,
49+
TestConstants.NIGHTLY_IMAGE + "," + TestConstants.ASYNC_NIGHTLY_IMAGE,
50+
TestConstants.NIGHTLY_NATIVE_IMAGE + "," + TestConstants.ASYNC_NIGHTLY_NATIVE_IMAGE,
51+
})
52+
void testAsyncFeatureAMQPMockingFunctionality(String microcksImage, String asyncMinionImage) throws Exception {
53+
try (
54+
MicrocksContainersEnsemble ensemble = new MicrocksContainersEnsemble(microcksImage)
55+
.withMainArtifacts("pastry-orders-asyncapi.yml")
56+
.withAsyncFeature(asyncMinionImage)
57+
.withAMQPConnection(new GenericConnection("rabbitmq:5672", "test", "test"));
58+
59+
RabbitMQContainer rabbitmq = new RabbitMQContainer(DockerImageName.parse("rabbitmq:3.9.13-management-alpine"))
60+
.withNetwork(ensemble.getNetwork())
61+
.withNetworkAliases("rabbitmq");
62+
) {
63+
rabbitmq.start();
64+
rabbitmq.execInContainer("rabbitmqctl", "add_user", "test", "test");
65+
rabbitmq.execInContainer("rabbitmqctl", "set_permissions", "-p", "/", "test", ".*", ".*", ".*");
66+
67+
ensemble.start();
68+
69+
testMicrocksAsyncAMQPMocking(ensemble, rabbitmq);
70+
}
71+
}
72+
73+
private void testMicrocksAsyncAMQPMocking(MicrocksContainersEnsemble ensemble, RabbitMQContainer rabbitmq) {
74+
// PastryordersAPI-0.1.0-pastry/orders
75+
String amqpDestination = ensemble.getAsyncMinionContainer().getAMQPMockDestination("Pastry orders API", "0.1.0", "SUBSCRIBE pastry/orders");
76+
String expectedMessage = "{\"id\":\"4dab240d-7847-4e25-8ef3-1530687650c8\",\"customerId\":\"fe1088b3-9f30-4dc1-a93d-7b74f0a072b9\",\"status\":\"VALIDATED\",\"productQuantities\":[{\"quantity\":2,\"pastryName\":\"Croissant\"},{\"quantity\":1,\"pastryName\":\"Millefeuille\"}]}";
77+
78+
// Initialize RabbitMQ consumer to receive 1 message.
79+
ConnectionFactory factory = new ConnectionFactory();
80+
factory.setHost(rabbitmq.getHost());
81+
factory.setPort(rabbitmq.getAmqpPort());
82+
83+
AtomicReference<String> message = new AtomicReference<>();
84+
85+
try (Connection connection = factory.newConnection()) {
86+
Channel channel = connection.createChannel();
87+
88+
channel.exchangeDeclare(amqpDestination, "topic", false);
89+
String queueName = channel.queueDeclare().getQueue();
90+
channel.queueBind(queueName, amqpDestination, "#");
91+
92+
String consumerTag = channel.basicConsume(queueName, false, new DefaultConsumer(channel) {
93+
@Override
94+
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
95+
throws IOException {
96+
message.set(new String(body, StandardCharsets.UTF_8));
97+
channel.basicAck(envelope.getDeliveryTag(), false);
98+
}
99+
});
100+
101+
Thread.sleep(4000L);
102+
103+
channel.close();
104+
} catch (Exception e) {
105+
fail("Exception while connecting to AMQP broker", e);
106+
}
107+
108+
// Compare messages.
109+
assertNotNull(message.get());
110+
assertTrue(message.get().length() > 1);
111+
assertEquals(expectedMessage, message.get());
112+
}
113+
}

0 commit comments

Comments
 (0)