Skip to content

Commit d852214

Browse files
committed
test: tests for Sink task
Signed-off-by: Dale Lane <dale.lane@uk.ibm.com>
1 parent 5f0b375 commit d852214

File tree

2 files changed

+335
-18
lines changed

2 files changed

+335
-18
lines changed

src/integration/java/com/ibm/eventstreams/connect/mqsink/AbstractJMSContextIT.java

Lines changed: 84 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,73 +15,139 @@
1515
*/
1616
package com.ibm.eventstreams.connect.mqsink;
1717

18+
import java.util.ArrayList;
19+
import java.util.List;
1820
import java.util.concurrent.TimeoutException;
1921

22+
import javax.jms.Connection;
23+
import javax.jms.Destination;
2024
import javax.jms.JMSContext;
25+
import javax.jms.JMSException;
26+
import javax.jms.Message;
27+
import javax.jms.MessageConsumer;
28+
import javax.jms.Session;
2129

2230
import org.junit.ClassRule;
2331
import org.testcontainers.containers.GenericContainer;
2432
import org.testcontainers.containers.output.WaitingConsumer;
2533

2634
import com.ibm.mq.jms.MQConnectionFactory;
35+
import com.ibm.msg.client.jms.JmsConnectionFactory;
36+
import com.ibm.msg.client.jms.JmsFactoryFactory;
2737
import com.ibm.msg.client.wmq.WMQConstants;
2838

2939

3040
/**
3141
* Helper class for integration tests that have a dependency on JMSContext.
32-
*
42+
*
3343
* It starts a queue manager in a test container, and uses it to create
3444
* a JMSContext instance, that can be used in tests.
3545
*/
3646
public abstract class AbstractJMSContextIT {
3747

3848
private static final String QMGR_NAME = "MYQMGR";
39-
49+
private static final String CHANNEL_NAME = "DEV.APP.SVRCONN";
50+
4051
@ClassRule
4152
public static GenericContainer<?> MQ_CONTAINER = new GenericContainer<>("icr.io/ibm-messaging/mq:latest")
4253
.withEnv("LICENSE", "accept")
4354
.withEnv("MQ_QMGR_NAME", QMGR_NAME)
4455
.withEnv("MQ_ENABLE_EMBEDDED_WEB_SERVER", "false")
4556
.withExposedPorts(1414);
46-
57+
4758
private JMSContext jmsContext;
48-
49-
59+
60+
5061
/**
51-
* Returns a JMS context pointing at a developer queue manager running in a
52-
* test container.
62+
* Returns a JMS context pointing at a developer queue manager running in a
63+
* test container.
5364
*/
5465
public JMSContext getJmsContext() throws Exception {
5566
if (jmsContext == null) {
5667
waitForQueueManagerStartup();
57-
68+
5869
MQConnectionFactory mqcf = new MQConnectionFactory();
5970
mqcf.setTransportType(WMQConstants.WMQ_CM_CLIENT);
60-
mqcf.setChannel("DEV.APP.SVRCONN");
71+
mqcf.setChannel(CHANNEL_NAME);
6172
mqcf.setQueueManager(QMGR_NAME);
62-
mqcf.setConnectionNameList("localhost(" + getMQPort().toString() + ")");
63-
73+
mqcf.setConnectionNameList(getConnectionName());
74+
6475
jmsContext = mqcf.createContext();
6576
}
66-
77+
6778
return jmsContext;
6879
}
69-
70-
80+
81+
7182
/**
7283
* Gets the host port that has been mapped to the default MQ 1414 port in the test container.
7384
*/
74-
private Integer getMQPort() {
75-
return MQ_CONTAINER.getMappedPort(1414);
85+
public Integer getMQPort() {
86+
return MQ_CONTAINER.getMappedPort(1414);
87+
}
88+
89+
public String getQmgrName() {
90+
return QMGR_NAME;
91+
}
92+
public String getChannelName() {
93+
return CHANNEL_NAME;
94+
}
95+
public String getConnectionName() {
96+
return "localhost(" + getMQPort().toString() + ")";
7697
}
7798

78-
/**
99+
100+
/**
79101
* Waits until we see a log line in the queue manager test container that indicates
80102
* the queue manager is ready.
81103
*/
82104
private void waitForQueueManagerStartup() throws TimeoutException {
83105
WaitingConsumer logConsumer = new WaitingConsumer();
84106
MQ_CONTAINER.followOutput(logConsumer);
85107
logConsumer.waitUntil(logline -> logline.getUtf8String().contains("AMQ5975I"));
86-
}
108+
}
109+
110+
111+
/**
112+
* Retrieves all messages from the specified MQ queue (destructively). Used in
113+
* tests to verify that the expected messages were put to the test queue.
114+
*/
115+
public List<Message> getAllMessagesFromQueue(String queueName) throws JMSException {
116+
Connection connection = null;
117+
Session session = null;
118+
Destination destination = null;
119+
MessageConsumer consumer = null;
120+
121+
JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
122+
123+
JmsConnectionFactory cf = ff.createConnectionFactory();
124+
cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, "localhost");
125+
cf.setIntProperty(WMQConstants.WMQ_PORT, getMQPort());
126+
cf.setStringProperty(WMQConstants.WMQ_CHANNEL, getChannelName());
127+
cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
128+
cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, getQmgrName());
129+
cf.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, false);
130+
131+
connection = cf.createConnection();
132+
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
133+
134+
destination = session.createQueue(queueName);
135+
consumer = session.createConsumer(destination);
136+
137+
connection.start();
138+
139+
List<Message> messages = new ArrayList<>();
140+
Message message;
141+
do {
142+
message = consumer.receiveNoWait();
143+
if (message != null) {
144+
messages.add(message);
145+
}
146+
}
147+
while (message != null);
148+
149+
connection.close();
150+
151+
return messages;
152+
}
87153
}

0 commit comments

Comments
 (0)