Thursday, April 25, 2013

How to write WSO2 ESB Integration using WSO2 Test Automation Framework


Your First WSO2 ESB Integration test experience ....

This blog illustrate how to write a simple ESB Integration test class. Most of ESB Integration test is based on synapse configuration or synapse artifacts. so that try to write a test class to test proxy service deployment and undeployment. below is the synapse configuration we are going to test.


<?xml version="1.0" encoding="UTF-8"?>    
  <definitions xmlns="http://ws.apache.org/ns/synapse">    
   <proxy name="simpleProxy" transports="https http"  startOnLoad="true" trace="disable">  
    <target>    
     <inSequence>    
       <send>    
        <endpoint>  <address uri="http://localhost:9000/services/SimpleStockQuoteService" />  </endpoint>    
       </send>    
     </inSequence>    
     <outSequence>   <send />  </outSequence>    
    </target>    
   </proxy>    
  </definitions>  

above proxy service simple rout the incomming message to SimpleStockQuote service and get back the response to the client

Before writing the test case we have to make sure that there must be a ESB instance up and running. as well as another server which has a actual backend service running. Test Automation Framework look after all the preconditions which needed to run the test class successfully. Before running the test class, ESB server and axis2 server with SimpleStockQuoteService are started by framework using the testng listeners. and populate the users defined in userlist.csv(tenantlist.csv) files which are located in resources directory.


within the test class, we should focus on the test scenario only. you can start writing your tests in the following location according to your esb server version :

platform/branches/x.x.x/products/esb/x.x.x/modules/integration/tests

eg:
all the test classes are placed in one module called tests and found under integration module.

Then create a proper packaging and add a java class extending ESBIntegrationTest. then put the synapse configuration file in artifact/ESB under resources folder.

package org.wso2.carbon.esb.proxyservice.test.passThroughProxy;   
  import org.apache.axiom.om.OMElement;   
  import org.testng.annotations.AfterClass;   
  import org.testng.annotations.BeforeClass;   
  import org.testng.annotations.Test;   
  import org.wso2.carbon.esb.ESBIntegrationTest;   
  import javax.xml.namespace.QName;   
  import static org.testng.Assert.assertEquals;   
    
 public class ProxyServiceDeploymentTestCase extends ESBIntegrationTest {   
   @BeforeClass(alwaysRun = true)   
   public void deployService() throws Exception {   
    /* initializing server configuration with the user id 2*/   
    super.init(2);   
    /* deploying the artifact defined in the proxy_service.xml */   
    loadESBConfigurationFromClasspath( "/artifacts/ESB/proxyconfig/proxy/passThroughProxy/proxy_service.xml");   
   }   
   
   @Test(groups = "wso2.esb", description = "Pass through proxy service invocation test")   
   public void testPassThroughProxy() throws Exception {   
    /* invoking the proxy service and getting the response using a service client */   
    OMElement response = axis2Client.sendSimpleStockQuoteRequest(getProxyServiceURL("StockQuoteProxy"), null, "WSO2");   
    /* response assertions */   
    String symbol = response.getFirstElement().getFirstChildWithName(new QName("http://services.samples/xsd", "symbol"))  .getText();   
    assertEquals(symbol, "WSO2", "Fault: value 'symbol' mismatched");   
   }   
   
   @AfterClass(alwaysRun = true)   
   public void unDeployService() throws Exception {   
    /* undeploying deployed artifact */   
    super.cleanup();   
   }   
  }  


ESBIntegrationTest class abstract most of useful method for esb testing and once it is inherited it will ease your work by providing important features.

init(userid) will build the esb server configuration such as backend url, service url, etc.. and authenticate user which is defined in userlist.csv with the server and it is called under @BeforeClass(). then it will runs before all the test methods.

loadESBConfigurationFromClasspath("/artifacts/ESB/$path/synapse_config.xml") will deployed the artifact defined in the file(proxy, endpoints, sequence, message store, etc..) using admin services. it go through the configuration and deployed artifact one by one. if the same artifact name is already in the system, it is deleted and new one is deployed.

getProxyServiceURL("StockQuoteProxy") will returns the proxy service url http

cleanup() will undeploye the deployed artifact and it is called under @AfterClass(). then it will runs after all the test methods.

Above flow will executes your test scenario clearly and if any exception thrown an assertion failure happens, it will indicate as a test failure.

as above we can write lot of test cases and there are more other utility methods provided by automation framework.

to run the test class you have to add your test classes in testng.xml( can be found under resources directory)

 <test name="first-test" preserve-order="true" verbose="2">   
   <classes>   
    <class name="org.wso2.carbon.esb.proxyservice.test.passThroughProxy.ProxyServiceDeploymentTestCase"/>   
   </classes>   
  </test> 

or you can add the all classes to run by adding package name
<test name="first-test" preserve-order="true" verbose="2">   
   <packages>   
    <package name="org.wso2.carbon.esb.proxyservice.test.passThroughProxy"/>   
   </packages>   
  </test>  

Then issuing mvn clean install execute your test classes and generated the reports in target/surefire-reports

Running Test on Different Environments....
Once you write a test case you can run that test case in different environments. Just need to change the property file called automation.properties (can be found under resources directory) and executing.  

Executing test on integration Environment as a user
By default test runs on the integration environment(while building the product from source) as a user . Setting builder.enable=true will handle the ESB and axis2 servers startups.
 stratos.test=false   
 #execution.environment = integration|platform|stratos   
 execution.environment=integration   
 #execution.mode = user|tenant|all   
 execution.mode=user   
 port.enable=true   
 carbon.web.context.enable=false   
 builder.enable=true 


Executing as a tenant
if you want to run the test as a tenant. setting below will execute the test as tenant.

 execution.mode=tenant   

Executing test on Platform Environment
if you want to run the test against the ESB instance already up and running, you can do it by changing the below properties as mentioned.

 execution.environment=platform  
 builder.enable=false  

once you change the execution environment to platform, you need to change the server configurations too.

 esb.host.name=192.168.1.10   
 esb.http.port=9763   
 esb.https.port=9443   
 esb.nhttp.port=8280   
 esb.nhttps.port=8243   
 #esb.webContext.root  

when execution environment is changed to platform, we host the actual backend services(which were available in axis2 server for integration environment) in WSO2 Application server. so AS configurations is also need to configure and server must be up and running.

 as.host.name=localhost   
 as.http.port=9763   
 as.https.port=9443   
 #as.webContext.root  

Executing test on stratos or cloud
if you want to run your test class against stratos, change setting as below. 

 stratos.test=true   
 execution.environment=stratos   
 execution.mode=tenant   
 cluster.enable=true  
 #Stratos server details   
 app.service.host.name=manager.appserver.stratoslive.wso2.com, appserver.stratoslive.wso2.com   
 esb.service.host.name=manager.esb.stratoslive.wso2.com,esb.stratoslive.wso2.com    
 manager.service.host.name=stratoslive.wso2.com


No comments:

Post a Comment