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 an App Server Integration Test

This page describes how to write an automated test for the scenario where you upload a JAXWS web app to the WSO2 Application Server and then deploy and invoke its services for testing purposes. It includes the following sections:

Creating the class

You can start writing your tests in the following location according to your Application Server version:

platform/branches/x.x.x/products/as/x.x.x/modules/integration/tests-new

For example:

https://svn.wso2.org/repos/wso2/carbon/platform/branches/4.1.0/products/as/5.1.1/modules/integration/tests-new
  1. Create a new test package and class: jaxwssampleservice and JAXWSSampleTestCase.java
  2. Extend the ASIntegrationTest.java class.
  3. Create a method called testInitialize() in JAXWSSampleTestCase.java under the annotation @BeforeClass, and then invoke the init() method of ASIntegrationTest.java inside the init() method you just created. For example:

    @BeforeClass(alwaysRun = true)
    public void testInitialize() throws Exception {
    super.init();
    }

    The line super.init(); will create the Application Server environment that is to be used in the rest of the test class and enables us to get some static and dynamic properties of Application Server.

  4. Use the @Test annotation to add test cases to your test class by creating a method such as testWebApplicationUpload inside your JAXWSSampleTestCase.java test class.

 

Uploading artifacts

To upload your artifacts to the Application Server, you can invoke the relevant admin service client from the test automation framework. Since we are testing a JAXWS service, we can invoke JAXWSWebappAdminClient to upload the artifact java_first_jaxws.war to the server. Note that we need to pass the backEndUrl and the session cookie when creating an instance from JAXWSWebappAdminClient. We can derive values for these arguments by referring to the asServer environment variable of ASIntegrationTest.java inside the JAXWSWebappAdminClient constructor.

For example:

JAXWSWebappAdminClient   jaxwsWebappAdminClient =
     new JAXWSWebappAdminClient(asServer.getBackEndUrl(), sServer.getSessionCookie());

You can use the uploadWebapp method of the JAXWSWebappAdminClient.java class to upload the artifact. Note that you need to provide the location where java_first_jaxws.war resides in your application. For example, if you added the package src/test/resources/artifacts/AS/jaxws and placed your java_first_jaxws.war inside it, the artifact location would be as follows:

String location = ProductConstant.SYSTEM_TEST_RESOURCE_LOCATION +"artifacts" + File.separator + "AS" + File.separator + "jaxws" + File.separator +"java_first_jaxws.war";

You pass this location and the name of the artifact to the uploadWebapp method. You can do this using the instance you created from JAXWSWebappAdminClient.java. For example:

jaxwsWebappAdminClient.uploadWebapp(location, "java_first_jaxws.war");

To verify whether the deployment of the service was successful, you can call waitForServiceDeployment method of the AxisServiceClientUtils.java class. Note that you must provide the service URL of the service you deployed. If java_first_jaxws.war contains a service “hello_world”, you can verify the deployment as follows:

AxisServiceClientUtils.waitForServiceDeployment(asServer.getWebAppURL() +
"/java_first_jaxws/services/hello_world");      

Running the example

In this example, we will call the “sayHiToUser” operation inside the “hello_world“ service.

  1. To invoke the service, create a method (request) under the annotation @Test and create the request as follows.

    String request = "<ns2:sayHiToUser xmlns:ns2=\"http://server.hw.demo/\">" +
    "<arg0><name>Galaxy</name></arg0></ns2:sayHiToUser>";
  2. Create a new method called createPayload and pass the request to it to create the payload.

    private OMElement createPayload(String request) throws XMLStreamException {
    return new StAXOMBuilder(new ByteArrayInputStream(request.getBytes())).getDocumentElement();
    }
  3. Create an instance of the AxisServiceClient.java class. To send the request, you can call the sendReceive method of this class, which will return the response from the server.

    String endpoint = asServer.getWebAppURL() + "/java_first_jaxws/services/hello_world";
    OMElement response = axisServiceClient.sendReceive(createPayload(request), endpoint,
    "sayHiToUser");
  4. Assert the response using TestNG.

    assertEquals(("<ns2:sayHiToUserResponse xmlns:ns2=\"http://server.hw.demo/\">" +
    "<return>Hello Galaxy</return></ns2:sayHiToUserResponse>"),
    response.toString().trim());
  5. After the response validation, you can delete the artifact from the Application Server by invoking the deleteWebAppFile method of the WebAppAdminClient.javaclass under the @AfterClass annotation.

    @AfterClass(alwaysRun = true)
    public void webApplicationDelete() throws Exception {
    WebAppAdminClient webAppAdminClient = new WebAppAdminClient(asServer.getBackEndUrl(),
    asServer.getSessionCookie());
    webAppAdminClient.deleteWebAppFile("java_first_jaxws.war");
    log.info("java_first_jaxws.war deleted successfully");
    }

The JAXWSSampleTestCase.java class should now look similar to the following:

