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