JAL-3829 ensure tests use mocked service - proper exception for mocked 400 response
[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.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNotNull;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import jalview.datamodel.DBRefEntry;
28 import jalview.datamodel.DBRefSource;
29 import jalview.datamodel.PDBEntry;
30 import jalview.datamodel.Sequence;
31 import jalview.datamodel.SequenceI;
32 import jalview.fts.api.FTSData;
33 import jalview.fts.core.FTSRestClient;
34 import jalview.fts.core.FTSRestRequest;
35 import jalview.fts.service.pdb.PDBFTSRestClient;
36 import jalview.fts.service.threedbeacons.TDBeaconsFTSRestClient;
37 import jalview.fts.threedbeacons.TDBeaconsFTSRestClientTest;
38 import jalview.gui.structurechooser.PDBStructureChooserQuerySource;
39 import jalview.gui.structurechooser.StructureChooserQuerySource;
40 import jalview.gui.structurechooser.ThreeDBStructureChooserQuerySource;
41 import jalview.jbgui.FilterOption;
42 import jalview.ws.params.InvalidArgumentException;
43
44 import java.util.Collection;
45 import java.util.Vector;
46
47 import org.junit.Assert;
48 import org.testng.annotations.AfterMethod;
49 import org.testng.annotations.BeforeClass;
50 import org.testng.annotations.BeforeMethod;
51 import org.testng.annotations.Test;
52
53 import junit.extensions.PA;
54
55 public class StructureChooserTest
56 {
57
58   @BeforeClass(alwaysRun = true)
59   public void setUpJvOptionPane()
60   {
61     JvOptionPane.setInteractiveMode(false);
62     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
63   }
64
65   Sequence seq,upSeq;
66
67   @BeforeMethod(alwaysRun = true)
68   public void setUp() throws Exception
69   {
70     seq = new Sequence("PDB|4kqy|4KQY|A", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1,
71             26);
72     seq.createDatasetSequence();
73     for (int x = 1; x < 5; x++)
74     {
75       DBRefEntry dbRef = new DBRefEntry();
76       dbRef.setAccessionId("XYZ_" + x);
77       seq.addDBRef(dbRef);
78     }
79
80     PDBEntry dbRef = new PDBEntry();
81     dbRef.setId("1tim");
82
83     Vector<PDBEntry> pdbIds = new Vector<>();
84     pdbIds.add(dbRef);
85
86     seq.setPDBId(pdbIds);
87     
88     // Uniprot sequence for 3D-Beacons mocks
89     upSeq = new Sequence("P38398", 
90             "MDLSALRVEEVQNVINAMQKILECPICLELIKEPVSTKCDHIFCKFCMLKLLNQKKGPSQCPLCKNDITKRS\n"
91             + "LQESTRFSQLVEELLKIICAFQLDTGLEYANSYNFAKKENNSPEHLKDEVSIIQSMGYRNRAKRLLQSEPEN\n"
92             + "PSLQETSLSVQLSNLGTVRTLRTKQRIQPQKTSVYIELGSDSSEDTVNKATYCSVGDQELLQITPQGTRDEI\n"
93             + "SLDSAKKAACEFSETDVTNTEHHQPSNNDLNTTEKRAAERHPEKYQGSSVSNLHVEPCGTNTHASSLQHENS\n"
94             + "SLLLTKDRMNVEKAEFCNKSKQPGLARSQHNRWAGSKETCNDRRTPSTEKKVDLNADPLCERKEWNKQKLPC\n"
95             + "SENPRDTEDVPWITLNSSIQKVNEWFSRSDELLGSDDSHDGESESNAKVADVLDVLNEVDEYSGSSEKIDLL\n"
96             + "ASDPHEALICKSERVHSKSVESNIEDKIFGKTYRKKASLPNLSHVTENLIIGAFVTEPQIIQERPLTNKLKR\n"
97             + "KRRPTSGLHPEDFIKKADLAVQKTPEMINQGTNQTEQNGQVMNITNSGHENKTKGDSIQNEKNPNPIESLEK\n"
98             + "ESAFKTKAEPISSSISNMELELNIHNSKAPKKNRLRRKSSTRHIHALELVVSRNLSPPNCTELQIDSCSSSE\n"
99             + "EIKKKKYNQMPVRHSRNLQLMEGKEPATGAKKSNKPNEQTSKRHDSDTFPELKLTNAPGSFTKCSNTSELKE\n"
100             + "FVNPSLPREEKEEKLETVKVSNNAEDPKDLMLSGERVLQTERSVESSSISLVPGTDYGTQESISLLEVSTLG\n"
101             + "KAKTEPNKCVSQCAAFENPKGLIHGCSKDNRNDTEGFKYPLGHEVNHSRETSIEMEESELDAQYLQNTFKVS\n"
102             + "KRQSFAPFSNPGNAEEECATFSAHSGSLKKQSPKVTFECEQKEENQGKNESNIKPVQTVNITAGFPVVGQKD\n"
103             + "KPVDNAKCSIKGGSRFCLSSQFRGNETGLITPNKHGLLQNPYRIPPLFPIKSFVKTKCKKNLLEENFEEHSM\n"
104             + "SPEREMGNENIPSTVSTISRNNIRENVFKEASSSNINEVGSSTNEVGSSINEIGSSDENIQAELGRNRGPKL\n"
105             + "NAMLRLGVLQPEVYKQSLPGSNCKHPEIKKQEYEEVVQTVNTDFSPYLISDNLEQPMGSSHASQVCSETPDD\n"
106             + "LLDDGEIKEDTSFAENDIKESSAVFSKSVQKGELSRSPSPFTHTHLAQGYRRGAKKLESSEENLSSEDEELP\n"
107             + "CFQHLLFGKVNNIPSQSTRHSTVATECLSKNTEENLLSLKNSLNDCSNQVILAKASQEHHLSEETKCSASLF\n"
108             + "SSQCSELEDLTANTNTQDPFLIGSSKQMRHQSESQGVGLSDKELVSDDEERGTGLEENNQEEQSMDSNLGEA\n"
109             + "ASGCESETSVSEDCSGLSSQSDILTTQQRDTMQHNLIKLQQEMAELEAVLEQHGSQPSNSYPSIISDSSALE\n"
110             + "DLRNPEQSTSEKAVLTSQKSSEYPISQNPEGLSADKFEVSADSSTSKNKEPGVERSSPSKCPSLDDRWYMHS\n"
111             + "CSGSLQNRNYPSQEELIKVVDVEEQQLEESGPHDLTETSYLPRQDLEGTPYLESGISLFSDDPESDPSEDRA\n"
112             + "PESARVGNIPSSTSALKVPQLKVAESAQSPAAAHTTDTAGYNAMEESVSREKPELTASTERVNKRMSMVVSG\n"
113             + "LTPEEFMLVYKFARKHHITLTNLITEETTHVVMKTDAEFVCERTLKYFLGIAGGKWVVSYFWVTQSIKERKM\n"
114             + "LNEHDFEVRGDVVNGRNHQGPKRARESQDRKIFRGLEICCYGPFTNMPTDQLEWMVQLCGASVVKELSSFTL\n"
115             + "GTGVHPIVVVQPDAWTEDNGFHAIGQMCEAPVVTREWVLDSVALYQCQELDTYLIPQIPHSHY\n"
116             + "", 1,
117 1863);
118     upSeq.createDatasetSequence();
119     upSeq.setDescription("Breast cancer type 1 susceptibility protein");
120     upSeq.addDBRef(new DBRefEntry("UNIPROT","0","P38398",null,true));
121   }
122
123   @AfterMethod(alwaysRun = true)
124   public void tearDown() throws Exception
125   {
126     seq = null;
127     upSeq=null;
128   }
129
130   @Test(groups = { "Functional" })
131   public void populateFilterComboBoxTest() throws InterruptedException
132   {
133     TDBeaconsFTSRestClientTest.setMock();
134     PDBFTSRestClient.setMock();
135
136     SequenceI[] selectedSeqs = new SequenceI[] { seq };
137     StructureChooser sc = new StructureChooser(selectedSeqs, seq, null);
138     sc.populateFilterComboBox(false, false);
139     int optionsSize = sc.getCmbFilterOption().getItemCount();
140     assertEquals(2, optionsSize); // if structures are not discovered then don't
141                                   // populate filter options
142
143     sc.populateFilterComboBox(true, false);
144     optionsSize = sc.getCmbFilterOption().getItemCount();
145     assertTrue(optionsSize > 3); // if structures are found, filter options
146                                  // should be populated
147
148     sc.populateFilterComboBox(true, true);
149     assertTrue(sc.getCmbFilterOption().getSelectedItem() != null);
150     FilterOption filterOpt = (FilterOption) sc.getCmbFilterOption()
151             .getSelectedItem();
152     assertEquals("Cached Structures", filterOpt.getName());
153   }
154
155   @Test(groups = { "Network" })
156   public void fetchStructuresInfoTest()
157   {
158     FTSRestClient.unMock((FTSRestClient) TDBeaconsFTSRestClient.getInstance());
159     PDBFTSRestClient.unMock((FTSRestClient) PDBFTSRestClient.getInstance());
160     SequenceI[] selectedSeqs = new SequenceI[] { seq };
161     StructureChooser sc = new StructureChooser(selectedSeqs, seq, null);
162     sc.fetchStructuresMetaData();
163     Collection<FTSData> ss = (Collection<FTSData>) PA.getValue(sc,
164             "discoveredStructuresSet");
165     assertNotNull(ss);
166     assertTrue(ss.size() > 0);
167   }
168
169   @Test(groups = { "Functional" })
170   public void fetchStructuresInfoMockedTest()
171   {
172     TDBeaconsFTSRestClientTest.setMock();
173     PDBFTSRestClient.setMock();
174     SequenceI[] selectedSeqs = new SequenceI[] { upSeq };
175     StructureChooser sc = new StructureChooser(selectedSeqs, seq, null);
176     sc.fetchStructuresMetaData();
177     Collection<FTSData> ss = (Collection<FTSData>) PA.getValue(sc,
178             "discoveredStructuresSet");
179     assertNotNull(ss);
180     assertTrue(ss.size() > 0);
181   }
182
183   @Test(groups = { "Functional" })
184   public void sanitizeSeqNameTest()
185   {
186     String name = "ab_cdEF|fwxyz012349";
187     assertEquals(name, PDBStructureChooserQuerySource.sanitizeSeqName(name));
188
189     // remove a [nn] substring
190     name = "abcde12[345]fg";
191     assertEquals("abcde12fg", PDBStructureChooserQuerySource.sanitizeSeqName(name));
192
193     // remove characters other than a-zA-Z0-9 | or _
194     name = "ab[cd],.\t£$*!- \\\"@:e";
195     assertEquals("abcde", PDBStructureChooserQuerySource.sanitizeSeqName(name));
196
197     name = "abcde12[345a]fg";
198     assertEquals("abcde12345afg", PDBStructureChooserQuerySource.sanitizeSeqName(name));
199   }
200 }