Remove test jobs from statistics
[jabaws.git] / webservices / compbio / ws / client / WSTester.java
index 4db6778..f4a4708 100644 (file)
@@ -1,6 +1,6 @@
-/* Copyright (c) 2009 Peter Troshin\r
+/* Copyright (c) 2011 Peter Troshin\r
  *  \r
- *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     \r
+ *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0     \r
  * \r
  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
  *  Apache License version 2 as published by the Apache Software Foundation\r
@@ -26,9 +26,12 @@ import java.io.ByteArrayInputStream;
 import java.io.Closeable;\r
 import java.io.IOException;\r
 import java.io.PrintWriter;\r
+import java.net.ConnectException;\r
 import java.util.Arrays;\r
 import java.util.List;\r
 \r
+import javax.xml.ws.WebServiceException;\r
+\r
 import org.apache.log4j.Logger;\r
 \r
 import compbio.data.msa.JABAService;\r
@@ -63,15 +66,15 @@ public class WSTester {
        /**\r
         * Sequences to be used as input for all WS\r
         */\r
-       static final String fastaInput = ">Foo\n"\r
+       public static final String fastaInput = ">Foo\n"\r
                        + "MTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV"\r
                        + "\n>Bar\n"\r
-                       + "ASDAAPEHPGIALWLHALEDAGQAEAAAAYTRAHQLLPEEPYITAQLLNAVA";\r
+                       + "ASDAAPEHPGIALWLHALEDAGQAEAAAAYTRAHQLLPEEPYITAQLLNAVA\n";\r
 \r
-       static final String fastaAlignment = ">Foo\r\n"\r
-                       + "MTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV--------\r\n"\r
-                       + ">Bar\r\n"\r
-                       + "ASDAAPEH------------PGIALWLHALE-DAGQAEAAA---AYTRAHQLLPEEPYITAQLLNAVA\r\n"\r
+       public static final String fastaAlignment = ">Foo\n"\r
+                       + "MTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV--------\n"\r
+                       + ">Bar\n"\r
+                       + "ASDAAPEH------------PGIALWLHALE-DAGQAEAAA---AYTRAHQLLPEEPYITAQLLNAVA\n"\r
                        + "";\r
 \r
        static final List<FastaSequence> seqs = loadSeqs();\r
