JAL-629 test fixing
[jalview.git] / test / jalview / gui / StructureChooserTest.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.gui;
22
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.AssertJUnit.assertNotNull;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import java.util.Collection;
28 import java.util.Vector;
29
30 import org.junit.Assert;
31 import org.testng.annotations.AfterMethod;
32 import org.testng.annotations.BeforeClass;
33 import org.testng.annotations.BeforeMethod;
34 import org.testng.annotations.Test;
35
36 import jalview.datamodel.DBRefEntry;
37 import jalview.datamodel.PDBEntry;
38 import jalview.datamodel.Sequence;
39 import jalview.datamodel.SequenceI;
40 import jalview.fts.api.FTSData;
41 import jalview.fts.core.FTSRestClient;
42 import jalview.fts.service.pdb.PDBFTSRestClient;
43 import jalview.fts.service.pdb.PDBFTSRestClientTest;
44 import jalview.fts.service.threedbeacons.TDBeaconsFTSRestClient;
45 import jalview.fts.threedbeacons.TDBeaconsFTSRestClientTest;
46 import jalview.gui.structurechooser.PDBStructureChooserQuerySource;
47 import jalview.jbgui.FilterOption;
48 import junit.extensions.PA;
49
50 @Test(singleThreaded = true)
51 public class StructureChooserTest
52 {
53
54   @BeforeClass(alwaysRun = true)
55   public void setUpJvOptionPane()
56   {
57     JvOptionPane.setInteractiveMode(false);
58     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
59   }
60
61   Sequence seq, upSeq, upSeq_nocanonical;
62
63   @BeforeMethod(alwaysRun = true)
64   public void setUp() throws Exception
65   {
66     seq = new Sequence("PDB|4kqy|4KQY|A", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1,
67             26);
68     seq.createDatasetSequence();
69     for (int x = 1; x < 5; x++)
70     {
71       DBRefEntry dbRef = new DBRefEntry();
72       dbRef.setAccessionId("XYZ_" + x);
73       seq.addDBRef(dbRef);
74     }
75
76     PDBEntry dbRef = new PDBEntry();
77     dbRef.setId("1tim");
78
79     Vector<PDBEntry> pdbIds = new Vector<>();
80     pdbIds.add(dbRef);
81
82     seq.setPDBId(pdbIds);
83
84     // Uniprot sequence for 3D-Beacons mocks
85     upSeq = new Sequence("P38398",
86             "MDLSALRVEEVQNVINAMQKILECPICLELIKEPVSTKCDHIFCKFCMLKLLNQKKGPSQCPLCKNDITKRS\n"
87                     + "LQESTRFSQLVEELLKIICAFQLDTGLEYANSYNFAKKENNSPEHLKDEVSIIQSMGYRNRAKRLLQSEPEN\n"
88                     + "PSLQETSLSVQLSNLGTVRTLRTKQRIQPQKTSVYIELGSDSSEDTVNKATYCSVGDQELLQITPQGTRDEI\n"
89                     + "SLDSAKKAACEFSETDVTNTEHHQPSNNDLNTTEKRAAERHPEKYQGSSVSNLHVEPCGTNTHASSLQHENS\n"
90                     + "SLLLTKDRMNVEKAEFCNKSKQPGLARSQHNRWAGSKETCNDRRTPSTEKKVDLNADPLCERKEWNKQKLPC\n"
91                     + "SENPRDTEDVPWITLNSSIQKVNEWFSRSDELLGSDDSHDGESESNAKVADVLDVLNEVDEYSGSSEKIDLL\n"
92                     + "ASDPHEALICKSERVHSKSVESNIEDKIFGKTYRKKASLPNLSHVTENLIIGAFVTEPQIIQERPLTNKLKR\n"
93                     + "KRRPTSGLHPEDFIKKADLAVQKTPEMINQGTNQTEQNGQVMNITNSGHENKTKGDSIQNEKNPNPIESLEK\n"
94                     + "ESAFKTKAEPISSSISNMELELNIHNSKAPKKNRLRRKSSTRHIHALELVVSRNLSPPNCTELQIDSCSSSE\n"
95                     + "EIKKKKYNQMPVRHSRNLQLMEGKEPATGAKKSNKPNEQTSKRHDSDTFPELKLTNAPGSFTKCSNTSELKE\n"
96                     + "FVNPSLPREEKEEKLETVKVSNNAEDPKDLMLSGERVLQTERSVESSSISLVPGTDYGTQESISLLEVSTLG\n"
97                     + "KAKTEPNKCVSQCAAFENPKGLIHGCSKDNRNDTEGFKYPLGHEVNHSRETSIEMEESELDAQYLQNTFKVS\n"
98                     + "KRQSFAPFSNPGNAEEECATFSAHSGSLKKQSPKVTFECEQKEENQGKNESNIKPVQTVNITAGFPVVGQKD\n"
99                     + "KPVDNAKCSIKGGSRFCLSSQFRGNETGLITPNKHGLLQNPYRIPPLFPIKSFVKTKCKKNLLEENFEEHSM\n"
100                     + "SPEREMGNENIPSTVSTISRNNIRENVFKEASSSNINEVGSSTNEVGSSINEIGSSDENIQAELGRNRGPKL\n"
101                     + "NAMLRLGVLQPEVYKQSLPGSNCKHPEIKKQEYEEVVQTVNTDFSPYLISDNLEQPMGSSHASQVCSETPDD\n"
102                     + "LLDDGEIKEDTSFAENDIKESSAVFSKSVQKGELSRSPSPFTHTHLAQGYRRGAKKLESSEENLSSEDEELP\n"
103                     + "CFQHLLFGKVNNIPSQSTRHSTVATECLSKNTEENLLSLKNSLNDCSNQVILAKASQEHHLSEETKCSASLF\n"
104                     + "SSQCSELEDLTANTNTQDPFLIGSSKQMRHQSESQGVGLSDKELVSDDEERGTGLEENNQEEQSMDSNLGEA\n"
105                     + "ASGCESETSVSEDCSGLSSQSDILTTQQRDTMQHNLIKLQQEMAELEAVLEQHGSQPSNSYPSIISDSSALE\n"
106                     + "DLRNPEQSTSEKAVLTSQKSSEYPISQNPEGLSADKFEVSADSSTSKNKEPGVERSSPSKCPSLDDRWYMHS\n"
107                     + "CSGSLQNRNYPSQEELIKVVDVEEQQLEESGPHDLTETSYLPRQDLEGTPYLESGISLFSDDPESDPSEDRA\n"
108                     + "PESARVGNIPSSTSALKVPQLKVAESAQSPAAAHTTDTAGYNAMEESVSREKPELTASTERVNKRMSMVVSG\n"
109                     + "LTPEEFMLVYKFARKHHITLTNLITEETTHVVMKTDAEFVCERTLKYFLGIAGGKWVVSYFWVTQSIKERKM\n"
110                     + "LNEHDFEVRGDVVNGRNHQGPKRARESQDRKIFRGLEICCYGPFTNMPTDQLEWMVQLCGASVVKELSSFTL\n"
111                     + "GTGVHPIVVVQPDAWTEDNGFHAIGQMCEAPVVTREWVLDSVALYQCQELDTYLIPQIPHSHY\n"
112                     + "",
113             1, 1863);
114     upSeq.setDescription("Breast cancer type 1 susceptibility protein");
115     upSeq_nocanonical = new Sequence(upSeq);
116     upSeq.createDatasetSequence();
117     upSeq.addDBRef(new DBRefEntry("UNIPROT", "0", "P38398", null, true));
118
119     upSeq_nocanonical.createDatasetSequence();
120     // not a canonical reference
121     upSeq_nocanonical.addDBRef(
122             new DBRefEntry("UNIPROT", "0", "P38398", null, false));
123
124   }
125
126   @AfterMethod(alwaysRun = true)
127   public void tearDown() throws Exception
128   {
129     seq = null;
130     upSeq = null;
131     upSeq_nocanonical = null;
132   }
133
134   @Test(groups = { "Functional" })
135   public void populateFilterComboBoxTest() throws InterruptedException
136   {
137     TDBeaconsFTSRestClientTest.setMock();
138     PDBFTSRestClientTest.setMock();
139
140     SequenceI[] selectedSeqs = new SequenceI[] { seq };
141     StructureChooser sc = new StructureChooser(selectedSeqs, seq, null);
142     ThreadwaitFor(200, sc);
143
144     // if structures are not discovered then don't
145     // populate filter options
146     sc.populateFilterComboBox(false, false);
147     int optionsSize = sc.getCmbFilterOption().getItemCount();
148     System.out.println("Items (no data, no cache): ");
149     StringBuilder items = new StringBuilder();
150     for (int p = 0; p < optionsSize; p++)
151     {
152       items.append("- ")
153               .append(sc.getCmbFilterOption().getItemAt(p).getName())
154               .append("\n");
155
156     }
157     // report items when this fails - seems to be a race condition
158     Assert.assertEquals(items.toString(), optionsSize, 2);
159
160     sc.populateFilterComboBox(true, false);
161     optionsSize = sc.getCmbFilterOption().getItemCount();
162     assertTrue(optionsSize > 3); // if structures are found, filter options
163                                  // should be populated
164
165     sc.populateFilterComboBox(true, true);
166     assertTrue(sc.getCmbFilterOption().getSelectedItem() != null);
167     FilterOption filterOpt = (FilterOption) sc.getCmbFilterOption()
168             .getSelectedItem();
169     assertEquals("Cached Structures", filterOpt.getName());
170     FTSRestClient
171             .unMock((FTSRestClient) TDBeaconsFTSRestClient.getInstance());
172     FTSRestClient.unMock((FTSRestClient) PDBFTSRestClient.getInstance());
173
174   }
175
176   @Test(groups = { "Functional" })
177   public void displayTDBQueryTest() throws InterruptedException
178   {
179     TDBeaconsFTSRestClientTest.setMock();
180     PDBFTSRestClientTest.setMock();
181
182     SequenceI[] selectedSeqs = new SequenceI[] { upSeq_nocanonical };
183     StructureChooser sc = new StructureChooser(selectedSeqs,
184             upSeq_nocanonical, null);
185     // mock so should be quick. Exceptions from mocked PDBFTS are expected too
186     ThreadwaitFor(500, sc);
187
188     assertTrue(sc.isCanQueryTDB() && sc.isNotQueriedTDBYet());
189   }
190
191   @Test(groups = { "Network" })
192   public void fetchStructuresInfoTest()
193   {
194     FTSRestClient
195             .unMock((FTSRestClient) TDBeaconsFTSRestClient.getInstance());
196     PDBFTSRestClient.unMock((FTSRestClient) PDBFTSRestClient.getInstance());
197     SequenceI[] selectedSeqs = new SequenceI[] { seq };
198     StructureChooser sc = new StructureChooser(selectedSeqs, seq, null);
199     // not mocked, wait for 2s
200     ThreadwaitFor(2000, sc);
201
202     sc.fetchStructuresMetaData();
203     Collection<FTSData> ss = (Collection<FTSData>) PA.getValue(sc,
204             "discoveredStructuresSet");
205     assertNotNull(ss);
206     assertTrue(ss.size() > 0);
207   }
208
209   @Test(groups = { "Functional" })
210   public void fetchStructuresInfoMockedTest()
211   {
212     TDBeaconsFTSRestClientTest.setMock();
213     PDBFTSRestClientTest.setMock();
214     SequenceI[] selectedSeqs = new SequenceI[] { upSeq };
215     StructureChooser sc = new StructureChooser(selectedSeqs, seq, null);
216     ThreadwaitFor(500, sc);
217
218     sc.fetchStructuresMetaData();
219     Collection<FTSData> ss = (Collection<FTSData>) PA.getValue(sc,
220             "discoveredStructuresSet");
221     assertNotNull(ss);
222     assertTrue(ss.size() > 0);
223   }
224
225   private void ThreadwaitFor(int i, StructureChooser sc)
226   {
227     long timeout = i + System.currentTimeMillis();
228     while (!sc.isDialogVisible() && timeout > System.currentTimeMillis())
229     {
230       try
231       {
232         Thread.sleep(50);
233       } catch (InterruptedException x)
234       {
235
236       }
237     }
238
239   }
240
241   @Test(groups = { "Functional" })
242   public void sanitizeSeqNameTest()
243   {
244     String name = "ab_cdEF|fwxyz012349";
245     assertEquals(name,
246             PDBStructureChooserQuerySource.sanitizeSeqName(name));
247
248     // remove a [nn] substring
249     name = "abcde12[345]fg";
250     assertEquals("abcde12fg",
251             PDBStructureChooserQuerySource.sanitizeSeqName(name));
252
253     // remove characters other than a-zA-Z0-9 | or _
254     name = "ab[cd],.\t£$*!- \\\"@:e";
255     assertEquals("abcde",
256             PDBStructureChooserQuerySource.sanitizeSeqName(name));
257
258     name = "abcde12[345a]fg";
259     assertEquals("abcde12345afg",
260             PDBStructureChooserQuerySource.sanitizeSeqName(name));
261   }
262 }