X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=test%2Fjalview%2Fws%2Fdbsources%2FPDBRestClientTest.java;h=679cfe49f4761064ceb5d9ff1347b09027fcdaf0;hb=34a685e3f7f8eab7a6f5b2b93a9ff199172d38a4;hp=ba07562a5f60de84638e9b42fd3b7eccf37827f2;hpb=fd235cc7542d6e308e054a8a76580a6c511ae631;p=jalview.git diff --git a/test/jalview/ws/dbsources/PDBRestClientTest.java b/test/jalview/ws/dbsources/PDBRestClientTest.java index ba07562..679cfe4 100644 --- a/test/jalview/ws/dbsources/PDBRestClientTest.java +++ b/test/jalview/ws/dbsources/PDBRestClientTest.java @@ -1,7 +1,28 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b1) + * Copyright (C) 2015 The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ package jalview.ws.dbsources; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertTrue; + import jalview.ws.dbsources.PDBRestClient.PDBDocField; import jalview.ws.uimodel.PDBRestRequest; import jalview.ws.uimodel.PDBRestResponse; @@ -10,26 +31,40 @@ 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 org.junit.After; -import org.junit.Before; -import org.junit.Test; +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.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.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 { - @Before + @BeforeMethod(alwaysRun = true) public void setUp() throws Exception { } - @After + @AfterMethod public void tearDown() throws Exception { } - @Test + @Test(groups = { "External", "Network" }) public void executeRequestTest() { List wantedFields = new ArrayList(); @@ -46,13 +81,22 @@ public class PDBRestClientTest request.setSearchTerm("abc"); request.setWantedFields(wantedFields); - PDBRestResponse response = new PDBRestClient().executeRequest(request); + PDBRestResponse response; + try + { + response = new PDBRestClient().executeRequest(request); + } catch (Exception e) + { + e.printStackTrace(); + Assert.fail("Couldn't execute webservice call!"); + return; + } assertTrue(response.getNumberOfItemsFound() > 99); assertTrue(response.getSearchSummary() != null); assertTrue(response.getSearchSummary().size() > 99); } - @Test + @Test(groups = { "Functional" }) public void getPDBDocFieldsAsCommaDelimitedStringTest() { List wantedFields = new ArrayList(); @@ -61,12 +105,15 @@ public class PDBRestClientTest wantedFields.add(PDBDocField.GENUS); wantedFields.add(PDBDocField.GENE_NAME); wantedFields.add(PDBDocField.TITLE); - assertEquals("molecule_type,pdb_id,genus,gene_name,title", - PDBRestClient - .getPDBDocFieldsAsCommaDelimitedString(wantedFields)); + + String expectedResult = "molecule_type,pdb_id,genus,gene_name,title"; + String actualResult = PDBRestClient + .getPDBDocFieldsAsCommaDelimitedString(wantedFields); + + assertEquals("", expectedResult, actualResult); } - @Test + @Test(groups = { "External, Network" }) public void parsePDBJsonExceptionStringTest() { List wantedFields = new ArrayList(); @@ -94,13 +141,20 @@ public class PDBRestClientTest String parsedErrorResponse = PDBRestClient .parseJsonExceptionString(jsonErrorResponse); - String expectedErrorMsg = "org.apache.solr.search.SyntaxError: Cannot parse 'text:abc OR text:go:abc AND molecule_sequence:['' TO *]': Encountered \" \":\" \": \"\" at line 1, column 19.{\"q\":\"text:abc OR text:go:abc AND molecule_sequence:['' TO *]\",\"fl\":\"pdb_id\",\"sort\":\"\",\"rows\":\"100\",\"wt\":\"json\"}"; + + String expectedErrorMsg = "\n============= PDB Rest Client RunTime error =============\n" + + "Status: 400\n" + + "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" + + "query: text:abc OR text:go:abc AND molecule_sequence:['' TO *]\n" + + "fl: pdb_id\n"; assertEquals(expectedErrorMsg, parsedErrorResponse); } - @Test(expected = RuntimeException.class) - public void testForExpectedRuntimeException() + @Test( + groups = { "External", "Network" }, + expectedExceptions = Exception.class) + public void testForExpectedRuntimeException() throws Exception { List wantedFields = new ArrayList(); wantedFields.add(PDBDocField.PDB_ID); @@ -112,7 +166,7 @@ public class PDBRestClientTest new PDBRestClient().executeRequest(request); } - @Test + @Test(groups = { "External" }) public void parsePDBJsonResponseTest() { List wantedFields = new ArrayList(); @@ -141,7 +195,7 @@ public class PDBRestClientTest assertTrue(response.getSearchSummary().size() == 14); } - @Test + @Test(groups = { "Functional" }) public void getPDBIdColumIndexTest() { List wantedFields = new ArrayList(); @@ -154,6 +208,75 @@ public class PDBRestClientTest assertEquals(4, PDBRestClient.getPDBIdColumIndex(wantedFields, false)); } + @Test(groups = { "External" }) + 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) + { + Assert.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.getCode() + + " has been removed from PDB doc Entity", + !pdbJsonResponseString.contains(field.getCode())); + } + } + } catch (ParseException e) + { + Assert.fail(">>> Test failed due to exception while parsing pdb response json !!!"); + e.printStackTrace(); + } + } + } + public String readJsonStringFromFile(String filePath) throws IOException { String fileContent; @@ -168,7 +291,7 @@ public class PDBRestClientTest sb.append(line); sb.append(System.lineSeparator()); line = br.readLine(); - } + } fileContent = sb.toString(); } finally { @@ -177,5 +300,4 @@ public class PDBRestClientTest return fileContent; } - }