JAL-4311 failing test case for 3d-beacons epas1_human including levyLab alphaFold2...
[jalview.git] / test / jalview / fts / threedbeacons / TDBeaconsFTSRestClientTest.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.fts.threedbeacons;
22
23 import static org.testng.Assert.assertNull;
24 import static org.testng.AssertJUnit.assertEquals;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import java.io.IOException;
28 import java.net.URL;
29 import java.nio.charset.StandardCharsets;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Set;
35
36 import org.testng.Assert;
37 import org.testng.annotations.AfterMethod;
38 import org.testng.annotations.BeforeClass;
39 import org.testng.annotations.BeforeMethod;
40 import org.testng.annotations.Test;
41
42 import jalview.datamodel.Sequence;
43 import jalview.fts.api.FTSData;
44 import jalview.fts.api.FTSDataColumnI;
45 import jalview.fts.api.FTSDataColumnI.FTSDataColumnGroupI;
46 import jalview.fts.core.FTSRestClient;
47 import jalview.fts.core.FTSRestRequest;
48 import jalview.fts.core.FTSRestResponse;
49 import jalview.fts.service.pdb.PDBFTSRestClientTest;
50 import jalview.fts.service.threedbeacons.TDB_FTSData;
51 import jalview.fts.service.threedbeacons.TDBeaconsFTSRestClient;
52 import jalview.gui.JvOptionPane;
53 import jalview.gui.structurechooser.PDBStructureChooserQuerySource;
54 import jalview.gui.structurechooser.TDBResultAnalyser;
55 import jalview.gui.structurechooser.ThreeDBStructureChooserQuerySource;
56
57 public class TDBeaconsFTSRestClientTest
58 {
59   @BeforeClass(alwaysRun = true)
60   public void setUpJvOptionPane()
61   {
62     JvOptionPane.setInteractiveMode(false);
63     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
64   }
65
66   private FTSRestClient ftsRestClient;
67
68   @BeforeMethod(alwaysRun = true)
69   public void setUp() throws Exception
70   {
71     ftsRestClient = new FTSRestClient()
72     {
73
74       @Override
75       public String getColumnDataConfigFileName()
76       {
77         return "/fts/tdbeacons_data_columns.txt";
78       }
79
80       @Override
81       public FTSRestResponse executeRequest(FTSRestRequest ftsRequest)
82               throws Exception
83       {
84         return null;
85       }
86     };
87   }
88
89   @AfterMethod(alwaysRun = true)
90   public void tearDown() throws Exception
91   {
92   }
93
94   @Test
95   public void getAllDefaulDisplayedDataColumns()
96   {
97     // to change when resources.tdbeacons_data_columns.txt is changed
98     Assert.assertNotNull(
99             ftsRestClient.getAllDefaultDisplayedFTSDataColumns());
100     System.out
101             .println(ftsRestClient.getAllDefaultDisplayedFTSDataColumns());
102     Assert.assertTrue(!ftsRestClient.getAllDefaultDisplayedFTSDataColumns()
103             .isEmpty());
104     Assert.assertEquals(
105             ftsRestClient.getAllDefaultDisplayedFTSDataColumns().size(),
106             15);
107   }
108
109   @Test(groups = { "Functional" })
110   public void getPrimaryKeyColumIndexTest()
111   {
112     Collection<FTSDataColumnI> wantedFields = ftsRestClient
113             .getAllDefaultDisplayedFTSDataColumns();
114     int foundIndex = -1;
115     try
116     {
117       Assert.assertEquals(foundIndex, -1);
118       foundIndex = ftsRestClient.getPrimaryKeyColumIndex(wantedFields,
119               false);
120       Assert.assertEquals(foundIndex, 12);
121       foundIndex = ftsRestClient.getPrimaryKeyColumIndex(wantedFields,
122               true);
123       // 1+primary key index
124       Assert.assertEquals(foundIndex, 13);
125     } catch (Exception e)
126     {
127       e.printStackTrace();
128       Assert.fail("Exception thrown while testing...");
129     }
130   }
131
132   @Test(groups = { "Functional" })
133   public void getDataColumnsFieldsAsCommaDelimitedString()
134   {
135     // to change when resources.tdbeacons_data_columns.txt is changed
136     Collection<FTSDataColumnI> wantedFields = ftsRestClient
137             .getAllDefaultDisplayedFTSDataColumns();
138     String actual = ftsRestClient
139             .getDataColumnsFieldsAsCommaDelimitedString(wantedFields);
140     Assert.assertEquals(actual,
141             "uniprot_start,uniprot_end,provider,model_identifier,model_category,model_title,resolution,confidence_avg_local_score,confidence_type,confidence_version,coverage,created,model_url,model_format,model_page_url");
142   }
143
144   @Test(groups = { "Functional" })
145   public void getAllFTSDataColumns()
146   {
147     Collection<FTSDataColumnI> allFields = ftsRestClient
148             .getAllFTSDataColumns();
149     Assert.assertNotNull(allFields);
150     // System.out.println(allFields.size());
151     Assert.assertEquals(allFields.size(), 20);
152   }
153
154   @Test(groups = { "Functional" })
155   public void getSearchableDataColumns()
156   {
157     // to change when resources.tdbeacons_data_columns.txt is changed
158     Collection<FTSDataColumnI> searchableFields = ftsRestClient
159             .getSearchableDataColumns();
160     Assert.assertNotNull(searchableFields);
161     // System.out.println(searchableFields.size());
162     Assert.assertEquals(searchableFields.size(), 1); // only 1: uniprot
163                                                      // accession
164   }
165
166   @Test(groups = { "Functional" })
167   public void getPrimaryKeyColumn()
168   {
169     // to change when resources.tdbeacons_data_columns.txt is changed
170     FTSDataColumnI expectedPKColumn;
171     try
172     {
173       expectedPKColumn = ftsRestClient.getDataColumnByNameOrCode("Url");
174       Assert.assertNotNull(ftsRestClient.getPrimaryKeyColumn());
175       Assert.assertEquals(ftsRestClient.getPrimaryKeyColumn(),
176               expectedPKColumn);
177     } catch (Exception e)
178     {
179       e.printStackTrace();
180       Assert.fail("Exception thrown while testing...");
181     }
182   }
183
184   @Test(groups = { "Functional" })
185   public void getDataColumnByNameOrCode()
186   {
187     try
188     {
189       FTSDataColumnI foundDataCol = ftsRestClient
190               .getDataColumnByNameOrCode("uniprot_accession");
191       Assert.assertNotNull(foundDataCol);
192       Assert.assertEquals(foundDataCol.getName(), "UniProt Accession");
193     } catch (Exception e)
194     {
195       e.printStackTrace();
196       Assert.fail("Exception thrown while testing...");
197     }
198   }
199
200   @Test(groups = { "Functional" })
201   public void getDataColumnGroupById()
202   {
203     FTSDataColumnGroupI foundDataColGroup;
204     try
205     {
206       foundDataColGroup = ftsRestClient.getDataColumnGroupById("g2");
207       Assert.assertNotNull(foundDataColGroup);
208       Assert.assertEquals(foundDataColGroup.getName(), "Quality");
209     } catch (Exception e)
210     {
211       e.printStackTrace();
212     }
213   }
214
215   @Test(groups = { "Functional" })
216   public void getDefaultResponsePageSize()
217   {
218     int defaultResSize = ftsRestClient.getDefaultResponsePageSize();
219     Assert.assertEquals(defaultResSize, 100); // why 100 or 500 ? pdb is 100,
220                                               // uniprot 500
221   }
222
223   @Test(groups = { "Functional" })
224   public void getColumnMinWidthTest()
225   {
226     try
227     {
228       FTSDataColumnI foundDataCol = ftsRestClient
229               .getDataColumnByNameOrCode("uniprot_accession");
230       Assert.assertNotNull(foundDataCol);
231       int actualColMinWidth = foundDataCol.getMinWidth();
232       Assert.assertEquals(actualColMinWidth, 50);
233     } catch (Exception e)
234     {
235       e.printStackTrace();
236       Assert.fail("Exception thrown while testing...");
237     }
238   }
239   // could add test for MaxWidth & PreferedWith
240
241   @Test(groups = { "Functional" })
242   public void getColumnClassTest()
243   {
244     try
245     {
246       FTSDataColumnI foundDataCol = ftsRestClient
247               .getDataColumnByNameOrCode("uniprot_accession");
248       Assert.assertNotNull(foundDataCol);
249       Assert.assertEquals(foundDataCol.getDataType().getDataTypeClass(),
250               String.class);
251       foundDataCol = ftsRestClient.getDataColumnByNameOrCode("id");
252       Assert.assertNotNull(foundDataCol);
253       Assert.assertEquals(foundDataCol.getDataType().getDataTypeClass(),
254               String.class);
255     } catch (Exception e)
256     {
257       e.printStackTrace();
258       Assert.fail("Exception thrown while testing...");
259     }
260   }
261
262   @Test(groups = { "Functional" })
263   public void coverageForEqualsAndHashFunction()
264   {
265     Set<FTSDataColumnI> uniqueSet = new HashSet<FTSDataColumnI>();
266     Collection<FTSDataColumnI> searchableCols = ftsRestClient
267             .getSearchableDataColumns();
268     System.out.println(searchableCols);
269     for (FTSDataColumnI foundCol : searchableCols)
270     {
271       System.out.println(foundCol.toString());
272       uniqueSet.add(foundCol);
273       uniqueSet.add(foundCol);
274     }
275     Assert.assertTrue(!uniqueSet.isEmpty());
276     // Assert.assertEquals(uniqueSet.size(), 22); -> 1 or 2 currently for 3DB
277   }
278
279   @Test(groups = { "Functional" })
280   public void getTDBIdColumIndexTest()
281   {
282     List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
283     try
284     {
285       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
286               .getDataColumnByNameOrCode("Model id"));
287       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
288               .getDataColumnByNameOrCode("uniprot_accession"));
289       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
290               .getDataColumnByNameOrCode("Title"));
291     } catch (Exception e)
292     {
293       e.printStackTrace();
294     }
295     try
296     {
297       assertEquals(4, TDBeaconsFTSRestClient.getInstance()
298               .getPrimaryKeyColumIndex(wantedFields, true));
299       // assertEquals(3, TDBeaconsFTSRestClient.getInstance()
300       // .getPrimaryKeyColumIndex(wantedFields, true));
301     } catch (Exception e)
302     {
303       e.printStackTrace();
304     }
305   }
306
307   private static String[][] mocks = { { "P38398.json", null },
308       { "P01308.json", null },
309       { "P0DTD1.json", null },
310       { "P27787.json", null },
311       { "Q99814.json", null }
312
313       // , { "P0DTD3.json", "{}" } actually results in 404, but {} is in body
314   };
315
316   private static void setMockData()
317   {
318     try
319     {
320       mocks[0][1] = PDBFTSRestClientTest.readJsonStringFromFile(
321               "test/jalview/fts/threedbeacons/p38398_tdb_fts_query_resp.txt");
322
323       mocks[1][1] = PDBFTSRestClientTest.readJsonStringFromFile(
324               "test/jalview/fts/threedbeacons/p01308_tdb_fts_query_resp.txt");
325
326       mocks[2][1] = PDBFTSRestClientTest.readJsonStringFromFile(
327               "test/jalview/fts/threedbeacons/p0dtd1_tdb_fts_query_resp.txt");
328
329       mocks[3][1] = PDBFTSRestClientTest.readJsonStringFromFile(
330               "test/jalview/fts/threedbeacons/p27787_tdb_fts_query_resp.txt");
331       mocks[4][1] = PDBFTSRestClientTest.readJsonStringFromFile(
332               "test/jalview/fts/threedbeacons/q99814_tdb_fts_query_resp.txt");
333
334     } catch (IOException e)
335     {
336       Assert.fail("Couldn't read mock response data", e);
337     }
338   }
339
340   public static void setMock()
341   {
342     setMockData();
343     FTSRestClient.createMockFTSRestClient(
344             (FTSRestClient) TDBeaconsFTSRestClient.getInstance(), mocks);
345   }
346
347   private static String dev_url = "https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
348
349   private static String prod_url = "https://www.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
350
351   /**
352    * check that the mock request and response are the same as the response from
353    * a live 3D-beacons endpoint
354    * 
355    * Note - servers often have rapidly changing ids / URIs so this might fail,
356    * but the overall structure will remain.
357    * 
358    * @throws Exception
359    */
360   @Test(groups = { "Network", "Integration" })
361   public void verifyMockTDBRequest() throws Exception
362   {
363     setMockData();
364     for (String[] otherMock : mocks)
365     {
366       verifyMockTDBRequest(otherMock[0], otherMock[1]);
367     }
368   }
369
370   private void verifyMockTDBRequest(String mockRequest,
371           String _mockResponse) throws Exception
372   {
373     URL tdb_req = new URL(prod_url + mockRequest);
374     byte[] resp = tdb_req.openStream().readAllBytes();
375     String tresp = new String(resp, StandardCharsets.UTF_8);
376     // this simple test fails for responses containing multi-chain structures -
377     // since chain order in the json elements is arbitrary and varies between
378     // queries.
379     assertEquals(_mockResponse.trim(), tresp.trim());
380   }
381
382   @Test(groups = { "Functional" })
383   public void testMockTDBRequest()
384   {
385
386     setMock();
387     List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
388     try
389     {
390       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
391               .getDataColumnByNameOrCode("Model Id"));
392       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
393               .getDataColumnByNameOrCode("model_url"));
394       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
395               .getDataColumnByNameOrCode("provider"));
396       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
397               .getDataColumnByNameOrCode("model_category"));
398       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
399               .getDataColumnByNameOrCode("qmean_avg_local_score"));
400       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
401               .getDataColumnByNameOrCode("uniprot_start"));
402       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
403               .getDataColumnByNameOrCode("uniprot_end"));
404     } catch (Exception e1)
405     {
406       e1.printStackTrace();
407     }
408     System.out.println("wantedFields >>" + wantedFields);
409
410     FTSRestRequest request = new FTSRestRequest();
411     FTSRestResponse response;
412
413     request.setResponseSize(100);
414     request.setFieldToSearchBy("");
415     request.setWantedFields(wantedFields);
416     // check 404 behaviour
417     request.setSearchTerm("P00000.json");
418
419     try
420     {
421       response = TDBeaconsFTSRestClient.getInstance()
422               .executeRequest(request);
423
424       assertNull(response);
425     } catch (Exception e)
426     {
427       e.printStackTrace();
428       Assert.fail("Unexpected failure during mock 3DBeacons 404 test");
429     }
430
431     // check 200 behaviour
432     request.setSearchTerm("P38398.json");
433     System.out.println("request : " + request.getFieldToSearchBy());
434     // System.out.println(request.toString());
435
436     try
437     {
438       response = TDBeaconsFTSRestClient.getInstance()
439               .executeRequest(request);
440     } catch (Exception e)
441     {
442       e.printStackTrace();
443       Assert.fail("Couldn't execute webservice call!");
444       return;
445     }
446     assertTrue(response.getSearchSummary() != null);
447     assertTrue(response.getNumberOfItemsFound() > 3); // 4 atm
448     System.out.println("Search summary : \n" + response.getSearchSummary());
449
450     // System.out.println(response.getSearchSummary().size());
451   }
452
453   @Test(groups = { "External", "Network" })
454   public void executeRequestTest()
455   {
456     List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
457     try
458     {
459       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
460               .getDataColumnByNameOrCode("Model Id"));
461       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
462               .getDataColumnByNameOrCode("model_url"));
463       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
464               .getDataColumnByNameOrCode("provider"));
465       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
466               .getDataColumnByNameOrCode("model_category"));
467       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
468               .getDataColumnByNameOrCode("confidence_avg_local_score"));
469       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
470               .getDataColumnByNameOrCode("uniprot_start"));
471       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
472               .getDataColumnByNameOrCode("uniprot_end"));
473     } catch (Exception e1)
474     {
475       e1.printStackTrace();
476     }
477     System.out.println("wantedFields >>" + wantedFields);
478
479     FTSRestRequest request = new FTSRestRequest();
480     request.setResponseSize(100);
481     request.setFieldToSearchBy("");
482     request.setSearchTerm("P01318.json");
483     request.setWantedFields(wantedFields);
484     System.out.println("request : " + request.getFieldToSearchBy());
485     // System.out.println(request.toString());
486
487     FTSRestResponse response;
488     try
489     {
490       response = TDBeaconsFTSRestClient.getInstance()
491               .executeRequest(request);
492     } catch (Exception e)
493     {
494       e.printStackTrace();
495       Assert.fail("Couldn't execute webservice call!");
496       return;
497     }
498     assertTrue(response.getSearchSummary() != null);
499     assertTrue(response.getNumberOfItemsFound() > 3); // 4 atm
500     System.out.println("Search summary : \n" + response.getSearchSummary());
501     // System.out.println(response.getSearchSummary().size());
502   }
503   
504 }