remove project files and keep sources only
[jabaws.git] / webservices / compbio / ws / client / WSTester.java
index 483c162..5450a1d 100644 (file)
@@ -1,6 +1,7 @@
 /* Copyright (c) 2011 Peter Troshin\r
+ * Copyright (c) 2013 Alexander Sherstnev\r
  *  \r
- *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.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
@@ -27,9 +28,13 @@ import java.io.Closeable;
 import java.io.IOException;\r
 import java.io.PrintWriter;\r
 import java.net.ConnectException;\r
+import java.net.MalformedURLException;\r
+import java.net.URL;\r
 import java.util.Arrays;\r
 import java.util.List;\r
+import java.util.logging.Level;\r
 \r
+import javax.xml.ws.Service;\r
 import javax.xml.ws.WebServiceException;\r
 \r
 import compbio.data.msa.JABAService;\r
@@ -56,32 +61,36 @@ import compbio.util.Util;
  * \r
  * @author pvtroshin\r
  * \r
- * @version 1.0 February 2010\r
+ * @version 1.5 February 2013\r
  */\r
 public class WSTester {\r
 \r
        /**\r
-        * Sequences to be used as input for all WS\r
+        * Test sequences to be used as input for WS\r
+        */\r
+       public static final String fastaInput2records = \r
+                       ">Foo\nMTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV\n"\r
+               +   ">Bar\nASDAAPEHPGIALWLHALEDAGQAEAAAAYTRAHQLLPEEPYITAQLLNAVA\n";\r
+       public static final String fastaInput1record = \r
+                       ">Foo\nMTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV\n";\r
+       public static final String fastaAlignment = \r
+                       ">Foo\nMTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV--------\n"\r
+               +   ">Bar\nASDAAPEH------------PGIALWLHALE-DAGQAEAAA---AYTRAHQLLPEEPYITAQLLNAVA\n";\r
+       public static final String fastaRNAAlignment = \r
+                       ">Foo\nC-UUGCGUUAAUGAGAACAGAAACG-UAAA--CUAUAA-CCUAG-G-GGUUUCUGUUGGAUGGUUG----GCAAC\n"\r
+               +   ">Bar\nG-UGGCGCUUAUGACGCAGUUGUCU-UAAA-CUCGAAC--UCGA-GCGGGCAAUUGCUGAU-UACGAUUAACCAC\n";\r
+\r
+       public static final String clustalRNAAlignment = \r
+                       "CLUSTAL\n" +\r
+                       "Foo             C-UUGCGUUAAUGAGAACAGAAACG-UAAA--CUAUAA-CCUAG-G-GGUUUCUGUUGGA\n" +\r
+                       "Bar             G-UGGCGCUUAUGACGCAGUUGUCU-UAAA-CUCGAAC--UCGA-GCGGGCAAUUGCUGA\n" +\r
+                       "Foo             UGGUUG----GCAAC\n" +\r
+                       "Bar             U-UACGAUUAACCAC";\r
+       /**\r
+        * Status strings\r
         */\r
-       public static final String fastaInput = ">Foo\n"\r
-                       + "MTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV"\r
-                       + "\n>Bar\n"\r
-                       + "ASDAAPEHPGIALWLHALEDAGQAEAAAAYTRAHQLLPEEPYITAQLLNAVA\n";\r
-\r
-       public static final String fastaAlignment = ">Foo\n"\r
-                       + "MTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV--------\n"\r
-                       + ">Bar\n"\r
-                       + "ASDAAPEH------------PGIALWLHALE-DAGQAEAAA---AYTRAHQLLPEEPYITAQLLNAVA\n"\r
-                       + "";\r
-\r
-       public static final String fastaRNAAlignment = ">Foo\n"\r
-                       + "C-UUGCGUUAAUGAGAACAGAAACG-UAAA--CUAUAA-CCUAG-G-GGUUUCUGUUGGAUGGUUG----GCAAC\n"\r
-                       + ">Bar\n"\r
-                       + "G-UGGCGCUUAUGACGCAGUUGUCU-UAAA-CUCGAAC--UCGA-GCGGGCAAUUGCUGAU-UACGAUUAACCAC\n";\r
-\r
        private static final String FAILED = "FAILED";\r
        private static final String OK = "OK";\r
-\r
        private static final String UNSUPPORTED = "UNSUPPORTED";\r
 \r
        /**\r
@@ -89,10 +98,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
@@ -103,7 +114,7 @@ public class WSTester {
        /**\r
         * Converting input to a form accepted by WS\r
         * \r
-        * @return List of FastaSequence records with aligned protein sequences\r
+        * @return List of FastaSequence records\r
         */\r
        private static List<FastaSequence> loadAlignment() {\r
                try {\r
@@ -158,12 +169,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
@@ -187,7 +196,7 @@ public class WSTester {
                        writer.print("Aligning with preset '" + preset.getName() + "'... ");\r
                        Alignment al = null;\r
                        try {\r
-                               String taskId = msaws.presetAlign(loadSeqs(), preset);\r
+                               String taskId = msaws.presetAlign(loadSeqs(2), preset);\r
                                al = msaws.getResult(taskId);\r
                                if (al != null) {\r
                                        writer.println(OK);\r
@@ -208,10 +217,10 @@ public class WSTester {
                return succeed;\r
        }\r
 \r
-       private <T> boolean testMsaWS(MsaWS<T> msaws) throws Exception {\r
+       private <T> boolean testMsaWS(MsaWS<T> msaws, Services service) throws Exception {\r
                assert msaws != null;\r
 \r
-               boolean succeed = testDefaultAlignment(msaws);\r
+               boolean succeed = testDefaultAlignment(msaws, service);\r
                // If exception above is thrown than the tests below is not run\r
 \r
                PresetManager<T> pmanager = msaws.getPresets();\r
@@ -223,7 +232,6 @@ public class WSTester {
                testMetadata(msaws);\r
                return succeed;\r
        }\r
-       \r
        /**\r
         * Call most of web services functions and check the output\r
         * \r
@@ -239,18 +247,16 @@ public class WSTester {
        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
+                               return testMsaWS((MsaWS<T>) wservice, service);\r
                        } else if (wservice instanceof SequenceAnnotation) {\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
@@ -260,9 +266,9 @@ public class WSTester {
 \r
        private <T> boolean testSequenceAnnotationWS(\r
                        SequenceAnnotation<T> wservice, Services service) throws Exception {\r
-               writer.print("Calling analyse.........");\r
+               writer.print("Calling annotation test.........");\r
 \r
-               List<FastaSequence> input = loadSeqs();\r
+               List<FastaSequence> input = loadSeqs(2);\r
                if (service == Services.AAConWS ) {\r
                        input = loadAlignment();\r
                } else if (service == Services.RNAalifoldWS) {\r
@@ -308,8 +314,7 @@ public class WSTester {
 \r
        private void reportException(Exception e) {\r
                writer.println(FAILED);\r
-               writer.println("Exception while waiting for results. "\r
-                               + "Exception details are below:");\r
+               writer.println("Exception while waiting for results. Exception details are below:");\r
                writer.println(e.getLocalizedMessage());\r
                e.printStackTrace(writer);\r
        }\r
@@ -359,12 +364,17 @@ public class WSTester {
         * @param msaws\r
         * @throws UnsupportedRuntimeException\r
         */\r
-       private <T> boolean testDefaultAlignment(MsaWS<T> msaws) throws Exception {\r
+       private <T> boolean testDefaultAlignment(MsaWS<T> msaws, Services service) throws Exception {\r
                writer.print("Testing alignment with default parameters:");\r
                Alignment al = null;\r
                boolean succeed = false;\r
+               String taskId;\r
 \r
-               String taskId = msaws.align(loadSeqs());\r
+               if (service == Services.JpredWS) {\r
+                       taskId = msaws.align(loadAlignment());\r
+               } else {\r
+                       taskId = msaws.align(loadSeqs(2));\r
+               }\r
                writer.print("\nQuerying job status...");\r
                JobStatus status = msaws.getJobStatus(taskId);\r
                while (status != JobStatus.FINISHED) {\r
@@ -380,7 +390,6 @@ public class WSTester {
                }\r
                return succeed;\r
        }\r
-       \r
        /**\r
         * Test JWS2 web services\r
         * \r
@@ -403,8 +412,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
@@ -412,8 +420,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
@@ -423,13 +430,51 @@ 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
                        tester.checkService(serv);\r
                }\r
+\r
+       }\r
+       \r
+       /**\r
+        * Connects to a web service by the host and the service name web service type\r
+        * \r
+        * @param host\r
+        *            the fully qualified name of JABAWS server including JABAWS\r
+        *            context name e.g\r
+        *            http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba\r
+        * @param service\r
+        *            the name of the JABAWS service to connect to\r
+        * @return JABAService<T>\r
+        * @throws WebServiceException\r
+        * @throws ConnectException\r
+        *             if fails to connect to the service on the host\r
+        */\r
+       private JABAService connect(String host, Services service)\r
+                       throws WebServiceException, ConnectException {\r
+               URL url = null;\r
+               System.out.println ("Attempting to connect with " + service.toString() + "...");\r
+               try {\r
+                       url = new URL(host + "/" + service.toString() + "?wsdl");\r
+                       System.out.println ("URL: " + url.toString());\r
+               } catch (MalformedURLException e) {\r
+                       e.printStackTrace();\r
+               }\r
+               Service serv = null;\r
+               try {\r
+                       serv = service.getService(url, service.getServiceNamespace());\r
+               } catch (WebServiceException wse) {\r
+                       wse.printStackTrace();\r
+               }\r
+               if (serv == null) {\r
+                       throw new ConnectException("Could not connect to " + url + ". Is the server down?");\r
+               }\r
+               JABAService srv = service.getInterface(serv);\r
+               System.out.println ("Connected successfully!");\r
+               return srv;\r
        }\r
 \r
        /**\r
@@ -443,10 +488,10 @@ public class WSTester {
         */\r
        public boolean checkService(Services service) throws ConnectException,\r
                        WebServiceException {\r
-               JABAService ws = Jws2Client.connect(hostname, service);\r
+               JABAService ws = 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