@@ -116,6 +119,15 @@ public class WSTester {
        private final PrintWriter writer;\r
        private final String hostname;\r
 \r
+       /**\r
+        * Construct an instance of JABAWS tester\r
+        * \r
+        * @param hostname\r
+        *            - fully qualified host and context name of JABAWS e.g.\r
+        *            http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba\r
+        * @param writer\r
+        *            a PrintWriter instance to writer test log to.\r
+        */\r
        public WSTester(String hostname, PrintWriter writer) {\r
                if (Util.isEmpty(hostname)) {\r
                        throw new NullPointerException("Hostname must be provided!");\r
@@ -182,8 +194,7 @@ public class WSTester {
                return succeed;\r
        }\r
 \r
-       private <T> boolean testMsaWS(MsaWS<T> msaws)\r
-                       throws UnsupportedRuntimeException {\r
+       private <T> boolean testMsaWS(MsaWS<T> msaws) throws Exception {\r
                assert msaws != null;\r
 \r
                boolean succeed = testDefaultAlignment(msaws);\r
@@ -210,7 +221,7 @@ public class WSTester {
         *             executable does not exists for a server platform\r
         */\r
        @SuppressWarnings("unchecked")\r
-       private <T> boolean checkService(JABAService wservice) {\r
+       private <T> boolean checkService(JABAService wservice, Services service) {\r
                try {\r
                        if (wservice == null) {\r
                                throw new NullPointerException(\r
@@ -220,22 +231,27 @@ public class WSTester {
                        if (wservice instanceof MsaWS) {\r
                                return testMsaWS((MsaWS<T>) wservice);\r
                        } else if (wservice instanceof SequenceAnnotation) {\r
-                               return testSequenceAnnotationWS((SequenceAnnotation<T>) wservice);\r
+                               return testSequenceAnnotationWS(\r
+                                               (SequenceAnnotation<T>) wservice, service);\r
                        } else {\r
                                throw new UnsupportedOperationException("The service: "\r
                                                + wservice.getClass() + " is not supported! ");\r
                        }\r
-               } catch (UnsupportedRuntimeException e) {\r
+               } catch (Exception e) {\r
                        reportException(e);\r
                        return false;\r
                }\r
        }\r
 \r
-       private <T> boolean testSequenceAnnotationWS(SequenceAnnotation<T> wservice)\r
-                       throws UnsupportedRuntimeException {\r
+       private <T> boolean testSequenceAnnotationWS(\r
+                       SequenceAnnotation<T> wservice, Services service) throws Exception {\r
                writer.print("Calling analyse.........");\r
-               boolean success = testDefaultAnalyse(loadAlignment(), wservice, null,\r
-                               null);\r
+\r
+               List<FastaSequence> input = loadSeqs();\r
+               if (service == Services.AAConWS) {\r
+                       input = loadAlignment();\r
+               }\r
+               boolean success = testDefaultAnalyse(input, wservice, null, null);\r
 \r
                PresetManager<T> presetman = wservice.getPresets();\r
                if (presetman != null) {\r
@@ -243,8 +259,7 @@ public class WSTester {
                        if (presets != null && !presets.isEmpty()) {\r
                                Preset<T> preset = presets.get(0);\r
                                writer.print("Calling analyse with Preset.........");\r
-                               success = testDefaultAnalyse(loadAlignment(), wservice, preset,\r
-                                               null);\r
+                               success = testDefaultAnalyse(input, wservice, preset, null);\r
                        }\r
                }\r
                testMetadata(wservice);\r
@@ -253,30 +268,24 @@ public class WSTester {
 \r
        private <T> boolean testDefaultAnalyse(List<FastaSequence> fastalist,\r
                        SequenceAnnotation<T> wsproxy, Preset<T> preset,\r
-                       List<Option<T>> customOptions) throws UnsupportedRuntimeException {\r
+                       List<Option<T>> customOptions) throws Exception {\r
 \r
                ScoreManager scores = null;\r
-               try {\r
-                       String jobId = null;\r
-                       if (customOptions != null) {\r
-                               jobId = wsproxy.customAnalize(fastalist, customOptions);\r
-                       } else if (preset != null) {\r
-                               jobId = wsproxy.presetAnalize(fastalist, preset);\r
-                       } else {\r
-                               jobId = wsproxy.analize(fastalist);\r
-                       }\r
-                       Thread.sleep(1000);\r
-                       scores = wsproxy.getAnnotation(jobId);\r
-                       if (scores != null) {\r
-                               writer.println(OK);\r
-                       }\r
-               } catch (Exception e) {\r
-                       if (e instanceof UnsupportedRuntimeException) {\r
-                               throw (UnsupportedRuntimeException) e;\r
-                       } else {\r
-                               reportException(e);\r
-                       }\r
+\r
+               String jobId = null;\r
+               if (customOptions != null) {\r
+                       jobId = wsproxy.customAnalize(fastalist, customOptions);\r
+               } else if (preset != null) {\r
+                       jobId = wsproxy.presetAnalize(fastalist, preset);\r
+               } else {\r
+                       jobId = wsproxy.analize(fastalist);\r
+               }\r
+               Thread.sleep(1000);\r
+               scores = wsproxy.getAnnotation(jobId);\r
+               if (scores != null) {\r
+                       writer.println(OK);\r
                }\r
+\r
                return scores != null;\r
        }\r
 \r
@@ -311,6 +320,7 @@ public class WSTester {
                LimitsManager<T> limits = msaws.getLimits();\r
                if (limits != null && limits.getLimits().size() > 0) {\r
                        writer.println(OK);\r
+                       // writer.println("Limits details: \n" + limits.toString());\r
                } else {\r
                        writer.println(UNSUPPORTED);\r
                }\r
@@ -332,30 +342,22 @@ public class WSTester {
         * @param msaws\r
         * @throws UnsupportedRuntimeException\r
         */\r
-       private <T> boolean testDefaultAlignment(MsaWS<T> msaws)\r
-                       throws UnsupportedRuntimeException {\r
+       private <T> boolean testDefaultAlignment(MsaWS<T> msaws) throws Exception {\r
                writer.print("Testing alignment with default parameters:");\r
                Alignment al = null;\r
                boolean succeed = false;\r
-               try {\r
-                       String taskId = msaws.align(seqs);\r
-                       writer.print("\nQuerying job status...");\r
-                       JobStatus status = msaws.getJobStatus(taskId);\r
-                       while (status != JobStatus.FINISHED) {\r
-                               Thread.sleep(1000);\r
-                               status = msaws.getJobStatus(taskId);\r
-                       }\r
-                       writer.println(OK);\r
-                       writer.print("Retrieving results...");\r
-                       al = msaws.getResult(taskId);\r
-                       succeed = true;\r
-               } catch (Exception e) {\r
-                       if (e instanceof UnsupportedRuntimeException) {\r
-                               throw (UnsupportedRuntimeException) e;\r
-                       } else {\r
-                               reportException(e);\r
-                       }\r
+\r
+               String taskId = msaws.align(seqs);\r
+               writer.print("\nQuerying job status...");\r
+               JobStatus status = msaws.getJobStatus(taskId);\r
+               while (status != JobStatus.FINISHED) {\r
+                       Thread.sleep(1000);\r
+                       status = msaws.getJobStatus(taskId);\r
                }\r
+               writer.println(OK);\r
+               writer.print("Retrieving results...");\r
+               al = msaws.getResult(taskId);\r
+               succeed = true;\r
                if (al != null) {\r
                        writer.println(OK);\r
                }\r
@@ -413,7 +415,17 @@ public class WSTester {
 \r
        }\r
 \r
-       public boolean checkService(Services service) {\r
+       /**\r
+        * Test JABA web service\r
+        * \r
+        * @param service\r
+        *            the service to test\r
+        * @return true if the service works as expected, false otherwise\r
+        * @throws WebServiceException\r
+        * @throws ConnectException\r
+        */\r
+       public boolean checkService(Services service) throws ConnectException,\r
+                       WebServiceException {\r
                JABAService ws = Jws2Client.connect(hostname, service);\r
                if (ws == null) {\r
                        writer.println("Cannot estabilish the connection to host "\r
@@ -423,7 +435,7 @@ public class WSTester {
                boolean succeed = false;\r
                try {\r
                        writer.println("Checking service " + service.toString());\r
-                       succeed = checkService(ws);\r
+                       succeed = checkService(ws, service);\r
                } finally {\r
                        FileUtil.closeSilently(((Closeable) ws));\r
                }\r
@@ -436,7 +448,7 @@ public class WSTester {
                        writer.println("Check is completed. The Service " + serv\r
                                        + " IS WORKING\n");\r
                } else {\r
-                       writer.println("Check is completed. The Service " + serv\r
+                       writer.println("Check is aborted. The Service " + serv\r
                                        + " HAS SOME PROBLEMS\n");\r
                }\r
        }\r