Changes which allow JpredWS test
[jabaws.git] / webservices / compbio / ws / client / WSTester.java
index 7091f02..2f33cb1 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,10 +26,11 @@ 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 org.apache.log4j.Logger;\r
+import javax.xml.ws.WebServiceException;\r
 \r
 import compbio.data.msa.JABAService;\r
 import compbio.data.msa.Metadata;\r
@@ -59,22 +60,24 @@ import compbio.util.Util;
  */\r
 public class WSTester {\r
 \r
-       private static Logger log = Logger.getLogger(WSTester.class);\r
        /**\r
         * Sequences to be used as input for all WS\r
         */\r
-       static final String fastaInput = ">Foo\n"\r
+       public static final String fastaInput2records = ">Foo\n"\r
                        + "MTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV"\r
                        + "\n>Bar\n"\r
-                       + "ASDAAPEHPGIALWLHALEDAGQAEAAAAYTRAHQLLPEEPYITAQLLNAVA";\r
+                       + "ASDAAPEHPGIALWLHALEDAGQAEAAAAYTRAHQLLPEEPYITAQLLNAVA\n";\r
+       public static final String fastaInput1record = ">Foo\n"\r
+                       + "MTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV"\r
+                       + "\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
+       static final List<FastaSequence> seqs = loadSeqs(2);\r
 \r
        private static final String FAILED = "FAILED";\r
        private static final String OK = "OK";\r
@@ -86,10 +89,12 @@ public class WSTester {
         * \r
         * @return List of FastaSequence records\r
         */\r
-       private static List<FastaSequence> loadSeqs() {\r
+       private static List<FastaSequence> loadSeqs(int nLines) {\r
                try {\r
-                       return SequenceUtil.readFasta(new ByteArrayInputStream(fastaInput\r
-                                       .getBytes()));\r
+                       if (nLines == 1) {\r
+                               return SequenceUtil.readFasta(new ByteArrayInputStream(fastaInput1record.getBytes()));\r
+                       }\r
+                       return SequenceUtil.readFasta(new ByteArrayInputStream(fastaInput2records.getBytes()));\r
                } catch (IOException ignored) {\r
                        // Should not happen as a source is not a external stream\r
                        ignored.printStackTrace();\r
@@ -116,6 +121,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
@@ -132,12 +146,10 @@ public class WSTester {
                                + pseparator + "host_and_context " + "<" + servicekey\r
                                + pseparator + "serviceName>");\r
                System.out.println();\r
-               System.out\r
-                               .println(hostkey\r
+               System.out.println(hostkey\r
                                                + pseparator\r
                                                + "<host_and_context> - a full URL to the JABAWS web server including context path e.g. http://10.31.1.159:8080/ws");\r
-               System.out\r
-                               .println(servicekey\r
+               System.out.println(servicekey\r
                                                + pseparator\r
                                                + "<ServiceName> - optional if unspecified all services are tested otherwise one of "\r
                                                + Arrays.toString(Services.values()));\r
@@ -209,20 +221,19 @@ 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
-                                               "JABAService instance must be provided!");\r
+                               throw new NullPointerException("JABAService instance must be provided!");\r
                        }\r
 \r
                        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
+                               throw new UnsupportedOperationException("The service: " + wservice.getClass() + " is not supported! ");\r
                        }\r
                } catch (Exception e) {\r
                        reportException(e);\r
@@ -230,11 +241,18 @@ public class WSTester {
                }\r
        }\r
 \r
-       private <T> boolean testSequenceAnnotationWS(SequenceAnnotation<T> wservice)\r
-                       throws Exception {\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(2);\r
+               if (service == Services.AAConWS) {\r
+                       input = loadAlignment();\r
+               }\r
+               if (service == Services.JpredWS) {\r
+                       input = loadSeqs(1);\r
+               }\r
+               boolean success = testDefaultAnalyse(input, wservice, null, null);\r
 \r
                PresetManager<T> presetman = wservice.getPresets();\r
                if (presetman != null) {\r
@@ -242,8 +260,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
@@ -304,6 +321,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
@@ -368,8 +386,7 @@ public class WSTester {
                String host = CmdHelper.getHost(args);\r
                String serviceName = CmdHelper.getServiceName(args);\r
                if (!Jws2Client.validURL(host)) {\r
-                       System.err\r
-                                       .println("<host_and_context> parameter is not provided or is incorrect!");\r
+                       System.err.println("<host_and_context> parameter is not provided or is incorrect!");\r
                        System.exit(1);\r
                }\r
                WSTester tester = new WSTester(host, new PrintWriter(System.out, true));\r
@@ -377,8 +394,7 @@ public class WSTester {
                if (serviceName != null) {\r
                        Services service = Services.getService(serviceName);\r
                        if (service == null) {\r
-                               tester.writer.println("Service '" + serviceName\r
-                                               + "' is not supported. Valid values are: "\r
+                               tester.writer.println("Service '" + serviceName + "' is not supported. Valid values are: "\r
                                                + Arrays.toString(Services.values()));\r
                                tester.writer.println();\r
                                printUsage();\r
@@ -388,8 +404,7 @@ public class WSTester {
                        System.exit(0);\r
                }\r
 \r
-               tester.writer\r
-                               .println("<ServiceName> is not provided checking all known services...");\r
+               tester.writer.println("<ServiceName> is not provided checking all known services...");\r
 \r
                for (Services serv : Services.values()) {\r
                        tester.writer.println();\r
@@ -398,17 +413,27 @@ 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
-                                       + hostname + " with service " + service.toString());\r
+                       String line = "Cannot estabilish the connection to host " + hostname + " with service ";\r
+                       writer.println(line + service.toString());\r
                        return false;\r
                }\r
                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
@@ -418,11 +443,9 @@ public class WSTester {
 \r
        private void reportResults(Services serv, boolean succeed) {\r
                if (succeed) {\r
-                       writer.println("Check is completed. The Service " + serv\r
-                                       + " IS WORKING\n");\r
+                       writer.println("Check is completed. The Service " + serv + " IS WORKING\n");\r
                } else {\r
-                       writer.println("Check is aborted. The Service " + serv\r
-                                       + " HAS SOME PROBLEMS\n");\r
+                       writer.println("Check is aborted. The Service " + serv + " HAS SOME PROBLEMS\n");\r
                }\r
        }\r
 }\r