JAL-2071 further refactoring, optimisation, and house keeping for the generic Free...
[jalview.git] / test / jalview / fts / service / pdb / PDBFTSRestClientTest.java
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.service.pdb;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertTrue;
25
26 import jalview.fts.api.FTSDataColumnI;
27 import jalview.fts.core.FTSRestRequest;
28 import jalview.fts.core.FTSRestResponse;
29
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;
36
37 import javax.ws.rs.core.MediaType;
38
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;
47
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;
53
54 public class PDBFTSRestClientTest
55 {
56
57   @BeforeMethod(alwaysRun = true)
58   public void setUp() throws Exception
59   {
60   }
61
62   @AfterMethod
63   public void tearDown() throws Exception
64   {
65   }
66
67   @Test(groups = { "External", "Network" })
68   public void executeRequestTest()
69   {
70     List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
71     try
72     {
73       wantedFields.add(PDBFTSRestClient.getInstance()
74               .getDataColumnByNameOrCode("molecule_type"));
75       wantedFields
76 .add(PDBFTSRestClient.getInstance()
77               .getDataColumnByNameOrCode("pdb_id"));
78       wantedFields.add(PDBFTSRestClient.getInstance()
79               .getDataColumnByNameOrCode("genus"));
80       wantedFields
81 .add(PDBFTSRestClient.getInstance()
82               .getDataColumnByNameOrCode("gene_name"));
83       wantedFields.add(PDBFTSRestClient.getInstance()
84               .getDataColumnByNameOrCode("title"));
85     } catch (Exception e1)
86     {
87       e1.printStackTrace();
88     }
89
90     FTSRestRequest request = new FTSRestRequest();
91     request.setAllowEmptySeq(false);
92     request.setResponseSize(100);
93     request.setFieldToSearchBy("text:");
94     request.setSearchTerm("abc");
95     request.setWantedFields(wantedFields);
96
97     FTSRestResponse response;
98     try
99     {
100       response = PDBFTSRestClient.getInstance().executeRequest(request);
101     } catch (Exception e)
102     {
103       e.printStackTrace();
104       Assert.fail("Couldn't execute webservice call!");
105       return;
106     }
107     assertTrue(response.getNumberOfItemsFound() > 99);
108     assertTrue(response.getSearchSummary() != null);
109     assertTrue(response.getSearchSummary().size() > 99);
110   }
111
112   @Test(groups = { "Functional" })
113   public void getPDBDocFieldsAsCommaDelimitedStringTest()
114   {
115     List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
116     try
117     {
118       wantedFields.add(PDBFTSRestClient.getInstance()
119               .getDataColumnByNameOrCode("molecule_type"));
120       wantedFields
121 .add(PDBFTSRestClient.getInstance()
122               .getDataColumnByNameOrCode("pdb_id"));
123       wantedFields.add(PDBFTSRestClient.getInstance()
124               .getDataColumnByNameOrCode("genus"));
125       wantedFields
126 .add(PDBFTSRestClient.getInstance()
127               .getDataColumnByNameOrCode("gene_name"));
128       wantedFields.add(PDBFTSRestClient.getInstance()
129               .getDataColumnByNameOrCode("title"));
130     } catch (Exception e)
131     {
132       e.printStackTrace();
133     }
134
135     String expectedResult = "molecule_type,pdb_id,genus,gene_name,title";
136     String actualResult = PDBFTSRestClient.getInstance()
137             .getDataColumnsFieldsAsCommaDelimitedString(wantedFields);
138
139     assertEquals("", expectedResult, actualResult);
140   }
141
142   @Test(groups = { "External, Network" })
143   public void parsePDBJsonExceptionStringTest()
144   {
145     List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
146     try
147     {
148       wantedFields.add(PDBFTSRestClient.getInstance()
149               .getDataColumnByNameOrCode("molecule_type"));
150       wantedFields
151 .add(PDBFTSRestClient.getInstance()
152               .getDataColumnByNameOrCode("pdb_id"));
153       wantedFields.add(PDBFTSRestClient.getInstance()
154               .getDataColumnByNameOrCode("genus"));
155       wantedFields
156 .add(PDBFTSRestClient.getInstance()
157               .getDataColumnByNameOrCode("gene_name"));
158       wantedFields.add(PDBFTSRestClient.getInstance()
159               .getDataColumnByNameOrCode("title"));
160     } catch (Exception e1)
161     {
162       e1.printStackTrace();
163     }
164
165     FTSRestRequest request = new FTSRestRequest();
166     request.setAllowEmptySeq(false);
167     request.setResponseSize(100);
168     request.setFieldToSearchBy("text:");
169     request.setSearchTerm("abc");
170     request.setWantedFields(wantedFields);
171
172     String jsonErrorResponse = "";
173     try
174     {
175       jsonErrorResponse = readJsonStringFromFile("test/jalview/io/pdb_request_json_error.txt");
176     } catch (IOException e)
177     {
178       e.printStackTrace();
179     }
180
181     String parsedErrorResponse = PDBFTSRestClient
182             .parseJsonExceptionString(jsonErrorResponse);
183
184     String expectedErrorMsg = "\n============= PDB Rest Client RunTime error =============\n"
185             + "Status: 400\n"
186             + "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"
187             + "query: text:abc OR text:go:abc AND molecule_sequence:['' TO *]\n"
188             + "fl: pdb_id\n";
189
190     assertEquals(expectedErrorMsg, parsedErrorResponse);
191   }
192
193   @Test(
194     groups = { "External", "Network" },
195     expectedExceptions = Exception.class)
196   public void testForExpectedRuntimeException() throws Exception
197   {
198     List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
199     wantedFields.add(PDBFTSRestClient.getInstance()
200             .getDataColumnByNameOrCode("pdb_id"));
201
202     FTSRestRequest request = new FTSRestRequest();
203     request.setFieldToSearchBy("text:");
204     request.setSearchTerm("abc OR text:go:abc");
205     request.setWantedFields(wantedFields);
206     PDBFTSRestClient.getInstance().executeRequest(request);
207   }
208
209   @Test(groups = { "External" })
210   public void parsePDBJsonResponseTest()
211   {
212     List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
213     try
214     {
215       wantedFields.add(PDBFTSRestClient.getInstance()
216               .getDataColumnByNameOrCode("molecule_type"));
217       wantedFields
218 .add(PDBFTSRestClient.getInstance()
219               .getDataColumnByNameOrCode("pdb_id"));
220       wantedFields.add(PDBFTSRestClient.getInstance()
221               .getDataColumnByNameOrCode("genus"));
222       wantedFields
223 .add(PDBFTSRestClient.getInstance()
224               .getDataColumnByNameOrCode("gene_name"));
225       wantedFields.add(PDBFTSRestClient.getInstance()
226               .getDataColumnByNameOrCode("title"));
227     } catch (Exception e1)
228     {
229       e1.printStackTrace();
230     }
231
232     FTSRestRequest request = new FTSRestRequest();
233     request.setAllowEmptySeq(false);
234     request.setWantedFields(wantedFields);
235
236     String jsonString = "";
237     try
238     {
239       jsonString = readJsonStringFromFile("test/jalview/io/pdb_response_json.txt");
240     } catch (IOException e)
241     {
242       e.printStackTrace();
243     }
244     FTSRestResponse response = PDBFTSRestClient.parsePDBJsonResponse(
245             jsonString, request);
246     assertTrue(response.getSearchSummary() != null);
247     assertTrue(response.getNumberOfItemsFound() == 931);
248     assertTrue(response.getSearchSummary().size() == 14);
249   }
250
251   @Test(groups = { "Functional" })
252   public void getPDBIdColumIndexTest()
253   {
254     List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
255     try
256     {
257       wantedFields.add(PDBFTSRestClient.getInstance()
258               .getDataColumnByNameOrCode("molecule_type"));
259       wantedFields.add(PDBFTSRestClient.getInstance()
260               .getDataColumnByNameOrCode("genus"));
261       wantedFields
262 .add(PDBFTSRestClient.getInstance()
263               .getDataColumnByNameOrCode("gene_name"));
264       wantedFields.add(PDBFTSRestClient.getInstance()
265               .getDataColumnByNameOrCode("title"));
266       wantedFields
267 .add(PDBFTSRestClient.getInstance()
268               .getDataColumnByNameOrCode("pdb_id"));
269     } catch (Exception e)
270     {
271       e.printStackTrace();
272     }
273     try
274     {
275       assertEquals(5,
276  PDBFTSRestClient.getInstance()
277               .getPrimaryKeyColumIndex(wantedFields, true));
278       assertEquals(4,
279  PDBFTSRestClient.getInstance()
280               .getPrimaryKeyColumIndex(wantedFields, false));
281     } catch (Exception e)
282     {
283       // TODO Auto-generated catch block
284       e.printStackTrace();
285     }
286   }
287
288   @Test(groups = { "External" })
289   public void externalServiceIntegrationTest()
290   {
291     ClientConfig clientConfig = new DefaultClientConfig();
292     Client client = Client.create(clientConfig);
293
294     // Build request parameters for the REST Request
295     WebResource webResource = client
296             .resource(PDBFTSRestClient.PDB_SEARCH_ENDPOINT)
297             .queryParam("wt", "json").queryParam("rows", String.valueOf(1))
298             .queryParam("q", "text:abc AND molecule_sequence:['' TO *]");
299
300     // Execute the REST request
301     ClientResponse clientResponse = webResource.accept(
302             MediaType.APPLICATION_JSON).get(ClientResponse.class);
303
304     // Get the JSON string from the response object
305     String pdbJsonResponseString = clientResponse.getEntity(String.class);
306
307     // Check the response status and report exception if one occurs
308     if (clientResponse.getStatus() != 200)
309     {
310       Assert.fail("Webservice call failed!!!");
311     }
312     else
313     {
314       try
315       {
316         JSONParser jsonParser = new JSONParser();
317         JSONObject jsonObj = (JSONObject) jsonParser
318                 .parse(pdbJsonResponseString);
319         JSONObject pdbResponse = (JSONObject) jsonObj.get("response");
320         String queryTime = ((JSONObject) jsonObj.get("responseHeader"))
321                 .get("QTime").toString();
322         String numFound = pdbResponse.get("numFound").toString();
323         JSONArray docs = (JSONArray) pdbResponse.get("docs");
324         Iterator<JSONObject> docIter = docs.iterator();
325
326         assertTrue("Couldn't Retrieve 'response' object",
327                 pdbResponse != null);
328         assertTrue("Couldn't Retrieve 'QTime' value", queryTime != null);
329         assertTrue("Couldn't Retrieve 'numFound' value", numFound != null);
330         assertTrue("Couldn't Retrieve 'docs' object", docs != null
331                 || !docIter.hasNext());
332
333         JSONObject pdbJsonDoc = docIter.next();
334
335         for (FTSDataColumnI field : PDBFTSRestClient.getInstance()
336                 .getAllFTSDataColumns())
337         {
338           if (field.getName().equalsIgnoreCase("ALL"))
339           {
340             continue;
341           }
342           if (pdbJsonDoc.get(field.getCode()) == null)
343           {
344             // System.out.println(">>>\t" + field.getCode());
345             assertTrue(field.getCode()
346                     + " has been removed from PDB doc Entity",
347                     !pdbJsonResponseString.contains(field.getCode()));
348           }
349         }
350       } catch (ParseException e)
351       {
352         Assert.fail(">>>  Test failed due to exception while parsing pdb response json !!!");
353         e.printStackTrace();
354       }
355     }
356   }
357
358   public String readJsonStringFromFile(String filePath) throws IOException
359   {
360     String fileContent;
361     BufferedReader br = new BufferedReader(new FileReader(filePath));
362     try
363     {
364       StringBuilder sb = new StringBuilder();
365       String line = br.readLine();
366
367       while (line != null)
368       {
369         sb.append(line);
370         sb.append(System.lineSeparator());
371         line = br.readLine();
372       }
373       fileContent = sb.toString();
374     } finally
375     {
376       br.close();
377     }
378     return fileContent;
379   }
380
381 }