JAL-2089 patch broken merge to master for Release 2.10.0b1
[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(alwaysRun = true)
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.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)
84     {
85       e1.printStackTrace();
86     }
87
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);
94
95     FTSRestResponse response;
96     try
97     {
98       response = PDBFTSRestClient.getInstance().executeRequest(request);
99     } catch (Exception e)
100     {
101       e.printStackTrace();
102       Assert.fail("Couldn't execute webservice call!");
103       return;
104     }
105     assertTrue(response.getNumberOfItemsFound() > 99);
106     assertTrue(response.getSearchSummary() != null);
107     assertTrue(response.getSearchSummary().size() > 99);
108   }
109
110   @Test(groups = { "Functional" })
111   public void getPDBDocFieldsAsCommaDelimitedStringTest()
112   {
113     List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
114     try
115     {
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)
127     {
128       e.printStackTrace();
129     }
130
131     String expectedResult = "molecule_type,pdb_id,genus,gene_name,title";
132     String actualResult = PDBFTSRestClient.getInstance()
133             .getDataColumnsFieldsAsCommaDelimitedString(wantedFields);
134
135     assertEquals("", expectedResult, actualResult);
136   }
137
138   @Test(groups = { "External, Network" })
139   public void parsePDBJsonExceptionStringTest()
140   {
141     List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
142     try
143     {
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)
155     {
156       e1.printStackTrace();
157     }
158
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);
165
166     String jsonErrorResponse = "";
167     try
168     {
169       jsonErrorResponse = readJsonStringFromFile("test/jalview/io/pdb_request_json_error.txt");
170     } catch (IOException e)
171     {
172       e.printStackTrace();
173     }
174
175     String parsedErrorResponse = PDBFTSRestClient
176             .parseJsonExceptionString(jsonErrorResponse);
177
178     String expectedErrorMsg = "\n============= PDB Rest Client RunTime error =============\n"
179             + "Status: 400\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"
182             + "fl: pdb_id\n";
183
184     assertEquals(expectedErrorMsg, parsedErrorResponse);
185   }
186
187   @Test(groups = { "External" }, expectedExceptions = Exception.class)
188   public void testForExpectedRuntimeException() throws Exception
189   {
190     List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
191     wantedFields.add(PDBFTSRestClient.getInstance()
192             .getDataColumnByNameOrCode("pdb_id"));
193
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);
199   }
200
201   // JBP: Is this actually external ? Looks like it is mocked
202   @Test(groups = { "External" })
203   public void parsePDBJsonResponseTest()
204   {
205     List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
206     try
207     {
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)
219     {
220       e1.printStackTrace();
221     }
222
223     FTSRestRequest request = new FTSRestRequest();
224     request.setAllowEmptySeq(false);
225     request.setWantedFields(wantedFields);
226
227     String jsonString = "";
228     try
229     {
230       jsonString = readJsonStringFromFile("test/jalview/io/pdb_response_json.txt");
231     } catch (IOException e)
232     {
233       e.printStackTrace();
234     }
235     FTSRestResponse response = PDBFTSRestClient.parsePDBJsonResponse(
236             jsonString, request);
237     assertTrue(response.getSearchSummary() != null);
238     assertTrue(response.getNumberOfItemsFound() == 931);
239     assertTrue(response.getSearchSummary().size() == 14);
240   }
241
242   @Test(groups = { "Functional" })
243   public void getPDBIdColumIndexTest()
244   {
245     List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
246     try
247     {
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)
259     {
260       e.printStackTrace();
261     }
262     try
263     {
264       assertEquals(5, PDBFTSRestClient.getInstance()
265               .getPrimaryKeyColumIndex(wantedFields, true));
266       assertEquals(4, PDBFTSRestClient.getInstance()
267               .getPrimaryKeyColumIndex(wantedFields, false));
268     } catch (Exception e)
269     {
270       // TODO Auto-generated catch block
271       e.printStackTrace();
272     }
273   }
274
275   @Test(groups = { "External" })
276   public void externalServiceIntegrationTest()
277   {
278     ClientConfig clientConfig = new DefaultClientConfig();
279     Client client = Client.create(clientConfig);
280
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 *]");
286
287     // Execute the REST request
288     ClientResponse clientResponse = webResource.accept(
289             MediaType.APPLICATION_JSON).get(ClientResponse.class);
290
291     // Get the JSON string from the response object
292     String pdbJsonResponseString = clientResponse.getEntity(String.class);
293
294     // Check the response status and report exception if one occurs
295     if (clientResponse.getStatus() != 200)
296     {
297       Assert.fail("Webservice call failed!!!");
298     }
299     else
300     {
301       try
302       {
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();
312
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());
319
320         JSONObject pdbJsonDoc = docIter.next();
321
322         for (FTSDataColumnI field : PDBFTSRestClient.getInstance()
323                 .getAllFTSDataColumns())
324         {
325           if (field.getName().equalsIgnoreCase("ALL"))
326           {
327             continue;
328           }
329           if (pdbJsonDoc.get(field.getCode()) == null)
330           {
331             // System.out.println(">>>\t" + field.getCode());
332             assertTrue(field.getCode()
333                     + " has been removed from PDB doc Entity",
334                     !pdbJsonResponseString.contains(field.getCode()));
335           }
336         }
337       } catch (ParseException e)
338       {
339         Assert.fail(">>>  Test failed due to exception while parsing pdb response json !!!");
340         e.printStackTrace();
341       }
342     }
343   }
344
345   public String readJsonStringFromFile(String filePath) throws IOException
346   {
347     String fileContent;
348     BufferedReader br = new BufferedReader(new FileReader(filePath));
349     try
350     {
351       StringBuilder sb = new StringBuilder();
352       String line = br.readLine();
353
354       while (line != null)
355       {
356         sb.append(line);
357         sb.append(System.lineSeparator());
358         line = br.readLine();
359       }
360       fileContent = sb.toString();
361     } finally
362     {
363       br.close();
364     }
365     return fileContent;
366   }
367
368 }