Merge branch 'Jalview-JS/develop' into merge_js_develop
[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.getInstance()
93             .getServices())
94     {
95       if (svc.getNameURI().toLowerCase().contains("aacon"))
96       {
97         aacon.add(new ServiceWithParameters[] { svc });
98       }
99     }
100     assertTrue("Couldn't discover any AACon services to use to test.",
101             aacon.size() > 0);
102   }
103
104   @AfterClass(alwaysRun = true)
105   public static void tearDownAfterClass() throws Exception
106   {
107     if (af != null)
108     {
109       af.setVisible(false);
110       af.dispose();
111       af = null;
112     }
113   }
114
115   @DataProvider(name = "aacons")
116   public Object[][] getAaconArray()
117   {
118
119     Object[][] rtn = new Object[aacon.size()][1];
120     int i = 0;
121     for (ServiceWithParameters[] aa : aacon)
122     {
123       rtn[i++] = aa;
124     }
125     return rtn;
126   }
127   /**
128    * Run AACon on an alignment with defaults and verify Just Shenkin annotation
129    * appears
130    */
131   @Test(groups = { "External", "Network" }, dataProvider = "aacons")
132   public void testAAConAnnotAndRecovery(ServiceWithParameters service)
133   {
134     jalview.io.FileLoader fl = new jalview.io.FileLoader(false);
135     AlignFrame _af = fl.LoadFileWaitTillLoaded(testseqs,
136             jalview.io.DataSourceType.FILE);
137     assertNotNull("Couldn't load test data ('" + testseqs + "')", _af);
138     af = _af;
139     try
140     {
141     testAAConClient(_af, service);
142     } finally
143     {
144       af = null;
145       _af.setVisible(false);
146       _af.dispose();
147       _af = null;
148
149     }
150   }
151
152   /**
153    * triggers the given aacon worker on the alignment, waits for 5s and gives up
154    * or verifies SHENKIN annotation is produced.
155    * 
156    * @param af
157    *          - test data in an alignment frame
158    * @param aacon
159    *          - the service to test
160    */
161   static void testAAConClient(AlignFrame af, ServiceWithParameters aacon)
162   {
163     SeqAnnotationServiceCalcWorker aaconClient = new SeqAnnotationServiceCalcWorker(
164             aacon, af, null,
165             null);
166     long current = System.currentTimeMillis(), limit = 15;
167     af.getViewport().getCalcManager().startWorker(aaconClient);
168     do
169     {
170       try
171       {
172         Thread.sleep(50);
173       } catch (InterruptedException x)
174       {
175       }
176       ;
177       assertTrue(
178               "Waited " + limit + "s for " + aacon.getHostURL()
179                       + " - giving up.",
180               (System.currentTimeMillis() - current) < limit * 1000);
181     } while (af.getViewport().getCalcManager().isWorking());
182     AlignmentI orig_alig = af.getViewport().getAlignment();
183     boolean foundShenkin = false;
184     Iterable<AlignmentAnnotation> _aa=orig_alig
185             .findAnnotation(aacon.getAlignAnalysisUI().getCalcId());
186     assertTrue("No annotation from service",
187             _aa != null && _aa.iterator().hasNext());
188
189     for (AlignmentAnnotation aa : _aa)
190     {
191       assertTrue("AACon annotation not marked as autocalculated!",
192               aa.autoCalculated);
193       if ("shenkin".equals(aa.label.toLowerCase()))
194       {
195         foundShenkin = true;
196         break;
197       }
198     }
199     assertTrue("Failed to locate 'SHENKIN' annotation row.", foundShenkin);
200   }
201 }