JAL-3829 mocking for testing 3D-Beacons FTS endpoint
authorJim Procter <j.procter@dundee.ac.uk>
Fri, 3 Sep 2021 17:41:32 +0000 (18:41 +0100)
committerJim Procter <j.procter@dundee.ac.uk>
Fri, 3 Sep 2021 17:41:32 +0000 (18:41 +0100)
src/jalview/fts/service/threedbeacons/TDBeaconsFTSRestClient.java
test/jalview/fts/threedbeacons/TDBeaconsFTSRestClientTest.java

index a57cf3b..9ca42e5 100644 (file)
@@ -41,7 +41,21 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
   protected TDBeaconsFTSRestClient()
   {
   }
-
+  public static void createMockTDBeaconsFSRestClient(String workingQuery, String jsonResponse) {
+    instance = new TDBeaconsFTSRestClient();
+    ((TDBeaconsFTSRestClient) instance).setMock(workingQuery,jsonResponse);
+  }
+  private String mockQuery = null;
+  
+  private String mockResponse = null;
+  protected void setMock(String workingQuery,String jsonResponse) {
+    mockQuery=workingQuery;
+    mockResponse = jsonResponse;
+  }
+  private boolean isMocked()
+  {
+    return mockQuery != null && mockResponse!=null;
+  }
   @SuppressWarnings("unchecked")
   @Override
   public FTSRestResponse executeRequest(FTSRestRequest tdbRestRequest)
@@ -75,8 +89,15 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
       System.out.println(uri.toString());
 
       // Execute the REST request
-      ClientResponse clientResponse = webResource
-              .accept(MediaType.APPLICATION_JSON).get(clientResponseClass);
+      ClientResponse clientResponse;
+      if (isMocked()) { 
+        clientResponse = null;
+      }
+      else
+      {
+        clientResponse = webResource.accept(MediaType.APPLICATION_JSON)
+                .get(clientResponseClass);
+      }
 
       // Get the JSON string from the response object or directly from the
       // client (JavaScript)
@@ -84,7 +105,7 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
       String responseString = null;
 
       // Check the response status and report exception if one occurs
-      int responseStatus = clientResponse.getStatus();
+      int responseStatus = isMocked() ? (mockQuery.equals(query) ? 200 : 404) : clientResponse.getStatus();
       switch (responseStatus)
       {
       // if success
@@ -95,11 +116,13 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
         }
         else
         {
-          responseString = clientResponse.getEntity(String.class);
+          responseString = isMocked() ? mockResponse: clientResponse.getEntity(String.class);
         }
         break;
       case 400:
         throw new Exception(parseJsonExceptionString(responseString));
+      case 404:
+        return emptyTDBeaconsJsonResponse();
       default:
         throw new Exception(
                 getMessageByHTTPStatusCode(responseStatus, "3DBeacons"));
@@ -130,6 +153,17 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
 
   }
 
+  /**
+   * returns response for when the 3D-Beacons service doesn't have a record for
+   * the given query - in 2.11.2 this triggers a failover to the PDBe FTS 
+   * 
+   * @return null
+   */
+  private FTSRestResponse emptyTDBeaconsJsonResponse()
+  {
+    return null;
+  }
+
   public String setSearchTerm(String term)
   {
     return term;
index 9a27cf3..ea3756c 100644 (file)
@@ -1,5 +1,6 @@
 package jalview.fts.threedbeacons;
 
+import static org.testng.Assert.assertNull;
 import static org.testng.AssertJUnit.assertEquals;
 import static org.testng.AssertJUnit.assertTrue;
 
@@ -270,6 +271,79 @@ public class TDBeaconsFTSRestClientTest
     }
   }
   