package org.wso2.carbon.integration.test.jaxwssampleservice;

 
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.wso2.carbon.automation.api.clients.webapp.mgt.JAXWSWebappAdminClient;
import org.wso2.carbon.automation.api.clients.webapp.mgt.WebAppAdminClient;
import org.wso2.carbon.automation.core.ProductConstant;
import org.wso2.carbon.automation.utils.axis2client.AxisServiceClient;
import org.wso2.carbon.automation.utils.axis2client.AxisServiceClientUtils;
import org.wso2.carbon.integration.test.ASIntegrationTest;

 
import javax.xml.stream.XMLStreamException;
import java.io.ByteArrayInputStream;
import java.io.File;

 
import static org.testng.Assert.assertEquals;

 
/*
This class uploads java_first_jaxws.war and handlers_jaxws.war to the server , verify deployment and invokes the service
 */

 
 
 
public class JAXWSSampleTestCase extends ASIntegrationTest {

 
    private static final Log log = LogFactory.getLog(JAXWSSampleTestCase.class);

 
    @BeforeClass(alwaysRun = true)
    public void init() throws Exception {
        super.init();
    }

 
    @AfterClass(alwaysRun = true)
    public void webApplicationDelete() throws Exception {
        WebAppAdminClient webAppAdminClient = new WebAppAdminClient(asServer.getBackEndUrl(),
                asServer.getSessionCookie());
        webAppAdminClient.deleteWebAppFile("java_first_jaxws.war");
        webAppAdminClient.deleteWebAppFile("handlers_jaxws.war");
        log.info("handlers_jaxws.war deleted successfully");
    }

 
    @Test(groups = "wso2.as", description = "upload war file and verify deployment")
    public void webApplicationUpload() throws Exception {
        asServer = super.asServer;
        JAXWSWebappAdminClient jaxwsWebappAdminClient =
                new JAXWSWebappAdminClient(asServer.getBackEndUrl(), asServer.getSessionCookie());
        String location = ProductConstant.SYSTEM_TEST_RESOURCE_LOCATION +
                "artifacts" + File.separator + "AS" + File.separator + "jaxws" + File.separator;

 
        jaxwsWebappAdminClient.uploadWebapp(location + "handlers_jaxws.war", "handlers_jaxws.war");
        AxisServiceClientUtils.waitForServiceDeployment(asServer.getWebAppURL() +
                "/handlers_jaxws/services/HandlerServicePort");   // verify deployment
        log.info("handlers_jaxws.war file uploaded successfully");

 
        jaxwsWebappAdminClient.uploadWebapp(location +"java_first_jaxws.war", "java_first_jaxws.war");
        AxisServiceClientUtils.waitForServiceDeployment(asServer.getWebAppURL() +
                "/java_first_jaxws/services/hello_world");   // verify deployment
        log.info("java_first_jaxws.war file uploaded successfully");
    }

 
    @Test(groups = "wso2.as", description = "invoke service - sayHiToUser",
            dependsOnMethods = "webApplicationUpload")
    public void serviceRequest() throws Exception {
        AxisServiceClient axisServiceClient = new AxisServiceClient();
        String endpoint = asServer.getWebAppURL() + "/java_first_jaxws/services/hello_world";
        String request = "<ns2:sayHiToUser xmlns:ns2=\"http://server.hw.demo/\">" +
                "<arg0><name>World</name></arg0></ns2:sayHiToUser>";

 
        OMElement response = axisServiceClient.sendReceive(createPayload(request), endpoint,
                "sayHiToUser");
        assertEquals(("<ns2:sayHiToUserResponse xmlns:ns2=\"http://server.hw.demo/\">" +
                "<return>Hello World</return></ns2:sayHiToUserResponse>"),
                response.toString().trim());
    }

 
    private OMElement createPayload(String request) throws XMLStreamException {
        return new StAXOMBuilder(new ByteArrayInputStream(request.getBytes())).getDocumentElement();
    }
}

Class reference

ASIntegrationTest.java contains useful utility methods that we can use regularly inside our test classes. It contains a set of environmental variables that refer to the application server properties (serviceUrl, backendUrl, session cookies, etc.) and also authenticates the server login (using its init() method), allowing us to use its environmental properties inside our test classes as mentioned earlier. Following are some interpretations and sample values for these environmental variables:

ProductConstant.SYSTEM_TEST_RESOURCE_LOCATION returns the path where our artifacts are located (only up to the resources directory). For example:

/home/xxx/svn/carbon/platform/branches/4.0.0/products/as/5.1.0/modules/integration/tests-new/src/test/resources/ 

AxisServiceClientUtils.java is a utility class that contains useful methods like waitForServiceDeployment(), which checks whether a service was successfully deployed to the server once you upload an artifact that contains the service.

AxisServiceClient.java provides client access to a service. Every instance (not thread-safe) of this class is associated with a particular AxisService and operations.  

 

Copyright © WSO2 Inc. 2005-2015