JAL-3070 jaba/slivka independent tests for disorder and AACon services
[jalview.git] / test / jalview / ws / jabaws / AAConAnnotAndSettingsIO.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ws.jabaws;
22
23 import static org.testng.AssertJUnit.assertNotNull;
24 import static org.testng.AssertJUnit.assertTrue;
25
26 import jalview.bin.Cache;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.AlignmentI;
29 import jalview.gui.AlignFrame;
30 import jalview.gui.JvOptionPane;
31 import jalview.ws.api.ServiceWithParameters;
32 import jalview.ws.jws2.Jws2Discoverer;
33 import jalview.ws.jws2.SeqAnnotationServiceCalcWorker;
34 import jalview.ws.slivkaws.SlivkaWSDiscoverer;
35
36 import java.util.ArrayList;
37 import java.util.List;
38
39 import org.testng.annotations.AfterClass;
40 import org.testng.annotations.BeforeClass;
41 import org.testng.annotations.DataProvider;
42 import org.testng.annotations.Test;
43
44 /*
45  * All methods in this class are set to the Network group because setUpBeforeClass will fail
46  * if there is no network.
47  */
48 @Test(singleThreaded = true)
49 public class AAConAnnotAndSettingsIO
50 {
51
52   @BeforeClass(alwaysRun = true)
53   public void setUpJvOptionPane()
54   {
55     JvOptionPane.setInteractiveMode(false);
56     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
57   }
58
59   public static String testseqs = "examples/uniref50.fa";
60
61   public static Jws2Discoverer disc;
62
63   public static List<ServiceWithParameters[]> aacon;
64
65   jalview.ws.jws2.SeqAnnotationServiceCalcWorker aaconClient;
66
67   public static jalview.gui.AlignFrame af = null;
68
69   @BeforeClass(alwaysRun = true)
70   public static void setUpBeforeClass() throws Exception
71   {
72     Cache.loadProperties("test/jalview/io/testProps.jvprops");
73     Cache.initLogger();
74     disc = JalviewJabawsTestUtils.getJabawsDiscoverer();
75
76     while (disc.isRunning())
77     {
78       // don't get services until discoverer has finished
79       Thread.sleep(100);
80     }
81
82
83     aacon = new ArrayList<>();
84     for (ServiceWithParameters svc : disc.getServices())
85     {
86       if (svc.getNameURI().toLowerCase().contains("aacon"))
87       {
88         aacon.add(new ServiceWithParameters[] { svc });
89       }
90     }
91
92     for (ServiceWithParameters svc : SlivkaWSDiscoverer.getServices())
93     {
94       if (svc.getNameURI().toLowerCase().contains("aacon"))
95       {
96         aacon.add(new ServiceWithParameters[] { svc });
97       }
98     }
99     assertTrue("Couldn't discover any AACon services to use to test.",
100             aacon.size() > 0);
101   }
102
103   @AfterClass(alwaysRun = true)
104   public static void tearDownAfterClass() throws Exception
105   {
106     if (af != null)
107     {
108       af.setVisible(false);
109       af.dispose();
110       af = null;
111     }
112   }
113
114   @DataProvider(name = "aacons")
115   public Object[][] getAaconArray()
116   {
117
118     Object[][] rtn = new Object[aacon.size()][1];
119     int i = 0;
120     for (ServiceWithParameters[] aa : aacon)
121     {
122       rtn[i++] = aa;
123     }
124     return rtn;
125   }
126   /**
127    * Run AACon on an alignment with defaults and verify Just Shenkin annotation
128    * appears
129    */
130   @Test(groups = { "External", "Network" }, dataProvider = "aacons")
131   public void testAAConAnnotAndRecovery(ServiceWithParameters service)
132   {
133     jalview.io.FileLoader fl = new jalview.io.FileLoader(false);
134     AlignFrame _af = fl.LoadFileWaitTillLoaded(testseqs,
135             jalview.io.DataSourceType.FILE);
136     assertNotNull("Couldn't load test data ('" + testseqs + "')", _af);
137     af = _af;
138     try
139     {
140     testAAConClient(_af, service);
141     } finally
142     {
143       af = null;
144       _af.setVisible(false);
145       _af.dispose();
146       _af = null;
147
148     }
149   }
150
151   /**
152    * triggers the given aacon worker on the alignment, waits for 5s and gives up
153    * or verifies SHENKIN annotation is produced.
154    * 
155    * @param af
156    *          - test data in an alignment frame
157    * @param aacon
158    *          - the service to test
159    */
160   static void testAAConClient(AlignFrame af, ServiceWithParameters aacon)
161   {
162     SeqAnnotationServiceCalcWorker aaconClient = new SeqAnnotationServiceCalcWorker(
163             aacon, af, null,
164             null);
165     long current = System.currentTimeMillis(), limit = 15;
166     af.getViewport().getCalcManager().startWorker(aaconClient);
167     do
168     {
169       try
170       {
171         Thread.sleep(50);
172       } catch (InterruptedException x)
173       {
174       }
175       ;
176       assertTrue(
177               "Waited " + limit + "s for " + aacon.getHostURL()
178                       + " - giving up.",
179               (System.currentTimeMillis() - current) < limit * 1000);
180     } while (af.getViewport().getCalcManager().isWorking());
181     AlignmentI orig_alig = af.getViewport().getAlignment();
182     boolean foundShenkin = false;
183     Iterable<AlignmentAnnotation> _aa=orig_alig
184             .findAnnotation(aacon.getAlignAnalysisUI().getCalcId());
185     assertTrue("No annotation from service",
186             _aa != null && _aa.iterator().hasNext());
187
188     for (AlignmentAnnotation aa : _aa)
189     {
190       assertTrue("AACon annotation not marked as autocalculated!",
191               aa.autoCalculated);
192       if ("shenkin".equals(aa.label.toLowerCase()))
193       {
194         foundShenkin = true;
195         break;
196       }
197     }
198     assertTrue("Failed to locate 'SHENKIN' annotation row.", foundShenkin);
199   }
200 }