Merge branch 'alpha/JAL-3362_Jalview_212_alpha' into alpha/merge_212_JalviewJS_2112
[jalview.git] / test / jalview / ws / jabaws / AAConAnnotAndSettingsIO.java
diff --git a/test/jalview/ws/jabaws/AAConAnnotAndSettingsIO.java b/test/jalview/ws/jabaws/AAConAnnotAndSettingsIO.java
new file mode 100644 (file)
index 0000000..2a6c3d0
--- /dev/null
@@ -0,0 +1,200 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
+package jalview.ws.jabaws;
+
+import static org.testng.AssertJUnit.assertNotNull;
+import static org.testng.AssertJUnit.assertTrue;
+
+import jalview.bin.Cache;
+import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
+import jalview.gui.AlignFrame;
+import jalview.gui.JvOptionPane;
+import jalview.ws.api.ServiceWithParameters;
+import jalview.ws.jws2.Jws2Discoverer;
+import jalview.ws.jws2.SeqAnnotationServiceCalcWorker;
+import jalview.ws.slivkaws.SlivkaWSDiscoverer;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+/*
+ * All methods in this class are set to the Network group because setUpBeforeClass will fail
+ * if there is no network.
+ */
+@Test(singleThreaded = true)
+public class AAConAnnotAndSettingsIO
+{
+
+  @BeforeClass(alwaysRun = true)
+  public void setUpJvOptionPane()
+  {
+    JvOptionPane.setInteractiveMode(false);
+    JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
+  }
+
+  public static String testseqs = "examples/uniref50.fa";
+
+  public static Jws2Discoverer disc;
+
+  public static List<ServiceWithParameters[]> aacon;
+
+  jalview.ws.jws2.SeqAnnotationServiceCalcWorker aaconClient;
+
+  public static jalview.gui.AlignFrame af = null;
+
+  @BeforeClass(alwaysRun = true)
+  public static void setUpBeforeClass() throws Exception
+  {
+    Cache.loadProperties("test/jalview/io/testProps.jvprops");
+    Cache.initLogger();
+    disc = JalviewJabawsTestUtils.getJabawsDiscoverer();
+
+    while (disc.isRunning())
+    {
+      // don't get services until discoverer has finished
+      Thread.sleep(100);
+    }
+
+
+    aacon = new ArrayList<>();
+    for (ServiceWithParameters svc : disc.getServices())
+    {
+      if (svc.getNameURI().toLowerCase().contains("aacon"))
+      {
+        aacon.add(new ServiceWithParameters[] { svc });
+      }
+    }
+
+    for (ServiceWithParameters svc : SlivkaWSDiscoverer.getServices())
+    {
+      if (svc.getNameURI().toLowerCase().contains("aacon"))
+      {
+        aacon.add(new ServiceWithParameters[] { svc });
+      }
+    }
+    assertTrue("Couldn't discover any AACon services to use to test.",
+            aacon.size() > 0);
+  }
+
+  @AfterClass(alwaysRun = true)
+  public static void tearDownAfterClass() throws Exception
+  {
+    if (af != null)
+    {
+      af.setVisible(false);
+      af.dispose();
+      af = null;
+    }
+  }
+
+  @DataProvider(name = "aacons")
+  public Object[][] getAaconArray()
+  {
+
+    Object[][] rtn = new Object[aacon.size()][1];
+    int i = 0;
+    for (ServiceWithParameters[] aa : aacon)
+    {
+      rtn[i++] = aa;
+    }
+    return rtn;
+  }
+  /**
+   * Run AACon on an alignment with defaults and verify Just Shenkin annotation
+   * appears
+   */
+  @Test(groups = { "External", "Network" }, dataProvider = "aacons")
+  public void testAAConAnnotAndRecovery(ServiceWithParameters service)
+  {
+    jalview.io.FileLoader fl = new jalview.io.FileLoader(false);
+    AlignFrame _af = fl.LoadFileWaitTillLoaded(testseqs,
+            jalview.io.DataSourceType.FILE);
+    assertNotNull("Couldn't load test data ('" + testseqs + "')", _af);
+    af = _af;
+    try
+    {
+    testAAConClient(_af, service);
+    } finally
+    {
+      af = null;
+      _af.setVisible(false);
+      _af.dispose();
+      _af = null;
+
+    }
+  }
+
+  /**
+   * triggers the given aacon worker on the alignment, waits for 5s and gives up
+   * or verifies SHENKIN annotation is produced.
+   * 
+   * @param af
+   *          - test data in an alignment frame
+   * @param aacon
+   *          - the service to test
+   */
+  static void testAAConClient(AlignFrame af, ServiceWithParameters aacon)
+  {
+    SeqAnnotationServiceCalcWorker aaconClient = new SeqAnnotationServiceCalcWorker(
+            aacon, af, null,
+            null);
+    long current = System.currentTimeMillis(), limit = 15;
+    af.getViewport().getCalcManager().startWorker(aaconClient);
+    do
+    {
+      try
+      {
+        Thread.sleep(50);
+      } catch (InterruptedException x)
+      {
+      }
+      ;
+      assertTrue(
+              "Waited " + limit + "s for " + aacon.getHostURL()
+                      + " - giving up.",
+              (System.currentTimeMillis() - current) < limit * 1000);
+    } while (af.getViewport().getCalcManager().isWorking());
+    AlignmentI orig_alig = af.getViewport().getAlignment();
+    boolean foundShenkin = false;
+    Iterable<AlignmentAnnotation> _aa=orig_alig
+            .findAnnotation(aacon.getAlignAnalysisUI().getCalcId());
+    assertTrue("No annotation from service",
+            _aa != null && _aa.iterator().hasNext());
+
+    for (AlignmentAnnotation aa : _aa)
+    {
+      assertTrue("AACon annotation not marked as autocalculated!",
+              aa.autoCalculated);
+      if ("shenkin".equals(aa.label.toLowerCase()))
+      {
+        foundShenkin = true;
+        break;
+      }
+    }
+    assertTrue("Failed to locate 'SHENKIN' annotation row.", foundShenkin);
+  }
+}