diff --git a/pic-sure-resources/pic-sure-passthrough-resource/pom.xml b/pic-sure-resources/pic-sure-passthrough-resource/pom.xml index 6597de44..c0a6e250 100644 --- a/pic-sure-resources/pic-sure-passthrough-resource/pom.xml +++ b/pic-sure-resources/pic-sure-passthrough-resource/pom.xml @@ -47,10 +47,6 @@ org.apache.commons commons-lang3 - - com.google.guava - guava - org.junit.jupiter junit-jupiter diff --git a/pic-sure-resources/pic-sure-passthrough-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/passthru/PassThroughResourceRSTest.java b/pic-sure-resources/pic-sure-passthrough-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/passthru/PassThroughResourceRSTest.java index 24d68c50..1c8daf44 100644 --- a/pic-sure-resources/pic-sure-passthrough-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/passthru/PassThroughResourceRSTest.java +++ b/pic-sure-resources/pic-sure-passthrough-resource/src/test/java/edu/harvard/hms/dbmi/avillach/resource/passthru/PassThroughResourceRSTest.java @@ -10,6 +10,7 @@ import java.io.InputStream; import java.nio.charset.StandardCharsets; +import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.UUID; @@ -28,7 +29,6 @@ import org.mockito.junit.jupiter.MockitoExtension; import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.common.collect.ImmutableList; import edu.harvard.dbmi.avillach.domain.QueryFormat; import edu.harvard.dbmi.avillach.domain.GeneralQueryRequest; @@ -42,304 +42,307 @@ @ExtendWith(MockitoExtension.class) class PassThroughResourceRSTest { - HttpClientUtil httpClient; - PassThroughResourceRS resource; - ObjectMapper objectMapper = new ObjectMapper(); - - @BeforeEach - void init() { - ApplicationProperties appProperties = mock(ApplicationProperties.class); - lenient().when(appProperties.getTargetPicsureToken()).thenReturn("/tmp/unit_test"); - lenient().when(appProperties.getTargetPicsureUrl()).thenReturn("http://test"); - lenient().when(appProperties.getTargetResourceId()).thenReturn(UUID.randomUUID().toString()); - - httpClient = mock(HttpClientUtil.class); - - resource = new PassThroughResourceRS(appProperties, httpClient); - } - - @Test - void testInfo() throws Exception { - // setup http response mocks - HttpResponse httpResponse = mock(HttpResponse.class); - StringEntity httpResponseEntity = new StringEntity(""); - StatusLine statusLine = mock(StatusLine.class); - when(httpResponse.getStatusLine()).thenReturn(statusLine); - when(httpResponse.getEntity()).thenReturn(httpResponseEntity); - when(httpClient.retrievePostResponse(anyString(), any(Header[].class), anyString())).thenReturn(httpResponse); - - when(statusLine.getStatusCode()).thenReturn(500); - assertThrows(ResourceInterfaceException.class, () -> { - resource.info(new GeneralQueryRequest()); - }, "Downstream Resource returned 500 and should cause 'ResourceInterfaceException'"); - - when(statusLine.getStatusCode()).thenReturn(401); - assertThrows(ResourceInterfaceException.class, () -> { - resource.info(new GeneralQueryRequest()); - }, "Downstream Resource returned 401 and should cause 'ResourceInterfaceException'"); - - when(statusLine.getStatusCode()).thenReturn(200); - ResourceInfo resourceInfo = newResourceInfo(); - httpResponseEntity = new StringEntity(new String(objectMapper.writeValueAsBytes(resourceInfo))); - when(httpResponse.getEntity()).thenReturn(httpResponseEntity); - ResourceInfo returnVal = resource.info(new GeneralQueryRequest()); - assertEquals(resourceInfo.getId(), returnVal.getId(), "Downstream ResourceInfo Id should match output"); - assertEquals(resourceInfo.getName(), returnVal.getName(), "Downstream ResourceInfo Name should match output"); - assertEquals(resourceInfo.getQueryFormats().size(), returnVal.getQueryFormats().size(), - "Downstream ResourceInfo QueryFormats should match output"); - } - - @Test - void testQuery() throws Exception { - // setup http response mocks - HttpResponse httpResponse = mock(HttpResponse.class); - StringEntity httpResponseEntity = new StringEntity(""); - StatusLine statusLine = mock(StatusLine.class); - when(statusLine.getStatusCode()).thenReturn(200); - when(httpResponse.getStatusLine()).thenReturn(statusLine); - when(httpResponse.getEntity()).thenReturn(httpResponseEntity); - when(httpClient.retrievePostResponse(anyString(), any(Header[].class), anyString())).thenReturn(httpResponse); - - assertThrows(ProtocolException.class, () -> { - resource.query(null); - }, "QueryRequest is required"); - - assertThrows(ProtocolException.class, () -> { - resource.query(new GeneralQueryRequest()); - }, "Query is required"); - - when(statusLine.getStatusCode()).thenReturn(500); - assertThrows(ResourceInterfaceException.class, () -> { - resource.query(newQueryRequest("test")); - }, "Downstream Resource returned 500 and should cause 'ResourceInterfaceException'"); - - when(statusLine.getStatusCode()).thenReturn(401); - assertThrows(ResourceInterfaceException.class, () -> { - resource.query(newQueryRequest("test")); - }, "Downstream Resource returned 401 and should cause 'ResourceInterfaceException'"); - - when(statusLine.getStatusCode()).thenReturn(200); - QueryStatus queryStatus = newQueryStatus(null); - httpResponseEntity = new StringEntity(new String(objectMapper.writeValueAsBytes(queryStatus))); - when(httpResponse.getEntity()).thenReturn(httpResponseEntity); - GeneralQueryRequest queryRequest = newQueryRequest("test"); - QueryStatus returnVal = resource.query(queryRequest); - assertEquals(queryRequest.getResourceUUID(), returnVal.getResourceID()); - } - - @Test - void testQueryResult() throws Exception { - // setup http response mocks - HttpResponse httpResponse = mock(HttpResponse.class); - StringEntity httpResponseEntity = new StringEntity(""); - StatusLine statusLine = mock(StatusLine.class); - when(statusLine.getStatusCode()).thenReturn(200); - when(httpResponse.getStatusLine()).thenReturn(statusLine); - when(httpResponse.getEntity()).thenReturn(httpResponseEntity); - when(httpClient.retrievePostResponse(anyString(), any(Header[].class), anyString())).thenReturn(httpResponse); - when(httpClient.readObjectFromResponse(any(HttpResponse.class))).thenReturn("4"); - - assertThrows(ProtocolException.class, () -> { - resource.queryResult("", null); - }, "QueryID is required"); - - assertThrows(ProtocolException.class, () -> { - resource.queryResult(UUID.randomUUID().toString(), null); - }, "QueryRequest is required"); - - when(statusLine.getStatusCode()).thenReturn(500); - assertThrows(ResourceInterfaceException.class, () -> { - resource.queryResult(UUID.randomUUID().toString(), newQueryRequest(null)); - }, "Downstream Resource returned 500 and should cause 'ResourceInterfaceException'"); - - when(statusLine.getStatusCode()).thenReturn(401); - assertThrows(ResourceInterfaceException.class, () -> { - resource.queryResult(UUID.randomUUID().toString(), newQueryRequest(null)); - }, "Downstream Resource returned 401 and should cause 'ResourceInterfaceException'"); - - when(statusLine.getStatusCode()).thenReturn(200); - String resultId = UUID.randomUUID().toString(); - UUID queryId = UUID.randomUUID(); - lenient().when(httpResponse.getFirstHeader("resultId")).thenReturn(newHeader("resultId", resultId)); - httpResponseEntity = new StringEntity("4"); - when(httpResponse.getEntity()).thenReturn(httpResponseEntity); - GeneralQueryRequest queryRequest = newQueryRequest(null); - javax.ws.rs.core.Response returnVal = resource.queryResult(queryId.toString(), queryRequest); - assertEquals("4", returnVal.getEntity()); - //assertEquals(resultId, returnVal.getHeaderString("resultId")); - } - - @Test - void testQueryStatus() throws Exception { - // setup http response mocks - HttpResponse httpResponse = mock(HttpResponse.class); - StringEntity httpResponseEntity = new StringEntity(""); - StatusLine statusLine = mock(StatusLine.class); - when(statusLine.getStatusCode()).thenReturn(200); - when(httpResponse.getStatusLine()).thenReturn(statusLine); - when(httpResponse.getEntity()).thenReturn(httpResponseEntity); - when(httpClient.retrievePostResponse(anyString(), any(Header[].class), anyString())).thenReturn(httpResponse); - - assertThrows(ProtocolException.class, () -> { - resource.queryStatus("", null); - }, "QueryID is required"); - - assertThrows(ProtocolException.class, () -> { - resource.queryStatus(UUID.randomUUID().toString(), null); - }, "QueryRequest is required"); - - when(statusLine.getStatusCode()).thenReturn(500); - assertThrows(ResourceInterfaceException.class, () -> { - resource.queryStatus(UUID.randomUUID().toString(), newQueryRequest(null)); - }, "Downstream Resource returned 500 and should cause 'ResourceInterfaceException'"); - - when(statusLine.getStatusCode()).thenReturn(401); - assertThrows(ResourceInterfaceException.class, () -> { - resource.queryStatus(UUID.randomUUID().toString(), newQueryRequest(null)); - }, "Downstream Resource returned 401 and should cause 'ResourceInterfaceException'"); - - when(statusLine.getStatusCode()).thenReturn(200); - UUID queryId = UUID.randomUUID(); - QueryStatus queryStatus = newQueryStatus(queryId); - httpResponseEntity = new StringEntity(new String(objectMapper.writeValueAsBytes(queryStatus))); - when(httpResponse.getEntity()).thenReturn(httpResponseEntity); - GeneralQueryRequest queryRequest = newQueryRequest(null); - QueryStatus returnVal = resource.queryStatus(queryId.toString(), queryRequest); - assertEquals(queryId, returnVal.getPicsureResultId()); - assertEquals(queryStatus.getStatus(), returnVal.getStatus()); - assertEquals(queryStatus.getResourceStatus(), returnVal.getResourceStatus()); - } - - @Test - void testQuerySync() throws Exception { - // setup http response mocks - HttpResponse httpResponse = mock(HttpResponse.class); - StringEntity httpResponseEntity = new StringEntity(""); - StatusLine statusLine = mock(StatusLine.class); - when(statusLine.getStatusCode()).thenReturn(200); - when(httpResponse.getStatusLine()).thenReturn(statusLine); - when(httpResponse.getEntity()).thenReturn(httpResponseEntity); - when(httpClient.retrievePostResponse(anyString(), any(Header[].class), anyString())).thenReturn(httpResponse); - - assertThrows(ProtocolException.class, () -> { - resource.querySync(null); - }, "QueryRequest is required"); - - assertThrows(ProtocolException.class, () -> { - resource.querySync(new GeneralQueryRequest()); - }, "Query is required"); - - when(statusLine.getStatusCode()).thenReturn(500); - assertThrows(ResourceInterfaceException.class, () -> { - resource.querySync(newQueryRequest("test")); - }, "Downstream Resource returned 500 and should cause 'ResourceInterfaceException'"); - - when(statusLine.getStatusCode()).thenReturn(401); - assertThrows(ResourceInterfaceException.class, () -> { - resource.querySync(newQueryRequest("test")); - }, "Downstream Resource returned 401 and should cause 'ResourceInterfaceException'"); - - when(statusLine.getStatusCode()).thenReturn(200); - String resultId = UUID.randomUUID().toString(); - lenient().when(httpResponse.getFirstHeader("resultId")).thenReturn(newHeader("resultId", resultId)); - httpResponseEntity = new StringEntity("4"); - when(httpResponse.getEntity()).thenReturn(httpResponseEntity); - GeneralQueryRequest queryRequest = newQueryRequest(newQuery()); - javax.ws.rs.core.Response returnVal = resource.querySync(queryRequest); - assertEquals("4", IOUtils.toString((InputStream) returnVal.getEntity(), StandardCharsets.UTF_8)); - //assertEquals(resultId, returnVal.getHeaderString("resultId")); - } - - @Test - void testSearch() throws Exception { - // setup http response mocks - HttpResponse httpResponse = mock(HttpResponse.class); - StringEntity httpResponseEntity = new StringEntity(""); - StatusLine statusLine = mock(StatusLine.class); - when(statusLine.getStatusCode()).thenReturn(200); - when(httpResponse.getStatusLine()).thenReturn(statusLine); - when(httpResponse.getEntity()).thenReturn(httpResponseEntity); - when(httpClient.retrievePostResponse(anyString(), any(Header[].class), anyString())).thenReturn(httpResponse); - - assertThrows(ProtocolException.class, () -> { - resource.search(null); - }, "QueryRequest is required"); - - assertThrows(ProtocolException.class, () -> { - resource.search(new GeneralQueryRequest()); - }, "Query is required"); - - when(statusLine.getStatusCode()).thenReturn(500); - assertThrows(ResourceInterfaceException.class, () -> { - resource.search(newQueryRequest("test")); - }, "Downstream Resource returned 500 and should cause 'ResourceInterfaceException'"); - - when(statusLine.getStatusCode()).thenReturn(401); - assertThrows(ResourceInterfaceException.class, () -> { - resource.search(newQueryRequest("test")); - }, "Downstream Resource returned 401 and should cause 'ResourceInterfaceException'"); - - String queryText = "test"; - when(statusLine.getStatusCode()).thenReturn(200); - SearchResults searchResults = new SearchResults(); - searchResults.setSearchQuery(queryText); - httpResponseEntity = new StringEntity(new String(objectMapper.writeValueAsBytes(searchResults))); - when(httpResponse.getEntity()).thenReturn(httpResponseEntity); - GeneralQueryRequest queryRequest = newQueryRequest(queryText); - SearchResults returnVal = resource.search(queryRequest); - assertEquals(queryText, returnVal.getSearchQuery()); - } - - private ResourceInfo newResourceInfo() { - ResourceInfo info = new ResourceInfo(); - info.setName("PhenoCube v1.0-SNAPSHOT"); - info.setId(UUID.randomUUID()); - info.setQueryFormats(ImmutableList - .of(new QueryFormat().setDescription("PhenoCube Query Format").setName("PhenoCube Query Format"))); - return info; - } - - private QueryStatus newQueryStatus(UUID picsureResultId) { - QueryStatus status = new QueryStatus(); - status.setStatus(PicSureStatus.QUEUED); - status.setResourceID(UUID.randomUUID()); - status.setResourceStatus("PENDING"); - status.setPicsureResultId(picsureResultId); - status.setResourceResultId(UUID.randomUUID().toString()); - return status; - } - - private GeneralQueryRequest newQueryRequest(Object query) { - GeneralQueryRequest request = new GeneralQueryRequest(); - request.setResourceUUID(UUID.randomUUID()); - request.setQuery(query); - return request; - } - - // replace with Map - private Map newQuery() { - Map queryMap = new HashMap<>(); - queryMap.put("requiredFields", "\\TEST"); - queryMap.put("expectedResultType", "COUNT"); - return queryMap; - } - - private Header newHeader(final String name, final String value) { - return new Header() { - @Override - public String getValue() { - return value; - } - - @Override - public String getName() { - return name; - } - - @Override - public HeaderElement[] getElements() throws ParseException { - return null; - } - }; - } + HttpClientUtil httpClient; + PassThroughResourceRS resource; + ObjectMapper objectMapper = new ObjectMapper(); + + @BeforeEach + void init() { + ApplicationProperties appProperties = mock(ApplicationProperties.class); + lenient().when(appProperties.getTargetPicsureToken()).thenReturn("/tmp/unit_test"); + lenient().when(appProperties.getTargetPicsureUrl()).thenReturn("http://test"); + lenient().when(appProperties.getTargetResourceId()).thenReturn(UUID.randomUUID().toString()); + + httpClient = mock(HttpClientUtil.class); + + resource = new PassThroughResourceRS(appProperties, httpClient); + } + + @Test + void testInfo() throws Exception { + // setup http response mocks + HttpResponse httpResponse = mock(HttpResponse.class); + StringEntity httpResponseEntity = new StringEntity(""); + StatusLine statusLine = mock(StatusLine.class); + when(httpResponse.getStatusLine()).thenReturn(statusLine); + when(httpResponse.getEntity()).thenReturn(httpResponseEntity); + when(httpClient.retrievePostResponse(anyString(), any(Header[].class), anyString())).thenReturn(httpResponse); + + when(statusLine.getStatusCode()).thenReturn(500); + assertThrows(ResourceInterfaceException.class, () -> { + resource.info(new GeneralQueryRequest()); + }, "Downstream Resource returned 500 and should cause 'ResourceInterfaceException'"); + + when(statusLine.getStatusCode()).thenReturn(401); + assertThrows(ResourceInterfaceException.class, () -> { + resource.info(new GeneralQueryRequest()); + }, "Downstream Resource returned 401 and should cause 'ResourceInterfaceException'"); + + when(statusLine.getStatusCode()).thenReturn(200); + ResourceInfo resourceInfo = newResourceInfo(); + httpResponseEntity = new StringEntity(new String(objectMapper.writeValueAsBytes(resourceInfo))); + when(httpResponse.getEntity()).thenReturn(httpResponseEntity); + ResourceInfo returnVal = resource.info(new GeneralQueryRequest()); + assertEquals(resourceInfo.getId(), returnVal.getId(), "Downstream ResourceInfo Id should match output"); + assertEquals(resourceInfo.getName(), returnVal.getName(), "Downstream ResourceInfo Name should match output"); + assertEquals( + resourceInfo.getQueryFormats().size(), returnVal.getQueryFormats().size(), + "Downstream ResourceInfo QueryFormats should match output" + ); + } + + @Test + void testQuery() throws Exception { + // setup http response mocks + HttpResponse httpResponse = mock(HttpResponse.class); + StringEntity httpResponseEntity = new StringEntity(""); + StatusLine statusLine = mock(StatusLine.class); + when(statusLine.getStatusCode()).thenReturn(200); + when(httpResponse.getStatusLine()).thenReturn(statusLine); + when(httpResponse.getEntity()).thenReturn(httpResponseEntity); + when(httpClient.retrievePostResponse(anyString(), any(Header[].class), anyString())).thenReturn(httpResponse); + + assertThrows(ProtocolException.class, () -> { + resource.query(null); + }, "QueryRequest is required"); + + assertThrows(ProtocolException.class, () -> { + resource.query(new GeneralQueryRequest()); + }, "Query is required"); + + when(statusLine.getStatusCode()).thenReturn(500); + assertThrows(ResourceInterfaceException.class, () -> { + resource.query(newQueryRequest("test")); + }, "Downstream Resource returned 500 and should cause 'ResourceInterfaceException'"); + + when(statusLine.getStatusCode()).thenReturn(401); + assertThrows(ResourceInterfaceException.class, () -> { + resource.query(newQueryRequest("test")); + }, "Downstream Resource returned 401 and should cause 'ResourceInterfaceException'"); + + when(statusLine.getStatusCode()).thenReturn(200); + QueryStatus queryStatus = newQueryStatus(null); + httpResponseEntity = new StringEntity(new String(objectMapper.writeValueAsBytes(queryStatus))); + when(httpResponse.getEntity()).thenReturn(httpResponseEntity); + GeneralQueryRequest queryRequest = newQueryRequest("test"); + QueryStatus returnVal = resource.query(queryRequest); + assertEquals(queryRequest.getResourceUUID(), returnVal.getResourceID()); + } + + @Test + void testQueryResult() throws Exception { + // setup http response mocks + HttpResponse httpResponse = mock(HttpResponse.class); + StringEntity httpResponseEntity = new StringEntity(""); + StatusLine statusLine = mock(StatusLine.class); + when(statusLine.getStatusCode()).thenReturn(200); + when(httpResponse.getStatusLine()).thenReturn(statusLine); + when(httpResponse.getEntity()).thenReturn(httpResponseEntity); + when(httpClient.retrievePostResponse(anyString(), any(Header[].class), anyString())).thenReturn(httpResponse); + when(httpClient.readObjectFromResponse(any(HttpResponse.class))).thenReturn("4"); + + assertThrows(ProtocolException.class, () -> { + resource.queryResult("", null); + }, "QueryID is required"); + + assertThrows(ProtocolException.class, () -> { + resource.queryResult(UUID.randomUUID().toString(), null); + }, "QueryRequest is required"); + + when(statusLine.getStatusCode()).thenReturn(500); + assertThrows(ResourceInterfaceException.class, () -> { + resource.queryResult(UUID.randomUUID().toString(), newQueryRequest(null)); + }, "Downstream Resource returned 500 and should cause 'ResourceInterfaceException'"); + + when(statusLine.getStatusCode()).thenReturn(401); + assertThrows(ResourceInterfaceException.class, () -> { + resource.queryResult(UUID.randomUUID().toString(), newQueryRequest(null)); + }, "Downstream Resource returned 401 and should cause 'ResourceInterfaceException'"); + + when(statusLine.getStatusCode()).thenReturn(200); + String resultId = UUID.randomUUID().toString(); + UUID queryId = UUID.randomUUID(); + lenient().when(httpResponse.getFirstHeader("resultId")).thenReturn(newHeader("resultId", resultId)); + httpResponseEntity = new StringEntity("4"); + when(httpResponse.getEntity()).thenReturn(httpResponseEntity); + GeneralQueryRequest queryRequest = newQueryRequest(null); + javax.ws.rs.core.Response returnVal = resource.queryResult(queryId.toString(), queryRequest); + assertEquals("4", returnVal.getEntity()); + // assertEquals(resultId, returnVal.getHeaderString("resultId")); + } + + @Test + void testQueryStatus() throws Exception { + // setup http response mocks + HttpResponse httpResponse = mock(HttpResponse.class); + StringEntity httpResponseEntity = new StringEntity(""); + StatusLine statusLine = mock(StatusLine.class); + when(statusLine.getStatusCode()).thenReturn(200); + when(httpResponse.getStatusLine()).thenReturn(statusLine); + when(httpResponse.getEntity()).thenReturn(httpResponseEntity); + when(httpClient.retrievePostResponse(anyString(), any(Header[].class), anyString())).thenReturn(httpResponse); + + assertThrows(ProtocolException.class, () -> { + resource.queryStatus("", null); + }, "QueryID is required"); + + assertThrows(ProtocolException.class, () -> { + resource.queryStatus(UUID.randomUUID().toString(), null); + }, "QueryRequest is required"); + + when(statusLine.getStatusCode()).thenReturn(500); + assertThrows(ResourceInterfaceException.class, () -> { + resource.queryStatus(UUID.randomUUID().toString(), newQueryRequest(null)); + }, "Downstream Resource returned 500 and should cause 'ResourceInterfaceException'"); + + when(statusLine.getStatusCode()).thenReturn(401); + assertThrows(ResourceInterfaceException.class, () -> { + resource.queryStatus(UUID.randomUUID().toString(), newQueryRequest(null)); + }, "Downstream Resource returned 401 and should cause 'ResourceInterfaceException'"); + + when(statusLine.getStatusCode()).thenReturn(200); + UUID queryId = UUID.randomUUID(); + QueryStatus queryStatus = newQueryStatus(queryId); + httpResponseEntity = new StringEntity(new String(objectMapper.writeValueAsBytes(queryStatus))); + when(httpResponse.getEntity()).thenReturn(httpResponseEntity); + GeneralQueryRequest queryRequest = newQueryRequest(null); + QueryStatus returnVal = resource.queryStatus(queryId.toString(), queryRequest); + assertEquals(queryId, returnVal.getPicsureResultId()); + assertEquals(queryStatus.getStatus(), returnVal.getStatus()); + assertEquals(queryStatus.getResourceStatus(), returnVal.getResourceStatus()); + } + + @Test + void testQuerySync() throws Exception { + // setup http response mocks + HttpResponse httpResponse = mock(HttpResponse.class); + StringEntity httpResponseEntity = new StringEntity(""); + StatusLine statusLine = mock(StatusLine.class); + when(statusLine.getStatusCode()).thenReturn(200); + when(httpResponse.getStatusLine()).thenReturn(statusLine); + when(httpResponse.getEntity()).thenReturn(httpResponseEntity); + when(httpClient.retrievePostResponse(anyString(), any(Header[].class), anyString())).thenReturn(httpResponse); + + assertThrows(ProtocolException.class, () -> { + resource.querySync(null); + }, "QueryRequest is required"); + + assertThrows(ProtocolException.class, () -> { + resource.querySync(new GeneralQueryRequest()); + }, "Query is required"); + + when(statusLine.getStatusCode()).thenReturn(500); + assertThrows(ResourceInterfaceException.class, () -> { + resource.querySync(newQueryRequest("test")); + }, "Downstream Resource returned 500 and should cause 'ResourceInterfaceException'"); + + when(statusLine.getStatusCode()).thenReturn(401); + assertThrows(ResourceInterfaceException.class, () -> { + resource.querySync(newQueryRequest("test")); + }, "Downstream Resource returned 401 and should cause 'ResourceInterfaceException'"); + + when(statusLine.getStatusCode()).thenReturn(200); + String resultId = UUID.randomUUID().toString(); + lenient().when(httpResponse.getFirstHeader("resultId")).thenReturn(newHeader("resultId", resultId)); + httpResponseEntity = new StringEntity("4"); + when(httpResponse.getEntity()).thenReturn(httpResponseEntity); + GeneralQueryRequest queryRequest = newQueryRequest(newQuery()); + javax.ws.rs.core.Response returnVal = resource.querySync(queryRequest); + assertEquals("4", IOUtils.toString((InputStream) returnVal.getEntity(), StandardCharsets.UTF_8)); + // assertEquals(resultId, returnVal.getHeaderString("resultId")); + } + + @Test + void testSearch() throws Exception { + // setup http response mocks + HttpResponse httpResponse = mock(HttpResponse.class); + StringEntity httpResponseEntity = new StringEntity(""); + StatusLine statusLine = mock(StatusLine.class); + when(statusLine.getStatusCode()).thenReturn(200); + when(httpResponse.getStatusLine()).thenReturn(statusLine); + when(httpResponse.getEntity()).thenReturn(httpResponseEntity); + when(httpClient.retrievePostResponse(anyString(), any(Header[].class), anyString())).thenReturn(httpResponse); + + assertThrows(ProtocolException.class, () -> { + resource.search(null); + }, "QueryRequest is required"); + + assertThrows(ProtocolException.class, () -> { + resource.search(new GeneralQueryRequest()); + }, "Query is required"); + + when(statusLine.getStatusCode()).thenReturn(500); + assertThrows(ResourceInterfaceException.class, () -> { + resource.search(newQueryRequest("test")); + }, "Downstream Resource returned 500 and should cause 'ResourceInterfaceException'"); + + when(statusLine.getStatusCode()).thenReturn(401); + assertThrows(ResourceInterfaceException.class, () -> { + resource.search(newQueryRequest("test")); + }, "Downstream Resource returned 401 and should cause 'ResourceInterfaceException'"); + + String queryText = "test"; + when(statusLine.getStatusCode()).thenReturn(200); + SearchResults searchResults = new SearchResults(); + searchResults.setSearchQuery(queryText); + httpResponseEntity = new StringEntity(new String(objectMapper.writeValueAsBytes(searchResults))); + when(httpResponse.getEntity()).thenReturn(httpResponseEntity); + GeneralQueryRequest queryRequest = newQueryRequest(queryText); + SearchResults returnVal = resource.search(queryRequest); + assertEquals(queryText, returnVal.getSearchQuery()); + } + + private ResourceInfo newResourceInfo() { + ResourceInfo info = new ResourceInfo(); + info.setName("PhenoCube v1.0-SNAPSHOT"); + info.setId(UUID.randomUUID()); + info.setQueryFormats( + Collections.singletonList(new QueryFormat().setDescription("PhenoCube Query Format").setName("PhenoCube Query Format")) + ); + return info; + } + + private QueryStatus newQueryStatus(UUID picsureResultId) { + QueryStatus status = new QueryStatus(); + status.setStatus(PicSureStatus.QUEUED); + status.setResourceID(UUID.randomUUID()); + status.setResourceStatus("PENDING"); + status.setPicsureResultId(picsureResultId); + status.setResourceResultId(UUID.randomUUID().toString()); + return status; + } + + private GeneralQueryRequest newQueryRequest(Object query) { + GeneralQueryRequest request = new GeneralQueryRequest(); + request.setResourceUUID(UUID.randomUUID()); + request.setQuery(query); + return request; + } + + // replace with Map + private Map newQuery() { + Map queryMap = new HashMap<>(); + queryMap.put("requiredFields", "\\TEST"); + queryMap.put("expectedResultType", "COUNT"); + return queryMap; + } + + private Header newHeader(final String name, final String value) { + return new Header() { + @Override + public String getValue() { + return value; + } + + @Override + public String getName() { + return name; + } + + @Override + public HeaderElement[] getElements() throws ParseException { + return null; + } + }; + } } diff --git a/pic-sure-resources/pic-sure-visualization-resource/pom.xml b/pic-sure-resources/pic-sure-visualization-resource/pom.xml index c118a5e7..a3261db1 100644 --- a/pic-sure-resources/pic-sure-visualization-resource/pom.xml +++ b/pic-sure-resources/pic-sure-visualization-resource/pom.xml @@ -98,6 +98,22 @@ ${project.artifactId} + + org.apache.maven.plugins + maven-compiler-plugin + 3.10.1 + + 11 + 11 + + + org.projectlombok + lombok + 1.18.30 + + + + org.apache.maven.plugins maven-war-plugin diff --git a/pom.xml b/pom.xml index bce8d568..2a2f871c 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,6 @@ 2.16.0 - 32.1.3-jre 5.6.2 3.5.10 3.5.10 @@ -255,11 +254,6 @@ 2.27.2 test - - com.google.guava - guava - ${guava.version} - org.junit.jupiter junit-jupiter