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;
30 import java.io.BufferedReader;
31 import java.io.FileReader;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.Iterator;
35 import java.util.List;
37 import javax.ws.rs.core.MediaType;
39 import org.json.simple.JSONArray;
40 import org.json.simple.JSONObject;
41 import org.json.simple.parser.JSONParser;
42 import org.json.simple.parser.ParseException;
43 import org.testng.Assert;
44 import org.testng.annotations.AfterMethod;
45 import org.testng.annotations.BeforeMethod;
46 import org.testng.annotations.Test;
48 import com.sun.jersey.api.client.Client;
49 import com.sun.jersey.api.client.ClientResponse;
50 import com.sun.jersey.api.client.WebResource;
51 import com.sun.jersey.api.client.config.ClientConfig;
52 import com.sun.jersey.api.client.config.DefaultClientConfig;
54 public class PDBFTSRestClientTest
57 @BeforeMethod(alwaysRun = true)
58 public void setUp() throws Exception
62 @AfterMethod(alwaysRun = true)
63 public void tearDown() throws Exception
67 @Test(groups = { "External", "Network" })
68 public void executeRequestTest()
70 List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
73 wantedFields.add(PDBFTSRestClient.getInstance()
74 .getDataColumnByNameOrCode("molecule_type"));
75 wantedFields.add(PDBFTSRestClient.getInstance()
76 .getDataColumnByNameOrCode("pdb_id"));
77 wantedFields.add(PDBFTSRestClient.getInstance()
78 .getDataColumnByNameOrCode("genus"));
79 wantedFields.add(PDBFTSRestClient.getInstance()
80 .getDataColumnByNameOrCode("gene_name"));
81 wantedFields.add(PDBFTSRestClient.getInstance()
82 .getDataColumnByNameOrCode("title"));
83 } catch (Exception e1)
88 FTSRestRequest request = new FTSRestRequest();
89 request.setAllowEmptySeq(false);
90 request.setResponseSize(100);
91 request.setFieldToSearchBy("text:");
92 request.setSearchTerm("abc");
93 request.setWantedFields(wantedFields);
95 FTSRestResponse response;
98 response = PDBFTSRestClient.getInstance().executeRequest(request);
102 Assert.fail("Couldn't execute webservice call!");
105 assertTrue(response.getNumberOfItemsFound() > 99);
106 assertTrue(response.getSearchSummary() != null);
107 assertTrue(response.getSearchSummary().size() > 99);
110 @Test(groups = { "Functional" })
111 public void getPDBDocFieldsAsCommaDelimitedStringTest()
113 List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
116 wantedFields.add(PDBFTSRestClient.getInstance()
117 .getDataColumnByNameOrCode("molecule_type"));
118 wantedFields.add(PDBFTSRestClient.getInstance()
119 .getDataColumnByNameOrCode("pdb_id"));
120 wantedFields.add(PDBFTSRestClient.getInstance()
121 .getDataColumnByNameOrCode("genus"));
122 wantedFields.add(PDBFTSRestClient.getInstance()
123 .getDataColumnByNameOrCode("gene_name"));
124 wantedFields.add(PDBFTSRestClient.getInstance()
125 .getDataColumnByNameOrCode("title"));
126 } catch (Exception e)
131 String expectedResult = "molecule_type,pdb_id,genus,gene_name,title";
132 String actualResult = PDBFTSRestClient.getInstance()
133 .getDataColumnsFieldsAsCommaDelimitedString(wantedFields);
135 assertEquals("", expectedResult, actualResult);
138 @Test(groups = { "External, Network" })
139 public void parsePDBJsonExceptionStringTest()
141 List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
144 wantedFields.add(PDBFTSRestClient.getInstance()
145 .getDataColumnByNameOrCode("molecule_type"));
146 wantedFields.add(PDBFTSRestClient.getInstance()
147 .getDataColumnByNameOrCode("pdb_id"));
148 wantedFields.add(PDBFTSRestClient.getInstance()
149 .getDataColumnByNameOrCode("genus"));
150 wantedFields.add(PDBFTSRestClient.getInstance()
151 .getDataColumnByNameOrCode("gene_name"));
152 wantedFields.add(PDBFTSRestClient.getInstance()
153 .getDataColumnByNameOrCode("title"));
154 } catch (Exception e1)
156 e1.printStackTrace();
159 FTSRestRequest request = new FTSRestRequest();
160 request.setAllowEmptySeq(false);
161 request.setResponseSize(100);
162 request.setFieldToSearchBy("text:");
163 request.setSearchTerm("abc");
164 request.setWantedFields(wantedFields);
166 String jsonErrorResponse = "";
169 jsonErrorResponse = readJsonStringFromFile("test/jalview/io/pdb_request_json_error.txt");
170 } catch (IOException e)
175 String parsedErrorResponse = PDBFTSRestClient
176 .parseJsonExceptionString(jsonErrorResponse);
178 String expectedErrorMsg = "\n============= PDB Rest Client RunTime error =============\n"
180 + "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"
181 + "query: text:abc OR text:go:abc AND molecule_sequence:['' TO *]\n"
184 assertEquals(expectedErrorMsg, parsedErrorResponse);
187 @Test(groups = { "External" }, expectedExceptions = Exception.class)
188 public void testForExpectedRuntimeException() throws Exception
190 List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
191 wantedFields.add(PDBFTSRestClient.getInstance()
192 .getDataColumnByNameOrCode("pdb_id"));
194 FTSRestRequest request = new FTSRestRequest();
195 request.setFieldToSearchBy("text:");
196 request.setSearchTerm("abc OR text:go:abc");
197 request.setWantedFields(wantedFields);
198 PDBFTSRestClient.getInstance().executeRequest(request);
201 // JBP: Is this actually external ? Looks like it is mocked
202 @Test(groups = { "External" })
203 public void parsePDBJsonResponseTest()
205 List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
208 wantedFields.add(PDBFTSRestClient.getInstance()
209 .getDataColumnByNameOrCode("molecule_type"));
210 wantedFields.add(PDBFTSRestClient.getInstance()
211 .getDataColumnByNameOrCode("pdb_id"));
212 wantedFields.add(PDBFTSRestClient.getInstance()
213 .getDataColumnByNameOrCode("genus"));
214 wantedFields.add(PDBFTSRestClient.getInstance()
215 .getDataColumnByNameOrCode("gene_name"));
216 wantedFields.add(PDBFTSRestClient.getInstance()
217 .getDataColumnByNameOrCode("title"));
218 } catch (Exception e1)
220 e1.printStackTrace();
223 FTSRestRequest request = new FTSRestRequest();
224 request.setAllowEmptySeq(false);
225 request.setWantedFields(wantedFields);
227 String jsonString = "";
230 jsonString = readJsonStringFromFile("test/jalview/io/pdb_response_json.txt");
231 } catch (IOException e)
235 FTSRestResponse response = PDBFTSRestClient.parsePDBJsonResponse(
236 jsonString, request);
237 assertTrue(response.getSearchSummary() != null);
238 assertTrue(response.getNumberOfItemsFound() == 931);
239 assertTrue(response.getSearchSummary().size() == 14);
242 @Test(groups = { "Functional" })
243 public void getPDBIdColumIndexTest()
245 List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
248 wantedFields.add(PDBFTSRestClient.getInstance()
249 .getDataColumnByNameOrCode("molecule_type"));
250 wantedFields.add(PDBFTSRestClient.getInstance()
251 .getDataColumnByNameOrCode("genus"));
252 wantedFields.add(PDBFTSRestClient.getInstance()
253 .getDataColumnByNameOrCode("gene_name"));
254 wantedFields.add(PDBFTSRestClient.getInstance()
255 .getDataColumnByNameOrCode("title"));
256 wantedFields.add(PDBFTSRestClient.getInstance()
257 .getDataColumnByNameOrCode("pdb_id"));
258 } catch (Exception e)
264 assertEquals(5, PDBFTSRestClient.getInstance()
265 .getPrimaryKeyColumIndex(wantedFields, true));
266 assertEquals(4, PDBFTSRestClient.getInstance()
267 .getPrimaryKeyColumIndex(wantedFields, false));
268 } catch (Exception e)
270 // TODO Auto-generated catch block
275 @Test(groups = { "External" })
276 public void externalServiceIntegrationTest()
278 ClientConfig clientConfig = new DefaultClientConfig();
279 Client client = Client.create(clientConfig);
281 // Build request parameters for the REST Request
282 WebResource webResource = client
283 .resource(PDBFTSRestClient.PDB_SEARCH_ENDPOINT)
284 .queryParam("wt", "json").queryParam("rows", String.valueOf(1))
285 .queryParam("q", "text:abc AND molecule_sequence:['' TO *]");
287 // Execute the REST request
288 ClientResponse clientResponse = webResource.accept(
289 MediaType.APPLICATION_JSON).get(ClientResponse.class);
291 // Get the JSON string from the response object
292 String pdbJsonResponseString = clientResponse.getEntity(String.class);
294 // Check the response status and report exception if one occurs
295 if (clientResponse.getStatus() != 200)
297 Assert.fail("Webservice call failed!!!");
303 JSONParser jsonParser = new JSONParser();
304 JSONObject jsonObj = (JSONObject) jsonParser
305 .parse(pdbJsonResponseString);
306 JSONObject pdbResponse = (JSONObject) jsonObj.get("response");
307 String queryTime = ((JSONObject) jsonObj.get("responseHeader"))
308 .get("QTime").toString();
309 String numFound = pdbResponse.get("numFound").toString();
310 JSONArray docs = (JSONArray) pdbResponse.get("docs");
311 Iterator<JSONObject> docIter = docs.iterator();
313 assertTrue("Couldn't Retrieve 'response' object",
314 pdbResponse != null);
315 assertTrue("Couldn't Retrieve 'QTime' value", queryTime != null);
316 assertTrue("Couldn't Retrieve 'numFound' value", numFound != null);
317 assertTrue("Couldn't Retrieve 'docs' object", docs != null
318 || !docIter.hasNext());
320 JSONObject pdbJsonDoc = docIter.next();
322 for (FTSDataColumnI field : PDBFTSRestClient.getInstance()
323 .getAllFTSDataColumns())
325 if (field.getName().equalsIgnoreCase("ALL"))
329 if (pdbJsonDoc.get(field.getCode()) == null)
331 // System.out.println(">>>\t" + field.getCode());
332 assertTrue(field.getCode()
333 + " has been removed from PDB doc Entity",
334 !pdbJsonResponseString.contains(field.getCode()));
337 } catch (ParseException e)
339 Assert.fail(">>> Test failed due to exception while parsing pdb response json !!!");
345 public String readJsonStringFromFile(String filePath) throws IOException
348 BufferedReader br = new BufferedReader(new FileReader(filePath));
351 StringBuilder sb = new StringBuilder();
352 String line = br.readLine();
357 sb.append(System.lineSeparator());
358 line = br.readLine();
360 fileContent = sb.toString();