This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.
Testing ESB Integration with a JMS Broker
In addition to the basic steps of writing an ESB integration test class, you can write a class that tests the functionality of a JMS proxy service to ensure the JMS broker is correctly integrated with WSO2 ESB. The test should include the following steps:
- Start a JMS broker (ActiveMQ)
- Change
axis2.xml
to enable the JMS transport in the ESB - Copy
activemq-core-5.7.0.jar
andgeronimo-j2ee-management_1.1_spec-1.0.1.jar
to<ESB_HOME>/repository/components/lib
- Start the ESB server
- Deploy the proxy service
- Send messages to the destination queue for proxy service to consume
The Test Automation Framework provides a startup class that handles the first four steps for you.
Using the JMSBrokerStartupTestCase case class
The package org.wso2.carbon.esb.jms.transport.test
provides the test class JMSBrokerStartupTestCase
, which handles the startup and configuration for you. Because this class is in the @BeforeTest() annotation, which executes before all other text executions in the jms.transport
package, you do not need to repeat the JMS broker startup and ESB configuration within your test classes, thereby reducing the time it takes to execute all tests. You will focus your test class on the test scenario itself and put it in the org.wso2.carbon.esb.jms.transport.test
package in platform/branches/x.x.x/products/esb/x.x.x/modules/integration/tests
.
Following is the JMSBrokerStartupTestCase
class:
package org.wso2.carbon.esb.jms.transport.test; import org.testng.Assert; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.wso2.carbon.automation.core.ProductConstant; import org.wso2.carbon.automation.core.utils.environmentutils.EnvironmentBuilder; import org.wso2.carbon.automation.core.utils.environmentutils.EnvironmentVariables; import org.wso2.carbon.automation.core.utils.jmsbrokerutils.controller.JMSBrokerController; import org.wso2.carbon.automation.core.utils.jmsbrokerutils.controller.config.JMSBrokerConfiguration; import org.wso2.carbon.automation.core.utils.jmsbrokerutils.controller.config.JMSBrokerConfigurationProvider; import org.wso2.carbon.automation.core.utils.serverutils.ServerConfigurationManager; import java.io.File; public class JMSBrokerStartupTestCase { private EnvironmentBuilder builder = null; private JMSBrokerController activeMqBroker; private ServerConfigurationManager serverManager = null; private final String ACTIVEMQ_CORE = "activemq-core-5.2.0.jar"; private final String GERONIMO_J2EE_MANAGEMENT = "geronimo-j2ee-management_1.1_spec-1.0.1.jar"; private final String GERONIMO_JMS = "geronimo-jms_1.1_spec-1.1.1.jar"; @BeforeTest(alwaysRun = true) public void startJMSBroker() throws Exception { builder = new EnvironmentBuilder().esb(ProductConstant.ADMIN_USER_ID); EnvironmentVariables esbServer = builder.build().getEsb(); serverManager = new ServerConfigurationManager(esbServer.getBackEndUrl()); if (builder.getFrameworkSettings().getEnvironmentSettings() .is_builderEnabled()) { //starting jms broker activeMqBroker = new JMSBrokerController("localhost", getJMSBrokerConfiguration()); if (!JMSBrokerController.isBrokerStarted()) { Assert.assertTrue(activeMqBroker.start(), "JMS Broker(ActiveMQ) stating failed"); } //copping dependency jms jar files to component/lib serverManager.copyToComponentLib(new File(ProductConstant.getResourceLocations(ProductConstant.ESB_SERVER_NAME) + File.separator + "jar" + File.separator + ACTIVEMQ_CORE)); serverManager.copyToComponentLib(new File(ProductConstant.getResourceLocations(ProductConstant.ESB_SERVER_NAME) + File.separator + "jar" + File.separator + GERONIMO_J2EE_MANAGEMENT)); serverManager.copyToComponentLib(new File(ProductConstant.getResourceLocations(ProductConstant.ESB_SERVER_NAME) + File.separator + "jar" + File.separator + GERONIMO_JMS)); //enabling jms transport with ActiveMQ by copping axis2.xml in resource directory and restarting ESB server serverManager.applyConfiguration(new File(ProductConstant.getResourceLocations(ProductConstant.ESB_SERVER_NAME) + File.separator + "jms" + File.separator + "transport" + File.separator + "axis2config" + File.separator + "activemq" + File.separator + "axis2.xml")); } } @AfterTest(alwaysRun = true) public void stopJMSBroker() throws Exception { if (builder.getFrameworkSettings().getEnvironmentSettings().is_builderEnabled()) { try { //reverting the changes done to esb sever if (serverManager != null) { serverManager.removeFromComponentLib(ACTIVEMQ_CORE); serverManager.removeFromComponentLib(GERONIMO_J2EE_MANAGEMENT); serverManager.removeFromComponentLib(GERONIMO_JMS); serverManager.restoreToLastConfiguration(); } } finally { if (activeMqBroker != null) { Assert.assertTrue(activeMqBroker.stop(), "JMS Broker(ActiveMQ) Stopping failed"); } } } } private JMSBrokerConfiguration getJMSBrokerConfiguration() { return JMSBrokerConfigurationProvider.getInstance().getBrokerConfiguration(); } }
Because this class starts the JMS broker and handles the configuration, your test class only needs to automate deploying the proxy service and sending messages to the queue.
Creating the proxy service
To deploy the proxy service, create jms_transport_proxy_service.xml
as follows and save it in /artifacts/ESB/jms/transport
under your test resources directory:
<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://ws.apache.org/ns/synapse"> <proxy name="JmsProxy" transports="jms" startOnLoad="true" trace="disable"> <target> <inSequence> <property action="set" name="OUT_ONLY" value="true"/> <property name="FORCE_SC_ACCEPTED" value="true" scope="axis2"/> </inSequence> <endpoint> <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> </endpoint> <outSequence> <send/> </outSequence> <parameter name="transport.jms.ContentType"> <rules> <jmsProperty>contentType</jmsProperty> <default>application/xml</default> </rules> </parameter> </target> </proxy> </definitions>
Creating the test class
Next, you create the test class JMSTransportProxyTestCase as follows:
package org.wso2.carbon.esb.jms.transport.test; import org.apache.axiom.om.OMElement; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.wso2.carbon.automation.core.utils.jmsbrokerutils.client.JMSQueueMessageConsumer; import org.wso2.carbon.automation.core.utils.jmsbrokerutils.client.JMSQueueMessageProducer; import org.wso2.carbon.automation.core.utils.jmsbrokerutils.controller.config.JMSBrokerConfigurationProvider; import org.wso2.carbon.esb.ESBIntegrationTest; import org.wso2.carbon.esb.util.JMSEndpointManager; public class JMSTransportProxyTestCase extends ESBIntegrationTest { @BeforeClass(alwaysRun = true) public void deployService() throws Exception { super.init(); OMElement synapse = esbUtils.loadClasspathResource("/artifacts/ESB/jms/transport/jms_transport_proxy_service.xml"); updateESBConfiguration(JMSEndpointManager.setConfigurations(synapse)); } @Test(groups = {"wso2.esb"}, description = "Test proxy service with jms transport") public void testJMSProxy() throws Exception { JMSQueueMessageProducer sender = new JMSQueueMessageProducer(JMSBrokerConfigurationProvider.getInstance().getBrokerConfiguration()); String queueName = "JmsProxy"; try { sender.connect(queueName); for (int i = 0; i < 3; i++) { sender.pushMessage("<?xml version='1.0' encoding='UTF-8'?>" + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"" + " xmlns:ser=\"http://services.samples\" xmlns:xsd=\"http://services.samples/xsd\">" + " <soapenv:Header/>" + " <soapenv:Body>" + " <ser:placeOrder>" + " <ser:order>" + " <xsd:price>100</xsd:price>" + " <xsd:quantity>2000</xsd:quantity>" + " <xsd:symbol>JMSTransport</xsd:symbol>" + " </ser:order>" + " </ser:placeOrder>" + " </soapenv:Body>" + "</soapenv:Envelope>"); } } finally { sender.disconnect(); } Thread.sleep(10000); JMSQueueMessageConsumer consumer = new JMSQueueMessageConsumer(JMSBrokerConfigurationProvider.getInstance().getBrokerConfiguration()); try { consumer.connect(queueName); for (int i = 0; i < 3; i++) { if (consumer.popMessage() != null) { Assert.fail("JMS Proxy service failed to pick the messages from Queue"); } } } finally { consumer.disconnect(); } } @AfterClass(alwaysRun = true) public void UndeployService() throws Exception { super.cleanup(); } }
This test class has the following methods:
deployService()
under the @BeforeClass annotation sets the JMS endpoint (if needed) and deploys your proxy service.testJMSProxy()
under the @Test annotation sends a message to the destination queue and waits for the proxy service to consume the message. It then verifies that the message is available in the destination queue. If it is not available, the proxy service is working correctly. It removes the message from the queue.UndeployService()
under the @AfterClass annotation undeploys the proxy service.
Adding the queue or topic
Next, you add your queue or topic to the resource file artifacts/ESB/jms/transport/jndi.properties
under the test resource directory. For example:
# register some connection factories # connectionfactory.[jndiname] = [ConnectionURL] connectionfactory.QueueConnectionFactory = amqp://admin:admin@clientID/carbon?brokerlist='tcp://localhost:5676' connectionfactory.TopicConnectionFactory = amqp://admin:admin@clientID/carbon?brokerlist='tcp://localhost:5676' # register some queues in JNDI using the form # queue.[jndiName] = [physicalName] queue.JmsProxy = JmsProxy # register some topics in JNDI using the form # topic.[jndiName] = [physicalName] topic.TestTopic = TestTopic
When you run a test in the platform environment, you can copy this file into your <ESB_HOME>/repository/conf
directory and run the test.
Running the test
Now that you have finished writing the test scenario, you must enable it to run by adding an entry in testng.xml
in the resource directory.
To run all classes in the org.wso2.carbon.esb.jms.transport.test
package, you add the following entry:
<test name="jms-transport" preserve-order="true" verbose="2"> <packages> <package name="org.wso2.carbon.esb.jms.transport.test.*"/> </packages> </test>
To run a single class, you must specify your test class AND the JMSBrokerStartupTestCase class:
<test name="jms-transport" preserve-order="true" verbose="2"> <classes> <class name="org.wso2.carbon.esb.jms.transport.test.JMSBrokerStartupTestCase"/> <class name="org.wso2.carbon.esb.jms.transport.test.JMSQueueAsProxyEndpointTestCase"/> </classes> </test>
Finally, you run the Maven command mvn clean install
to run these test classes.
Running with Message Broker instead of ActiveMQ
By default ActiveMQ is used when running tests in the integration environment. When running in the platform environment, your test classes run with WSO2 Message Broker as the JMS broker instead of the embedded ActiveMQ broker that is started by the Test Automation Framwork. You specify the environment by setting the execution.environment
property in the automation.properties
file under the resource directory:
ActiveMQ:
execution.environment=integration builder.enable=true
Message Broker:
execution.environment=platform builder.enable=false
If you change to the platform environment, the JMSEndpointManager
method under deployService
replaces the endpoint details in your Synapse configuration for WSO2 Message Broker as follows:
org.apache.activemq.jndi.ActiveMQInitialContextFactory
is changed toorg.wso2.andes.jndi.PropertiesFileInitialContextFactory
tcp://127.0.0.1:61616
is changed torepository/conf/jndi.properties
Note that in our example above, this replacement does not occur because no JMS endpoint is defined in the Synapse configuration.
When running your test class with WSO2 Message Broker, you must also change your proxy service destination queue URI. Therefore, you must define any queue or topic you defined in your Synapse configuration or JMS queue in the jndi.properties
file in the <ESB_HOME>/repository/conf
directory as described above.
For more information on configuring ESB and Message Broker integration, see Configure with WSO2 Message Broker in the ESB documentation.
JMS client classes
You can use the following JMS client classes provided by the Test Automation Framework:
- Consumes messages from a queue:
org.wso2.carbon.automation.core.utils.jmsbrokerutils.client.JMSQueueMessageConsumer
- Send messages to a queue:
org.wso2.carbon.automation.core.utils.jmsbrokerutils.client.JMSQueueMessageProducer
- Consume messages from a topic:
org.wso2.carbon.automation.core.utils.jmsbrokerutils.client.JMSTopicMessageConsumer
- Publish messages to a topic:
org.wso2.carbon.automation.core.utils.jmsbrokerutils.client.JMSTopicMessagePublisher
Copyright © WSO2 Inc. 2005-2015