Author: enridaga
Date: Wed Nov 23 15:41:24 2011
New Revision: 1205456

URL: http://svn.apache.org/viewvc?rev=1205456&view=rev
Log:
Test framework and services tests are now aligned with the new commons jobs api 
(STANBOL-343)

Modified:
    incubator/stanbol/branches/lto-reasoners/reasoners/services-tests/pom.xml
    
incubator/stanbol/branches/lto-reasoners/reasoners/services-tests/src/test/java/org/apache/stanbol/reasoners/it/concurrency/ReasonersConcurrencyTest.java
    
incubator/stanbol/branches/lto-reasoners/reasoners/services-tests/src/test/java/org/apache/stanbol/reasoners/it/offline/ReasonersOfflineJobsTest.java
    incubator/stanbol/branches/lto-reasoners/reasoners/test/pom.xml
    
incubator/stanbol/branches/lto-reasoners/reasoners/test/src/main/java/org/apache/stanbol/reasoners/test/ReasonersTestBase.java

Modified: 
incubator/stanbol/branches/lto-reasoners/reasoners/services-tests/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/branches/lto-reasoners/reasoners/services-tests/pom.xml?rev=1205456&r1=1205455&r2=1205456&view=diff
==============================================================================
--- incubator/stanbol/branches/lto-reasoners/reasoners/services-tests/pom.xml 
(original)
+++ incubator/stanbol/branches/lto-reasoners/reasoners/services-tests/pom.xml 
Wed Nov 23 15:41:24 2011
@@ -112,7 +112,7 @@
     <plugins>
       <plugin>
         <artifactId>maven-clean-plugin</artifactId>
-        <!-- why overriding here? -->
+        <!-- FIXME why overriding here? -->
         <version>2.2</version>
       </plugin>
       <plugin>

Modified: 
incubator/stanbol/branches/lto-reasoners/reasoners/services-tests/src/test/java/org/apache/stanbol/reasoners/it/concurrency/ReasonersConcurrencyTest.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/branches/lto-reasoners/reasoners/services-tests/src/test/java/org/apache/stanbol/reasoners/it/concurrency/ReasonersConcurrencyTest.java?rev=1205456&r1=1205455&r2=1205456&view=diff
==============================================================================
--- 
incubator/stanbol/branches/lto-reasoners/reasoners/services-tests/src/test/java/org/apache/stanbol/reasoners/it/concurrency/ReasonersConcurrencyTest.java
 (original)
+++ 
incubator/stanbol/branches/lto-reasoners/reasoners/services-tests/src/test/java/org/apache/stanbol/reasoners/it/concurrency/ReasonersConcurrencyTest.java
 Wed Nov 23 15:41:24 2011
@@ -2,8 +2,10 @@ package org.apache.stanbol.reasoners.it.
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
 import java.io.IOException;
+import java.net.MalformedURLException;
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.List;
@@ -14,11 +16,18 @@ import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.commons.io.IOUtils;
+import org.apache.http.Header;
+import org.apache.http.HeaderElement;
 import org.apache.http.HttpResponse;
+import org.apache.http.ParseException;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.message.BasicHeader;
+import org.apache.stanbol.commons.jobs.api.JobInfo;
 import org.apache.stanbol.reasoners.test.ReasonersTestBase;
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -73,25 +82,25 @@ public class ReasonersConcurrencyTest ex
         // We start in parallel a set of background jobs
         List<JobClient> tasks = buildStarters();
         
-        List<String> jids = new ArrayList<String>();
+        List<String> locations = new ArrayList<String>();
         List<Future<Result>> futures = executor.invokeAll(tasks);
         for (Future<Result> future : futures) {
-            String j = future.get().assertResult().getContentString();
-            log.info("Got job id: {}",j);
-            jids.add(j);
+            String location = 
future.get().assertResult().getResponse().getFirstHeader("Location").getValue();
+            log.info("job created: {}",location);
+            locations.add(location);
         }
         
         // We ping in parallel all jobs.
         // On each iteration, we prepare a new set of calls only on jobs
         // which are not terminated
         List<String> done = new ArrayList<String>();
