diff --git a/pic-sure-resources/pic-sure-resource-api/src/main/java/edu/harvard/dbmi/avillach/service/ResourceWebClient.java b/pic-sure-resources/pic-sure-resource-api/src/main/java/edu/harvard/dbmi/avillach/service/ResourceWebClient.java index 5331fe67..3b8da77d 100644 --- a/pic-sure-resources/pic-sure-resource-api/src/main/java/edu/harvard/dbmi/avillach/service/ResourceWebClient.java +++ b/pic-sure-resources/pic-sure-resource-api/src/main/java/edu/harvard/dbmi/avillach/service/ResourceWebClient.java @@ -79,13 +79,7 @@ public ResourceInfo info(String rsURL, QueryRequest queryRequest) { } catch (JsonProcessingException e) { throw new NotAuthorizedException("Unable to encode resource credentials", e); } finally { - if (resourcesResponse != null) { - try { - EntityUtils.consume(resourcesResponse.getEntity()); - } catch (IOException e) { - logger.error("Failed to close HttpResponse entity", e); - } - } + closeHttpResponse(resourcesResponse); } } @@ -116,13 +110,7 @@ public PaginatedSearchResult searchConceptValues( } catch (URISyntaxException e) { throw new ApplicationException("rsURL invalid : " + rsURL, e); } finally { - if (resourcesResponse != null) { - try { - EntityUtils.consume(resourcesResponse.getEntity()); - } catch (IOException e) { - logger.error("Failed to close HttpResponse entity", e); - } - } + closeHttpResponse(resourcesResponse); } } @@ -156,13 +144,7 @@ public SearchResults search(String rsURL, QueryRequest searchQueryRequest) { // TODO Write custom exception throw new ProtocolException("Unable to serialize search query", e); } finally { - if (resourcesResponse != null) { - try { - EntityUtils.consume(resourcesResponse.getEntity()); - } catch (IOException e) { - logger.error("Failed to close HttpResponse entity", e); - } - } + closeHttpResponse(resourcesResponse); } } @@ -193,18 +175,13 @@ public QueryStatus query(String rsURL, QueryRequest dataQueryRequest) { logger.error("Unable to encode data query"); throw new ProtocolException("Unable to encode data query", e); } finally { - if (resourcesResponse != null) { - try { - EntityUtils.consume(resourcesResponse.getEntity()); - } catch (IOException e) { - logger.error("Failed to close HttpResponse entity", e); - } - } + closeHttpResponse(resourcesResponse); } } public QueryStatus queryStatus(String rsURL, String queryId, QueryRequest queryRequest) { logger.debug("Calling ResourceWebClient query()"); + HttpResponse resourcesResponse = null; try { if (queryRequest == null) { throw new ProtocolException(ProtocolException.MISSING_DATA); @@ -222,7 +199,7 @@ public QueryStatus queryStatus(String rsURL, String queryId, QueryRequest queryR String body = json.writeValueAsString(queryRequest); logger.debug(httpClientUtil.composeURL(rsURL, pathName)); logger.debug(body); - HttpResponse resourcesResponse = httpClientUtil.retrievePostResponse( + resourcesResponse = httpClientUtil.retrievePostResponse( httpClientUtil.composeURL(rsURL, pathName), createHeaders(queryRequest.getResourceCredentials()), body ); if (resourcesResponse.getStatusLine().getStatusCode() != 200) { @@ -233,11 +210,25 @@ public QueryStatus queryStatus(String rsURL, String queryId, QueryRequest queryR } catch (JsonProcessingException e) { logger.error("Unable to encode resource credentials"); throw new ProtocolException("Unable to encode resource credentials", e); + } finally { + closeHttpResponse(resourcesResponse); + } + } + + private void closeHttpResponse(HttpResponse resourcesResponse) { + if (resourcesResponse != null) { + try { + EntityUtils.consume(resourcesResponse.getEntity()); + } catch (IOException e) { + logger.error("Failed to close HttpResponse entity", e); + } } } public Response queryResult(String rsURL, String queryId, QueryRequest queryRequest) { logger.debug("Calling ResourceWebClient query()"); + + HttpResponse resourcesResponse = null; try { if (queryRequest == null) { throw new ProtocolException(ProtocolException.MISSING_DATA); @@ -253,19 +244,21 @@ public Response queryResult(String rsURL, String queryId, QueryRequest queryRequ } String pathName = "/query/" + queryId + "/result"; String body = json.writeValueAsString(queryRequest); - HttpResponse resourcesResponse = httpClientUtil.retrievePostResponse( - httpClientUtil.composeURL(rsURL, pathName), createHeaders(queryRequest.getResourceCredentials()), body + resourcesResponse = httpClientUtil.retrievePostResponse( + HttpClientUtil.composeURL(rsURL, pathName), createHeaders(queryRequest.getResourceCredentials()), body ); + + String content = httpClientUtil.readObjectFromResponse(resourcesResponse); if (resourcesResponse.getStatusLine().getStatusCode() != 200) { logger.error("ResourceRS did not return a 200"); - httpClientUtil.throwResponseError(resourcesResponse, rsURL); + HttpClientUtil.throwResponseError(resourcesResponse, rsURL); } - return Response.ok(resourcesResponse.getEntity().getContent()).build(); + return Response.ok(content).build(); } catch (JsonProcessingException e) { logger.error("Unable to encode resource credentials"); throw new NotAuthorizedException("Unable to encode resource credentials", e); - } catch (IOException e) { - throw new ResourceInterfaceException("Error getting results", e); + } finally { + closeHttpResponse(resourcesResponse); } } @@ -290,30 +283,26 @@ public Response queryResultSignedUrl(String rsURL, String queryId, QueryRequest resourcesResponse = httpClientUtil.retrievePostResponse( httpClientUtil.composeURL(rsURL, pathName), createHeaders(queryRequest.getResourceCredentials()), body ); + if (resourcesResponse.getStatusLine().getStatusCode() != 200) { logger.error("ResourceRS did not return a 200"); httpClientUtil.throwResponseError(resourcesResponse, rsURL); } - return Response.ok(resourcesResponse.getEntity().getContent()).build(); + + String content = httpClientUtil.readObjectFromResponse(resourcesResponse); + return Response.ok(content).build(); } catch (JsonProcessingException e) { logger.error("Unable to encode resource credentials"); throw new NotAuthorizedException("Unable to encode resource credentials", e); - } catch (IOException e) { - throw new ResourceInterfaceException("Error getting results", e); } finally { - if (resourcesResponse != null) { - try { - EntityUtils.consume(resourcesResponse.getEntity()); - } catch (IOException e) { - logger.error("Failed to close HttpResponse entity", e); - } - } + closeHttpResponse(resourcesResponse); } } public Response queryFormat(String rsURL, QueryRequest queryRequest) { logger.debug("Calling ResourceWebClient queryFormat()"); + HttpResponse resourcesResponse = null; try { if (queryRequest == null) { throw new ProtocolException(ProtocolException.MISSING_DATA); @@ -326,20 +315,22 @@ public Response queryFormat(String rsURL, QueryRequest queryRequest) { } String pathName = "/query/format"; String body = json.writeValueAsString(queryRequest); - HttpResponse resourcesResponse = httpClientUtil.retrievePostResponse( + resourcesResponse = httpClientUtil.retrievePostResponse( httpClientUtil.composeURL(rsURL, pathName), createHeaders(queryRequest.getResourceCredentials()), body ); + + String content = httpClientUtil.readObjectFromResponse(resourcesResponse); int status = resourcesResponse.getStatusLine().getStatusCode(); if (status != 200) { - logger.error("Query format request did not return a 200: " + resourcesResponse.getStatusLine().getStatusCode()); - return Response.status(status).entity(resourcesResponse.getEntity().getContent()).build(); + logger.error("Query format request did not return a 200: {}", resourcesResponse.getStatusLine().getStatusCode()); + return Response.status(status).entity(content).build(); } - return Response.ok(resourcesResponse.getEntity().getContent()).build(); + return Response.ok(content).build(); } catch (JsonProcessingException e) { logger.error("Unable to encode resource credentials"); throw new NotAuthorizedException("Unable to encode resource credentials", e); - } catch (IOException e) { - throw new ResourceInterfaceException("Error getting results", e); + } finally { + closeHttpResponse(resourcesResponse); } } @@ -377,25 +368,17 @@ public Response querySync(String rsURL, QueryRequest queryRequest, String reques throwError(resourcesResponse, rsURL); } + String content = httpClientUtil.readObjectFromResponse(resourcesResponse); if (resourcesResponse.containsHeader(QUERY_METADATA_FIELD)) { Header metadataHeader = ((Header[]) resourcesResponse.getHeaders(QUERY_METADATA_FIELD))[0]; - return Response.ok(resourcesResponse.getEntity().getContent()).header(QUERY_METADATA_FIELD, metadataHeader.getValue()) - .build(); + return Response.ok(content).header(QUERY_METADATA_FIELD, metadataHeader.getValue()).build(); } - return Response.ok(resourcesResponse.getEntity().getContent()).build(); + return Response.ok(content).build(); } catch (JsonProcessingException e) { logger.error("Unable to encode resource credentials"); throw new NotAuthorizedException("Unable to encode resource credentials", e); - } catch (IOException e) { - throw new ResourceInterfaceException("Error getting results", e); } finally { - if (resourcesResponse != null) { - try { - EntityUtils.consume(resourcesResponse.getEntity()); - } catch (IOException e) { - logger.error("Failed to close HttpResponse entity", e); - } - } + closeHttpResponse(resourcesResponse); } } @@ -437,24 +420,17 @@ public Response queryContinuous(String rsURL, QueryRequest queryRequest, String } logger.debug("Calling ResourceWebClient queryContinuous() with body: " + body + " and headers: " + queryRequest); - resourcesResponse = httpClientUtil.retrievePostResponse(httpClientUtil.composeURL(rsURL, pathName), headers, body); + resourcesResponse = httpClientUtil.retrievePostResponse(HttpClientUtil.composeURL(rsURL, pathName), headers, body); if (resourcesResponse.getStatusLine().getStatusCode() != 200) { throwError(resourcesResponse, rsURL); } - return Response.ok(resourcesResponse.getEntity().getContent()).build(); + String content = httpClientUtil.readObjectFromResponse(resourcesResponse); + return Response.ok(content).build(); } catch (JsonProcessingException e) { throw new RuntimeException(e); - } catch (IOException e) { - throw new ResourceInterfaceException("Error getting results", e); } finally { - if (resourcesResponse != null) { - try { - EntityUtils.consume(resourcesResponse.getEntity()); - } catch (IOException e) { - logger.error("Failed to close HttpResponse entity", e); - } - } + closeHttpResponse(resourcesResponse); } } @@ -467,6 +443,7 @@ private void throwError(HttpResponse response, String baseURL) { errorMessage += "/n" + responseNode.get("message").asText(); } } catch (IOException e) { + logger.error(e.getMessage()); } if (response.getStatusLine().getStatusCode() == 401) { throw new NotAuthorizedException(errorMessage); diff --git a/pic-sure-resources/pic-sure-resource-api/src/test/java/edu/harvard/dbmi/avillach/service/ResourceWebClientTest.java b/pic-sure-resources/pic-sure-resource-api/src/test/java/edu/harvard/dbmi/avillach/service/ResourceWebClientTest.java index be0f51d6..6ed3c094 100644 --- a/pic-sure-resources/pic-sure-resource-api/src/test/java/edu/harvard/dbmi/avillach/service/ResourceWebClientTest.java +++ b/pic-sure-resources/pic-sure-resource-api/src/test/java/edu/harvard/dbmi/avillach/service/ResourceWebClientTest.java @@ -35,7 +35,7 @@ public class ResourceWebClientTest { private final static ObjectMapper json = new ObjectMapper(); private final static String token = "testToken"; private final static int port = 8079; - private final static String testURL = "http://localhost:"+port; + private final static String testURL = "http://localhost:" + port; private final ResourceWebClient cut = new ResourceWebClient(); @Rule @@ -44,23 +44,20 @@ public class ResourceWebClientTest { @BeforeClass public static void beforeClass() { - //Need to be able to throw exceptions without container so we can verify correct errors are being thrown + // Need to be able to throw exceptions without container so we can verify correct errors are being thrown RuntimeDelegate runtimeDelegate = new RuntimeDelegateImpl(); RuntimeDelegate.setInstance(runtimeDelegate); } @Test - public void testInfo() throws JsonProcessingException{ + public void testInfo() throws JsonProcessingException { String resourceInfo = json.writeValueAsString(new ResourceInfo()); - wireMockRule.stubFor(any(urlEqualTo("/info")) - .willReturn(aResponse() - .withStatus(200) - .withBody(resourceInfo))); + wireMockRule.stubFor(any(urlEqualTo("/info")).willReturn(aResponse().withStatus(200).withBody(resourceInfo))); - //Any targetURL that matches /info will trigger wiremock + // Any targetURL that matches /info will trigger wiremock String targetURL = "/info"; - //Should throw an error if any parameters are missing + // Should throw an error if any parameters are missing try { cut.info(testURL, null); fail(); @@ -71,8 +68,8 @@ public void testInfo() throws JsonProcessingException{ Map credentials = new HashMap<>(); credentials.put(ResourceWebClient.BEARER_TOKEN_KEY, token); queryRequest.setResourceCredentials(credentials); -// queryRequest.setTargetURL(targetURL); - //Obviously should fail without the rsURL + // queryRequest.setTargetURL(targetURL); + // Obviously should fail without the rsURL try { cut.info(null, queryRequest); fail(); @@ -80,58 +77,50 @@ public void testInfo() throws JsonProcessingException{ assertEquals(ApplicationException.MISSING_RESOURCE_PATH, e.getContent()); } - //Should fail without a targetURL + // Should fail without a targetURL -// queryRequest.setTargetURL(null); -// try { -// cut.info(testURL, queryRequest); -// fail(); -// } catch (ApplicationException e) { -// assertEquals(ApplicationException.MISSING_TARGET_URL, e.getContent()); -// } + // queryRequest.setTargetURL(null); + // try { + // cut.info(testURL, queryRequest); + // fail(); + // } catch (ApplicationException e) { + // assertEquals(ApplicationException.MISSING_TARGET_URL, e.getContent()); + // } - //Assuming everything goes right -// queryRequest.setTargetURL(targetURL); + // Assuming everything goes right + // queryRequest.setTargetURL(targetURL); ResourceInfo result = cut.info(testURL, queryRequest); assertNotNull("Result should not be null", result); - //What if the resource has a problem? - wireMockRule.stubFor(any(urlEqualTo("/info")) - .willReturn(aResponse() - .withStatus(500))); + // What if the resource has a problem? + wireMockRule.stubFor(any(urlEqualTo("/info")).willReturn(aResponse().withStatus(500))); try { cut.info(testURL, queryRequest); fail(); } catch (Exception e) { - assertTrue( e.getMessage().contains("500 Server Error")); + assertTrue(e.getMessage().contains("500 Server Error")); } - //What if resource returns the wrong type of object for some reason? + // What if resource returns the wrong type of object for some reason? String incorrectResponse = json.writeValueAsString(new SearchResults()); - wireMockRule.stubFor(any(urlEqualTo("/info")) - .willReturn(aResponse() - .withStatus(200) - .withBody(incorrectResponse))); + wireMockRule.stubFor(any(urlEqualTo("/info")).willReturn(aResponse().withStatus(200).withBody(incorrectResponse))); try { cut.info(testURL, queryRequest); fail(); } catch (Exception e) { - assertTrue( e.getMessage().contains("Incorrect object type returned")); + assertTrue(e.getMessage().contains("Incorrect object type returned")); } } @Test - public void testSearch() throws JsonProcessingException{ + public void testSearch() throws JsonProcessingException { String searchResults = json.writeValueAsString(new SearchResults()); - wireMockRule.stubFor(any(urlEqualTo("/search")) - .willReturn(aResponse() - .withStatus(200) - .withBody(searchResults))); + wireMockRule.stubFor(any(urlEqualTo("/search")).willReturn(aResponse().withStatus(200).withBody(searchResults))); - //Should throw an error if any parameters are missing + // Should throw an error if any parameters are missing try { cut.search(testURL, null); fail(); @@ -149,15 +138,15 @@ public void testSearch() throws JsonProcessingException{ request.setQuery("query"); -// try { -// cut.search(testURL, request); -// fail(); -// } catch (ApplicationException e) { -// assertEquals(ApplicationException.MISSING_TARGET_URL, e.getContent()); -// } + // try { + // cut.search(testURL, request); + // fail(); + // } catch (ApplicationException e) { + // assertEquals(ApplicationException.MISSING_TARGET_URL, e.getContent()); + // } String targetURL = "/search"; -// request.setTargetURL(targetURL); + // request.setTargetURL(targetURL); try { cut.search(null, request); @@ -166,84 +155,73 @@ public void testSearch() throws JsonProcessingException{ assertEquals(ApplicationException.MISSING_RESOURCE_PATH, e.getContent()); } -// //Should fail if no credentials given -// request.setQuery("query"); -// request.setTargetURL(targetURL); -// try { -// cut.search(testURL, request); -// fail(); -// } catch (Exception e) { -// assertEquals("HTTP 401 Unauthorized", e.getMessage()); -// } - - //With credentials but not search term - /* Map credentials = new HashMap<>(); - credentials.put(ResourceWebClient.BEARER_TOKEN_KEY, token); - request.setQuery(null); - request.setResourceCredentials(credentials); - try { - cut.search(testURL, request); - fail(); - } catch (ProtocolException e) { - assertEquals(ProtocolException.MISSING_DATA, e.getContent()); - }*/ - - //Should fail with no targetURL + // //Should fail if no credentials given + // request.setQuery("query"); + // request.setTargetURL(targetURL); + // try { + // cut.search(testURL, request); + // fail(); + // } catch (Exception e) { + // assertEquals("HTTP 401 Unauthorized", e.getMessage()); + // } + + // With credentials but not search term + /* + * Map credentials = new HashMap<>(); credentials.put(ResourceWebClient.BEARER_TOKEN_KEY, token); + * request.setQuery(null); request.setResourceCredentials(credentials); try { cut.search(testURL, request); fail(); } catch + * (ProtocolException e) { assertEquals(ProtocolException.MISSING_DATA, e.getContent()); } + */ + + // Should fail with no targetURL Map credentials = new HashMap<>(); credentials.put(ResourceWebClient.BEARER_TOKEN_KEY, token); request.setResourceCredentials(credentials); -// request.setTargetURL(null); + // request.setTargetURL(null); request.setQuery("%blood%"); -// try { -// cut.search(testURL, request); -// fail(); -// } catch (ApplicationException e) { -// assertEquals(ApplicationException.MISSING_TARGET_URL, e.getContent()); -// } - -// request.setTargetURL(targetURL); + // try { + // cut.search(testURL, request); + // fail(); + // } catch (ApplicationException e) { + // assertEquals(ApplicationException.MISSING_TARGET_URL, e.getContent()); + // } + + // request.setTargetURL(targetURL); SearchResults result = cut.search(testURL, request); assertNotNull("Result should not be null", result); - //What if the resource has a problem? - wireMockRule.stubFor(any(urlEqualTo("/search")) - .willReturn(aResponse() - .withStatus(500))); + // What if the resource has a problem? + wireMockRule.stubFor(any(urlEqualTo("/search")).willReturn(aResponse().withStatus(500))); try { cut.search(testURL, request); fail(); } catch (Exception e) { - assertTrue( e.getMessage().contains("500 Server Error")); + assertTrue(e.getMessage().contains("500 Server Error")); } - //What if resource returns the wrong type of object for some reason? + // What if resource returns the wrong type of object for some reason? ResourceInfo incorrectResponse = new ResourceInfo(); incorrectResponse.setName("resource name"); incorrectResponse.setId(new UUID(1L, 1L)); - wireMockRule.stubFor(any(urlEqualTo("/search")) - .willReturn(aResponse() - .withStatus(200) - .withBody(json.writeValueAsString(incorrectResponse)))); + wireMockRule.stubFor( + any(urlEqualTo("/search")).willReturn(aResponse().withStatus(200).withBody(json.writeValueAsString(incorrectResponse))) + ); try { cut.search(testURL, request); fail(); } catch (Exception e) { - assertTrue( e.getMessage().contains("Incorrect object type returned")); + assertTrue(e.getMessage().contains("Incorrect object type returned")); } } @Test - public void testQuery() throws JsonProcessingException{ + public void testQuery() throws JsonProcessingException { String queryResults = json.writeValueAsString(new QueryStatus()); - wireMockRule.stubFor(any(urlEqualTo("/query")) - .willReturn(aResponse() - .withStatus(200) - .withBody(queryResults))); + wireMockRule.stubFor(any(urlEqualTo("/query")).willReturn(aResponse().withStatus(200).withBody(queryResults))); - //Should fail if any parameters are missing + // Should fail if any parameters are missing try { cut.query(testURL, null); fail(); @@ -251,7 +229,7 @@ public void testQuery() throws JsonProcessingException{ assertEquals(ProtocolException.MISSING_DATA, e.getContent()); } GeneralQueryRequest request = new GeneralQueryRequest(); -// request.setTargetURL("/query"); + // request.setTargetURL("/query"); try { cut.query(null, request); @@ -260,148 +238,134 @@ public void testQuery() throws JsonProcessingException{ assertEquals(ApplicationException.MISSING_RESOURCE_PATH, e.getContent()); } - //Should fail if no credentials given -// try { -// cut.query(testURL, request); -// fail(); -// } catch (Exception e) { -// assertEquals("HTTP 401 Unauthorized", e.getMessage()); -// } + // Should fail if no credentials given + // try { + // cut.query(testURL, request); + // fail(); + // } catch (Exception e) { + // assertEquals("HTTP 401 Unauthorized", e.getMessage()); + // } Map credentials = new HashMap<>(); request.setResourceCredentials(credentials); -// request.setTargetURL(null); - //Should fail without a targetURL -// try { -// cut.query(testURL, request); -// fail(); -// } catch (ApplicationException e) { -// assertEquals(ApplicationException.MISSING_TARGET_URL, e.getContent()); -// } - -// request.setTargetURL("/query"); - - //Everything goes correctly + // request.setTargetURL(null); + // Should fail without a targetURL + // try { + // cut.query(testURL, request); + // fail(); + // } catch (ApplicationException e) { + // assertEquals(ApplicationException.MISSING_TARGET_URL, e.getContent()); + // } + + // request.setTargetURL("/query"); + + // Everything goes correctly QueryStatus result = cut.query(testURL, request); assertNotNull("Result should not be null", result); - //What if the resource has a problem? - wireMockRule.stubFor(any(urlEqualTo("/query")) - .willReturn(aResponse() - .withStatus(500))); + // What if the resource has a problem? + wireMockRule.stubFor(any(urlEqualTo("/query")).willReturn(aResponse().withStatus(500))); try { cut.query(testURL, request); fail(); } catch (Exception e) { - assertTrue( e.getMessage().contains("500 Server Error")); + assertTrue(e.getMessage().contains("500 Server Error")); } - //What if resource returns the wrong type of object for some reason? + // What if resource returns the wrong type of object for some reason? ResourceInfo incorrectResponse = new ResourceInfo(); incorrectResponse.setName("resource name"); incorrectResponse.setId(new UUID(1L, 1L)); - wireMockRule.stubFor(any(urlEqualTo("/query")) - .willReturn(aResponse() - .withStatus(200) - .withBody(json.writeValueAsString(incorrectResponse)))); + wireMockRule.stubFor( + any(urlEqualTo("/query")).willReturn(aResponse().withStatus(200).withBody(json.writeValueAsString(incorrectResponse))) + ); try { cut.query(testURL, request); fail(); } catch (Exception e) { - assertTrue( e.getMessage().contains("Incorrect object type returned")); + assertTrue(e.getMessage().contains("Incorrect object type returned")); } } @Test - public void testQueryResult() throws JsonProcessingException{ + public void testQueryResult() throws JsonProcessingException { String testId = "230048"; - String mockResult = "Any old response will work"; + String mockResult = "{}"; - wireMockRule.stubFor(any(urlMatching("/query/.*/result")) - .willReturn(aResponse() - .withStatus(200) - .withBody(mockResult))); + wireMockRule.stubFor(any(urlMatching("/query/.*/result")).willReturn(aResponse().withStatus(200).withBody(mockResult))); - //Should fail if missing any parameters -// try { -// cut.queryResult(testURL, testId, null); -// fail(); -// } catch (ProtocolException e) { -// assertEquals(ProtocolException.MISSING_DATA, e.getContent()); -// } + // Should fail if missing any parameters + // try { + // cut.queryResult(testURL, testId, null); + // fail(); + // } catch (ProtocolException e) { + // assertEquals(ProtocolException.MISSING_DATA, e.getContent()); + // } GeneralQueryRequest queryRequest = new GeneralQueryRequest(); Map credentials = new HashMap<>(); credentials.put(ResourceWebClient.BEARER_TOKEN_KEY, token); queryRequest.setResourceCredentials(credentials); -// String targetURL = "/query/13452134/result"; -//// queryRequest.setTargetURL(targetURL); -// -// try { -// cut.queryResult(testURL, null, queryRequest); -// fail(); -// } catch (ProtocolException e) { -// assertEquals(ProtocolException.MISSING_QUERY_ID, e.getContent()); -// } -// try { -// cut.queryResult(null, testId, queryRequest); -// fail(); -// } catch (ApplicationException e) { -// assertEquals(ApplicationException.MISSING_RESOURCE_PATH, e.getContent()); -// } - -//// queryRequest.setTargetURL(null); -// //Should fail without a targetURL -// try { -// cut.queryResult(testURL, testId, queryRequest); -// fail(); -// } catch (ApplicationException e) { -// assertEquals(ApplicationException.MISSING_TARGET_URL, e.getContent()); -// } -// -//// queryRequest.setTargetURL(targetURL); - - - //Everything should work here - Response result = cut.queryResult(testURL,testId, queryRequest); + // String targetURL = "/query/13452134/result"; + //// queryRequest.setTargetURL(targetURL); + // + // try { + // cut.queryResult(testURL, null, queryRequest); + // fail(); + // } catch (ProtocolException e) { + // assertEquals(ProtocolException.MISSING_QUERY_ID, e.getContent()); + // } + // try { + // cut.queryResult(null, testId, queryRequest); + // fail(); + // } catch (ApplicationException e) { + // assertEquals(ApplicationException.MISSING_RESOURCE_PATH, e.getContent()); + // } + + //// queryRequest.setTargetURL(null); + // //Should fail without a targetURL + // try { + // cut.queryResult(testURL, testId, queryRequest); + // fail(); + // } catch (ApplicationException e) { + // assertEquals(ApplicationException.MISSING_TARGET_URL, e.getContent()); + // } + // + //// queryRequest.setTargetURL(targetURL); + + + // Everything should work here + Response result = cut.queryResult(testURL, testId, queryRequest); assertNotNull("Result should not be null", result); - try { - String resultContent = IOUtils.toString((InputStream) result.getEntity(), "UTF-8"); - assertEquals("Result should match " + mockResult, mockResult, resultContent); - } catch (IOException e ){ - fail("Result content was unreadable"); - } + // String resultContent = IOUtils.toString((InputStream) result.getEntity(), "UTF-8"); + String resultContent = (String) result.getEntity(); + assertEquals("Result should match " + mockResult, mockResult, resultContent); - //What if the resource has a problem? - wireMockRule.stubFor(any(urlMatching("/query/.*/result")) - .willReturn(aResponse() - .withStatus(500))); + // What if the resource has a problem? + wireMockRule.stubFor(any(urlMatching("/query/.*/result")).willReturn(aResponse().withStatus(500))); try { cut.queryResult(testURL, testId, queryRequest); fail(); } catch (Exception e) { - assertTrue( e.getMessage().contains("500 Server Error")); + assertTrue(e.getMessage().contains("500 Server Error")); } } @Test - public void testQueryStatus() throws JsonProcessingException{ + public void testQueryStatus() throws JsonProcessingException { String testId = "230048"; QueryStatus testResult = new QueryStatus(); testResult.setStatus(PicSureStatus.PENDING); testResult.setResourceStatus("RUNNING"); String queryStatus = json.writeValueAsString(testResult); - wireMockRule.stubFor(any(urlMatching("/query/.*/status")) - .willReturn(aResponse() - .withStatus(200) - .withBody(queryStatus))); + wireMockRule.stubFor(any(urlMatching("/query/.*/status")).willReturn(aResponse().withStatus(200).withBody(queryStatus))); - //Fails with any missing parameters + // Fails with any missing parameters try { cut.queryStatus(testURL, testId, null); fail(); @@ -412,8 +376,8 @@ public void testQueryStatus() throws JsonProcessingException{ Map credentials = new HashMap<>(); credentials.put(ResourceWebClient.BEARER_TOKEN_KEY, token); queryRequest.setResourceCredentials(credentials); -// String targetURL = "/query/13452134/result"; -// queryRequest.setTargetURL(targetURL); + // String targetURL = "/query/13452134/result"; + // queryRequest.setTargetURL(targetURL); try { cut.queryStatus(testURL, null, queryRequest); @@ -428,55 +392,52 @@ public void testQueryStatus() throws JsonProcessingException{ assertEquals(ApplicationException.MISSING_RESOURCE_PATH, e.getContent()); } -// queryRequest.setTargetURL(null); + // queryRequest.setTargetURL(null); - //Should fail without a targetURL -// try { -// cut.queryStatus(testURL, testId, queryRequest); -// fail(); -// } catch (ApplicationException e) { -// assertEquals(ApplicationException.MISSING_TARGET_URL, e.getContent()); -// } + // Should fail without a targetURL + // try { + // cut.queryStatus(testURL, testId, queryRequest); + // fail(); + // } catch (ApplicationException e) { + // assertEquals(ApplicationException.MISSING_TARGET_URL, e.getContent()); + // } -// queryRequest.setTargetURL(targetURL); + // queryRequest.setTargetURL(targetURL); - //Everything should work here - QueryStatus result = cut.queryStatus(testURL,testId, queryRequest); + // Everything should work here + QueryStatus result = cut.queryStatus(testURL, testId, queryRequest); assertNotNull("Result should not be null", result); - //Make sure all necessary fields are present - assertNotNull("Duration should not be null",result.getDuration()); - assertNotNull("Expiration should not be null",result.getExpiration()); - assertNotNull("ResourceStatus should not be null",result.getResourceStatus()); - assertNotNull("Status should not be null",result.getStatus()); + // Make sure all necessary fields are present + assertNotNull("Duration should not be null", result.getDuration()); + assertNotNull("Expiration should not be null", result.getExpiration()); + assertNotNull("ResourceStatus should not be null", result.getResourceStatus()); + assertNotNull("Status should not be null", result.getStatus()); - //What if the resource has a problem? - wireMockRule.stubFor(any(urlMatching("/query/.*/status")) - .willReturn(aResponse() - .withStatus(500))); + // What if the resource has a problem? + wireMockRule.stubFor(any(urlMatching("/query/.*/status")).willReturn(aResponse().withStatus(500))); try { cut.queryStatus(testURL, testId, queryRequest); fail(); } catch (Exception e) { - assertTrue( e.getMessage().contains("500 Server Error")); + assertTrue(e.getMessage().contains("500 Server Error")); } - //What if resource returns the wrong type of object for some reason? + // What if resource returns the wrong type of object for some reason? ResourceInfo incorrect = new ResourceInfo(); incorrect.setName("resource name"); incorrect.setId(new UUID(1L, 1L)); - wireMockRule.stubFor(any(urlMatching("/query/.*/status")) - .willReturn(aResponse() - .withStatus(200) - .withBody(json.writeValueAsString(incorrect)))); + wireMockRule.stubFor( + any(urlMatching("/query/.*/status")).willReturn(aResponse().withStatus(200).withBody(json.writeValueAsString(incorrect))) + ); try { cut.queryStatus(testURL, testId, queryRequest); fail(); } catch (Exception e) { - assertTrue( e.getMessage().contains("Incorrect object type returned")); + assertTrue(e.getMessage().contains("Incorrect object type returned")); } } }