2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.fts.threedbeacons;
23 import static org.testng.Assert.assertNull;
24 import static org.testng.AssertJUnit.assertEquals;
25 import static org.testng.AssertJUnit.assertTrue;
27 import java.io.IOException;
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;
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;
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;
57 public class TDBeaconsFTSRestClientTest
59 @BeforeClass(alwaysRun = true)
60 public void setUpJvOptionPane()
62 JvOptionPane.setInteractiveMode(false);
63 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
66 private FTSRestClient ftsRestClient;
68 @BeforeMethod(alwaysRun = true)
69 public void setUp() throws Exception
71 ftsRestClient = new FTSRestClient()
75 public String getColumnDataConfigFileName()
77 return "/fts/tdbeacons_data_columns.txt";
81 public FTSRestResponse executeRequest(FTSRestRequest ftsRequest)
89 @AfterMethod(alwaysRun = true)
90 public void tearDown() throws Exception
95 public void getAllDefaulDisplayedDataColumns()
97 // to change when resources.tdbeacons_data_columns.txt is changed
99 ftsRestClient.getAllDefaultDisplayedFTSDataColumns());
101 .println(ftsRestClient.getAllDefaultDisplayedFTSDataColumns());
102 Assert.assertTrue(!ftsRestClient.getAllDefaultDisplayedFTSDataColumns()
105 ftsRestClient.getAllDefaultDisplayedFTSDataColumns().size(),
109 @Test(groups = { "Functional" })
110 public void getPrimaryKeyColumIndexTest()
112 Collection<FTSDataColumnI> wantedFields = ftsRestClient
113 .getAllDefaultDisplayedFTSDataColumns();
117 Assert.assertEquals(foundIndex, -1);
118 foundIndex = ftsRestClient.getPrimaryKeyColumIndex(wantedFields,
120 Assert.assertEquals(foundIndex, 12);
121 foundIndex = ftsRestClient.getPrimaryKeyColumIndex(wantedFields,
123 // 1+primary key index
124 Assert.assertEquals(foundIndex, 13);
125 } catch (Exception e)
128 Assert.fail("Exception thrown while testing...");
132 @Test(groups = { "Functional" })
133 public void getDataColumnsFieldsAsCommaDelimitedString()
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");
144 @Test(groups = { "Functional" })
145 public void getAllFTSDataColumns()
147 Collection<FTSDataColumnI> allFields = ftsRestClient
148 .getAllFTSDataColumns();
149 Assert.assertNotNull(allFields);
150 // System.out.println(allFields.size());
151 Assert.assertEquals(allFields.size(), 20);
154 @Test(groups = { "Functional" })
155 public void getSearchableDataColumns()
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
166 @Test(groups = { "Functional" })
167 public void getPrimaryKeyColumn()
169 // to change when resources.tdbeacons_data_columns.txt is changed
170 FTSDataColumnI expectedPKColumn;
173 expectedPKColumn = ftsRestClient.getDataColumnByNameOrCode("Url");
174 Assert.assertNotNull(ftsRestClient.getPrimaryKeyColumn());
175 Assert.assertEquals(ftsRestClient.getPrimaryKeyColumn(),
177 } catch (Exception e)
180 Assert.fail("Exception thrown while testing...");
184 @Test(groups = { "Functional" })
185 public void getDataColumnByNameOrCode()
189 FTSDataColumnI foundDataCol = ftsRestClient
190 .getDataColumnByNameOrCode("uniprot_accession");
191 Assert.assertNotNull(foundDataCol);
192 Assert.assertEquals(foundDataCol.getName(), "UniProt Accession");
193 } catch (Exception e)
196 Assert.fail("Exception thrown while testing...");
200 @Test(groups = { "Functional" })
201 public void getDataColumnGroupById()
203 FTSDataColumnGroupI foundDataColGroup;
206 foundDataColGroup = ftsRestClient.getDataColumnGroupById("g2");
207 Assert.assertNotNull(foundDataColGroup);
208 Assert.assertEquals(foundDataColGroup.getName(), "Quality");
209 } catch (Exception e)
215 @Test(groups = { "Functional" })
216 public void getDefaultResponsePageSize()
218 int defaultResSize = ftsRestClient.getDefaultResponsePageSize();
219 Assert.assertEquals(defaultResSize, 100); // why 100 or 500 ? pdb is 100,
223 @Test(groups = { "Functional" })
224 public void getColumnMinWidthTest()
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)
236 Assert.fail("Exception thrown while testing...");
239 // could add test for MaxWidth & PreferedWith
241 @Test(groups = { "Functional" })
242 public void getColumnClassTest()
246 FTSDataColumnI foundDataCol = ftsRestClient
247 .getDataColumnByNameOrCode("uniprot_accession");
248 Assert.assertNotNull(foundDataCol);
249 Assert.assertEquals(foundDataCol.getDataType().getDataTypeClass(),
251 foundDataCol = ftsRestClient.getDataColumnByNameOrCode("id");
252 Assert.assertNotNull(foundDataCol);
253 Assert.assertEquals(foundDataCol.getDataType().getDataTypeClass(),
255 } catch (Exception e)
258 Assert.fail("Exception thrown while testing...");
262 @Test(groups = { "Functional" })
263 public void coverageForEqualsAndHashFunction()
265 Set<FTSDataColumnI> uniqueSet = new HashSet<FTSDataColumnI>();
266 Collection<FTSDataColumnI> searchableCols = ftsRestClient
267 .getSearchableDataColumns();
268 System.out.println(searchableCols);
269 for (FTSDataColumnI foundCol : searchableCols)
271 System.out.println(foundCol.toString());
272 uniqueSet.add(foundCol);
273 uniqueSet.add(foundCol);
275 Assert.assertTrue(!uniqueSet.isEmpty());
276 // Assert.assertEquals(uniqueSet.size(), 22); -> 1 or 2 currently for 3DB
279 @Test(groups = { "Functional" })
280 public void getTDBIdColumIndexTest()
282 List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
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)
297 assertEquals(4, TDBeaconsFTSRestClient.getInstance()
298 .getPrimaryKeyColumIndex(wantedFields, true));
299 // assertEquals(3, TDBeaconsFTSRestClient.getInstance()
300 // .getPrimaryKeyColumIndex(wantedFields, true));
301 } catch (Exception e)
307 private static String[][] mocks = { { "P38398.json", null },
308 { "P01308.json", null },
309 { "P0DTD1.json", null },
310 { "P27787.json", null },
311 { "Q99814.json", null }
313 // , { "P0DTD3.json", "{}" } actually results in 404, but {} is in body
316 private static void setMockData()
320 mocks[0][1] = PDBFTSRestClientTest.readJsonStringFromFile(
321 "test/jalview/fts/threedbeacons/p38398_tdb_fts_query_resp.txt");
323 mocks[1][1] = PDBFTSRestClientTest.readJsonStringFromFile(
324 "test/jalview/fts/threedbeacons/p01308_tdb_fts_query_resp.txt");
326 mocks[2][1] = PDBFTSRestClientTest.readJsonStringFromFile(
327 "test/jalview/fts/threedbeacons/p0dtd1_tdb_fts_query_resp.txt");
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");
334 } catch (IOException e)
336 Assert.fail("Couldn't read mock response data", e);
340 public static void setMock()
343 FTSRestClient.createMockFTSRestClient(
344 (FTSRestClient) TDBeaconsFTSRestClient.getInstance(), mocks);
347 private static String dev_url = "https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
349 private static String prod_url = "https://www.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
352 * check that the mock request and response are the same as the response from
353 * a live 3D-beacons endpoint
355 * Note - servers often have rapidly changing ids / URIs so this might fail,
356 * but the overall structure will remain.
360 @Test(groups = { "Network", "Integration" })
361 public void verifyMockTDBRequest() throws Exception
364 for (String[] otherMock : mocks)
366 verifyMockTDBRequest(otherMock[0], otherMock[1]);
370 private void verifyMockTDBRequest(String mockRequest,
371 String _mockResponse) throws Exception
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
379 assertEquals(_mockResponse.trim(), tresp.trim());
382 @Test(groups = { "Functional" })
383 public void testMockTDBRequest()
387 List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
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)
406 e1.printStackTrace();
408 System.out.println("wantedFields >>" + wantedFields);
410 FTSRestRequest request = new FTSRestRequest();
411 FTSRestResponse response;
413 request.setResponseSize(100);
414 request.setFieldToSearchBy("");
415 request.setWantedFields(wantedFields);
416 // check 404 behaviour
417 request.setSearchTerm("P00000.json");
421 response = TDBeaconsFTSRestClient.getInstance()
422 .executeRequest(request);
424 assertNull(response);
425 } catch (Exception e)
428 Assert.fail("Unexpected failure during mock 3DBeacons 404 test");
431 // check 200 behaviour
432 request.setSearchTerm("P38398.json");
433 System.out.println("request : " + request.getFieldToSearchBy());
434 // System.out.println(request.toString());
438 response = TDBeaconsFTSRestClient.getInstance()
439 .executeRequest(request);
440 } catch (Exception e)
443 Assert.fail("Couldn't execute webservice call!");
446 assertTrue(response.getSearchSummary() != null);
447 assertTrue(response.getNumberOfItemsFound() > 3); // 4 atm
448 System.out.println("Search summary : \n" + response.getSearchSummary());
450 // System.out.println(response.getSearchSummary().size());
453 @Test(groups = { "External", "Network" })
454 public void executeRequestTest()
456 List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
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)
475 e1.printStackTrace();
477 System.out.println("wantedFields >>" + wantedFields);
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());
487 FTSRestResponse response;
490 response = TDBeaconsFTSRestClient.getInstance()
491 .executeRequest(request);
492 } catch (Exception e)
495 Assert.fail("Couldn't execute webservice call!");
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());