-        while((done.size() < jids.size())){
+        while((done.size() < locations.size())){
             // List of calls
             tasks = new ArrayList<JobClient>();
             // Prepare a Pinger on each unfinished job
-            for(String j : jids){
-                if(!done.contains(j)){
-                    tasks.add(new Pinger(j));
+            for(String l : locations){
+                if(!done.contains(l)){
+                    tasks.add(new Pinger(l));
                 }
             }
             // Invoke all unfinished jobs in parallel
@@ -99,13 +108,18 @@ public class ReasonersConcurrencyTest ex
             // Query each response
             for (Future<Result> future : futures) {
                 PingerResult pr = (PingerResult) future.get();
-                String r = pr.assertResult().getContentString();
-                String jid = pr.jid();
-                if(!r.equals("Job is still working")){
-                    log.info("{} is done!", jid);
-                    done.add(jid);
+                String content = pr.assertResult().getContentString();
+                // Explore JSON here
+                log.info("Content:\n\n{}\n\n",content);
+                JSONObject json = new JSONObject(content);
+                String status = json.getString("status");
+                
+                String location = pr.location();
+                if(status.equals(JobInfo.FINISHED)){
+                    log.info("{} is done!", location);
+                    done.add(location);
                 }else{
-                    log.info("{} is still working ... ",jid);
+                    log.info("{} is still working ... ", location);
                 }
             }
         }
@@ -136,6 +150,12 @@ public class ReasonersConcurrencyTest ex
     private abstract class JobClient implements Callable<Result> {
         abstract URI uri(String queryString);
         
+        private List<Header> headers = new ArrayList<Header>();
+        
+        protected void addHeader(String key, String value){
+            headers.add(new BasicHeader(key,value));
+        }
+        
         protected HttpResponse get() throws Exception{
             return get(new String[0]);
         }
@@ -171,18 +191,20 @@ public class ReasonersConcurrencyTest ex
     }
     
     private class Pinger extends JobClient {
-        String jid = null;
+        String location = null;
         
-        Pinger(String jid){
-            this.jid = jid;
+        Pinger(String location){
+            this.location = location;
         }
         
         URI uri(String queryString){
-            return 
URI.create(ReasonersConcurrencyTest.this.builder.buildUrl(REASONERS_PATH+"/jobs/ping/"+jid+queryString));
+            return URI.create(location + queryString);
         }
         
         public PingerResult call() throws Exception {
-            return new PingerResult(jid, get());
+            // We ping the Job service with mime type application/json
+            this.addHeader("Accept", "application/json");
+            return new PingerResult(location, get());
         }
     }
 
@@ -225,20 +247,28 @@ public class ReasonersConcurrencyTest ex
     }
 
     private class PingerResult extends Result {
-        private String jid= null;
-        PingerResult(String jid, HttpResponse response){
+        
+        private String location= null;
+        
+        PingerResult(String location, HttpResponse response){
             super(response);
-            this.jid = jid;
+            this.location = location;
         }
         
-        String jid(){
-            return jid;
+        String location(){
+            return location;
         }
+        
+        /**
+         * We assert that: 
+         * - The job must exists (response code 200) 
+         * - The Content-type header returns JSON 
+         */
         @Override
         public Result assertResult() {
-            // Result of a ping request must be 200
-            assertNotNull(this.toString(), response);
-            assertEquals(200, response.getStatusLine().getStatusCode());
+            assertNotNull("Response cannot be null", response);
+            assertEquals("Result of a ping request must be 200 (Job must 
exists)", 200, response.getStatusLine().getStatusCode());
+            assertEquals("Content type must be application/json", 
"application/json", response.getFirstHeader("Content-type").getValue());
             return this;
         }
     }
@@ -251,9 +281,19 @@ public class ReasonersConcurrencyTest ex
         
         @Override
         public Result assertResult() {
-            // Result of a start request must be 200
-            assertNotNull(this.toString(), response);
-            assertEquals(200, response.getStatusLine().getStatusCode());
+            assertNotNull("Response cannot be null", response);
+            // Response of a start request must be 201
+            assertEquals(201, response.getStatusLine().getStatusCode());
+            // Response must contain the Location header
+            assertNotNull(response.getFirstHeader("Location"));
+            // The location header must be unique
+            assertTrue(response.getHeaders("Location").length == 1);
+            // The location value must be a valid URL
+            try {
+                
URI.create(response.getFirstHeader("Location").getValue()).toURL();
+            } catch (MalformedURLException e) {
+                assertTrue("Malformed url in location header",false);
+            }
             return this;
         }
     }

Modified: 
incubator/stanbol/branches/lto-reasoners/reasoners/services-tests/src/test/java/org/apache/stanbol/reasoners/it/offline/ReasonersOfflineJobsTest.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/branches/lto-reasoners/reasoners/services-tests/src/test/java/org/apache/stanbol/reasoners/it/offline/ReasonersOfflineJobsTest.java?rev=1205456&r1=1205455&r2=1205456&view=diff
==============================================================================
--- 
incubator/stanbol/branches/lto-reasoners/reasoners/services-tests/src/test/java/org/apache/stanbol/reasoners/it/offline/ReasonersOfflineJobsTest.java
 (original)
+++ 
incubator/stanbol/branches/lto-reasoners/reasoners/services-tests/src/test/java/org/apache/stanbol/reasoners/it/offline/ReasonersOfflineJobsTest.java
 Wed Nov 23 15:41:24 2011
@@ -33,7 +33,7 @@ public class ReasonersOfflineJobsTest ex
     
     @Test
     public void testSubsequentJobs() throws Exception{
-        log.info("testSimpleJob()");
+        log.info("testSubsequentJobs()");
         // We send a file to a job then we ping it, we do this for all 
services and tasks
         
         for(String s : allServices() ){
@@ -51,21 +51,21 @@ public class ReasonersOfflineJobsTest ex
         log.info("testSubsequentJobs2()");
         
         // We start all jobs and the we ping all
-        List<String> jids = new ArrayList<String>();
+        List<String> locations = new ArrayList<String>();
         for(String s : allServices() ){
             for(String t : TASKS){
                 StringBuilder sb = new StringBuilder(REASONERS_PATH);
                 sb.append(s).append(t).append("/job");
                 Request request = buildMultipartRequest(sb.toString(), 
multiPart);
-                String jid = job(request);
-                log.info("Started job {}", jid);
-                jids.add(jid);
+                String location = createJob(request);
+                log.info("Started job {}", location);
+                locations.add(location);
             }
         }
         
         // We ping all in sequence
-        for(String jid : jids){
-            pingSingleJob(jid);
+        for(String l : locations){
+            pingSingleJob(l);
         }
     }
 }

Modified: incubator/stanbol/branches/lto-reasoners/reasoners/test/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/branches/lto-reasoners/reasoners/test/pom.xml?rev=1205456&r1=1205455&r2=1205456&view=diff
==============================================================================
--- incubator/stanbol/branches/lto-reasoners/reasoners/test/pom.xml (original)
+++ incubator/stanbol/branches/lto-reasoners/reasoners/test/pom.xml Wed Nov 23 
15:41:24 2011
@@ -45,6 +45,10 @@
 
   <dependencies>
     <dependency>
+      <groupId>org.codehaus.jettison</groupId>
+      <artifactId>jettison</artifactId>
+    </dependency>
+    <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <scope>provided</scope>
@@ -59,6 +63,11 @@
       <artifactId>org.apache.stanbol.commons.testing.stanbol</artifactId>
     </dependency>
     <dependency>
+      <groupId>org.apache.stanbol</groupId>
+      <artifactId>org.apache.stanbol.commons.jobs.api</artifactId>
+      <version>0.1</version>
+    </dependency>
+    <dependency>
                <groupId>org.apache.james</groupId>
                <artifactId>apache-mime4j</artifactId>
                <version>0.6</version>

Modified: 
incubator/stanbol/branches/lto-reasoners/reasoners/test/src/main/java/org/apache/stanbol/reasoners/test/ReasonersTestBase.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/branches/lto-reasoners/reasoners/test/src/main/java/org/apache/stanbol/reasoners/test/ReasonersTestBase.java?rev=1205456&r1=1205455&r2=1205456&view=diff
==============================================================================
--- 
incubator/stanbol/branches/lto-reasoners/reasoners/test/src/main/java/org/apache/stanbol/reasoners/test/ReasonersTestBase.java
 (original)
+++ 
incubator/stanbol/branches/lto-reasoners/reasoners/test/src/main/java/org/apache/stanbol/reasoners/test/ReasonersTestBase.java
 Wed Nov 23 15:41:24 2011
@@ -1,18 +1,29 @@
 package org.apache.stanbol.reasoners.test;
 
+import java.net.URI;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import static org.junit.Assert.*;
 
+import org.apache.http.Header;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpUriRequest;
 import org.apache.http.entity.mime.MultipartEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.stanbol.commons.jobs.api.JobInfo;
 import org.apache.stanbol.commons.testing.http.Request;
 import org.apache.stanbol.commons.testing.stanbol.StanbolTestBase;
+import org.codehaus.jettison.json.JSONObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class ReasonersTestBase extends StanbolTestBase{
     protected final String REASONERS_PATH = "/reasoners";
+    protected final String JOBS_PATH = "/jobs";
     protected final String[] SERVICES = {"/owl", "/owlmini", "/rdfs"};
     protected final String[] TASKS = {"/check", "/classify", "/enrich"};
 
@@ -35,33 +46,91 @@ public class ReasonersTestBase extends S
     }
     
 
-    // Evoke the job service, returns the job Id
-    protected String job(Request request) throws Exception{
-        return executor.execute(request).assertStatus(200).getContent();
+    /** 
+     * Create a job, returns the location of the job.
+     * We also assert that:
+     * - response status code is 201
+     * - Location header do exists, and is unique (no multiple values allowed)
+     * - The value of the Location header muyst be a valid URI
+     * 
+     * @param request
+     * @return
+     * @throws Exception
+     */
+    protected String createJob(Request request) throws Exception{
+        HttpClient client = new DefaultHttpClient();
+        HttpResponse response = client.execute(request.getRequest());
+        // Response status code must be 201 Created
+        assertEquals(201, response.getStatusLine().getStatusCode());
+        // Job location must be in the Location: http header
+        // Location header must exists and must be unique
+        assertTrue(response.getHeaders("Location").length==1);
+        // Analyze the location header
+        Header locationHeader = response.getFirstHeader("Location");
+        String location = locationHeader.getValue();
+        // Location must be a valid URI
+        URI locationURI = URI.create(location);
+        // We do *not* check here if the body of the response contains a 
description
+        return locationURI.toString();
     }
     
-    //
+    /** 
+     * Create the job and check for its status until it is complete
+     * 
+     * @param request
+     * @throws Exception
+     */
     protected void executeAndPingSingleJob(Request request) throws Exception{
         log.info("Executing: {}", request.getRequest().getURI());
-        String jid = job(request);
-        log.info("jid is {}", jid);
+        String location = createJob(request);
+        log.info("Job location is {}", location);
         // Get the result and ping the jId
-        pingSingleJob(jid);
+        pingSingleJob(location);
     }
     
-    //
-    protected void pingSingleJob(String jid) throws Exception{
-        String url = REASONERS_PATH + "/jobs/ping/" + jid;
-        log.info("Pinging {} ... ", url);
+    /** 
+     * Check for the status of a job, pinging it each 0.5s until it is ready.
+     * It does **not** invoke the result.
+     * Asks for the application/json representation of the job.
+     * 
+     * We assert that:
+     * - The job must exists (response code 200)
+     * - The Content-type header returns JSON
+     * - The content contains valid JSON
+     * 
+     * @param location
+     * @throws Exception
+     */
+    protected void pingSingleJob(String location) throws Exception{
+        log.info("Start pinging {} ... ", location);
         boolean waiting = true;
         while(waiting){
-            String content = 
executor.execute(builder.buildGetRequest(url)).assertStatus(200).getContent();
-            if(content.equals("Job is still working")){
-                log.info(" ... still working ...");
-                Thread.sleep(500);
-            }else{
-                waiting = false;
-                log.info(" ... done!");
+            Request req = builder.buildOtherRequest(new HttpGet(location));
+            req.withHeader("Accept", "application/json");
+            log.info("Ping method: {}", req.getRequest().getMethod());
+            log.info("Ping location: {}", req.getRequest().getURI());
+            req.getRequest().setHeader("Accept","application/json");
+            log.info("headers:");
+            for(Header h : req.getRequest().getAllHeaders()){
+                log.info("{}: {}", h.getName(), h.getValue());
+            }
+            log.info("Request line:\n\n {} \n\n", 
req.getRequest().getRequestLine().toString());
+            try{
+                String content = 
executor.execute(req).assertStatus(200).assertContentType("application/json").getContent();
+                log.info("JSON content:\n\n {} \n\n",content);
+                JSONObject json = new JSONObject(content);
+                String status = json.getString("status");
+                
+                if(status.equals(JobInfo.RUNNING)){
+                    log.info(" ... still working (wait for 0.5s)");
+                    Thread.sleep(500);
+                }else{
+                    waiting = false;
+                    log.info(" ... done!");
+                }
+            }catch(Exception e){
+                log.error("An error occurred",e);
+                assertTrue(false);
             }
         }
     }


Reply via email to