JAL-4114 updated mock tests and responses for v2.0 api
[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       { "P27787.json", null }
305
306       // , { "P0DTD3.json", "{}" } actually results in 404, but {} is in body
307   };
308
309   private static void setMockData()
310   {
311     try
312     {
313       mocks[0][1] = PDBFTSRestClientTest.readJsonStringFromFile(
314               "test/jalview/fts/threedbeacons/p38398_tdb_fts_query_resp.txt");
315
316       mocks[1][1] = PDBFTSRestClientTest.readJsonStringFromFile(
317               "test/jalview/fts/threedbeacons/p01308_tdb_fts_query_resp.txt");
318
319       mocks[2][1] = PDBFTSRestClientTest.readJsonStringFromFile(
320               "test/jalview/fts/threedbeacons/p0dtd1_tdb_fts_query_resp.txt");
321
322       mocks[3][1] = PDBFTSRestClientTest.readJsonStringFromFile(
323               "test/jalview/fts/threedbeacons/p27787_tdb_fts_query_resp.txt");
324
325     } catch (IOException e)
326     {
327       Assert.fail("Couldn't read mock response data", e);
328     }
329   }
330
331   public static void setMock()
332   {
333     setMockData();
334     FTSRestClient.createMockFTSRestClient(
335             (FTSRestClient) TDBeaconsFTSRestClient.getInstance(), mocks);
336   }
337
338   private static String dev_url = "https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
339
340   private static String prod_url = "https://www.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
341
342   /**
343    * check that the mock request and response are the same as the response from
344    * a live 3D-beacons endpoint
345    * 
346    * Note - servers often have rapidly changing ids / URIs so this might fail,
347    * but the overall structure will remain.
348    * 
349    * @throws Exception
350    */
351   @Test(groups = { "Network", "Integration" })
352   public void verifyMockTDBRequest() throws Exception
353   {
354     setMockData();
355     for (String[] otherMock : mocks)
356     {
357       verifyMockTDBRequest(otherMock[0], otherMock[1]);
358     }
359   }
360
361   private void verifyMockTDBRequest(String mockRequest,
362           String _mockResponse) throws Exception
363   {
364     URL tdb_req = new URL(prod_url + mockRequest);
365     byte[] resp = tdb_req.openStream().readAllBytes();
366     String tresp = new String(resp, StandardCharsets.UTF_8);
367     // this simple test fails for responses containing multi-chain structures -
368     // since chain order in the json elements is arbitrary and varies between
369     // queries.
370     assertEquals(_mockResponse.trim(), tresp.trim());
371   }
372
373   @Test(groups = { "Functional" })
374   public void testMockTDBRequest()
375   {
376
377     setMock();
378     List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
379     try
380     {
381       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
382               .getDataColumnByNameOrCode("Model Id"));
383       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
384               .getDataColumnByNameOrCode("model_url"));
385       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
386               .getDataColumnByNameOrCode("provider"));
387       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
388               .getDataColumnByNameOrCode("model_category"));
389       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
390               .getDataColumnByNameOrCode("qmean_avg_local_score"));
391       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
392               .getDataColumnByNameOrCode("uniprot_start"));
393       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
394               .getDataColumnByNameOrCode("uniprot_end"));
395     } catch (Exception e1)
396     {
397       e1.printStackTrace();
398     }
399     System.out.println("wantedFields >>" + wantedFields);
400
401     FTSRestRequest request = new FTSRestRequest();
402     FTSRestResponse response;
403
404     request.setResponseSize(100);
405     request.setFieldToSearchBy("");
406     request.setWantedFields(wantedFields);
407     // check 404 behaviour
408     request.setSearchTerm("P00000.json");
409
410     try
411     {
412       response = TDBeaconsFTSRestClient.getInstance()
413               .executeRequest(request);
414
415       assertNull(response);
416     } catch (Exception e)
417     {
418       e.printStackTrace();
419       Assert.fail("Unexpected failure during mock 3DBeacons 404 test");
420     }
421
422     // check 200 behaviour
423     request.setSearchTerm("P38398.json");
424     System.out.println("request : " + request.getFieldToSearchBy());
425     // System.out.println(request.toString());
426
427     try
428     {
429       response = TDBeaconsFTSRestClient.getInstance()
430               .executeRequest(request);
431     } catch (Exception e)
432     {
433       e.printStackTrace();
434       Assert.fail("Couldn't execute webservice call!");
435       return;
436     }
437     assertTrue(response.getSearchSummary() != null);
438     assertTrue(response.getNumberOfItemsFound() > 3); // 4 atm
439     System.out.println("Search summary : \n" + response.getSearchSummary());
440
441     // System.out.println(response.getSearchSummary().size());
442   }
443
444   @Test(groups = { "External", "Network" })
445   public void executeRequestTest()
446   {
447     List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
448     try
449     {
450       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
451               .getDataColumnByNameOrCode("Model Id"));
452       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
453               .getDataColumnByNameOrCode("model_url"));
454       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
455               .getDataColumnByNameOrCode("provider"));
456       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
457               .getDataColumnByNameOrCode("model_category"));
458       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
459               .getDataColumnByNameOrCode("confidence_avg_local_score"));
460       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
461               .getDataColumnByNameOrCode("uniprot_start"));
462       wantedFields.add(TDBeaconsFTSRestClient.getInstance()
463               .getDataColumnByNameOrCode("uniprot_end"));
464     } catch (Exception e1)
465     {
466       e1.printStackTrace();
467     }
468     System.out.println("wantedFields >>" + wantedFields);
469
470     FTSRestRequest request = new FTSRestRequest();
471     request.setResponseSize(100);
472     request.setFieldToSearchBy("");
473     request.setSearchTerm("P01318.json");
474     request.setWantedFields(wantedFields);
475     System.out.println("request : " + request.getFieldToSearchBy());
476     // System.out.println(request.toString());
477
478     FTSRestResponse response;
479     try
480     {
481       response = TDBeaconsFTSRestClient.getInstance()
482               .executeRequest(request);
483     } catch (Exception e)
484     {
485       e.printStackTrace();
486       Assert.fail("Couldn't execute webservice call!");
487       return;
488     }
489     assertTrue(response.getSearchSummary() != null);
490     assertTrue(response.getNumberOfItemsFound() > 3); // 4 atm
491     System.out.println("Search summary : \n" + response.getSearchSummary());
492     // System.out.println(response.getSearchSummary().size());
493   }
494 }