Clover icon

Coverage Report

  1. Project Clover database Wed Nov 13 2024 16:21:17 GMT
  2. Package jalview.fts.threedbeacons

File TDBeaconsFTSRestClientTest.java

 

Code metrics

0
157
24
1
504
404
38
0.24
6.54
24
1.58

Classes

Class Line # Actions
TDBeaconsFTSRestClientTest 57 157 38
0.6464088664.6%
 

Contributing tests

This file is covered by 22 tests. .

Source view

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