diff --git a/pic-sure-api-wildfly/pom.xml b/pic-sure-api-wildfly/pom.xml deleted file mode 100644 index 6d0e0b58..00000000 --- a/pic-sure-api-wildfly/pom.xml +++ /dev/null @@ -1,390 +0,0 @@ - - - 4.0.0 - - edu.harvard.hms.dbmi.avillach - pic-sure-api - 2.0.0-SNAPSHOT - - pic-sure-api-wildfly - war - pic-sure-api-wildfly - http://maven.apache.org - - UTF-8 - 17.0.0.Final - - ${project.build.directory}/wildfly-${wildfly.version} - - - - edu.harvard.hms.dbmi.avillach - pic-sure-api-data - ${project.version} - - - edu.harvard.hms.dbmi.avillach - pic-sure-passthrough-resource - classes - ${project.version} - - - edu.harvard.hms.dbmi.avillach - pic-sure-aggregate-data-sharing-resource - classes - ${project.version} - - - edu.harvard.hms.dbmi.avillach - pic-sure-resource-api - ${project.version} - - - edu.harvard.hms.dbmi.avillach - pic-sure-api-war - ${project.version} - war - - - edu.harvard.hms.dbmi.avillach - pic-sure-util - ${project.version} - - - javax - javaee-api - provided - - - javax.xml.bind - jaxb-api - - - com.sun.xml.bind - jaxb-core - - - com.sun.xml.bind - jaxb-impl - - - io.jsonwebtoken - jjwt - test - - - org.hibernate.javax.persistence - hibernate-jpa-2.1-api - - - com.github.tomakehurst - wiremock-standalone - - - org.mockito - mockito-core - test - - - - ${project.artifactId}-${project.version} - - - org.apache.maven.plugins - maven-war-plugin - - false - - - - maven-clean-plugin - 3.0.0 - - false - - - - target/ - false - - **/wildfly-*/** - - - - - - - org.apache.maven.plugins - maven-dependency-plugin - - - unpack-wildfly - generate-sources - - unpack - - false - - - - org.wildfly - wildfly-dist - ${wildfly.version} - zip - false - ${project.build.directory} - - - - - - retrieve-mysql-driver - generate-sources - - copy - - false - - - - com.mysql - mysql-connector-j - 8.0.32 - jar - true - ${jboss.home}/modules/system/layers/base/com/sql/mysql/main/ - - - edu.harvard.hms.dbmi.avillach - pic-sure-hsapi-resource - ${project.version} - war - true - ${jboss.home}/standalone/deployments - - - - - - - - - - - edu.harvard.hms.dbmi.avillach - pic-sure-passthrough-resource - ${project.version} - war - true - ${jboss.home}/standalone/deployments - - - edu.harvard.hms.dbmi.avillach - pic-sure-aggregate-data-sharing-resource - ${project.version} - war - true - ${jboss.home}/standalone/deployments - - - - - - - - org.apache.maven.plugins - maven-resources-plugin - 3.0.2 - - - copy-standalone-xml - generate-sources - - copy-resources - - - ${jboss.home}/standalone/configuration - - - src/main/resources/wildflyConfiguration - - - - - - copy-module-xml - generate-sources - - copy-resources - - - ${jboss.home}/modules/ - - - src/main/resources/modules/ - - - - - - - - org.wildfly.plugins - wildfly-maven-plugin - 2.0.1.Final - - 9990 - ${jboss.home} - - -Xmx1024m - -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - wildfly-run - pre-integration-test - - start - deploy - - - - - - - http://localhost:8079 - http://localhost:8079 - http://localhost:8079 - http://localhost:8079 - Patient Id - http://localhost:8080/pic-sure-irct-resource-${project.version}/pic-sure/v1.4 - http://localhost:8080/pic-sure-aggregate-resource-${project.version}/pic-sure/group - http://localhost:8080/pic-sure-gnome-i2b2-count-resource-${project.version}/pic-sure/gnome-i2b2-count - http://localhost:8080/${project.artifactId}-${project.version}/pic-sure - JSON - foo - email - - - - - - wildfly-stop - post-integration-test - - undeploy - shutdown - - - - - - - - - org.apache.maven.plugins - maven-failsafe-plugin - 2.22.0 - - - --illegal-access=permit - - - - - integration-test - - integration-test - - - - - foo - email - http://localhost:8079 - http://localhost:8079 - http://localhost:8079 - Patient Id - http://localhost:8080/${project.artifactId}-${project.version}/pic-sure - - - - http://localhost:8080/${project.artifactId}-${project.version}/pic-sure - http://localhost:8080/${project.artifactId}-${project.version}/ - http://localhost:8080/pic-sure-irct-resource-${project.version}/ - http://localhost:8080/pic-sure-aggregate-resource-${project.version}/pic-sure/group - http://localhost:8080/pic-sure-hsapi-resource-${project.version}/ - eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjg3OTM1NTE5NjI0LCJzdWIiOiJpbnRlZ3JhdGlvbi50ZXN0QGhtcy5oYXJ2YXJkLmVkdSIsImVtYWlsIjoiaW50ZWdyYXRpb24udGVzdEBobXMuaGFydmFyZC5lZHUiLCJpc3MiOiJiYXIiLCJqdGkiOiJGb28iLCJpYXQiOjE1MzU2MDYwMjR9.RUKkN5_Bsd5MWHhV_YTnotoMqTtrOLszsBTN9FC4n48 - - - - - verify - - verify - - - - - - javax.xml.bind - jaxb-api - 2.3.0 - - - com.sun.xml.bind - jaxb-core - 2.3.0 - - - com.sun.xml.bind - jaxb-impl - 2.3.0 - - - javax.activation - activation - 1.1.1 - - - - - - - diff --git a/pic-sure-api-wildfly/src/main/java/edu/harvard/dbmi/avillach/ResourceTestInitializer.java b/pic-sure-api-wildfly/src/main/java/edu/harvard/dbmi/avillach/ResourceTestInitializer.java deleted file mode 100644 index b151daab..00000000 --- a/pic-sure-api-wildfly/src/main/java/edu/harvard/dbmi/avillach/ResourceTestInitializer.java +++ /dev/null @@ -1,73 +0,0 @@ -package edu.harvard.dbmi.avillach; - -import javax.annotation.PostConstruct; -import javax.ejb.Singleton; -import javax.ejb.Startup; -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; - -import edu.harvard.dbmi.avillach.data.entity.Resource; -import edu.harvard.hms.dbmi.avillach.GnomeI2B2CountResourceRS; -import edu.harvard.hms.dbmi.avillach.IRCTResourceRS; - -import static edu.harvard.dbmi.avillach.util.HttpClientUtil.composeURL; - -@Singleton -@Startup -public class ResourceTestInitializer -{ - public static String TARGET_PICSURE_URL = System.getenv("TARGET_PICSURE_URL"); -// public static String TARGET_IRCT_URL = System.getenv("TARGET_IRCT_URL"); - public static String AGGREGATE_RS_URL = System.getenv("AGGREGATE_RS_URL"); - public static String IRCT_RS_URL = System.getenv("IRCT_RS_URL"); - public static String GNOME_I2B2_RS_URL = System.getenv("GNOME_I2B2_RS_URL"); - @PersistenceContext(unitName = "picsure") - private EntityManager em; - - @PostConstruct - public void insertTestResources() { - System.out.println("ResourceTestInitializer, target picsure url is!!!: " + TARGET_PICSURE_URL); - - Resource fooResource = new Resource() -// .setTargetURL(TARGET_IRCT_URL) -// .setTargetURL("http://localhost:8080/pic-sure-api-wildfly-2.0.0-SNAPSHOT/pic-sure/v1.4") - .setResourceRSPath(IRCT_RS_URL) - .setDescription("HMS DBMI NHANES PIC-SURE 1.4 Supply token with key '" + IRCTResourceRS.IRCT_BEARER_TOKEN_KEY + "'") - .setName("nhanes.hms.harvard.edu") - .setToken("eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJmb29AYmFyLmNvbSIsImlzcyI6ImJhciIsImV4cCI6ODY1NTI4Mzk4NTQzLCJpYXQiOjE1Mjg0ODQ5NDMsImp0aSI6IkZvbyIsImVtYWlsIjoiZm9vQGJhci5jb20ifQ.KE2NIfCzQnd_vhykhb0sHdPHEwvy2Wphc4UVsKAVTgM"); - em.persist(fooResource); - - Resource ga4ghResource = new Resource() - .setTargetURL("http://54.174.229.198:8080/ga4gh/dos/v1/") - .setResourceRSPath("http://54.174.229.198:8080/ga4gh/dos/v1/") - .setDescription("GA4GH DOS Resource Server at DBMI/AvillachLab") - .setName("ga4gh-dos-server"); - em.persist(ga4ghResource); - - Resource aggregateResource = new Resource() -// .setTargetURL("http://localhost:8080/pic-sure-api-wildfly-2.0.0-SNAPSHOT/pic-sure/group") - .setTargetURL(TARGET_PICSURE_URL) - .setResourceRSPath(AGGREGATE_RS_URL) - .setToken("eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJmb29AYmFyLmNvbSIsImlzcyI6ImJhciIsImV4cCI6ODY1NTI4Mzk4NTQzLCJpYXQiOjE1Mjg0ODQ5NDMsImp0aSI6IkZvbyIsImVtYWlsIjoiZm9vQGJhci5jb20ifQ.KE2NIfCzQnd_vhykhb0sHdPHEwvy2Wphc4UVsKAVTgM") - .setDescription("Aggregate Resource RS") - .setName("Aggregate Resource RS"); - em.persist(aggregateResource); - - Resource hsapiResource = new Resource() - .setTargetURL("https://beta.commonsshare.org/hsapi/") - .setResourceRSPath(composeURL(TARGET_PICSURE_URL,"hsapi")) - .setToken("eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJmb29AYmFyLmNvbSIsImlzcyI6ImJhciIsImV4cCI6ODY1NTI4Mzk4NTQzLCJpYXQiOjE1Mjg0ODQ5NDMsImp0aSI6IkZvbyIsImVtYWlsIjoiZm9vQGJhci5jb20ifQ.KE2NIfCzQnd_vhykhb0sHdPHEwvy2Wphc4UVsKAVTgM") - .setDescription("HSAPI Resource RS") - .setName("HSAPI Resource RS"); - em.persist(hsapiResource); - - Resource gnomeI2B2Resource = new Resource() - .setTargetURL(TARGET_PICSURE_URL) - .setResourceRSPath(GNOME_I2B2_RS_URL) - .setToken("eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJmb29AYmFyLmNvbSIsImlzcyI6ImJhciIsImV4cCI6ODY1NTI4Mzk4NTQzLCJpYXQiOjE1Mjg0ODQ5NDMsImp0aSI6IkZvbyIsImVtYWlsIjoiZm9vQGJhci5jb20ifQ.KE2NIfCzQnd_vhykhb0sHdPHEwvy2Wphc4UVsKAVTgM") - .setDescription("Gnome I2B2 Count Resource RS Supply tokens with keys " + GnomeI2B2CountResourceRS.I2B2_BEARER_TOKEN_KEY + " and " + GnomeI2B2CountResourceRS.GNOME_BEARER_TOKEN_KEY) - .setName("Gnome I2B2 Count Resource RS"); - em.persist(gnomeI2B2Resource); - } - -} diff --git a/pic-sure-api-wildfly/src/main/java/edu/harvard/dbmi/avillach/UserTestInitializer.java b/pic-sure-api-wildfly/src/main/java/edu/harvard/dbmi/avillach/UserTestInitializer.java deleted file mode 100644 index 192da95a..00000000 --- a/pic-sure-api-wildfly/src/main/java/edu/harvard/dbmi/avillach/UserTestInitializer.java +++ /dev/null @@ -1,43 +0,0 @@ -package edu.harvard.dbmi.avillach; - -import edu.harvard.dbmi.avillach.data.entity.User; -import edu.harvard.dbmi.avillach.util.PicsureNaming; - -import javax.annotation.PostConstruct; -import javax.ejb.Singleton; -import javax.ejb.Startup; -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; - -@Singleton -@Startup -public class UserTestInitializer -{ - @PersistenceContext(unitName = "picsure") - private EntityManager em; - - @PostConstruct - public void insertTestUsers() { - User systemUser = new User() - .setRoles(PicsureNaming.RoleNaming.ROLE_SYSTEM) - .setSubject("samlp|foo@bar.com") - .setUserId("foo@bar.com"); - User nonSystemUser = new User() - .setRoles("") - .setSubject("samlp|foo2@bar.com") - .setUserId("foo2@bar.com"); - User tokenInspectionSystem = new User() - .setRoles(PicsureNaming.RoleNaming.ROLE_TOKEN_INTROSPECTION) - .setSubject("samlp|foo3@bar.com") - .setUserId("foo3@bar.com"); - User tokenInspectionUser = new User() - .setRoles(PicsureNaming.RoleNaming.ROLE_INTROSPECTION_USER) - .setSubject("samlp|foo4@bar.com") - .setUserId("foo4@bar.com"); - em.persist(systemUser); - em.persist(nonSystemUser); - em.persist(tokenInspectionUser); - em.persist(tokenInspectionSystem); - } - -} diff --git a/pic-sure-api-wildfly/src/main/resources/META-INF/persistence.xml b/pic-sure-api-wildfly/src/main/resources/META-INF/persistence.xml deleted file mode 100644 index 84874384..00000000 --- a/pic-sure-api-wildfly/src/main/resources/META-INF/persistence.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - org.hibernate.ejb.HibernatePersistence - java:/PicsureDS - edu.harvard.dbmi.avillach.data.entity.AuthUser - edu.harvard.dbmi.avillach.data.entity.Resource - edu.harvard.dbmi.avillach.data.entity.Query - edu.harvard.dbmi.avillach.data.entity.NamedDataset - - - - - - - - \ No newline at end of file diff --git a/pic-sure-api-wildfly/src/main/resources/wildflyConfiguration/standalone.xml b/pic-sure-api-wildfly/src/main/resources/wildflyConfiguration/standalone.xml deleted file mode 100644 index cfc15b34..00000000 --- a/pic-sure-api-wildfly/src/main/resources/wildflyConfiguration/standalone.xml +++ /dev/null @@ -1,533 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE - h2 - - sa - sa - - - - jdbc:h2:mem:picsure;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE - h2 - - sa - sa - - - - - org.h2.jdbcx.JdbcDataSource - - - - - - - - - - false - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ${jboss.bind.address:127.0.0.1} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/AggregateResourceIT.java b/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/AggregateResourceIT.java deleted file mode 100644 index 7730c14d..00000000 --- a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/AggregateResourceIT.java +++ /dev/null @@ -1,372 +0,0 @@ -package edu.harvard.dbmi.avillach; - -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.JsonNode; -import edu.harvard.dbmi.avillach.domain.QueryRequest; -import edu.harvard.dbmi.avillach.util.PicSureStatus; -import edu.harvard.hms.dbmi.avillach.IRCTResourceRS; -import org.apache.commons.io.IOUtils; -import org.apache.http.HttpResponse; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.util.*; - -import static com.github.tomakehurst.wiremock.client.WireMock.*; - -import static edu.harvard.dbmi.avillach.util.HttpClientUtil.composeURL; -import static edu.harvard.dbmi.avillach.util.HttpClientUtil.retrieveGetResponse; -import static edu.harvard.dbmi.avillach.util.HttpClientUtil.retrievePostResponse; - -import static org.junit.Assert.*; - -//Need tests executed in order to fill in variables for later tests -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class AggregateResourceIT extends BaseIT { - - private Logger logger = LoggerFactory.getLogger(this.getClass()); - - private final static String token = System.getProperty("irct.token"); - private final static String queryString = "{" + - " \"select\": [" + - " {" + - " \"alias\": \"gender\", \"field\": {\"pui\": \"/i2b2-nhanes/Demo/demographics/demographics/SEX/male\", \"dataType\":\"STRING\"}" + - " }," + - " {" + - " \"alias\": \"gender\", \"field\": {\"pui\": \"/i2b2-nhanes/Demo/demographics/demographics/SEX/female\", \"dataType\":\"STRING\"}" + - " }," + - " {" + - " \"alias\": \"age\", \"field\": {\"pui\": \"/i2b2-nhanes/Demo/demographics/demographics/AGE\", \"dataType\":\"STRING\"}" + - " }" + - " ]," + - " \"where\": [" + - " {" + - " \"predicate\": \"CONTAINS\"," + - " \"field\": {" + - " \"pui\": \"/i2b2-nhanes/Demo/demographics/demographics/SEX/male/\"," + - " \"dataType\": \"STRING\"" + - " }," + - " \"fields\": {" + - " \"ENOUNTER\": \"YES\"" + - " }" + - " }" + - " ]" + - "}"; - private final static String errorQuery = "{" + - " \"where\": [" + - " {" + - " \"predicate\": \"CONTAINS\"," + - " \"field\": {" + - " \"pui\": \"/i2b2-nhanes/Demo/demographics/demographics/nonexistentpath\"," + - " \"dataType\": \"STRING\"" + - " }," + - " \"fields\": {" + - " \"ENOUNTER\": \"YES\"" + - " }" + - " }" + - " ]" + - "}"; - private static UUID resourceUUID; - private static UUID aggregateUUID; - private static String queryId; - private static String status; - - @BeforeClass - public static void setUp() throws IOException{ - HttpResponse response = retrieveGetResponse(endpointUrl+"/info/resources", headers); - assertEquals("Response status code should be 200", 200, response.getStatusLine().getStatusCode()); - List responseBody = objectMapper.readValue(response.getEntity().getContent(), new TypeReference>(){}); - assertFalse(responseBody.isEmpty()); - - for (JsonNode node : responseBody){ - if (node.get("name").asText().equals("Aggregate Resource RS")){ - aggregateUUID = UUID.fromString(node.get("uuid").asText()); - } else if (node.get("name").asText().contains("nhanes")) { - resourceUUID = UUID.fromString(node.get("uuid").asText()); - } - } - } - - @Test - public void testQuery() throws IOException { - Map resourceResponse = new HashMap<>(); - resourceResponse.put("resultId", "230958"); - resourceResponse.put("status", "AVAILABLE"); - - wireMockRule.stubFor(any(urlPathMatching("/queryService/runQuery")) - .withHeader("Authorization", containing("anInvalidToken")) - .willReturn(aResponse() - .withStatus(401))); - - wireMockRule.stubFor(any(urlPathMatching("/queryService/runQuery")) - .withRequestBody(equalTo("poorlyWordedQueryString")) - .willReturn(aResponse() - .withStatus(500))); - - wireMockRule.stubFor(any(urlPathMatching("/queryService/runQuery")) - .withRequestBody(containing("/i2b2-nhanes/Demo")) - .withHeader("Authorization", containing(token)) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(resourceResponse)))); - - wireMockRule.stubFor(any(urlPathMatching("/resultService/resultStatus/.*")) - .withHeader("Authorization", containing(token)) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(resourceResponse)))); - - - //Create multiple queries and add them to a main query as a list - QueryRequest queryRequest1 = new GeneralQueryRequest(); - QueryRequest queryRequest2 = new GeneralQueryRequest(); - Map credentials = new HashMap(); - queryRequest1.setResourceCredentials(credentials); - queryRequest1.setQuery(queryString); - queryRequest1.setResourceUUID(resourceUUID); - queryRequest2.setResourceCredentials(credentials); - queryRequest2.setQuery(queryString); - queryRequest2.setResourceUUID(resourceUUID); - List queryList = new ArrayList<>(); - queryList.add(queryRequest1); - queryList.add(queryRequest2); - - QueryRequest topQuery = new GeneralQueryRequest(); - topQuery.setQuery(queryList); - topQuery.setResourceUUID(aggregateUUID); - - String body = objectMapper.writeValueAsString(topQuery); - - //Should throw an error if credentials missing or wrong - System.out.println("401 URL: " + endpointUrl+"/query" + "|headers: " + headers + "|body: " + body); - HttpResponse response = retrievePostResponse(endpointUrl+"/query", headers, body); -// System.out.println("Test Response: " + EntityUtils.toString(response.getEntity().getContent(), "UTF-8")); - assertEquals("Missing credentials should return a 401", 401, response.getStatusLine().getStatusCode()); - JsonNode responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - String errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); - String errorMessage = responseMessage.get("message").asText(); - assertTrue("Error message should be Unauthorized", errorMessage.contains("Unauthorized")); - - credentials.put(IRCTResourceRS.IRCT_BEARER_TOKEN_KEY, "anInvalidToken"); - queryRequest2.setResourceCredentials(credentials); - body = objectMapper.writeValueAsString(topQuery); - response = retrievePostResponse(endpointUrl+"/query", headers, body); - assertEquals("Invalid credentials should return a 401", 401, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); - errorMessage = responseMessage.get("message").asText(); - assertTrue("Error message should be Unauthorized", errorMessage.contains("Unauthorized")); - - logger.info("Aggregate token is: " + token); - //Should throw an error if missing query string - credentials.put(IRCTResourceRS.IRCT_BEARER_TOKEN_KEY, token); - queryRequest1.setResourceCredentials(credentials); - queryRequest1.setQuery(null); - queryRequest2.setResourceCredentials(credentials); - topQuery.setResourceCredentials(credentials); - body = objectMapper.writeValueAsString(topQuery); - response = retrievePostResponse(endpointUrl+"/query", headers, body); - assertEquals("Missing query should return a 500", 500, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - - //Try a poorly worded queryString - queryRequest1.setQuery("poorly worded query"); - body = objectMapper.writeValueAsString(topQuery); - response = retrievePostResponse(endpointUrl+"/query", headers, body); - assertEquals("Incorrectly formatted query should return a 500", 500, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - - //Make sure all queries work - queryRequest1.setQuery(queryString); - body = objectMapper.writeValueAsString(topQuery); - - response = retrievePostResponse(endpointUrl+"/query", headers, body); - assertEquals("Should return a 200", 200, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - assertNotNull("Status should not be null", responseMessage.get("status")); - System.out.println("Aggregate response message from " + endpointUrl+"/query is: " + responseMessage.toString()); - queryId = responseMessage.get("picsureResultId").asText(); - System.out.println("Aggregate Resource IT, queryResultId is: " + queryId); - assertNotNull("picsureResultId should not be null", queryId); - - //Want the status to be ERROR if one query errors - send query to be tested by queryStatus - queryRequest2.setQuery(errorQuery); - body = objectMapper.writeValueAsString(topQuery); - response = retrievePostResponse(endpointUrl+"/query", headers, body); - assertEquals("Should return a 200", 200, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - String errorQueryId = responseMessage.get("picsureResultId").asText(); - assertNotNull("Status should not be null", responseMessage.get("status")); - } - - @Test - public void testQueryStatus() throws IOException { - Map resourceResponse = new HashMap<>(); - resourceResponse.put("resultId", "230958"); - resourceResponse.put("status", "AVAILABLE"); - - Map errorResponse = new HashMap<>(); - errorResponse.put("resultId", "230999"); - errorResponse.put("status", "ERROR"); - - - wireMockRule.stubFor(any(urlPathMatching("/resultService/resultStatus/.*")) - .withHeader("Authorization", containing("anInvalidToken")) - .willReturn(aResponse() - .withStatus(401))); - - wireMockRule.stubFor(any(urlPathMatching("/resultService/resultStatus/.*")) - .withHeader("Authorization", containing(token)) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(resourceResponse)))); - - QueryRequest request = new GeneralQueryRequest(); - Map credentials = new HashMap(); - request.setResourceCredentials(credentials); - String body = objectMapper.writeValueAsString(request); - - //Should get 401 for missing or invalid credentials - System.out.println("401 URL: "+endpointUrl+"/query/" + queryId + "/status"); - HttpResponse response = retrievePostResponse(endpointUrl+"/query/" + queryId + "/status", headers, body); - assertEquals("Missing credentials should return a 401", 401, response.getStatusLine().getStatusCode()); - JsonNode responseMessage = objectMapper.readTree(response.getEntity().getContent()); - System.out.println(("AggregateResourceIT - test missing or invalid credentials returns: " + responseMessage)); - assertNotNull("Response message should not be null", responseMessage); - String errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); - String errorMessage = responseMessage.get("message").asText(); - assertTrue("Error message should be Unauthorized", errorMessage.contains("Unauthorized")); - - request.getResourceCredentials().put(IRCTResourceRS.IRCT_BEARER_TOKEN_KEY, "anInvalidToken"); - body = objectMapper.writeValueAsString(request); - - response = retrievePostResponse(composeURL(endpointUrl,"/query/" + queryId + "/status"), headers, body); - assertEquals("Invalid credentials should return a 401", 401, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); - errorMessage = responseMessage.get("message").asText(); - assertTrue("Error message should be Unauthorized", errorMessage.contains("Unauthorized")); - - //This should retrieve the status of the query successfully - request.getResourceCredentials().put(IRCTResourceRS.IRCT_BEARER_TOKEN_KEY, token); - body = objectMapper.writeValueAsString(request); - - response = retrievePostResponse(endpointUrl+"/query/" + queryId + "/status", headers, body); - assertEquals("Should return a 200", 200, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - status = responseMessage.get("status").asText(); - assertNotNull("Status should not be null", status); - - - //TODO What if only one query errors - //Create an errored response - wireMockRule.stubFor(any(urlPathMatching("/resultService/resultStatus/.*")) - .withHeader("Authorization", containing(token)) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(errorResponse)))); - - response = retrievePostResponse(endpointUrl+"/query/" + queryId + "/status", headers, body); - assertEquals("Should return a 200", 200, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - String errorStatus = responseMessage.get("status").asText(); - assertEquals("Status should be ERROR", PicSureStatus.ERROR.name(), errorStatus); - - - } - - @Test - public void testResult() throws IOException, InterruptedException { - String resultResponse = "aResultOfSomeKind"; - - wireMockRule.stubFor(any(urlPathMatching("/resultService/result/.*")) - .withHeader("Authorization", containing("anInvalidToken")) - .willReturn(aResponse() - .withStatus(401))); - - - wireMockRule.stubFor(any(urlPathMatching("/resultService/result/.*")) - .withHeader("Authorization", containing(token)) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(resultResponse)))); - - QueryRequest resultRequest = new GeneralQueryRequest(); - - Map credentials = new HashMap(); - credentials.put(IRCTResourceRS.IRCT_BEARER_TOKEN_KEY, token); - - QueryRequest request = new GeneralQueryRequest(); - request.setResourceCredentials(credentials); - String body = objectMapper.writeValueAsString(request); - - //Need to make sure result is ready - while (!status.equals(PicSureStatus.AVAILABLE.name()) && !status.equals(PicSureStatus.ERROR.name())){ - Thread.sleep(2000); - HttpResponse response = retrievePostResponse(endpointUrl+"/query/" + queryId + "/status", headers, body); - assertEquals("Should return a 200", 200, response.getStatusLine().getStatusCode()); - JsonNode responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - status = responseMessage.get("status").asText(); - } - if (status.equals(PicSureStatus.ERROR.name())){ - fail("Query ended with an ERROR"); - } - request.setResourceCredentials(new HashMap<>()); - body = objectMapper.writeValueAsString(request); - - //Missing or invalid credentials should return 401 - HttpResponse response = retrievePostResponse(endpointUrl+"/query/" + queryId + "/result", headers, body); - assertEquals("Missing credentials should return a 401", 401, response.getStatusLine().getStatusCode()); - JsonNode responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - String errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); - String errorMessage = responseMessage.get("message").asText(); - assertTrue("Error message should be Unauthorized", errorMessage.contains("Unauthorized")); - - request.getResourceCredentials().put(IRCTResourceRS.IRCT_BEARER_TOKEN_KEY, "anInvalidToken"); - body = objectMapper.writeValueAsString(request); - - response = retrievePostResponse(endpointUrl+"/query/" + queryId + "/result", headers, body); - assertEquals("Missing credentials should return a 401", 401, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); - errorMessage = responseMessage.get("message").asText(); - assertTrue("Error message should be Unauthorized", errorMessage.contains("Unauthorized")); - - //Should return an array of results - - request.getResourceCredentials().put(IRCTResourceRS.IRCT_BEARER_TOKEN_KEY, token); - body = objectMapper.writeValueAsString(request); - - response = retrievePostResponse(endpointUrl+"/query/" + queryId + "/result", headers, body); - assertEquals("Should return a 200", 200, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - //There were 2 queries so there should be 2 results - assertEquals("There should be 2 results in the array", 2, responseMessage.size()); - - } - -} diff --git a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/BaseIT.java b/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/BaseIT.java deleted file mode 100644 index 1d2b3a3a..00000000 --- a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/BaseIT.java +++ /dev/null @@ -1,169 +0,0 @@ -package edu.harvard.dbmi.avillach; - -import java.io.IOException; -import java.time.LocalDateTime; -import java.time.ZoneId; -import java.util.*; - -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.github.tomakehurst.wiremock.junit.WireMockClassRule; -import edu.harvard.dbmi.avillach.data.entity.Resource; -import org.apache.http.Header; -import org.apache.http.HttpResponse; -import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.message.BasicHeader; -import org.junit.BeforeClass; - -import io.jsonwebtoken.Jwts; -import io.jsonwebtoken.SignatureAlgorithm; -import org.apache.http.client.HttpClient; -import org.junit.Rule; - -import javax.ws.rs.core.HttpHeaders; - -import static edu.harvard.dbmi.avillach.util.HttpClientUtil.composeURL; -import static edu.harvard.dbmi.avillach.util.HttpClientUtil.retrieveGetResponse; -import static edu.harvard.dbmi.avillach.util.HttpClientUtil.retrievePostResponse; -import static org.junit.Assert.*; - -public class BaseIT { - - private static final String CLIENT_SECRET = System.getenv("PIC_SURE_CLIENT_SECRET"); - private static final String USER_ID_CLAIM = System.getenv("PIC_SURE_USER_ID_CLAIM"); - - protected static String endpointUrl; - protected static String irctEndpointUrl; - protected static String aggregate_url; - protected static String hsapiEndpointUrl; - protected static UUID resourceId; - - //These need to be established here to prevent multiplication of headers - protected static String jwt = generateJwtForSystemUser(); - protected static List
headers = new ArrayList<>(Arrays.asList(new BasicHeader(HttpHeaders.AUTHORIZATION, "Bearer " + jwt), new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"))); - - protected static HttpClient client = HttpClientBuilder.create().build(); - protected final static ObjectMapper objectMapper = new ObjectMapper(); - - protected final static int port = 8079; - protected final static String testURL = "http://localhost:"+port; - - @Rule - public WireMockClassRule wireMockRule = new WireMockClassRule(port); - - @BeforeClass - public static void beforeClass() { - endpointUrl = System.getProperty("service.url"); - System.out.println("endpointUrl is: " + endpointUrl); - irctEndpointUrl = System.getProperty("irct.rs.url"); - System.out.println("irctEndpointUrl is: " + irctEndpointUrl); - aggregate_url = System.getProperty("aggregate.rs.url"); - hsapiEndpointUrl = System.getProperty("hsapi.service.url"); - - //insert a resource for testing if necessary - try { - String uri = composeURL(endpointUrl, "/resource"); - HttpResponse response = retrieveGetResponse(uri, headers); - assertEquals("Response status code should be 200", 200, response.getStatusLine().getStatusCode()); - List resources = objectMapper.readValue(response.getEntity().getContent(), new TypeReference>() { - }); - assertFalse(resources.isEmpty()); - - String resourceRSPath = null; - boolean testResourceInserted = false; - for (Resource r : resources){ - if ("Test Resource".equals(r.getName())){ - testResourceInserted = true; - resourceId = r.getUuid(); - break; - } else if (resourceRSPath == null){ - //We'll need a random resourceRSPath for testing - resourceRSPath = r.getResourceRSPath(); - } - } - - if (!testResourceInserted){ - List> resourcesToAdd = new ArrayList<>(); - Map testResource = new HashMap<>(); - testResource.put("resourceRSPath", resourceRSPath); - testResource.put("description", "Test Resource"); - testResource.put("name", "Test Resource"); - testResource.put("token", "testToken"); - testResource.put("targetURL", testURL); - - resourcesToAdd.add(testResource); - response = retrievePostResponse(uri, headers, objectMapper.writeValueAsString(resourcesToAdd)); - assertEquals("Response status code should be 200", 200, response.getStatusLine().getStatusCode()); - JsonNode responseBody = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response should not be null", responseBody); - JsonNode content = responseBody.get("content"); - assertNotNull("Content should not be null", content); - JsonNode resource = content.get(0); - assertNotNull("Response should have a resource", resource); - assertTrue("Resource response should have an id", resource.has("uuid")); - resourceId = UUID.fromString(resource.get("uuid").asText()); - } - } catch(IOException e) { - e.printStackTrace(); - fail("Unable to set up test resource"); - } - } - - /* These users are initialized in the database in the UserTestInitializer class. An instance - * of which is declared here for your IDE navigation convenience. - */ - UserTestInitializer whereYourTestDataLives; - - protected static String generateJwtForSystemUser() { - return Jwts.builder() - .setSubject("samlp|foo@bar.com") - .setIssuer("http://localhost:8080") - .setIssuedAt(new Date()) - .addClaims(Map - .of(USER_ID_CLAIM,"foo@bar.com")) - .setExpiration(Date.from(LocalDateTime.now().plusMinutes(15L).atZone(ZoneId.systemDefault()).toInstant())) - .signWith(SignatureAlgorithm.HS512, CLIENT_SECRET.getBytes()) - .compact(); - } - - public String generateJwtForNonSystemUser() { - return Jwts.builder() - .setSubject("samlp|foo2@bar.com") - .setIssuer("http://localhost:8080") - .setIssuedAt(new Date()).addClaims(Map.of(USER_ID_CLAIM,"foo2@bar.com")) - .setExpiration(Date.from(LocalDateTime.now().plusMinutes(15L).atZone(ZoneId.systemDefault()).toInstant())) - .signWith(SignatureAlgorithm.HS512, CLIENT_SECRET.getBytes()) - .compact(); - } - - public String generateJwtForCallingTokenInspection() { - return Jwts.builder() - .setSubject("samlp|foo3@bar.com") - .setIssuer("http://localhost:8080") - .setIssuedAt(new Date()).addClaims(Map.of("email","foo3@bar.com")) - .setExpiration(Date.from(LocalDateTime.now().plusMinutes(15L).atZone(ZoneId.systemDefault()).toInstant())) - .signWith(SignatureAlgorithm.HS512, "foo".getBytes()) - .compact(); - } - - public String generateJwtForTokenInspectionUser() { - return Jwts.builder() - .setSubject("samlp|foo4@bar.com") - .setIssuer("http://localhost:8080") - .setIssuedAt(new Date()).addClaims(Map.of("email","foo4@bar.com")) - .setExpiration(Date.from(LocalDateTime.now().plusMinutes(15L).atZone(ZoneId.systemDefault()).toInstant())) - .signWith(SignatureAlgorithm.HS512, "foo".getBytes()) - .compact(); - } - - public String generateExpiredJwt() { - return Jwts.builder() - .setSubject("samlp|foo@bar.com") - .setIssuer("http://localhost:8080") - .setIssuedAt(new Date()).addClaims(Map.of("email","foo@bar.com")) - .setExpiration(Date.from(LocalDateTime.now().minusMinutes(15L).atZone(ZoneId.systemDefault()).toInstant())) - .signWith(SignatureAlgorithm.HS512, "foo".getBytes()) - .compact(); - } -} diff --git a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/GnomeI2B2ResourceIT.java b/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/GnomeI2B2ResourceIT.java deleted file mode 100644 index 14216d7e..00000000 --- a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/GnomeI2B2ResourceIT.java +++ /dev/null @@ -1,449 +0,0 @@ -package edu.harvard.dbmi.avillach; - -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.JsonNode; -import edu.harvard.dbmi.avillach.domain.QueryRequest; -import edu.harvard.dbmi.avillach.util.PicSureStatus; -import edu.harvard.hms.dbmi.avillach.GnomeI2B2CountResourceRS; -import org.apache.http.Header; -import org.apache.http.HttpResponse; -import org.apache.http.message.BasicHeader; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; - -import javax.ws.rs.core.HttpHeaders; -import java.io.IOException; -import java.util.*; - -import static com.github.tomakehurst.wiremock.client.WireMock.*; -import static edu.harvard.dbmi.avillach.util.HttpClientUtil.retrieveGetResponse; -import static edu.harvard.dbmi.avillach.util.HttpClientUtil.retrievePostResponse; -import static edu.harvard.dbmi.avillach.util.HttpClientUtil.composeURL; -import static org.junit.Assert.*; - -//Need tests executed in order to fill in variables for later tests -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class GnomeI2B2ResourceIT extends BaseIT { - - private final static String token ="a.supposedly-Valid.token"; - private final static String i2b2queryString = "{" + - " \"select\": [" + - " {" + - " \"alias\": \"gender\", \"field\": {\"pui\": \"/i2b2-wildfly-grin-patient-mapping/Demo/GRIN/GRIN/DEMOGRAPHIC/SEX/M\", \"dataType\":\"STRING\"}" + - " }," + - " {" + - " \"alias\": \"gender\", \"field\": {\"pui\": \"/i2b2-wildfly-grin-patient-mapping/Demo/GRIN/GRIN/DEMOGRAPHIC/SEX/F\", \"dataType\":\"STRING\"}" + - " }," + - " {" + - " \"alias\": \"age\", \"field\": {\"pui\": \"/i2b2-wildfly-grin-patient-mapping/Demo/GRIN/GRIN/DEMOGRAPHIC/Age\", \"dataType\":\"STRING\"}" + - " }" + - " ]," + - " \"where\": [" + - " {" + - " \"predicate\": \"CONTAINS\"," + - " \"field\": {" + - " \"pui\": \"/i2b2-wildfly-grin-patient-mapping/Demo/GRIN/GRIN/DEMOGRAPHIC/SEX/M\"," + - " \"dataType\": \"STRING\"" + - " }," + - " \"fields\": {" + - " \"ENOUNTER\": \"YES\"" + - " }" + - " }" + - " ]" + - "}"; - private final static String gnomeQueryString = "{" + - " \"where\": [" + - " { \"field\" : " + - " {" + - " \"pui\" : \"/gnome/query_rest.cgi\"," + - " \"dataType\": \"STRING\"" + - " }," + - " \"predicate\" : \"CONTAINS\"," + - " \"fields\" : {" + - " \"qtype\" : \"variants\"," + - " \"vqueries\" : [\"chr16,2120487,2120487,G,A\"]" + - " }" + - " }]} "; - private final static String errorQueryString = "{" + - " \"where\": [" + - " {" + - " \"predicate\": \"CONTAINS\"," + - " \"field\": {" + - " \"pui\": \"/i2b2-grin-patient-mapping/Demo/GRIN/GRIN/DEMOGRAPHIC/nonexistentpath\"," + - " \"dataType\": \"STRING\"" + - " }," + - " \"fields\": {" + - " \"ENOUNTER\": \"YES\"" + - " }" + - " }" + - " ]" + - "}"; - - private static JsonNode i2b2Query; - private static JsonNode gnomeQuery; - private static JsonNode errorQuery; - - private static UUID gnomeI2B2UUID; - private static Header[] headers = new Header[1]; - private static String queryId; - private static String errorQueryId; - private static String status; - private static String gnomeResultId = "239057"; - private static String i2b2ResultId = "2355911"; - private static String errorResultId = "2003913"; - - @BeforeClass - public static void setUp() throws IOException{ - //Will need to know the resource uuid - String jwt = generateJwtForSystemUser(); - headers[0] = new BasicHeader(HttpHeaders.AUTHORIZATION, "Bearer " + jwt); - HttpResponse response = retrieveGetResponse(endpointUrl+"/info/resources", headers); - assertEquals("Response status code should be 200", 200, response.getStatusLine().getStatusCode()); - List responseBody = objectMapper.readValue(response.getEntity().getContent(), new TypeReference>(){}); - assertFalse(responseBody.isEmpty()); - - for (JsonNode node : responseBody){ - if (node.get("name").asText().equals("Gnome I2B2 Count Resource RS")){ - gnomeI2B2UUID = UUID.fromString(node.get("uuid").asText()); - } - } - - i2b2Query = objectMapper.readTree(i2b2queryString); - gnomeQuery = objectMapper.readTree(gnomeQueryString); - errorQuery = objectMapper.readTree(errorQueryString); - } - - @Test - public void testQuery() throws IOException { - //Set up responses - Map gnomeResponse = new HashMap<>(); - gnomeResponse.put("resultId", gnomeResultId); - - Map i2b2Response = new HashMap<>(); - i2b2Response.put("resultId", i2b2ResultId); - i2b2Response.put("status", "AVAILABLE"); - - Map errorResponse = new HashMap<>(); - errorResponse.put("resultId", errorResultId); - errorResponse.put("status", "ERROR"); - - wireMockRule.stubFor(any(urlPathMatching("/queryService/runQuery")) - .withHeader("Authorization", containing("anInvalidToken")) - .willReturn(aResponse() - .withStatus(401))); - - wireMockRule.stubFor(any(urlPathMatching("/queryService/runQuery")) - .withRequestBody(containing("nonexistentpath")) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(errorResponse)))); - - wireMockRule.stubFor(any(urlPathMatching("/queryService/runQuery")) - .withRequestBody(containing("poorly worded query")) - .willReturn(aResponse() - .withStatus(500))); - - wireMockRule.stubFor(any(urlPathMatching("/queryService/runQuery")) - .withRequestBody(containing("/i2b2-wildfly-grin-patient-mapping")) - .withHeader("Authorization", containing(token)) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(i2b2Response)))); - - wireMockRule.stubFor(any(urlPathMatching("/queryService/runQuery")) - .withRequestBody(containing("/gnome/query_rest.cgi")) - .withHeader("Authorization", containing(token)) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(gnomeResponse)))); - - //Create a query - QueryRequest queryRequest = new GeneralQueryRequest(); - Map credentials = new HashMap(); - Map queryMap = new HashMap<>(); - queryRequest.setResourceCredentials(credentials); - queryMap.put("i2b2", i2b2Query); - queryMap.put("gnome", gnomeQuery); - queryRequest.setQuery(queryMap); - queryRequest.setResourceUUID(gnomeI2B2UUID); - - String body = objectMapper.writeValueAsString(queryRequest); - - //Should throw an error if credentials missing or wrong - HttpResponse response = retrievePostResponse(composeURL(endpointUrl,"/query/"), headers, body); - assertEquals("Missing credentials should return a 401", 401, response.getStatusLine().getStatusCode()); - JsonNode responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - String errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); - String errorMessage = responseMessage.get("message").asText(); - assertTrue("Error message should be Unauthorized", errorMessage.contains("Unauthorized")); - - credentials.put(GnomeI2B2CountResourceRS.GNOME_BEARER_TOKEN_KEY, "anInvalidToken"); - credentials.put(GnomeI2B2CountResourceRS.I2B2_BEARER_TOKEN_KEY, token); - queryRequest.setResourceCredentials(credentials); - body = objectMapper.writeValueAsString(queryRequest); - response = retrievePostResponse(composeURL(endpointUrl,"/query/"), headers, body); - assertEquals("Missing credentials should return a 401", 401, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); - errorMessage = responseMessage.get("message").asText(); - assertTrue("Error message should be Unauthorized", errorMessage.contains("Unauthorized")); - - //Should throw an error if missing any query - credentials.put(GnomeI2B2CountResourceRS.GNOME_BEARER_TOKEN_KEY, token); - queryRequest.setResourceCredentials(credentials); - queryRequest.setQuery(null); - body = objectMapper.writeValueAsString(queryRequest); - response = retrievePostResponse(composeURL(endpointUrl,"/query/"), headers, body); - assertEquals("Missing query should return a 500", 500, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - - queryMap.remove("gnome"); - queryRequest.setQuery(queryMap); - body = objectMapper.writeValueAsString(queryRequest); - response = retrievePostResponse(composeURL(endpointUrl,"/query/"), headers, body); - assertEquals("Missing query should return a 500", 500, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - - - //Try a poorly worded queryString - queryMap.put("gnome", "poorly worded query"); - body = objectMapper.writeValueAsString(queryRequest); - response = retrievePostResponse(composeURL(endpointUrl,"/query/"), headers, body); - assertEquals("Incorrectly formatted query should return a 500", 500, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - - //Make sure all queries work - queryMap.put("gnome", gnomeQuery); - body = objectMapper.writeValueAsString(queryRequest); - response = retrievePostResponse(composeURL(endpointUrl,"/query/"), headers, body); - assertEquals("Should return a 200", 200, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - assertNotNull("Status should not be null", responseMessage.get("status")); - queryId = responseMessage.get("picsureResultId").asText(); - assertNotNull("picsureResultId should not be null", queryId); - - //Want the status to be ERROR if one query errors - send query to be tested by queryStatus - queryMap.put("i2b2", errorQuery); - body = objectMapper.writeValueAsString(queryRequest); - response = retrievePostResponse(composeURL(endpointUrl,"/query/"), headers, body); - assertEquals("Should return a 200", 200, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - errorQueryId = responseMessage.get("picsureResultId").asText(); - assertNotNull("Status should not be null", responseMessage.get("status")); - - } - - @Test - public void testQueryStatus() throws IOException { - Map gnomeResponse = new HashMap<>(); - gnomeResponse.put("resultId", "230958"); - gnomeResponse.put("status", "AVAILABLE"); - - Map i2b2Response = new HashMap<>(); - i2b2Response.put("resultId", "230958"); - i2b2Response.put("status", "AVAILABLE"); - - Map errorResponse = new HashMap<>(); - errorResponse.put("resultId", "230999"); - errorResponse.put("status", "ERROR"); - - - wireMockRule.stubFor(any(urlPathMatching("/resultService/resultStatus/.*")) - .withHeader("Authorization", containing("anInvalidToken")) - .willReturn(aResponse() - .withStatus(401))); - - wireMockRule.stubFor(any(urlPathMatching("/resultService/resultStatus/"+gnomeResultId)) - .withHeader("Authorization", containing(token)) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(gnomeResponse)))); - - wireMockRule.stubFor(any(urlPathMatching("/resultService/resultStatus/"+i2b2ResultId)) - .withHeader("Authorization", containing(token)) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(i2b2Response)))); - - wireMockRule.stubFor(any(urlPathMatching("/resultService/resultStatus/"+errorResultId)) - .withHeader("Authorization", containing(token)) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(errorResponse)))); - - QueryRequest statusQuery = new GeneralQueryRequest(); - Map credentials = new HashMap(); - statusQuery.setResourceCredentials(credentials); - String body = objectMapper.writeValueAsString(statusQuery); - - //Should get 401 for missing or invalid credentials - HttpResponse response = retrievePostResponse(composeURL(endpointUrl,"/query/"+queryId+"/status"), headers, body); - assertEquals("Missing credentials should return a 401", 401, response.getStatusLine().getStatusCode()); - JsonNode responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - String errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); - String errorMessage = responseMessage.get("message").asText(); - assertTrue("Error message should be Unauthorized", errorMessage.contains("Unauthorized")); - - credentials.put(GnomeI2B2CountResourceRS.I2B2_BEARER_TOKEN_KEY, "anInvalidToken"); - credentials.put(GnomeI2B2CountResourceRS.GNOME_BEARER_TOKEN_KEY, token); - body = objectMapper.writeValueAsString(statusQuery); - - response = retrievePostResponse(composeURL(endpointUrl,"/query/"+queryId+"/status"), headers, body); - assertEquals("Missing credentials should return a 401", 401, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); - errorMessage = responseMessage.get("message").asText(); - assertTrue("Error message should be Unauthorized", errorMessage.contains("Unauthorized")); - - //This should retrieve the status of the query successfully - credentials.put(GnomeI2B2CountResourceRS.I2B2_BEARER_TOKEN_KEY, token); - body = objectMapper.writeValueAsString(statusQuery); - response = retrievePostResponse(composeURL(endpointUrl,"/query/"+queryId+"/status"), headers, body); - assertEquals("Should return a 200", 200, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - status = responseMessage.get("status").asText(); - assertNotNull("Status should not be null", status); - - //This query should eventually result in an error, since one of the queries should have errored - String errorStatus = PicSureStatus.PENDING.name(); - while (errorStatus.equals(PicSureStatus.PENDING.name())){ - response = retrievePostResponse(composeURL(endpointUrl,"/query/"+errorQueryId+"/status"), headers, body); - assertEquals("Should return a 200", 200, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - errorStatus = responseMessage.get("status").asText(); - } - assertEquals("Status should be ERROR", PicSureStatus.ERROR.name(), errorStatus); - - - } - - @Test - public void testResult() throws IOException, InterruptedException { - //Prepare responses - Map gnomeResponse = new HashMap<>(); - ArrayList data = new ArrayList<>(); - ArrayList> innerArray = new ArrayList<>(); - Map value = new HashMap<>(); - value.put(GnomeI2B2CountResourceRS.GNOME_LABEL, "abc-123"); - innerArray.add(value); - data.add(innerArray); - innerArray = new ArrayList<>(); - value = new HashMap<>(); - value.put(GnomeI2B2CountResourceRS.GNOME_LABEL, "def-456"); - innerArray.add(value); - data.add(innerArray); - innerArray = new ArrayList<>(); - value = new HashMap<>(); - value.put(GnomeI2B2CountResourceRS.GNOME_LABEL, "ghi-789"); - innerArray.add(value); - data.add(innerArray); - gnomeResponse.put("data", data); - - Map i2b2Response = new HashMap<>(); - data = new ArrayList<>(); - innerArray = new ArrayList<>(); - value = new HashMap<>(); - value.put(GnomeI2B2CountResourceRS.I2B2_LABEL, "abc_123"); - innerArray.add(value); - data.add(innerArray); - innerArray = new ArrayList<>(); - value = new HashMap<>(); - value.put(GnomeI2B2CountResourceRS.I2B2_LABEL, "jfk_010"); - innerArray.add(value); - data.add(innerArray); - innerArray = new ArrayList<>(); - value = new HashMap<>(); - value.put(GnomeI2B2CountResourceRS.I2B2_LABEL, "ghi_789"); - innerArray.add(value); - data.add(innerArray); - i2b2Response.put("data", data); - - wireMockRule.stubFor(any(urlPathMatching("/resultService/result/.*")) - .withHeader("Authorization", containing("anInvalidToken")) - .willReturn(aResponse() - .withStatus(401))); - - wireMockRule.stubFor(any(urlPathMatching("/resultService/result/"+gnomeResultId+"/.*")) - .withHeader("Authorization", containing(token)) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(gnomeResponse)))); - - wireMockRule.stubFor(any(urlPathMatching("/resultService/result/"+i2b2ResultId+"/.*")) - .withHeader("Authorization", containing(token)) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(i2b2Response)))); - - QueryRequest resultRequest = new GeneralQueryRequest(); - Map credentials = new HashMap(); - credentials.put(GnomeI2B2CountResourceRS.I2B2_BEARER_TOKEN_KEY, token); - credentials.put(GnomeI2B2CountResourceRS.GNOME_BEARER_TOKEN_KEY, token); - resultRequest.setResourceCredentials(credentials); - String body = objectMapper.writeValueAsString(resultRequest); - - //Need to make sure result is ready - while (!status.equals(PicSureStatus.AVAILABLE.name())){ - Thread.sleep(2000); - HttpResponse response = retrievePostResponse(composeURL(endpointUrl,"/query/"+queryId+"/status"), headers, body); - assertEquals("Should return a 200", 200, response.getStatusLine().getStatusCode()); - JsonNode responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - status = responseMessage.get("status").asText(); - } - - credentials.remove(GnomeI2B2CountResourceRS.I2B2_BEARER_TOKEN_KEY); - body = objectMapper.writeValueAsString(resultRequest); - - //Missing or invalid credentials should return 401 - HttpResponse response = retrievePostResponse(composeURL(endpointUrl,"/query/"+queryId+"/result"), headers, body); - assertEquals("Missing credentials should return a 401", 401, response.getStatusLine().getStatusCode()); - JsonNode responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - String errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); - String errorMessage = responseMessage.get("message").asText(); - assertTrue("Error message should be Unauthorized", errorMessage.contains("Unauthorized")); - - credentials.put(GnomeI2B2CountResourceRS.I2B2_BEARER_TOKEN_KEY, "anInvalidToken"); - body = objectMapper.writeValueAsString(resultRequest); - - response = retrievePostResponse(composeURL(endpointUrl,"/query/"+queryId+"/result"), headers, body); - assertEquals("Missing credentials should return a 401", 401, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); - errorMessage = responseMessage.get("message").asText(); - assertTrue("Error message should be Unauthorized", errorMessage.contains("Unauthorized")); - - //Should return an array of results - credentials.put(GnomeI2B2CountResourceRS.I2B2_BEARER_TOKEN_KEY, token); - body = objectMapper.writeValueAsString(resultRequest); - response = retrievePostResponse(composeURL(endpointUrl,"/query/"+queryId+"/result"), headers, body); - assertEquals("Should return a 200", 200, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - //In testing this was the result, but probably the test shouldn't rely on it - assertEquals("Result should be 2", 2, Integer.parseInt(responseMessage.toString())); - - } - -} diff --git a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/IRCTResourceIT.java b/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/IRCTResourceIT.java deleted file mode 100644 index 3cd9dd09..00000000 --- a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/IRCTResourceIT.java +++ /dev/null @@ -1,501 +0,0 @@ -package edu.harvard.dbmi.avillach; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.node.ObjectNode; -import edu.harvard.dbmi.avillach.domain.QueryFormat; -import edu.harvard.dbmi.avillach.domain.QueryRequest; -import edu.harvard.dbmi.avillach.domain.QueryStatus; -import edu.harvard.dbmi.avillach.util.exception.ApplicationException; -import edu.harvard.hms.dbmi.avillach.IRCTResourceRS; -import org.apache.commons.io.IOUtils; -import org.apache.http.HttpResponse; -import edu.harvard.dbmi.avillach.util.exception.ProtocolException; -import org.junit.Test; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import static com.github.tomakehurst.wiremock.client.WireMock.*; -import static edu.harvard.dbmi.avillach.util.HttpClientUtil.*; -import static org.junit.Assert.*; - -public class IRCTResourceIT extends BaseIT { - - private final static String token = "a.valid-Token"; - private final static String queryString = "{" + - " \"select\": [" + - " {" + - " \"alias\": \"gender\", \"field\": {\"pui\": \"/i2b2-nhanes/Demo/demographics/demographics/SEX/male\", \"dataType\":\"STRING\"}" + - " }," + - " {" + - " \"alias\": \"gender\", \"field\": {\"pui\": \"/i2b2-nhanes/Demo/demographics/demographics/SEX/female\", \"dataType\":\"STRING\"}" + - " }," + - " {" + - " \"alias\": \"age\", \"field\": {\"pui\": \"/i2b2-nhanes/Demo/demographics/demographics/AGE\", \"dataType\":\"STRING\"}" + - " }" + - " ]," + - " \"where\": [" + - " {" + - " \"predicate\": \"CONTAINS\"," + - " \"field\": {" + - " \"pui\": \"/i2b2-nhanes/Demo/demographics/demographics/SEX/male/\"," + - " \"dataType\": \"STRING\"" + - " }," + - " \"fields\": {" + - " \"ENOUNTER\": \"YES\"" + - " }" + - " }" + - " ]" + - "}"; - //This is a previously created query id, uncertain if this is the best way to go - private String testQueryResultId = "231066"; - private final String targetURL = "http://localhost:8079"; - - @Test - public void testStatus() throws UnsupportedOperationException, IOException { - System.out.println(irctEndpointUrl); - HttpResponse response = retrieveGetResponse(irctEndpointUrl+"pic-sure/v1.4/status", headers); - assertEquals("Status should return a 200", 200, response.getStatusLine().getStatusCode()); - } - - @Test - public void testInfo() throws UnsupportedOperationException, IOException { - wireMockRule.stubFor(any(urlPathMatching("/resourceService/resources")) - .withHeader("Authorization", containing("anIncorrectToken")) - .willReturn(aResponse() - .withStatus(401))); - - List qfs = new ArrayList<>(); - - wireMockRule.stubFor(any(urlPathMatching("/resourceService/resources")) - .withHeader("Authorization", containing(token)) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(qfs)))); - - QueryRequest request = new GeneralQueryRequest(); - - //Should throw an error if credentials missing or wrong - Map credentials = new HashMap(); - request.setResourceCredentials(credentials); - String body = objectMapper.writeValueAsString(request); - HttpResponse response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/info", headers, body); - assertEquals("Missing credentials should return a 401",401, response.getStatusLine().getStatusCode()); - JsonNode responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - String errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); - String errorMessage = responseMessage.get("message").asText(); - assertTrue("Error message should be " + IRCTResourceRS.MISSING_CREDENTIALS_MESSAGE, errorMessage.contains(IRCTResourceRS.MISSING_CREDENTIALS_MESSAGE)); - - - credentials.put(IRCTResourceRS.IRCT_BEARER_TOKEN_KEY, "anIncorrectToken"); - body = objectMapper.writeValueAsString(request); - response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/info", headers, body); - assertEquals("Incorrect token should return a 401",401, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); - - - credentials.put(IRCTResourceRS.IRCT_BEARER_TOKEN_KEY, token); -// body = objectMapper.writeValueAsString(request); -// response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/info", headers, body); -// assertEquals("Missing target URL should return a 500",500, response.getStatusLine().getStatusCode()); -// responseMessage = objectMapper.readTree(response.getEntity().getContent()); -// assertNotNull("Response message should not be null", responseMessage); -// errorMessage = responseMessage.get("message").asText(); -// assertTrue("Error message should be " + ApplicationException.MISSING_TARGET_URL, errorMessage.contains(ApplicationException.MISSING_TARGET_URL)); - - - //This should work - body = objectMapper.writeValueAsString(request); - response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/info", headers, body); - assertEquals("Correct request should return a 200",200, response.getStatusLine().getStatusCode()); - JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response should not be null", responseNode); - assertNotNull("Response should have a name", responseNode.get("name")); - assertNotNull("Response should have data in queryFormats", responseNode.get("queryFormats")); - } - - @Test - public void testSearch() throws UnsupportedOperationException, IOException { - HashMap anyOldResult = new HashMap<>(); - anyOldResult.put("results", "aResult"); - - HashMap emptyResult = new HashMap<>(); - - wireMockRule.stubFor(any(urlPathMatching("/resourceService/find")) - .withHeader("Authorization", containing("anIncorrectToken")) - .willReturn(aResponse() - .withStatus(401))); - - wireMockRule.stubFor(any(urlEqualTo("/resourceService/find?term=%25antibody%25")) - .withHeader("Authorization", containing(token)) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(anyOldResult)))); - - wireMockRule.stubFor(any(urlEqualTo("/resourceService/find?term=thisShouldFindNothing")) - .withHeader("Authorization", containing(token)) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(emptyResult)))); - - - QueryRequest queryRequest = new GeneralQueryRequest(); - Map credentials = new HashMap(); - queryRequest.setResourceCredentials(credentials); - queryRequest.setQuery("%antibody%"); - - String body = objectMapper.writeValueAsString(queryRequest); - - //Should throw an error if credentials missing or wrong - HttpResponse response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/search", headers, body); - assertEquals("Missing credentials should return a 401", 401, response.getStatusLine().getStatusCode()); - JsonNode responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - String errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); - String errorMessage = responseMessage.get("message").asText(); - assertTrue("Error message should be " + IRCTResourceRS.MISSING_CREDENTIALS_MESSAGE, errorMessage.contains(IRCTResourceRS.MISSING_CREDENTIALS_MESSAGE)); - - - credentials.put(IRCTResourceRS.IRCT_BEARER_TOKEN_KEY, "anIncorrectToken"); - queryRequest.setResourceCredentials(credentials); - body = objectMapper.writeValueAsString(queryRequest); - response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/search", headers, body); - assertEquals("Incorrect token should return a 401", 401, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); -/* errorMessage = responseMessage.get("message").asText(); - assertTrue("Error message should be " + IRCTResourceRS.MISSING_CREDENTIALS_MESSAGE, errorMessage.contains(IRCTResourceRS.MISSING_CREDENTIALS_MESSAGE)); -*/ - - //Should throw an error if missing query string - credentials.put(IRCTResourceRS.IRCT_BEARER_TOKEN_KEY, token); - queryRequest.setResourceCredentials(credentials); - queryRequest.setQuery(null); - body = objectMapper.writeValueAsString(queryRequest); - response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/search", headers, body); - assertEquals("Missing query string should return a 500",500, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); - errorMessage = responseMessage.get("message").asText(); - assertEquals("Error message should be " + ProtocolException.MISSING_DATA, ProtocolException.MISSING_DATA, errorMessage); - - - queryRequest.setQuery("%antibody%"); -// body = objectMapper.writeValueAsString(queryRequest); -// response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/search", headers, body); -// assertEquals("Missing target URL should return a 500",500, response.getStatusLine().getStatusCode()); -// responseMessage = objectMapper.readTree(response.getEntity().getContent()); -// assertNotNull("Response message should not be null", responseMessage); -// errorMessage = responseMessage.get("message").asText(); -// assertEquals("Error message should be " + ApplicationException.MISSING_TARGET_URL, ApplicationException.MISSING_TARGET_URL, errorMessage); - - //This should work - body = objectMapper.writeValueAsString(queryRequest); - response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/search", headers, body); - assertEquals("Correct request should return a 200",200, response.getStatusLine().getStatusCode()); - JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Result should not be null", responseNode); - assertNotNull("Search results should not be null", responseNode.get("results")); - assertFalse("Search results should not be empty", responseNode.get("results").size() == 0); - assertEquals("Searchquery should match input query", "%antibody%", responseNode.get("searchQuery").asText()); - - //Valid request with no results should return an empty result - queryRequest.setQuery("thisShouldFindNothing"); - body = objectMapper.writeValueAsString(queryRequest); - response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/search", headers, body); - assertEquals("Correct request should return a 200",200, response.getStatusLine().getStatusCode()); - responseNode = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Result should not be null", responseNode); - assertNotNull("Search results should not be null", responseNode.get("results")); - assertTrue("Search results should be empty", responseNode.get("results").size() == 0); - assertEquals("Searchquery should match input query", "thisShouldFindNothing", responseNode.get("searchQuery").asText()); - } - - @Test - public void testQuery() throws UnsupportedOperationException, IOException { - Map resourceResponse = new HashMap<>(); - resourceResponse.put("resultId", "230958"); - resourceResponse.put("status", "AVAILABLE"); - - wireMockRule.stubFor(any(urlPathMatching("/queryService/runQuery")) - .withHeader("Authorization", containing("anIncorrectToken")) - .willReturn(aResponse() - .withStatus(401))); - - wireMockRule.stubFor(any(urlPathMatching("/queryService/runQuery")) - .withRequestBody(equalTo("poorlyWordedQueryString")) - .willReturn(aResponse() - .withStatus(500))); - - wireMockRule.stubFor(any(urlPathMatching("/queryService/runQuery")) - .withRequestBody(containing("/i2b2-nhanes/Demo")) - .withHeader("Authorization", containing(token)) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(resourceResponse)))); - - wireMockRule.stubFor(any(urlPathMatching("/resultService/resultStatus/.*")) - .withHeader("Authorization", containing(token)) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(resourceResponse)))); - - - QueryRequest queryRequest = new GeneralQueryRequest(); - Map credentials = new HashMap(); - queryRequest.setResourceCredentials(credentials); - queryRequest.setQuery(queryString); - String body = objectMapper.writeValueAsString(queryRequest); - - //Should throw an error if credentials missing or wrong - HttpResponse response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/query", headers, body); - assertEquals("Missing credentials should return a 401", 401, response.getStatusLine().getStatusCode()); - JsonNode responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - String errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); - String errorMessage = responseMessage.get("message").asText(); - assertTrue("Error message should be " + IRCTResourceRS.MISSING_CREDENTIALS_MESSAGE, errorMessage.contains(IRCTResourceRS.MISSING_CREDENTIALS_MESSAGE)); - - credentials.put(IRCTResourceRS.IRCT_BEARER_TOKEN_KEY, "anIncorrectToken"); - queryRequest.setResourceCredentials(credentials); - body = objectMapper.writeValueAsString(queryRequest); - response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/query", headers, body); - assertEquals("Incorrect token should return a 401",401, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); - - //Should throw an error if missing query string - credentials.put(IRCTResourceRS.IRCT_BEARER_TOKEN_KEY, token); - queryRequest.setResourceCredentials(credentials); - queryRequest.setQuery(null); - body = objectMapper.writeValueAsString(queryRequest); - response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/query", headers, body); - assertEquals("Missing query string should return a 500",500, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); - errorMessage = responseMessage.get("message").asText(); - assertTrue("Error message should be " + ProtocolException.MISSING_DATA, errorMessage.contains(ProtocolException.MISSING_DATA)); - - //Try a poorly worded queryString - queryRequest.setQuery("poorlyWordedQueryString"); - body = objectMapper.writeValueAsString(queryRequest); - response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/query", headers, body); - assertEquals("Incorrectly formatted string should return 500",500, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "ri_error", errorType); - /*errorMessage = responseMessage.get("message").asText(); - assertTrue("Error message should be " + IRCTResourceRS.MISSING_CREDENTIALS_MESSAGE, errorMessage.contains(IRCTResourceRS.MISSING_CREDENTIALS_MESSAGE)); -*/ - - queryRequest.setQuery(queryString); -// body = objectMapper.writeValueAsString(queryRequest); -// response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/query", headers, body); -// assertEquals("Missing target URL should return 500",500, response.getStatusLine().getStatusCode()); -// responseMessage = objectMapper.readTree(response.getEntity().getContent()); -// assertNotNull("Response message should not be null", responseMessage); -// errorMessage = responseMessage.get("message").asText(); -// assertEquals("Error message should be " + ApplicationException.MISSING_TARGET_URL, ApplicationException.MISSING_TARGET_URL, errorMessage); - - JsonNode jsonNode = objectMapper.readTree(queryString); - - //Request can be an object that also requests the format - ObjectNode queryNode = objectMapper.createObjectNode(); - queryNode.put("queryString", jsonNode); - queryRequest.setQuery(queryNode); - - body = objectMapper.writeValueAsString(queryRequest); - response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/query", headers, body); - assertEquals(200, response.getStatusLine().getStatusCode()); - QueryStatus result = readObjectFromResponse(response, QueryStatus.class); - assertNotNull("Result should not be null", result); - //Make sure all necessary fields are present - assertNotNull("Status should not be null",result.getStatus()); - assertNotNull("ResourceResultId should not be null",result.getResourceResultId()); - - //Or else just a query - queryRequest.setQuery(jsonNode); - body = objectMapper.writeValueAsString(queryRequest); - response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/query", headers, body); - assertEquals(200, response.getStatusLine().getStatusCode()); - result = readObjectFromResponse(response, QueryStatus.class); - assertNotNull("Result should not be null", result); - //Make sure all necessary fields are present - assertNotNull("Status should not be null",result.getStatus()); - assertNotNull("ResourceResultId should not be null",result.getResourceResultId()); - } - - @Test - public void testQueryResult() throws UnsupportedOperationException, IOException { - String resultResponse = "aResultOfSomeKind"; - - wireMockRule.stubFor(any(urlPathMatching("/resultService/result/.*")) - .withHeader("Authorization", containing("anIncorrectToken")) - .willReturn(aResponse() - .withStatus(401))); - - wireMockRule.stubFor(any(urlPathMatching("/resultService/result/111/.*")) - .willReturn(aResponse() - .withStatus(500))); - - wireMockRule.stubFor(any(urlPathMatching("/resultService/result/" + testQueryResultId + "/.*")) - .withHeader("Authorization", containing(token)) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(resultResponse)))); - - QueryRequest queryRequest = new GeneralQueryRequest(); - Map credentials = new HashMap(); - queryRequest.setResourceCredentials(credentials); - String body = objectMapper.writeValueAsString(queryRequest); - - //Should throw an error if credentials missing or wrong - HttpResponse response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/query/"+testQueryResultId+"/result", headers, body); - assertEquals("Missing credentials should return a 401",401, response.getStatusLine().getStatusCode()); - JsonNode responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - String errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); - String errorMessage = responseMessage.get("message").asText(); - assertTrue("Error message should be " + IRCTResourceRS.MISSING_CREDENTIALS_MESSAGE, errorMessage.contains(IRCTResourceRS.MISSING_CREDENTIALS_MESSAGE)); - - - credentials.put(IRCTResourceRS.IRCT_BEARER_TOKEN_KEY, "anIncorrectToken"); - body = objectMapper.writeValueAsString(queryRequest); - response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/query/"+testQueryResultId+"/result", headers, body); - assertEquals("Incorrect token should return a 401",401, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); - - credentials.put(IRCTResourceRS.IRCT_BEARER_TOKEN_KEY, token); - body = objectMapper.writeValueAsString(queryRequest); - - //TODO This is just returning what IRCT returns - do we need to test it? - //False query id should return a failure message - response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/query/111/result", headers, body); - assertEquals("Should return a 500",500, response.getStatusLine().getStatusCode()); - -// body = objectMapper.writeValueAsString(queryRequest); -// response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/query/"+testQueryResultId+"/result", headers, body); -// assertEquals("Missing target URL should return 500",500, response.getStatusLine().getStatusCode()); -// responseMessage = objectMapper.readTree(response.getEntity().getContent()); -// assertNotNull("Response message should not be null", responseMessage); -// errorMessage = responseMessage.get("message").asText(); -// assertEquals("Error message should be " + ApplicationException.MISSING_TARGET_URL, ApplicationException.MISSING_TARGET_URL, errorMessage); - - //This should work - body = objectMapper.writeValueAsString(queryRequest); - response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/query/"+testQueryResultId+"/result", headers, body); - assertEquals("Correct request should return a 200",200, response.getStatusLine().getStatusCode()); - String responseBody = EntityUtils.toString(response.getEntity().getContent(), "UTF-8"); - assertFalse("Response content should not be empty", responseBody.isEmpty()); - } - - @Test - public void testQueryStatus() throws UnsupportedOperationException, IOException { - Map resourceResponse = new HashMap<>(); - resourceResponse.put("resultId", "230958"); - resourceResponse.put("status", "AVAILABLE"); - - wireMockRule.stubFor(any(urlPathMatching("/resultService/resultStatus/.*")) - .withHeader("Authorization", containing("anIncorrectToken")) - .willReturn(aResponse() - .withStatus(401))); - - wireMockRule.stubFor(any(urlPathMatching("/resultService/resultStatus/111/.*")) - .willReturn(aResponse() - .withStatus(500))); - - wireMockRule.stubFor(any(urlPathMatching("/resultService/resultStatus/" + testQueryResultId)) - .withHeader("Authorization", containing(token)) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(resourceResponse)))); - - QueryRequest request = new GeneralQueryRequest(); - Mapcredentials = new HashMap(); - request.setResourceCredentials(credentials); - String body = objectMapper.writeValueAsString(request); - - //Should throw an error if credentials missing or wrong - HttpResponse response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/query/"+testQueryResultId+"/status", headers, body); - assertEquals("Missing credentials should return a 401", 401, response.getStatusLine().getStatusCode()); - JsonNode responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - String errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); - String errorMessage = responseMessage.get("message").asText(); - assertTrue("Error message should be " + IRCTResourceRS.MISSING_CREDENTIALS_MESSAGE, errorMessage.contains(IRCTResourceRS.MISSING_CREDENTIALS_MESSAGE)); - - - credentials.put(IRCTResourceRS.IRCT_BEARER_TOKEN_KEY, "anIncorrectToken"); - request.setResourceCredentials(credentials); - body = objectMapper.writeValueAsString(request); - response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/query/"+testQueryResultId+"/status", headers, body); - assertEquals("Incorrect token should return a 401",401, response.getStatusLine().getStatusCode()); - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "error", errorType); -/* errorMessage = responseMessage.get("message").asText(); - assertTrue("Error message should be " + IRCTResourceRS.MISSING_CREDENTIALS_MESSAGE, errorMessage.contains(IRCTResourceRS.MISSING_CREDENTIALS_MESSAGE)); -*/ - - credentials.put(IRCTResourceRS.IRCT_BEARER_TOKEN_KEY, token); - request.setResourceCredentials(credentials); -// body = objectMapper.writeValueAsString(request); -// response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/query/"+testQueryResultId+"/status", headers, body); -// assertEquals("Missing target URL should return 500",500, response.getStatusLine().getStatusCode()); -// responseMessage = objectMapper.readTree(response.getEntity().getContent()); -// assertNotNull("Response message should not be null", responseMessage); -// errorMessage = responseMessage.get("message").asText(); -// assertEquals("Error message should be " + ApplicationException.MISSING_TARGET_URL, ApplicationException.MISSING_TARGET_URL, errorMessage); - - //This should work - body = objectMapper.writeValueAsString(request); - response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/query/"+testQueryResultId+"/status", headers, body); - assertEquals("Correct request should return a 200",200, response.getStatusLine().getStatusCode()); - QueryStatus queryStatus = readObjectFromResponse(response, QueryStatus.class); - assertNotNull("Result should not be null", queryStatus); - //Make sure all necessary fields are present - //TODO The numerical values are set in PicSureRS layer, may not apply here - assertNotNull("Duration should not be null",queryStatus.getDuration()); - - assertNotNull("Expiration should not be null",queryStatus.getExpiration()); - assertNotNull("ResourceStatus should not be null",queryStatus.getResourceStatus()); - assertNotNull("Status should not be null",queryStatus.getStatus()); - assertNotNull("Starttime should not be null",queryStatus.getStartTime()); - - //Try a queryId that doesn't exist - response = retrievePostResponse(irctEndpointUrl+"pic-sure/v1.4/query/111/status", headers, body); - //assertEquals("Nonexistent queryId should return a 500",500, response.getStatusLine().getStatusCode()); - - responseMessage = objectMapper.readTree(response.getEntity().getContent()); - assertNotNull("Response message should not be null", responseMessage); - errorType = responseMessage.get("errorType").asText(); - assertEquals("Error type should be error", "ri_error", errorType); - - //TODO Do we need to check for different statuses? If so, how? - } -} diff --git a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/PicsureInfoServiceIT.java b/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/PicsureInfoServiceIT.java deleted file mode 100644 index 921092b6..00000000 --- a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/PicsureInfoServiceIT.java +++ /dev/null @@ -1,56 +0,0 @@ -package edu.harvard.dbmi.avillach; - -import edu.harvard.dbmi.avillach.domain.QueryFormat; -import edu.harvard.dbmi.avillach.domain.QueryRequest; -import edu.harvard.dbmi.avillach.domain.ResourceInfo; -import edu.harvard.dbmi.avillach.util.HttpClientUtil; -import edu.harvard.hms.dbmi.avillach.IRCTResourceRS; -import org.apache.http.HttpResponse; -import org.junit.Test; - -import java.util.*; - -import static com.github.tomakehurst.wiremock.client.WireMock.*; -import static edu.harvard.dbmi.avillach.util.HttpClientUtil.composeURL; -import static edu.harvard.dbmi.avillach.util.HttpClientUtil.retrievePostResponse; -import static org.junit.Assert.*; - -public class PicsureInfoServiceIT extends BaseIT { - - @Test - public void testInfoEndpoints() throws Exception { - //We don't know what resource we fetched, so we don't want to actually try to reach it - //Result to return - List qfs = new ArrayList<>(); - - wireMockRule.stubFor(any(anyUrl()) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(qfs)))); - - String uri = composeURL(endpointUrl, "/info/" + resourceId); - - - HttpResponse response = retrievePostResponse(uri, headers, ""); - assertEquals("Missing credentials should return 401", 401, response.getStatusLine().getStatusCode()); - - QueryRequest infoRequest = new GeneralQueryRequest(); - Map clientCredentials = new HashMap(); - //TODO I guess we need some way to identify the token key? Maybe V1.4_BEARER_TOKEN - clientCredentials.put(IRCTResourceRS.IRCT_BEARER_TOKEN_KEY, "testToken"); - infoRequest.setResourceCredentials(clientCredentials); - String body = objectMapper.writeValueAsString(infoRequest); - - response = retrievePostResponse(uri, headers, body); - assertEquals("Response status code should be 200", 200, response.getStatusLine().getStatusCode()); - ResourceInfo responseInfo = HttpClientUtil.readObjectFromResponse(response, ResourceInfo.class); - assertNotNull("Resource response should not be null", responseInfo); - assertNotNull("Resource response should have queryFormats", responseInfo.getQueryFormats()); - assertNotNull("Resource response should have a name", responseInfo.getName()); - - //Try with a non-existent id - uri = composeURL(endpointUrl , "/info/3b2437fe-df56-4360-8156-27bcf0b1a467"); - response = retrievePostResponse(uri, headers, body); - assertEquals("Incorrect resource Id should return 500", 500, response.getStatusLine().getStatusCode()); - } -} \ No newline at end of file diff --git a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/PicsureQueryServiceIT.java b/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/PicsureQueryServiceIT.java deleted file mode 100644 index 82977041..00000000 --- a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/PicsureQueryServiceIT.java +++ /dev/null @@ -1,169 +0,0 @@ -package edu.harvard.dbmi.avillach; - -import com.fasterxml.jackson.databind.JsonNode; -import edu.harvard.dbmi.avillach.domain.QueryRequest; -import edu.harvard.dbmi.avillach.domain.QueryStatus; -import edu.harvard.hms.dbmi.avillach.IRCTResourceRS; -import org.apache.commons.io.IOUtils; -import org.apache.http.HttpResponse; -import org.apache.http.util.EntityUtils; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; - -import java.util.*; - -import static com.github.tomakehurst.wiremock.client.WireMock.*; -import static edu.harvard.dbmi.avillach.util.HttpClientUtil.composeURL; -import static edu.harvard.dbmi.avillach.util.HttpClientUtil.retrievePostResponse; -import static org.junit.Assert.*; - -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class PicsureQueryServiceIT extends BaseIT { - - private final static String token = "fakeToken"; - - private static String resultId; - - //This only needs to be a jsonnode, does not need to be a fully functional query - private static final String queryString = "{ \"select\": \"alias\" }"; - - @Test - public void testQuery() throws Exception { - //We don't know what resource we fetched, so we don't want to actually try to reach it - //Result to return - HashMap anyOldResult = new HashMap<>(); - //TODO: This might be different for different resources - anyOldResult.put("resultId", "123abc"); - anyOldResult.put("status", "RUNNING"); - - //TODO make this work for different resources - wireMockRule.stubFor(any(urlPathMatching("/queryService/runQuery")) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(anyOldResult)))); - - wireMockRule.stubFor(any(urlPathMatching("/resultService/resultStatus/.*")) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(anyOldResult)))); - - - //Test missing info - String uri = composeURL(endpointUrl, "/query/"); - QueryRequest dataQueryRequest = new GeneralQueryRequest(); - HttpResponse response = retrievePostResponse(uri, headers, ""); - assertEquals("Missing query request info should return 500", 500, response.getStatusLine().getStatusCode()); - EntityUtils.consume(response.getEntity()); - - //Test missing query string - Map clientCredentials = new HashMap(); - clientCredentials.put(IRCTResourceRS.IRCT_BEARER_TOKEN_KEY, token); - dataQueryRequest.setResourceCredentials(clientCredentials); - response = retrievePostResponse(uri, headers, objectMapper.writeValueAsString(dataQueryRequest)); - assertEquals("Missing query info should return 500", 500, response.getStatusLine().getStatusCode()); - EntityUtils.consume(response.getEntity()); - - //Test missing resourceId - JsonNode jsonNode = objectMapper.readTree(queryString); - dataQueryRequest.setQuery(jsonNode); - response = retrievePostResponse(uri, headers, objectMapper.writeValueAsString(dataQueryRequest)); - assertEquals("Missing resource Id should return 500", 500, response.getStatusLine().getStatusCode()); - EntityUtils.consume(response.getEntity()); - - //Test correct request - dataQueryRequest.setResourceUUID(resourceId); - response = retrievePostResponse(uri, headers, objectMapper.writeValueAsString(dataQueryRequest)); - assertEquals("Response should be 200", 200, response.getStatusLine().getStatusCode()); - - QueryStatus results = objectMapper.readValue(response.getEntity().getContent(), QueryStatus.class); - assertNotNull("Status should not be null", results.getStatus()); - assertNotNull("Resource result id should not be null", results.getResourceResultId()); - assertNotNull("Resource Status should not be null", results.getResourceStatus()); - assertNotNull("Picsure result id should not be null", results.getPicsureResultId()); - //Store this resultId to use in subsequent tests - resultId = results.getPicsureResultId().toString(); - } - - @Test - public void testQueryStatus() throws Exception { - - QueryRequest statusRequest = new GeneralQueryRequest(); - Map clientCredentials = new HashMap(); - clientCredentials.put(IRCTResourceRS.IRCT_BEARER_TOKEN_KEY, token); - statusRequest.setResourceCredentials(clientCredentials); - //Result to return - HashMap anyOldResult = new HashMap<>(); - //TODO: This might be different for different resources - anyOldResult.put("resultId", "123abc"); - anyOldResult.put("status", "RUNNING"); - - wireMockRule.stubFor(any(urlPathMatching("/resultService/resultStatus/.*")) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(anyOldResult)))); - - //Test it without credentials - String uri = composeURL(endpointUrl , "/query/"+resultId.toString() + "/status"); - HttpResponse response = retrievePostResponse(uri, headers, objectMapper.writeValueAsString(new HashMap())); - assertEquals("Missing credentials should return 401", 401, response.getStatusLine().getStatusCode()); - EntityUtils.consume(response.getEntity()); - - response = retrievePostResponse(uri, headers, objectMapper.writeValueAsString(statusRequest)); - assertEquals("Response should be 200", 200, response.getStatusLine().getStatusCode()); - QueryStatus results = objectMapper.readValue(response.getEntity().getContent(), QueryStatus.class); - assertNotNull("Status should not be null", results.getStatus()); - assertNotNull("Resource result id should not be null", results.getResourceResultId()); - assertNotNull("Resource Status should not be null", results.getResourceStatus()); - - //Nonexistent resultId - uri = composeURL(endpointUrl , "/query/20f22062-f63b-4bca-919e-fcfd8d41d15c/status"); - response = retrievePostResponse(uri, headers, objectMapper.writeValueAsString(statusRequest)); - assertEquals("Nonexistent resultId should return 500", 500, response.getStatusLine().getStatusCode()); - EntityUtils.consume(response.getEntity()); - - //Not a uuid - uri = composeURL(endpointUrl , "/query/20f2241d15c/status"); - response = retrievePostResponse(uri, headers, objectMapper.writeValueAsString(statusRequest)); - assertEquals("Incorrectly formatted resultId should return 404", 404, response.getStatusLine().getStatusCode()); - EntityUtils.consume(response.getEntity()); - } - - @Test - public void testResult() throws Exception { - - wireMockRule.stubFor(any(urlPathMatching("/resultService/result/.*")) - .willReturn(aResponse() - .withStatus(200) - .withBody("anyOldResultWillDo"))); - - QueryRequest resultRequest = new GeneralQueryRequest(); - Map clientCredentials = new HashMap(); - clientCredentials.put(IRCTResourceRS.IRCT_BEARER_TOKEN_KEY, token); - resultRequest.setResourceCredentials(clientCredentials); - - String uri = composeURL(endpointUrl , "/query/"+resultId.toString() + "/result"); - HttpResponse response = retrievePostResponse(uri, headers, objectMapper.writeValueAsString(new HashMap())); - assertEquals("Missing credentials should return 401", 401, response.getStatusLine().getStatusCode()); - EntityUtils.consume(response.getEntity()); - - //This should return some kind of result - response = retrievePostResponse(uri, headers, objectMapper.writeValueAsString(resultRequest)); - assertEquals("Correct request should return a 200", 200, response.getStatusLine().getStatusCode()); - String result = EntityUtils.toString(response.getEntity().getContent(), "UTF-8"); - assertNotNull("Result should not be null, result"); - - //Nonexistent resultId - uri = composeURL(endpointUrl , "/query/20f22062-f63b-4bca-919e-fcfd8d41d15c/result"); - response = retrievePostResponse(uri, headers, objectMapper.writeValueAsString(resultRequest)); - assertEquals("Nonexistent resultId should return 500", 500, response.getStatusLine().getStatusCode()); - EntityUtils.consume(response.getEntity()); - - //Not a uuid - uri = composeURL(endpointUrl , "/query/20f2241d15c/result"); - response = retrievePostResponse(uri, headers, objectMapper.writeValueAsString(resultRequest)); - assertEquals("Incorrectly formatted resultId should return 404", 404, response.getStatusLine().getStatusCode()); - EntityUtils.consume(response.getEntity()); - } - -} diff --git a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/PicsureSearchServiceIT.java b/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/PicsureSearchServiceIT.java deleted file mode 100644 index 9538d463..00000000 --- a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/PicsureSearchServiceIT.java +++ /dev/null @@ -1,57 +0,0 @@ -package edu.harvard.dbmi.avillach; - -import edu.harvard.dbmi.avillach.domain.QueryRequest; -import edu.harvard.dbmi.avillach.domain.SearchResults; -import edu.harvard.hms.dbmi.avillach.IRCTResourceRS; -import org.apache.http.HttpResponse; -import org.apache.http.util.EntityUtils; -import org.junit.Test; - -import java.util.*; - -import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; -import static com.github.tomakehurst.wiremock.client.WireMock.any; -import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; -import static edu.harvard.dbmi.avillach.util.HttpClientUtil.composeURL; -import static edu.harvard.dbmi.avillach.util.HttpClientUtil.retrievePostResponse; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -public class PicsureSearchServiceIT extends BaseIT{ - - @Test - public void testSearch() throws Exception { - //We don't know what resource we fetched, so we don't want to actually try to reach it - //Result to return - HashMap anyOldResult = new HashMap<>(); - - wireMockRule.stubFor(any(anyUrl()) - .willReturn(aResponse() - .withStatus(200) - .withBody(objectMapper.writeValueAsString(anyOldResult)))); - - - String uri = composeURL(endpointUrl, "/search/"+resourceId); - HttpResponse response = retrievePostResponse(uri, headers, ""); - assertEquals("Missing query request info should return 500", 500, response.getStatusLine().getStatusCode()); - EntityUtils.consume(response.getEntity()); - - QueryRequest searchQueryRequest = new GeneralQueryRequest(); - Map clientCredentials = new HashMap(); - //TODO This needs to not assume which resource is being used/what the token is... what to do!!... maybe we don't need to test this because it's tested by individual resourceRs tests? - clientCredentials.put(IRCTResourceRS.IRCT_BEARER_TOKEN_KEY, "testToken"); - searchQueryRequest.setResourceCredentials(clientCredentials); - response = retrievePostResponse(uri, headers, objectMapper.writeValueAsString(searchQueryRequest)); - assertEquals("Missing query search info should return 500", 500, response.getStatusLine().getStatusCode()); - EntityUtils.consume(response.getEntity()); - - searchQueryRequest.setQuery("blood"); - response = retrievePostResponse(uri, headers, objectMapper.writeValueAsString(searchQueryRequest)); - assertEquals("Response should be 200", 200, response.getStatusLine().getStatusCode()); - - SearchResults results = objectMapper.readValue(response.getEntity().getContent(), SearchResults.class); - assertNotNull("Results should not be null", results.getResults()); - assertEquals("Searchquery should match input query", results.getSearchQuery(), "blood"); - - } -} diff --git a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/ResourceIT.java b/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/ResourceIT.java deleted file mode 100644 index d25fa336..00000000 --- a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/ResourceIT.java +++ /dev/null @@ -1,29 +0,0 @@ -package edu.harvard.dbmi.avillach; - -import static org.junit.Assert.*; - -import org.apache.http.client.methods.HttpGet; -import org.junit.Test; - -import edu.harvard.dbmi.avillach.data.entity.Resource; - -import javax.ws.rs.core.HttpHeaders; - -public class ResourceIT extends BaseIT{ - - @Test - public void testListResources() throws Exception { - - String jwt = generateJwtForSystemUser(); - HttpGet get = new HttpGet(endpointUrl + "/info/resources"); - get.setHeader(HttpHeaders.AUTHORIZATION, "Bearer "+ jwt); - org.apache.http.HttpResponse response = client.execute(get); - assertEquals("Response status code should be 200", 200, response.getStatusLine().getStatusCode()); - Resource[] resources = objectMapper.readValue(response.getEntity().getContent(), Resource[].class); - - assertEquals("The first resource should be named nhanes.hms.harvard.edu", "nhanes.hms.harvard.edu", resources[0].getName()); - assertEquals("The first resource should have description HMS DBMI NHANES PIC-SURE 1.4", "HMS DBMI NHANES PIC-SURE 1.4 Supply token with key 'IRCT_BEARER_TOKEN'", resources[0].getDescription()); - assertNotNull("The first resource should have a valid UUID", resources[0].getUuid()); - } - -} diff --git a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/SystemServiceIT.java b/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/SystemServiceIT.java deleted file mode 100755 index 3a578622..00000000 --- a/pic-sure-api-wildfly/src/test/java/edu/harvard/dbmi/avillach/SystemServiceIT.java +++ /dev/null @@ -1,51 +0,0 @@ -package edu.harvard.dbmi.avillach; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -import javax.ws.rs.core.HttpHeaders; - -import com.fasterxml.jackson.databind.JsonNode; -import org.apache.commons.io.IOUtils; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.junit.Test; - - -public class SystemServiceIT extends BaseIT { - - @Test - public void testStatusIsAccessibleToSystemUser() throws Exception { - try { - String jwt = generateJwtForSystemUser(); - HttpGet get = new HttpGet(endpointUrl + "/system/status"); - get.setHeader(HttpHeaders.AUTHORIZATION, "Bearer "+ jwt); - HttpResponse response = client.execute(get); - assertEquals("Response status code should be 200", 200, response.getStatusLine().getStatusCode()); - assertEquals("System status should be RUNNING'", "RUNNING", EntityUtils.toString(response.getEntity().getContent(), "UTF-8")); - - }catch(Exception e) { - fail("Exception: " + e.getMessage()); - } - } - - @Test - public void testStatusIsNotAccessibleToNonSystemUser() { - - try{ - String jwt = generateJwtForNonSystemUser(); - - HttpGet get = new HttpGet(endpointUrl + "/system/status"); - get.setHeader(HttpHeaders.AUTHORIZATION, "Bearer "+ jwt); - HttpResponse response = client.execute(get); - assertEquals("Request should be rejected with 401", 401, response.getStatusLine().getStatusCode()); - JsonNode responseBody = objectMapper.readTree(response.getEntity().getContent()); - assertEquals("Message should report: User is not authorized. [doesn't match the required role restrictions.]", "User is not authorized. [doesn't match the required role restrictions.]", responseBody.get("message").asText()); - - }catch(Exception e) { - fail("Exception: " + e.getMessage()); - } - } - - -}