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.service.pdb;
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertTrue;
26 import jalview.fts.api.FTSDataColumnI;
27 import jalview.fts.core.FTSRestRequest;
28 import jalview.fts.core.FTSRestResponse;
29 import jalview.gui.JvOptionPane;
31 import java.io.BufferedReader;
32 import java.io.FileReader;
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.Iterator;
36 import java.util.List;
38 import javax.ws.rs.core.MediaType;
40 import org.json.simple.JSONArray;
41 import org.json.simple.JSONObject;
42 import org.json.simple.parser.JSONParser;
43 import org.json.simple.parser.ParseException;
44 import org.testng.Assert;
45 import org.testng.annotations.AfterMethod;
46 import org.testng.annotations.BeforeClass;
47 import org.testng.annotations.BeforeMethod;
48 import org.testng.annotations.Test;
50 import com.sun.jersey.api.client.Client;
51 import com.sun.jersey.api.client.ClientResponse;
52 import com.sun.jersey.api.client.WebResource;
53 import com.sun.jersey.api.client.config.ClientConfig;
54 import com.sun.jersey.api.client.config.DefaultClientConfig;
56 public class PDBFTSRestClientTest
59 @BeforeClass(alwaysRun = true)
60 public void setUpJvOptionPane()
62 JvOptionPane.setInteractiveMode(false);
63 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
66 @BeforeMethod(alwaysRun = true)
67 public void setUp() throws Exception
71 @AfterMethod(alwaysRun = true)
72 public void tearDown() throws Exception
76 @Test(groups = { "External", "Network" })
77 public void executeRequestTest()
79 List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
82 wantedFields.add(PDBFTSRestClient.getInstance()
83 .getDataColumnByNameOrCode("molecule_type"));
84 wantedFields.add(PDBFTSRestClient.getInstance()
85 .getDataColumnByNameOrCode("pdb_id"));
86 wantedFields.add(PDBFTSRestClient.getInstance()
87 .getDataColumnByNameOrCode("genus"));
88 wantedFields.add(PDBFTSRestClient.getInstance()
89 .getDataColumnByNameOrCode("gene_name"));
90 wantedFields.add(PDBFTSRestClient.getInstance()
91 .getDataColumnByNameOrCode("title"));
92 } catch (Exception e1)
96 System.out.println("wantedFields >>" + wantedFields);
99 FTSRestRequest request = new FTSRestRequest();
100 request.setAllowEmptySeq(false);
101 request.setResponseSize(100);
102 request.setFieldToSearchBy("text:");
103 request.setSearchTerm("abc");
104 request.setWantedFields(wantedFields);
106 FTSRestResponse response;
109 response = PDBFTSRestClient.getInstance().executeRequest(request);
110 } catch (Exception e)
113 Assert.fail("Couldn't execute webservice call!");
116 assertTrue(response.getNumberOfItemsFound() > 99);
117 assertTrue(response.getSearchSummary() != null);
118 assertTrue(response.getSearchSummary().size() > 99);
121 @Test(groups = { "Functional" })
122 public void getPDBDocFieldsAsCommaDelimitedStringTest()
124 List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
127 wantedFields.add(PDBFTSRestClient.getInstance()
128 .getDataColumnByNameOrCode("molecule_type"));
129 wantedFields.add(PDBFTSRestClient.getInstance()
130 .getDataColumnByNameOrCode("pdb_id"));
131 wantedFields.add(PDBFTSRestClient.getInstance()
132 .getDataColumnByNameOrCode("genus"));
133 wantedFields.add(PDBFTSRestClient.getInstance()
134 .getDataColumnByNameOrCode("gene_name"));
135 wantedFields.add(PDBFTSRestClient.getInstance()
136 .getDataColumnByNameOrCode("title"));
137 } catch (Exception e)
142 String expectedResult = "molecule_type,pdb_id,genus,gene_name,title";
143 String actualResult = PDBFTSRestClient.getInstance()
144 .getDataColumnsFieldsAsCommaDelimitedString(wantedFields);
146 assertEquals("", expectedResult, actualResult);
149 @Test(groups = { "External, Network" })
150 public void parsePDBJsonExceptionStringTest()
152 List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
155 wantedFields.add(PDBFTSRestClient.getInstance()
156 .getDataColumnByNameOrCode("molecule_type"));
157 wantedFields.add(PDBFTSRestClient.getInstance()
158 .getDataColumnByNameOrCode("pdb_id"));
159 wantedFields.add(PDBFTSRestClient.getInstance()
160 .getDataColumnByNameOrCode("genus"));
161 wantedFields.add(PDBFTSRestClient.getInstance()
162 .getDataColumnByNameOrCode("gene_name"));
163 wantedFields.add(PDBFTSRestClient.getInstance()
164 .getDataColumnByNameOrCode("title"));
165 } catch (Exception e1)
167 e1.printStackTrace();
170 FTSRestRequest request = new FTSRestRequest();
171 request.setAllowEmptySeq(false);
172 request.setResponseSize(100);
173 request.setFieldToSearchBy("text:");
174 request.setSearchTerm("abc");
175 request.setWantedFields(wantedFields);
177 String jsonErrorResponse = "";
180 jsonErrorResponse = readJsonStringFromFile("test/jalview/io/pdb_request_json_error.txt");
181 } catch (IOException e)
186 String parsedErrorResponse = PDBFTSRestClient
187 .parseJsonExceptionString(jsonErrorResponse);
189 String expectedErrorMsg = "\n============= PDB Rest Client RunTime error =============\n"
191 + "Message: org.apache.solr.search.SyntaxError: Cannot parse 'text:abc OR text:go:abc AND molecule_sequence:['' TO *]': Encountered \" \":\" \": \"\" at line 1, column 19.\n"
192 + "query: text:abc OR text:go:abc AND molecule_sequence:['' TO *]\n"
195 assertEquals(expectedErrorMsg, parsedErrorResponse);
198 @Test(groups = { "External" }, expectedExceptions = Exception.class)
199 public void testForExpectedRuntimeException() throws Exception
201 // JBPNote: looks like this test fails for no good reason - what exception was supposed to be raised ?
202 List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
203 wantedFields.add(PDBFTSRestClient.getInstance()
204 .getDataColumnByNameOrCode("pdb_id"));
206 FTSRestRequest request = new FTSRestRequest();
207 request.setFieldToSearchBy("text:");
208 request.setSearchTerm("abc OR text:go:abc");
209 request.setWantedFields(wantedFields);
210 PDBFTSRestClient.getInstance().executeRequest(request);
213 // JBP: Is this actually external ? Looks like it is mocked
214 // JBP looks like the mock is not up to date for this test
215 @Test(groups = { "External" })
216 public void parsePDBJsonResponseTest()
218 List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
221 wantedFields.add(PDBFTSRestClient.getInstance()
222 .getDataColumnByNameOrCode("molecule_type"));
223 wantedFields.add(PDBFTSRestClient.getInstance()
224 .getDataColumnByNameOrCode("pdb_id"));
225 wantedFields.add(PDBFTSRestClient.getInstance()
226 .getDataColumnByNameOrCode("genus"));
227 wantedFields.add(PDBFTSRestClient.getInstance()
228 .getDataColumnByNameOrCode("gene_name"));
229 wantedFields.add(PDBFTSRestClient.getInstance()
230 .getDataColumnByNameOrCode("title"));
231 } catch (Exception e1)
233 e1.printStackTrace();
236 FTSRestRequest request = new FTSRestRequest();
237 request.setAllowEmptySeq(false);
238 request.setWantedFields(wantedFields);
240 String jsonString = "";
243 jsonString = readJsonStringFromFile("test/jalview/io/pdb_response_json.txt");
244 } catch (IOException e)
248 FTSRestResponse response = PDBFTSRestClient.parsePDBJsonResponse(
249 jsonString, request);
250 assertTrue(response.getSearchSummary() != null);
251 assertTrue(response.getNumberOfItemsFound() == 931);
252 assertTrue(response.getSearchSummary().size() == 14);
253 System.out.println("Search summary : " + response.getSearchSummary());
256 @Test(groups = { "Functional" })
257 public void getPDBIdColumIndexTest()
259 List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
262 wantedFields.add(PDBFTSRestClient.getInstance()
263 .getDataColumnByNameOrCode("molecule_type"));
264 wantedFields.add(PDBFTSRestClient.getInstance()
265 .getDataColumnByNameOrCode("genus"));
266 wantedFields.add(PDBFTSRestClient.getInstance()
267 .getDataColumnByNameOrCode("gene_name"));
268 wantedFields.add(PDBFTSRestClient.getInstance()
269 .getDataColumnByNameOrCode("title"));
270 wantedFields.add(PDBFTSRestClient.getInstance()
271 .getDataColumnByNameOrCode("pdb_id"));
272 } catch (Exception e)
278 assertEquals(5, PDBFTSRestClient.getInstance()
279 .getPrimaryKeyColumIndex(wantedFields, true));
280 assertEquals(4, PDBFTSRestClient.getInstance()
281 .getPrimaryKeyColumIndex(wantedFields, false));
282 } catch (Exception e)
284 // TODO Auto-generated catch block
289 @Test(groups = { "External" })
290 public void externalServiceIntegrationTest()
292 ClientConfig clientConfig = new DefaultClientConfig();
293 Client client = Client.create(clientConfig);
295 // Build request parameters for the REST Request
296 WebResource webResource = client
297 .resource(PDBFTSRestClient.PDB_SEARCH_ENDPOINT)
298 .queryParam("wt", "json").queryParam("rows", String.valueOf(1))
299 .queryParam("q", "text:abc AND molecule_sequence:['' TO *]");
301 // Execute the REST request
302 ClientResponse clientResponse = webResource.accept(
303 MediaType.APPLICATION_JSON).get(ClientResponse.class);
305 // Get the JSON string from the response object
306 String pdbJsonResponseString = clientResponse.getEntity(String.class);
308 // Check the response status and report exception if one occurs
309 if (clientResponse.getStatus() != 200)
311 Assert.fail("Webservice call failed!!!");
317 JSONParser jsonParser = new JSONParser();
318 JSONObject jsonObj = (JSONObject) jsonParser
319 .parse(pdbJsonResponseString);
320 JSONObject pdbResponse = (JSONObject) jsonObj.get("response");
321 String queryTime = ((JSONObject) jsonObj.get("responseHeader"))
322 .get("QTime").toString();
323 String numFound = pdbResponse.get("numFound").toString();
324 JSONArray docs = (JSONArray) pdbResponse.get("docs");
325 Iterator<JSONObject> docIter = docs.iterator();
327 assertTrue("Couldn't Retrieve 'response' object",
328 pdbResponse != null);
329 assertTrue("Couldn't Retrieve 'QTime' value", queryTime != null);
330 assertTrue("Couldn't Retrieve 'numFound' value", numFound != null);
331 assertTrue("Couldn't Retrieve 'docs' object", docs != null
332 || !docIter.hasNext());
334 JSONObject pdbJsonDoc = docIter.next();
336 for (FTSDataColumnI field : PDBFTSRestClient.getInstance()
337 .getAllFTSDataColumns())
339 if (field.getName().equalsIgnoreCase("ALL"))
343 if (pdbJsonDoc.get(field.getCode()) == null)
345 // System.out.println(">>>\t" + field.getCode());
346 assertTrue(field.getCode()
347 + " has been removed from PDB doc Entity",
348 !pdbJsonResponseString.contains(field.getCode()));
351 } catch (ParseException e)
353 Assert.fail(">>> Test failed due to exception while parsing pdb response json !!!");
359 public String readJsonStringFromFile(String filePath) throws IOException
362 BufferedReader br = new BufferedReader(new FileReader(filePath));
365 StringBuilder sb = new StringBuilder();
366 String line = br.readLine();
371 sb.append(System.lineSeparator());
372 line = br.readLine();
374 fileContent = sb.toString();