+  private static String mockResponse = "{\"uniprot_entry\":{\"sequence_length\":1863,\"ac\":\"P38398\",\"id\":\"BRCA1_HUMAN\"},\"structures\":[{\"model_identifier\":\"4igk\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2012-12-17\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":1.75,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/4igk_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"1t15\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2004-04-15\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":1.85,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/1t15_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"4ifi\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2012-12-14\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":2.2,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/4ifi_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"1t29\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2004-04-20\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":2.3,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/1t29_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"3pxb\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2010-12-09\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":2.5,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/3pxb_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"4y2g\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2015-02-09\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":2.5,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/4y2g_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"1y98\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2004-12-14\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":2.5,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/1y98_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"1jnx\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2001-07-26\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":2.5,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/1jnx_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"3pxa\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2010-12-09\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":2.55,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/3pxa_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"3k0h\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2009-09-24\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":2.7,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/3k0h_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"3k0k\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2009-09-24\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":2.7,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/3k0k_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"1n5o\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2002-11-06\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":2.8,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/1n5o_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"3pxc\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2010-12-09\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":2.8,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/3pxc_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"3pxd\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2010-12-09\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":2.8,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/3pxd_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"1t2u\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2004-04-22\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":2.8,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/1t2u_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"3k15\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2009-09-25\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":2.8,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/3k15_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"3pxe\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2010-12-09\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":2.85,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/3pxe_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"3k16\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2009-09-25\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":3.0,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/3k16_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"4ofb\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2014-01-14\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":3.05,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/4ofb_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"3coj\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2008-03-28\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":3.21,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/3coj_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"7lyb\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2021-03-06\",\"sequence_identity\":100.0,\"uniprot_start\":1,\"uniprot_end\":100,\"resolution\":3.28,\"coverage\":5.37,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/7lyb_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"1t2v\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2004-04-22\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":3.3,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/1t2v_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"4y18\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2015-02-06\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":3.5,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/4y18_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"4jlu\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2013-03-13\",\"sequence_identity\":100.0,\"uniprot_start\":1649,\"uniprot_end\":1859,\"resolution\":3.5,\"coverage\":11.33,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/4jlu_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"4u4a\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2014-07-23\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":3.51,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/4u4a_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"2ing\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2006-10-07\",\"sequence_identity\":100.0,\"uniprot_start\":1649,\"uniprot_end\":1859,\"resolution\":3.6,\"coverage\":11.33,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/2ing_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"7jzv\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2020-09-02\",\"sequence_identity\":99.0,\"uniprot_start\":2,\"uniprot_end\":104,\"resolution\":3.9,\"coverage\":5.53,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/7jzv_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"6g2i\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2018-03-23\",\"sequence_identity\":100.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"resolution\":5.9,\"coverage\":11.49,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/6g2i_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"1jm7\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2001-07-17\",\"sequence_identity\":100.0,\"uniprot_start\":1,\"uniprot_end\":110,\"resolution\":null,\"coverage\":5.9,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/1jm7_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"1oqa\",\"model_category\":\"EXPERIMENTALLY DETERMINED\",\"provider\":\"PDBE\",\"created\":\"2003-03-07\",\"sequence_identity\":100.0,\"uniprot_start\":1755,\"uniprot_end\":1863,\"resolution\":null,\"coverage\":5.85,\"model_url\":\"https://www.ebi.ac.uk/pdbe/static/entry/1oqa_updated.cif\",\"model_format\":\"MMCIF\"},{\"model_identifier\":\"612f67c86ba77a10ab6f34ec\",\"model_category\":\"TEMPLATE-BASED\",\"provider\":\"SWISSMODEL\",\"created\":\"2021-09-01\",\"sequence_identity\":1.0,\"uniprot_start\":1646,\"uniprot_end\":1859,\"coverage\":0.115,\"qmean_version\":\"4.2.0\",\"qmean_avg_local_score\":0.776,\"model_url\":\"https://beta.swissmodel.expasy.org/3d-beacons/uniprot/P38398.pdb?range=1646-1859&template=6g2i.1.K&provider=swissmodel\",\"model_format\":\"PDB\"},{\"model_identifier\":\"612f67c86ba77a10ab6f34f0\",\"model_category\":\"TEMPLATE-BASED\",\"provider\":\"SWISSMODEL\",\"created\":\"2021-09-01\",\"sequence_identity\":1.0,\"uniprot_start\":1,\"uniprot_end\":103,\"coverage\":0.055,\"qmean_version\":\"4.2.0\",\"qmean_avg_local_score\":0.655,\"model_url\":\"https://beta.swissmodel.expasy.org/3d-beacons/uniprot/P38398.pdb?range=1-103&template=1jm7.1.A&provider=swissmodel\",\"model_format\":\"PDB\"},{\"model_identifier\":\"AF-P38398-F1\",\"model_category\":\"Deep learning\",\"provider\":\"AlphaFold DB\",\"created\":\"2021-07-01\",\"sequence_identity\":1.0,\"uniprot_start\":1,\"uniprot_end\":1863,\"coverage\":100.0,\"model_url\":\"https://alphafold.ebi.ac.uk/files/AF-P38398-F1-model_v1.cif\",\"model_format\":\"MMCIF\"}]}";
+  
+  public static void setMock() {
+    TDBeaconsFTSRestClient.createMockTDBeaconsFSRestClient("P38398.json", mockResponse);
+  }
+  @Test(groups= {"Functional"})
+  public void testMockTDBRequest()
+  {
+
+    setMock();
+    List<FTSDataColumnI> wantedFields = new ArrayList<FTSDataColumnI>();
+    try
+    {
+      wantedFields.add(TDBeaconsFTSRestClient.getInstance()
+              .getDataColumnByNameOrCode("Model Id"));
+      wantedFields.add(TDBeaconsFTSRestClient.getInstance()
+              .getDataColumnByNameOrCode("model_url"));
+      wantedFields.add(TDBeaconsFTSRestClient.getInstance()
+              .getDataColumnByNameOrCode("provider"));
+      wantedFields.add(TDBeaconsFTSRestClient.getInstance()
+              .getDataColumnByNameOrCode("model_category"));
+      wantedFields.add(TDBeaconsFTSRestClient.getInstance()
+              .getDataColumnByNameOrCode("qmean_avg_local_score"));
+      wantedFields.add(TDBeaconsFTSRestClient.getInstance()
+              .getDataColumnByNameOrCode("uniprot_start"));
+      wantedFields.add(TDBeaconsFTSRestClient.getInstance()
+              .getDataColumnByNameOrCode("uniprot_end"));
+    } catch (Exception e1)
+    {
+      e1.printStackTrace();
+    }
+    System.out.println("wantedFields >>" + wantedFields);
+
+    FTSRestRequest request = new FTSRestRequest();
+    FTSRestResponse response;
+
+    request.setResponseSize(100);
+    request.setFieldToSearchBy("");
+    request.setWantedFields(wantedFields);
+    // check 404 behaviour
+    request.setSearchTerm("P00000.json");
+
+
+    try {
+      response = TDBeaconsFTSRestClient.getInstance().executeRequest(request);
+      
+      assertNull(response);
+    } catch (Exception e)
+    {
+      e.printStackTrace();
+      Assert.fail("Unexpected failure during mock 3DBeacons 404 test");
+    }
+     
+    // check 200 behaviour
+    request.setSearchTerm("P38398.json");
+    System.out.println("request : " + request.getFieldToSearchBy());
+    //System.out.println(request.toString());
+
+    try
+    {
+      response = TDBeaconsFTSRestClient.getInstance().executeRequest(request);
+    } catch (Exception e)
+    {
+      e.printStackTrace();
+      Assert.fail("Couldn't execute webservice call!");
+      return;
+    }
+    assertTrue(response.getSearchSummary() != null);
+    assertTrue(response.getNumberOfItemsFound() > 3); //4 atm
+    System.out.println("Search summary : \n" + response.getSearchSummary());
+    
+    //System.out.println(response.getSearchSummary().size());
+  }
 
   @Test(groups = { "External", "Network" })  
   public void executeRequestTest()