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.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;
51 public class TDBeaconsFTSRestClientTest
53 @BeforeClass(alwaysRun = true)
54 public void setUpJvOptionPane()
56 JvOptionPane.setInteractiveMode(false);
57 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
60 private FTSRestClient ftsRestClient;
62 @BeforeMethod(alwaysRun = true)
63 public void setUp() throws Exception
65 ftsRestClient = new FTSRestClient()
69 public String getColumnDataConfigFileName()
71 return "/fts/tdbeacons_data_columns.txt";
75 public FTSRestResponse executeRequest(FTSRestRequest ftsRequest)
83 @AfterMethod(alwaysRun = true)
84 public void tearDown() throws Exception
89 public void getAllDefaulDisplayedDataColumns()
91 // to change when resources.tdbeacons_data_columns.txt is changed
93 ftsRestClient.getAllDefaultDisplayedFTSDataColumns());
95 .println(ftsRestClient.getAllDefaultDisplayedFTSDataColumns());
96 Assert.assertTrue(!ftsRestClient.getAllDefaultDisplayedFTSDataColumns()
99 ftsRestClient.getAllDefaultDisplayedFTSDataColumns().size(),
103 @Test(groups = { "Functional" })
104 public void getPrimaryKeyColumIndexTest()
106 Collection<FTSDataColumnI> wantedFields = ftsRestClient
107 .getAllDefaultDisplayedFTSDataColumns();
111 Assert.assertEquals(foundIndex, -1);
112 foundIndex = ftsRestClient.getPrimaryKeyColumIndex(wantedFields,
114 Assert.assertEquals(foundIndex, 12);
115 foundIndex = ftsRestClient.getPrimaryKeyColumIndex(wantedFields,
117 // 1+primary key index
118 Assert.assertEquals(foundIndex, 13);
119 } catch (Exception e)
122 Assert.fail("Exception thrown while testing...");
126 @Test(groups = { "Functional" })
127 public void getDataColumnsFieldsAsCommaDelimitedString()
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");
138 @Test(groups = { "Functional" })
139 public void getAllFTSDataColumns()
141 Collection<FTSDataColumnI> allFields = ftsRestClient
142 .getAllFTSDataColumns();
143 Assert.assertNotNull(allFields);
144 // System.out.println(allFields.size());
145 Assert.assertEquals(allFields.size(), 20);
148 @Test(groups = { "Functional" })
149 public void getSearchableDataColumns()
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
160 @Test(groups = { "Functional" })
161 public void getPrimaryKeyColumn()
163 // to change when resources.tdbeacons_data_columns.txt is changed
164 FTSDataColumnI expectedPKColumn;
167 expectedPKColumn = ftsRestClient.getDataColumnByNameOrCode("Url");
168 Assert.assertNotNull(ftsRestClient.getPrimaryKeyColumn());
169 Assert.assertEquals(ftsRestClient.getPrimaryKeyColumn(),
171 } catch (Exception e)
174 Assert.fail("Exception thrown while testing...");
178 @Test(groups = { "Functional" })
179 public void getDataColumnByNameOrCode()
183 FTSDataColumnI foundDataCol = ftsRestClient
184 .getDataColumnByNameOrCode("uniprot_accession");
185 Assert.assertNotNull(foundDataCol);
186 Assert.assertEquals(foundDataCol.getName(), "UniProt Accession");
187 } catch (Exception e)
190 Assert.fail("Exception thrown while testing...");
194 @Test(groups = { "Functional" })
195 public void getDataColumnGroupById()
197 FTSDataColumnGroupI foundDataColGroup;
200 foundDataColGroup = ftsRestClient.getDataColumnGroupById("g2");
201 Assert.assertNotNull(foundDataColGroup);
202 Assert.assertEquals(foundDataColGroup.getName(), "Quality");
203 } catch (Exception e)
209 @Test(groups = { "Functional" })
210 public void getDefaultResponsePageSize()
212 int defaultResSize = ftsRestClient.getDefaultResponsePageSize();
213 Assert.assertEquals(defaultResSize, 100); // why 100 or 500 ? pdb is 100,
217 @Test(groups = { "Functional" })
218 public void getColumnMinWidthTest()
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)
230 Assert.fail("Exception thrown while testing...");
233 // could add test for MaxWidth & PreferedWith
235 @Test(groups = { "Functional" })
236 public void getColumnClassTest()
240 FTSDataColumnI foundDataCol = ftsRestClient
241 .getDataColumnByNameOrCode("uniprot_accession");
242 Assert.assertNotNull(foundDataCol);
243 Assert.assertEquals(foundDataCol.getDataType().getDataTypeClass(),
245 foundDataCol = ftsRestClient.getDataColumnByNameOrCode("id");
246 Assert.assertNotNull(foundDataCol);
247 Assert.assertEquals(foundDataCol.getDataType().getDataTypeClass(),
249 } catch (Exception e)
252 Assert.fail("Exception thrown while testing...");
256 @Test(groups = { "Functional" })
257 public void coverageForEqualsAndHashFunction()
259 Set<FTSDataColumnI> uniqueSet = new HashSet<FTSDataColumnI>();
260 Collection<FTSDataColumnI> searchableCols = ftsRestClient
261 .getSearchableDataColumns();
262 System.out.println(searchableCols);
263 for (FTSDataColumnI foundCol : searchableCols)
265 System.out.println(foundCol.toString());
266 uniqueSet.add(foundCol);
267 uniqueSet.add(foundCol);
269 Assert.assertTrue(!uniqueSet.isEmpty());
270 // Assert.assertEquals(uniqueSet.size(), 22); -> 1 or 2 currently for 3DB
273 @Test(groups = { "Functional" })
274 public void getTDBIdColumIndexTest()
276 List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
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)
291 assertEquals(4, TDBeaconsFTSRestClient.getInstance()
292 .getPrimaryKeyColumIndex(wantedFields, true));
293 // assertEquals(3, TDBeaconsFTSRestClient.getInstance()
294 // .getPrimaryKeyColumIndex(wantedFields, true));
295 } catch (Exception e)
301 private static String[][] mocks = { { "P38398.json", null },
302 { "P01308.json", null },
303 { "P0DTD1.json", null },
304 { "P27787.json", null }
306 // , { "P0DTD3.json", "{}" } actually results in 404, but {} is in body
309 private static void setMockData()
313 mocks[0][1] = PDBFTSRestClientTest.readJsonStringFromFile(
314 "test/jalview/fts/threedbeacons/p38398_tdb_fts_query_resp.txt");
316 mocks[1][1] = PDBFTSRestClientTest.readJsonStringFromFile(
317 "test/jalview/fts/threedbeacons/p01308_tdb_fts_query_resp.txt");
319 mocks[2][1] = PDBFTSRestClientTest.readJsonStringFromFile(
320 "test/jalview/fts/threedbeacons/p0dtd1_tdb_fts_query_resp.txt");
322 mocks[3][1] = PDBFTSRestClientTest.readJsonStringFromFile(
323 "test/jalview/fts/threedbeacons/p27787_tdb_fts_query_resp.txt");
325 } catch (IOException e)
327 Assert.fail("Couldn't read mock response data", e);
331 public static void setMock()
334 FTSRestClient.createMockFTSRestClient(
335 (FTSRestClient) TDBeaconsFTSRestClient.getInstance(), mocks);
338 private static String dev_url = "https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
340 private static String prod_url = "https://www.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
343 * check that the mock request and response are the same as the response from
344 * a live 3D-beacons endpoint
346 * Note - servers often have rapidly changing ids / URIs so this might fail,
347 * but the overall structure will remain.
351 @Test(groups = { "Network", "Integration" })
352 public void verifyMockTDBRequest() throws Exception
355 for (String[] otherMock : mocks)
357 verifyMockTDBRequest(otherMock[0], otherMock[1]);
361 private void verifyMockTDBRequest(String mockRequest,
362 String _mockResponse) throws Exception
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
370 assertEquals(_mockResponse.trim(), tresp.trim());
373 @Test(groups = { "Functional" })
374 public void testMockTDBRequest()
378 List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
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)
397 e1.printStackTrace();
399 System.out.println("wantedFields >>" + wantedFields);
401 FTSRestRequest request = new FTSRestRequest();
402 FTSRestResponse response;
404 request.setResponseSize(100);
405 request.setFieldToSearchBy("");
406 request.setWantedFields(wantedFields);
407 // check 404 behaviour
408 request.setSearchTerm("P00000.json");
412 response = TDBeaconsFTSRestClient.getInstance()
413 .executeRequest(request);
415 assertNull(response);
416 } catch (Exception e)
419 Assert.fail("Unexpected failure during mock 3DBeacons 404 test");
422 // check 200 behaviour
423 request.setSearchTerm("P38398.json");
424 System.out.println("request : " + request.getFieldToSearchBy());
425 // System.out.println(request.toString());
429 response = TDBeaconsFTSRestClient.getInstance()
430 .executeRequest(request);
431 } catch (Exception e)
434 Assert.fail("Couldn't execute webservice call!");
437 assertTrue(response.getSearchSummary() != null);
438 assertTrue(response.getNumberOfItemsFound() > 3); // 4 atm
439 System.out.println("Search summary : \n" + response.getSearchSummary());
441 // System.out.println(response.getSearchSummary().size());
444 @Test(groups = { "External", "Network" })
445 public void executeRequestTest()
447 List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
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)
466 e1.printStackTrace();
468 System.out.println("wantedFields >>" + wantedFields);
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());
478 FTSRestResponse response;
481 response = TDBeaconsFTSRestClient.getInstance()
482 .executeRequest(request);
483 } catch (Exception e)
486 Assert.fail("Couldn't execute webservice call!");
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());