Tuesday, March 12, 2013

Jaggery Tests

How do I deal with jaggery .... ?????

Jaggery ???? 

Jaggery is a framework to write webapps and HTTP-focused web services for all aspects of the application: front-end, communication, Server-side logic and persistence in pure Javascript. One of the intents of this framework is to reduce the gap between writing web apps and web services. Importantly, Jaggery is open-source and released under Apache 2.0                                                                                                  
                                                                                                                                (Definition http://jaggeryjs.org/)
In this example I am going to show you how to upload a jaggery application to the web , read whats inside the a jaggery page (jaggery files contains the extension ".jag") and also how to delete / remove the uploaded jaggery application.

1. In the first place we need to create a package (jaggery/integration/testsapplicationobject) inside our project structure. (svn/carbon/platform/branches/4.1.0/products/as/5.1.0/modules/integration/tests-new/src/test/java)

2. Now create a java class called ApplicationObjectTestCase.java and extend ASIntegrationTest.java *.

3. Create a method call jaggeryFileUpload() inside your ApplicationObjectTestCase.java class. Now call the init() method of the ASIntegrationTest.java* class. This will enable us to use its environmental
properties inside our test class.
eg:    super.init();

4. Invoke the JaggeryApplicationUploaderClient from the Test Automation Framework. Remember we need to pass the server backendurl and a session cookie. How can we do it ?? since we extended ASIntegrationTest.java  its so simple now. its just a matter of calling its asServer environmental variable and get the job done. Here is the sample code,

JaggeryApplicationUploaderClient jaggeryAppUploaderClient  =
                new JaggeryApplicationUploaderClient(asServer.getBackEndUrl(),

5. By using this 'jaggeryApplicationUploaderClient' object we can now invoke one of its methods called uploadJaggeryFile(java.lang.String fileName, java.lang.String filePath) which can be used to upload our jaggery application (testapp.zip) to the server. Do it as follows,

                ProductConstant.SYSTEM_TEST_RESOURCE_LOCATION + "artifacts" +
                        File.separator + "AS" + File.separator + "jaggery" + File.separator +

Please note that ProductConstant.SYSTEM_TEST_RESOURCE_LOCATION refers to resources directory.
(svn/carbon/platform/branches/4.1.0/products/as/5.1.0/modules/integration/tests-new/src/test/resources). We can use this resources directory to place our artifacts in this case 'testapp.zip is the artifact which comprises application.jag file which we will be using very shortly for testing purposes.

6. Fine. whats next ??? ok we uploaded a jaggery appliction to the web server. Shouldn't we check whether its services were deployed successfully ?? . hmmmm....... how to do it??? . The answer to the last question is simple. Wso2 Test automation framework contains a class called WebAppUtil.java which contains a method waitForWebAppDeployment(java.lang.String serviceUrl, java.lang.String content). As the method signature indicate we need to pass our service url and contents of the jaggery page we are testing.

Inside testapp.zip we have a jaggery file called application.jag. Since we are going to read whats inside this jaggery file our serviceUrl will be htpps://yourip/.........

WebAppUtil.waitForWebAppDeployment(asServer.getWebAppURL() + "/testapp/application.jag",
                                                                                                                                       "test jaggery application value"); 

7. Now in order to read whats inside application.jag file firstly we need to establish a http connection to the serviceUrl. Therefore we shall begin our coding like below.

URLConnection jaggeryServerConnection;
                                     URL jaggeryURL = new URL(asServer.getWebAppURL() + "/testapp/application.jag");

  long timeoutExpiredMs = 500;
        URLConnection jaggeryServerConnection = null;
        try {
            jaggeryServerConnection = url.openConnection();

        } catch (IOException e) {

        while ((jaggeryServerConnection == null) && (System.currentTimeMillis() <= timeoutExpiredMs)) {
            try {
                jaggeryServerConnection = url.openConnection();
            } catch (IOException e) {

            try {
            } catch (InterruptedException e) {

       assertNotNull(jaggeryServerConnection, "Connection establishment failure"); 
       // checking the connection using       testNG

8. Connection established. Now we can use a buffered reader to read the contents inside application.jag file. Hence,

   long timeoutExpiredMs = 500;
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(

        } catch (IOException e) {

        while ((in == null) && (System.currentTimeMillis() <= timeoutExpiredMs)) {
            try {
                in = new BufferedReader(
                        new InputStreamReader(jaggeryServerConnection.getInputStream()));
            } catch (IOException e) {
            try {
            } catch (InterruptedException e) {

assertNotNull(in, "Input stream failure");

9.  Now we can get the response and validate it.

        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            response = inputLine;

        log.info("Response: " + response);
        assertNotNull(response, "Response cannot be null");
        assertEquals(response, "test jaggery application value");

10. In order to delete /remove the uploaded jaggery app from our server we can call WebAppAdminClient.java  class from 'Test Automation Framework' and use its deleteWebAppFile(java.lang.String fileName) method. To do so,

  WebAppAdminClient webAppAdminClient = new WebAppAdminClient(asServer.getBackEndUrl(),   

Thats all folks. We are done with our jaggery app testing.


* ASIntegrationTest.java contains some useful utility methods which we can use regularly inside our test classes. It contains set of environmental variables which refers to the appserver 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 / sample values for these environmental variables which are useful for this illustration. 

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 :
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.
@BeforeClass(alwaysRun = true)
public void init() throws Exception {

(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)
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.
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() +

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/\">" +

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.
String endpoint = asServer.getWebAppURL() + "/java_first_jaxws/services/hello_world";
OMElement response = axisServiceClient.sendReceive(createPayload(request), endpoint,

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

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.
@AfterClass(alwaysRun = true)
public void webApplicationDelete() throws Exception {
WebAppAdminClient webAppAdminClient = new WebAppAdminClient(asServer.getBackEndUrl(),
log.info("java_first_jaxws.war deleted successfully");