-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathSolaceContainerAMQPTest.java
More file actions
97 lines (85 loc) · 3.48 KB
/
SolaceContainerAMQPTest.java
File metadata and controls
97 lines (85 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package org.testcontainers.solace;
import org.apache.qpid.jms.JmsConnectionFactory;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import static org.assertj.core.api.Assertions.assertThat;
public class SolaceContainerAMQPTest {
private static final Logger LOGGER = LoggerFactory.getLogger(SolaceContainerAMQPTest.class);
private static final String MESSAGE = "HelloWorld";
private static final String TOPIC_NAME = "Topic/ActualTopic";
@Test
public void testSolaceContainer() throws JMSException {
try (
SolaceContainer solaceContainer = new SolaceContainer("solace/solace-pubsub-standard:10.25.0")
.withTopic(TOPIC_NAME, Service.AMQP)
.withVpn("amqp-vpn")
) {
solaceContainer.start();
// solaceContainerUsage {
Session session = createSession(
solaceContainer.getUsername(),
solaceContainer.getPassword(),
solaceContainer.getOrigin(Service.AMQP)
);
// }
assertThat(session).isNotNull();
assertThat(consumeMessageFromSolace(session)).isEqualTo(MESSAGE);
session.close();
}
}
private static Session createSession(String username, String password, String host) {
try {
ConnectionFactory connectionFactory = new JmsConnectionFactory(username, password, host);
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession();
connection.start();
return session;
} catch (Exception e) {
Assert.fail("Error connecting and setting up session! " + e.getMessage());
return null;
}
}
private void publishMessageToSolace(Session session, Topic topic) throws JMSException {
MessageProducer messageProducer = session.createProducer(topic);
TextMessage message = session.createTextMessage(MESSAGE);
messageProducer.send(message);
messageProducer.close();
}
private String consumeMessageFromSolace(Session session) {
CountDownLatch latch = new CountDownLatch(1);
try {
String[] result = new String[1];
Topic topic = session.createTopic(TOPIC_NAME);
MessageConsumer messageConsumer = session.createConsumer(topic);
messageConsumer.setMessageListener(message -> {
try {
if (message instanceof TextMessage) {
result[0] = ((TextMessage) message).getText();
}
latch.countDown();
} catch (Exception e) {
LOGGER.error("Exception received: " + e.getMessage());
latch.countDown();
}
});
publishMessageToSolace(session, topic);
assertThat(latch.await(10L, TimeUnit.SECONDS)).isTrue();
messageConsumer.close();
return result[0];
} catch (Exception e) {
throw new RuntimeException("Cannot receive message from solace", e);
}
}
}