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/.
Writing a Test Case for AS
This page explains how to write a simple AS integration test;Â a test to upload an aar service to the server, invocation of the service and deletion of the uploaded aar.
You can start writing your tests in the following location according to your AS server version:
svn location /platform/branches/x.x.x/products/as/x.x.x/modules/integration/tests-new
E.g.:
platform/branches/x.x.x/products/as/x.x.x/modules/integration/tests-new
You need to create an ASIntegrationTest.java
java class in which you can keep important common methods that you might call inside your test class. Refer the table below where an ASIntegrationTest.java
is created in a more common form (for illustration purposes only).
package org.wso2.carbon.as.integration.test; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.authenticator.stub.LoginAuthenticationExceptionException; import org.wso2.carbon.automation.engine.context.AutomationContext; import javax.xml.xpath.XPathExpressionException; import java.rmi.RemoteException; public class ASIntegrationTest { private static AutomationContext automationContext; protected static void init() throws Exception { automationContext = new AutomationContext("AS", TestUserMode.SUPER_TENANT_ADMIN); } protected String getBackendURL() throws XPathExpressionException { return automationContext.getContextUrls().getBackEndUrl(); } protected String getSessionCookie() throws XPathExpressionException, LoginAuthenticationExceptionException, RemoteException { return automationContext.login(); } protected String getServiceURL() throws XPathExpressionException { return automationContext.getContextUrls().getServiceUrl(); } } protected void deleteService(String serviceName) throws RemoteException, XPathExpressionException, LoginAuthenticationExceptionException { ServiceAdminClient adminServiceService = new ServiceAdminClient(getBackendURL(), getSessionCookie()); if (ServiceDeploymentUtil.isFaultyService(getBackendURL(), getSessionCookie(), serviceName)) { adminServiceService.deleteFaultyServiceByServiceName(serviceName); } else if (ServiceDeploymentUtil.isServiceExist(getBackendURL(), getSessionCookie(), serviceName)) { adminServiceService.deleteService(new String[]{adminServiceService.getServiceGroup(serviceName)}); } ServiceDeploymentUtil.isServiceDeleted(getBackendURL(), getSessionCookie(), serviceName); }
Test Automation Framework allows you to create an AutomationContext object in accordance with the provided parameters given at the initial stage of the test. It is more of a custom runtime environment that suits running your tests.
package org.wso2.carbon.integration.test.aarservice; import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.wso2.carbon.automation.api.clients.aar.services.AARServiceUploaderClient; import org.wso2.carbon.automation.utils.axis2client.AxisServiceClient; import org.wso2.carbon.integration.test.ASIntegrationTest; import static org.testng.Assert.assertTrue; /* This class can be used to upload an aar service to the server , invocation of the service and delete the uploaded aar */ public class AARServiceTestCase extends ASIntegrationTest { private static final Log log = LogFactory.getLog(AARServiceTestCase.class); private static String axis2Service = "Axis2Service"; @BeforeClass(alwaysRun = true) // 01 public void init() throws Exception { super.init(); } @AfterClass(alwaysRun = true) // 08 public void carAppDelete() throws Exception { deleteService("Axis2Service"); log.info("Axis2Service deleted"); } @Test(groups = "wso2.as", description = "Upload aar service and verify deployment") // 02 public void arrServiceUpload() throws Exception { AARServiceUploaderClient aarServiceUploaderClient = new AARServiceUploaderClient(getBackendURL(), getSessionCookie()); // 03 aarServiceUploaderClient.uploadAARFile("Axis2Service.aar", "/home/xxx/svn/platform/" + "branches/xxx/products/as/5.2.1/modules/integration/tests-new/src/test/" + "resources/artifacts/AS/aar/Axis2Service.aar", ""); // 04 log.info("Axis2Service.aar service uploaded successfully"); } // 05 @Test(groups = "wso2.as", description = "invoke aar service", dependsOnMethods = "arrServiceUpload") public void invokeService() throws Exception { AxisServiceClient axisServiceClient = new AxisServiceClient(); String endpoint = getServiceURL() + "Axis2Service"; OMElement response = axisServiceClient.sendReceive(createPayLoad(), endpoint, "echoInt"); log.info("Response : " + response); assertTrue(response.toString().contains("<ns:return>25</ns:return>")); // 07 } // 06 public static OMElement createPayLoad() { OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace("http://service.carbon.wso2.org", "ns"); OMElement getOme = fac.createOMElement("echoInt", omNs); OMElement getOmeTwo = fac.createOMElement("x", omNs); getOmeTwo.setText("25"); getOme.addChild(getOmeTwo); return getOme; } }
Prerequisites:
You should place the aar service file (Axis2Service.aar
) inside the /home/xxxx/svn/platform/branches/turing/products/as/5.2.1/modules/integration/tests-new/src/test/resources/artifacts/AS/aar
package. Â
1. Create a method called init() in the AARServiceTestCase.java
class under the annotation @BeforeClass. Now invoke the init() method of ASIntegrationTest.java
inside the init() method you just created.
E.g.:
@BeforeClass(alwaysRun = true) public void init() throws Exception { super.init(); }
2.  You can use the @Test annotation to add test cases to your test class. Create a method, for example, arrServiceUpload, inside the AARServiceTestCase.java
test class.Â
3. You can call AARServiceUploaderClient which TAF API, in order to create an object from this class you need to pass the backend URL and a session cookie to the constructor. (You can easily get values for these two attributes by calling the ASIntegration.java class, getBackendURL() and getSessionCookie() methods respectively.
4. Â Call the uploadAARFile() method to upload the aar to the server. (Note the parameters. You need to pass the file name, file location path and the service hierarchy in order.)
5. Now create a separate @Test annotation method called "invokeService" to invoke the service of the uploaded aar file. Â
Make an object from the AxisServiceClient and call the sendReceive() method to send a request to the server. You need to pass values for payload, endPointReference and the operation name (which operation of the uploaded aar service).
6.Note the createPayLoad() method which creates the payload for the request. Also note the namespaces, OMElement creation etc.
7. If you follow all the steps in the order given, you should be able to receive a response from the server.
8. To be sure that you receive the right response, assert the response. TestNG provides a set of comprehensive assertions to meet this requirement. You can find a sample assert statement used to validate the response in the above scenario. (Refer note: // 07)
8. Remove/delete the uploaded aar file service.