Skip to content

Commit

Permalink
Merge pull request #97 from dalelane/master
Browse files Browse the repository at this point in the history
tests: add tests for connector
  • Loading branch information
gmcrobert authored Jul 21, 2022
2 parents e535261 + f5cbec9 commit 8505dfe
Show file tree
Hide file tree
Showing 7 changed files with 1,034 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: java

services:
- docker

script: mvn clean verify
145 changes: 142 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<artifactId>connect-api</artifactId>
<version>2.6.0</version>
<scope>provided</scope>
</dependency>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>connect-json</artifactId>
Expand Down Expand Up @@ -84,6 +84,15 @@
<version>1.7.25</version>
<scope>test</scope>
</dependency>

<!-- tests in src/integration depend on a running MQ queue manager -->
<!-- in a container, configured using org.testcontainers -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.17.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -97,18 +106,44 @@
<compilerArgument>-Xlint:unchecked</compilerArgument>
</configuration>
</plugin>

<!-- run unit tests -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M1</version>
<version>3.0.0-M7</version>
<configuration>
<argLine>${surefire.jacoco.args}</argLine>
<systemPropertyVariables>
<connectorVersion>${project.version}</connectorVersion>
</systemPropertyVariables>
</configuration>
</plugin>

<!-- run integration tests -->
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<argLine>${failsafe.jacoco.args}</argLine>
<systemPropertyVariables>
<connectorVersion>${project.version}</connectorVersion>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>

<!-- build the release jar -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
Expand All @@ -123,6 +158,110 @@
</descriptors>
</configuration>
</plugin>

<!-- add the src/integration folder as a test folder, which lets us keep -->
<!-- tests that have a dependency on testcontainers separate from pure -->
<!-- unit tests with no external dependency -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>process-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/integration/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>

<!-- generate test code coverage report -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<id>before-unit-test-execution</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/jacoco-output/jacoco-unit-tests.exec</destFile>
<propertyName>surefire.jacoco.args</propertyName>
</configuration>
</execution>
<execution>
<id>after-unit-test-execution</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco-output/jacoco-unit-tests.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-unit-test-coverage-report</outputDirectory>
</configuration>
</execution>
<execution>
<id>before-integration-test-execution</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/jacoco-output/jacoco-integration-tests.exec</destFile>
<propertyName>failsafe.jacoco.args</propertyName>
</configuration>
</execution>
<execution>
<id>after-integration-test-execution</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco-output/jacoco-integration-tests.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-integration-test-coverage-report</outputDirectory>
</configuration>
</execution>
<execution>
<id>merge-unit-and-integration</id>
<phase>post-integration-test</phase>
<goals>
<goal>merge</goal>
</goals>
<configuration>
<fileSets>
<fileSet>
<directory>${project.build.directory}/jacoco-output/</directory>
<includes>
<include>*.exec</include>
</includes>
</fileSet>
</fileSets>
<destFile>${project.build.directory}/jacoco-output/merged.exec</destFile>
</configuration>
</execution>
<execution>
<id>create-merged-report</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco-output/merged.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-merged-test-coverage-report</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/**
* Copyright 2022 IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ibm.eventstreams.connect.mqsource;

import java.util.List;
import java.util.concurrent.TimeoutException;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.junit.ClassRule;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.WaitingConsumer;

import com.ibm.mq.jms.MQConnectionFactory;
import com.ibm.msg.client.jms.JmsConnectionFactory;
import com.ibm.msg.client.jms.JmsFactoryFactory;
import com.ibm.msg.client.wmq.WMQConstants;

/**
* Helper class for integration tests that have a dependency on JMSContext.
*
* It starts a queue manager in a test container, and uses it to create
* a JMSContext instance, that can be used in tests.
*/
public class AbstractJMSContextIT {

private static final String QMGR_NAME = "MYQMGR";
private static final String CHANNEL_NAME = "DEV.APP.SVRCONN";

@ClassRule
public static GenericContainer<?> MQ_CONTAINER = new GenericContainer<>("icr.io/ibm-messaging/mq:latest")
.withEnv("LICENSE", "accept")
.withEnv("MQ_QMGR_NAME", QMGR_NAME)
.withEnv("MQ_ENABLE_EMBEDDED_WEB_SERVER", "false")
.withExposedPorts(1414);

private JMSContext jmsContext;


/**
* Returns a JMS context pointing at a developer queue manager running in a
* test container.
*/
public JMSContext getJmsContext() throws Exception {
if (jmsContext == null) {
waitForQueueManagerStartup();

MQConnectionFactory mqcf = new MQConnectionFactory();
mqcf.setTransportType(WMQConstants.WMQ_CM_CLIENT);
mqcf.setChannel(CHANNEL_NAME);
mqcf.setQueueManager(QMGR_NAME);
mqcf.setConnectionNameList(getConnectionName());

jmsContext = mqcf.createContext();
}

return jmsContext;
}


/**
* Gets the host port that has been mapped to the default MQ 1414 port in the test container.
*/
public Integer getMQPort() {
return MQ_CONTAINER.getMappedPort(1414);
}

public String getQmgrName() {
return QMGR_NAME;
}
public String getChannelName() {
return CHANNEL_NAME;
}
public String getConnectionName() {
return "localhost(" + getMQPort().toString() + ")";
}


/**
* Waits until we see a log line in the queue manager test container that indicates
* the queue manager is ready.
*/
private void waitForQueueManagerStartup() throws TimeoutException {
WaitingConsumer logConsumer = new WaitingConsumer();
MQ_CONTAINER.followOutput(logConsumer);
logConsumer.waitUntil(logline -> logline.getUtf8String().contains("AMQ5975I"));
}




/**
* Puts all messages to the specified MQ queue. Used in tests to
* give the Connector something to get.
*/
public void putAllMessagesToQueue(String queueName, List<Message> messages) throws JMSException {
Connection connection = null;
Session session = null;
Destination destination = null;
MessageProducer producer = null;

JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);

JmsConnectionFactory cf = ff.createConnectionFactory();
cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, "localhost");
cf.setIntProperty(WMQConstants.WMQ_PORT, getMQPort());
cf.setStringProperty(WMQConstants.WMQ_CHANNEL, getChannelName());
cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, getQmgrName());
cf.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, false);

connection = cf.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

destination = session.createQueue(queueName);
producer = session.createProducer(destination);

connection.start();

for (Message message : messages) {
message.setJMSDestination(destination);
producer.send(message);
}

connection.close();
}
}
Loading

0 comments on commit 8505dfe

Please sign in to comment.