Friday, March 8, 2013

Your first integration test experience

       
Illustration here is a testing scenario in which we can upload JAXWS web app to the server , deploy and invoke its services for testing purposes. These are the steps you need to follow.
( Note : you can start writing your tests in the following location according to your as server version :
location/platform/branches/x.x.x/products/as/x.x.x/modules/integration/tests-new
eg: /home/xxxx/svn/carbon/platform/branches/x.x.x/products/as/x.x.x/modules/integration/tests-new)

1. Create a package and add a new test class. (eg: jaxwssampleservice /  JAXWSSampleTestCase.java)

2. Extend ASIntegrationTest.java class. (ASIntegrationTest.java contains some useful utility methods which we can use regularly inside our test classes.)

3. Create a method called init() in JAXWSSampleTestCase.java class under the annotation @BeforeClass . Now invoke init() method of ASIntegrationTest.java inside the init() method we just created.
eg:
@BeforeClass(alwaysRun = true)
public void init() throws Exception {
super.init();
}

(This enables us to get the backendurl of the server and a session cookie as described in step 5.)

4. We can use @Test annotation to add test cases to our test class. Create a method
(eg : webApplicationUpload ) inside our JAXWSSampleTestCase.java test class.

5 .In-order to upload our artifact to the app server we can invoke the relevant service client from the test automation framework. Since we are testing 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 invoking asServer environment variable of ASIntegrationTest.java inside JAXWSWebappAdminClient constructor.)
eg :
JAXWSWebappAdminClient   jaxwsWebappAdminClient =
                            new JAXWSWebappAdminClient(asServer.getBackEndUrl(), asServer.getSessionCookie());


We can use uploadWebapp method of JAXWSWebappAdminClient.java class to upload the artifact. (Note that we need to provide the location where "java_first_jaxws.war" resides in our application)
eg:
We can add a package (artifacts/AS/jaxws) under resources directory and place our java_first_jaxws.war inside it. Therefore our artifact location would now become :

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

Now we need to pass this location and the name of the artifact to the uploadwebapp method. We can do this using the instance we created from JAXWSWebappAdminClient.java.
eg:
jaxwsWebappAdminClient.uploadWebapp(location, "java_first_jaxws.war");

6. To check whether the deployment of the service was successful we can call waitForServiceDeployment method of AxisServiceClientUtils.java class.
(Note that we need to provide the service url of the service we deployed. If java_first_jaxws.war contains a service “hello_world” we can verify the deployment as follows)
AxisServiceClientUtils.waitForServiceDeployment(asServer.getWebAppURL() +
"/java_first_jaxws/services/hello_world");    

7. For this illustration we will call “SayHiToUser” operation inside “hello_world“ service. To invoke the service create a method (serviceRequest) 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>";

Create a new method createPayload .To create the payload pass the request made to it.

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

8. Create an instance of AxisServiceClient.java class. In order to send the request we can call sendReceive method of this class. This will return the response from the server.
eg:
String endpoint = asServer.getWebAppURL() + "/java_first_jaxws/services/hello_world";
OMElement response = axisServiceClient.sendReceive(createPayload(request), endpoint,
"sayHiToUser");

9. We can assert the response using TestNG.
assertEquals(("<ns2:sayHiToUserResponse xmlns:ns2=\"http://server.hw.demo/\">" +
"<return>Hello Galaxy</return></ns2:sayHiToUserResponse>"),
response.toString().trim());

10. After completion of the response validation we can delete / remove the artifact uploaded to the app server by invoking deleteWebAppFile method of WebAppAdminClient.java class. We can do this under @AfterClass annotation.
eg:
@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");
}


No comments:

Post a Comment