JAL-3829 mocking for testing 3D-Beacons FTS endpoint
[jalview.git] / src / jalview / fts / service / threedbeacons / TDBeaconsFTSRestClient.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;