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(),
                        asServer.getSessionCookie()); 

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,

jaggeryAppUploaderClient.uploadJaggeryFile("testapp.zip",
                ProductConstant.SYSTEM_TEST_RESOURCE_LOCATION + "artifacts" +
                        File.separator + "AS" + File.separator + "jaggery" + File.separator +
                        "testapp.zip");

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 {
                Thread.sleep(500);
            } 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(
                    jaggeryServerConnection.getInputStream()));

        } catch (IOException e) {
        }

        while ((in == null) && (System.currentTimeMillis() <= timeoutExpiredMs)) {
            try {
                in = new BufferedReader(
                        new InputStreamReader(jaggeryServerConnection.getInputStream()));
            } catch (IOException e) {
            }
            try {
                Thread.sleep(500);
            } 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;
        }

        in.close();
        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(),   
                                                                                                                                             asServer.getSessionCookie());
  webAppAdminClient.deleteWebAppFile("testapp");

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




Notes:

* 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. 

No comments:

Post a Comment