From dc53250eeeb8c854c876b0fc896c541bd8cd8710 Mon Sep 17 00:00:00 2001 From: tcofoegbu Date: Fri, 1 May 2015 12:02:17 +0100 Subject: [PATCH] JAL-1726 Added external integration test for PDB Rest API --- src/jalview/ws/dbsources/PDBRestClient.java | 3 +- test/jalview/ws/dbsources/PDBRestClientTest.java | 84 +++++++++++++++++++++- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/src/jalview/ws/dbsources/PDBRestClient.java b/src/jalview/ws/dbsources/PDBRestClient.java index c5642a9..02523da 100644 --- a/src/jalview/ws/dbsources/PDBRestClient.java +++ b/src/jalview/ws/dbsources/PDBRestClient.java @@ -31,7 +31,7 @@ import com.sun.jersey.api.json.JSONConfiguration; */ public class PDBRestClient { - private static String PDB_SEARCH_ENDPOINT = "http://www.ebi.ac.uk/pdbe/search/pdb/select?"; + public static final String PDB_SEARCH_ENDPOINT = "http://www.ebi.ac.uk/pdbe/search/pdb/select?"; private static int DEFAULT_RESPONSE_SIZE = 200; @@ -240,6 +240,7 @@ public class PDBRestClient return pdbFeildIndexCounter; } + /** * This enum represents the fields available in the PDB JSON response * diff --git a/test/jalview/ws/dbsources/PDBRestClientTest.java b/test/jalview/ws/dbsources/PDBRestClientTest.java index 181fcf1..86cb3a3 100644 --- a/test/jalview/ws/dbsources/PDBRestClientTest.java +++ b/test/jalview/ws/dbsources/PDBRestClientTest.java @@ -2,6 +2,7 @@ package jalview.ws.dbsources; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import jalview.ws.dbsources.PDBRestClient.PDBDocField; import jalview.ws.uimodel.PDBRestRequest; import jalview.ws.uimodel.PDBRestResponse; @@ -10,12 +11,25 @@ import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; +import javax.ws.rs.core.MediaType; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; import org.junit.After; import org.junit.Before; import org.junit.Test; +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.client.config.ClientConfig; +import com.sun.jersey.api.client.config.DefaultClientConfig; + public class PDBRestClientTest { @@ -162,6 +176,75 @@ public class PDBRestClientTest assertEquals(4, PDBRestClient.getPDBIdColumIndex(wantedFields, false)); } + @Test + public void externalServiceIntegrationTest() + { + ClientConfig clientConfig = new DefaultClientConfig(); + Client client = Client.create(clientConfig); + + // Build request parameters for the REST Request + WebResource webResource = client + .resource(PDBRestClient.PDB_SEARCH_ENDPOINT) + .queryParam("wt", "json").queryParam("rows", String.valueOf(1)) + .queryParam("q", "text:abc AND molecule_sequence:['' TO *]"); + + // Execute the REST request + ClientResponse clientResponse = webResource.accept( + MediaType.APPLICATION_JSON).get(ClientResponse.class); + + // Get the JSON string from the response object + String pdbJsonResponseString = clientResponse.getEntity(String.class); + + // Check the response status and report exception if one occurs + if (clientResponse.getStatus() != 200) + { + fail("Webservice call failed!!!"); + } + else + { + try + { + JSONParser jsonParser = new JSONParser(); + JSONObject jsonObj = (JSONObject) jsonParser + .parse(pdbJsonResponseString); + JSONObject pdbResponse = (JSONObject) jsonObj.get("response"); + String queryTime = ((JSONObject) jsonObj.get("responseHeader")) + .get("QTime").toString(); + String numFound = pdbResponse.get("numFound").toString(); + JSONArray docs = (JSONArray) pdbResponse.get("docs"); + Iterator docIter = docs.iterator(); + + assertTrue("Couldn't Retrieve 'response' object", + pdbResponse != null); + assertTrue("Couldn't Retrieve 'QTime' value", queryTime != null); + assertTrue("Couldn't Retrieve 'numFound' value", numFound != null); + assertTrue("Couldn't Retrieve 'docs' object", docs != null + || !docIter.hasNext()); + + JSONObject pdbJsonDoc = docIter.next(); + + for (PDBDocField field : PDBDocField.values()) + { + if (field == PDBDocField.ALL) + { + continue; + } + if (pdbJsonDoc.get(field.getCode()) == null) + { + // System.out.println(">>>\t" + field.getCode()); + assertTrue(field.getClass() + + " has been removed from PDB doc Entity", + !pdbJsonResponseString.contains(field.getCode())); + } + } + } catch (ParseException e) + { + fail(">>> Test failed due to exception while parsing pdb response json !!!"); + e.printStackTrace(); + } + } + } + public String readJsonStringFromFile(String filePath) throws IOException { String fileContent; @@ -185,5 +268,4 @@ public class PDBRestClientTest return fileContent; } - } -- 1.7.10.2