diff --git a/src/test/java/gov/nih/nci/bento/EsServiceTest.java b/src/test/java/gov/nih/nci/bento/EsServiceTest.java deleted file mode 100644 index 33b31ef..0000000 --- a/src/test/java/gov/nih/nci/bento/EsServiceTest.java +++ /dev/null @@ -1,85 +0,0 @@ -package gov.nih.nci.bento; - -import gov.nih.nci.bento.service.ESService; -import gov.nih.nci.bento.utility.TypeChecker; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -import java.util.List; -import java.util.Map; -import java.util.Set; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -@RunWith( SpringRunner.class ) -@SpringBootTest -public class EsServiceTest { - @Autowired - private ESService esService; - - @Test - public void testbuildListQuery() { - Map params = Map.of( - "param1", List.of("value1", "value2") - ); - Map builtQuery = esService.buildListQuery(params, Set.of()); - Map bool = null; - List> filter = null; - List param1 = null; - Map query = null; - Object boolRaw = null; - Object filterRaw = null; - Object queryRaw = null; - Object termsRaw = null; - - assertNotNull(builtQuery); - - queryRaw = builtQuery.get("query"); - - if (TypeChecker.isMapStringObject(queryRaw)) { - @SuppressWarnings("unchecked") - Map castedQuery = (Map) queryRaw; - query = castedQuery; - } - - assertNotNull(query); - - boolRaw = query.get("bool"); - - if (TypeChecker.isMapStringObject(boolRaw)) { - @SuppressWarnings("unchecked") - Map castedBool = (Map) query.get("bool"); - bool = castedBool; - } - - assertNotNull(bool); - - filterRaw = bool.get("filter"); - - if (TypeChecker.isListOfMapStringObject(filterRaw)) { - @SuppressWarnings("unchecked") - List> castedFilter = (List>) bool.get("filter"); - filter = castedFilter; - } - - assertNotNull(filter); - assertEquals(1, filter.size()); - - termsRaw = filter.get(0).get("terms"); - - if (TypeChecker.isMapStringListOfType(termsRaw, String.class)) { - @SuppressWarnings("unchecked") - Map> castedTerms = (Map>) termsRaw; - param1 = castedTerms.get("param1"); - } - - assertEquals(2, param1.size()); - assertEquals("value1", param1.get(0)); - assertEquals("value2", param1.get(1)); - } -} diff --git a/src/test/java/gov/nih/nci/bento/GraphQLControllerTest.java b/src/test/java/gov/nih/nci/bento/GraphQLControllerTest.java deleted file mode 100644 index 93fdb3b..0000000 --- a/src/test/java/gov/nih/nci/bento/GraphQLControllerTest.java +++ /dev/null @@ -1,127 +0,0 @@ -package gov.nih.nci.bento; - -import com.google.gson.Gson; -import com.google.gson.JsonObject; -import gov.nih.nci.bento.controller.GraphQLController; -import gov.nih.nci.bento.model.ConfigurationDAO; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.MvcResult; -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; -import org.springframework.test.web.servlet.result.MockMvcResultMatchers; - -import static org.junit.Assert.*; - -@RunWith(SpringRunner.class) -@WebMvcTest(GraphQLController.class) -public class GraphQLControllerTest { - - @Autowired - private MockMvc mockMvc; - - @Autowired - private ConfigurationDAO configurationDAO; - - /** - * Confirm that the "/version" endpoint accepts GET requests and verify the following within the response: - * - Http Status Code is 200 (OK) - * - Content Type is "application/json;charset=utf-8" - * - Content matches the String "Bento API Version: xx.xx.xx" where the version number matches - * "bento.api.version" from the application.properties file - * - * @throws Exception - */ - @Test - public void versionEndpointTestGET() throws Exception { - String expectedVersion = "Bento API Version: "+configurationDAO.getBentoApiVersion(); - MvcResult result = this.mockMvc.perform(MockMvcRequestBuilders.get("/version")) - .andExpect(MockMvcResultMatchers.status().isOk()) - .andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=utf-8")) - .andExpect(MockMvcResultMatchers.content().string(expectedVersion)) - .andReturn(); - //assert method to satisfy codacy requirement, this statement will not be reached if the test fails - assertNotNull(result); - } - - /** - * Confirm that the "/version" endpoint does NOT accept POST requests and verify the following within the response: - * - Http Status Code is 405 (METHOD NOT ALLOWED) - * - * @throws Exception - */ - @Test - public void versionEndpointTestPOST() throws Exception { - MvcResult result = this.mockMvc.perform(MockMvcRequestBuilders.post("/version")) - .andExpect(MockMvcResultMatchers.status().isMethodNotAllowed()) - .andReturn(); - //assert method to satisfy codacy requirement, this statement will not be reached if the test fails - assertNotNull(result); - } - - /** - * Confirm that the "/v1/graphql/" endpoint does NOT accept GET requests and verify the following within the - * response: - * - Http Status Code is 405 (METHOD NOT ALLOWED) - * - * @throws Exception - */ - @Test - public void graphQLEndpointTestGET() throws Exception { - MvcResult result = this.mockMvc.perform(MockMvcRequestBuilders.get("/v1/graphql/")) - .andExpect(MockMvcResultMatchers.status().isMethodNotAllowed()) - .andReturn(); - //assert method to satisfy codacy requirement, this statement will not be reached if the test fails - assertNotNull(result); - } - - /** - * Confirm that the "/v1/graphql/" endpoint accepts POST requests and verify the following within the response when - * sent an empty GraphQL request: - * - Http Status Code is 400 (BAD REQUEST) - * - * @throws Exception - */ - @Test - public void graphQLEndpointTestPOSTEmptyRequest() throws Exception { - MvcResult result = this.mockMvc.perform(MockMvcRequestBuilders - .post("/v1/graphql/") - .contentType("application/json") - .content("{\"query\":\"\",\"variables\":{}}")) - .andExpect(MockMvcResultMatchers.status().isBadRequest()) - .andReturn(); - //assert method to satisfy codacy requirement, this statement will not be reached if the test fails - assertNotNull(result); - } - - /** - * Confirm that the "/v1/graphql/" endpoint accepts POST requests and verify the following within the response when - * sent a valid GraphQL request containing a query NOT in the schema: - * - Http Status Code is 200 (OK) - * - Content Type is "application/json;charset=utf-8" - * - Content contains the expected "ValidationError" - * - * @throws Exception - */ - @Test - public void graphQLEndpointTestPOSTTestQuery() throws Exception { - MvcResult result = mockMvc.perform(MockMvcRequestBuilders - .post("/v1/graphql/") - .contentType("application/json") - .content("{\"query\":\"{testQuery}\",\"variables\":{}}")) - .andExpect(MockMvcResultMatchers.status().isOk()) - .andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=utf-8")) - .andReturn(); - Gson gson = new Gson(); - JsonObject jsonObject = gson.fromJson(result.getResponse().getContentAsString(), JsonObject.class); - assertTrue(jsonObject.keySet().contains("errors")); - JsonObject error = jsonObject.getAsJsonArray("errors").get(0).getAsJsonObject(); - assertTrue(error.keySet().contains("extensions")); - JsonObject extensions = error.getAsJsonObject("extensions"); - assertTrue(extensions.keySet().contains("classification")); - assertEquals("ValidationError", extensions.get("classification").getAsString()); - } -} diff --git a/src/test/java/gov/nih/nci/bento/IndexControllerTest.java b/src/test/java/gov/nih/nci/bento/IndexControllerTest.java index 11f7842..05634a8 100644 --- a/src/test/java/gov/nih/nci/bento/IndexControllerTest.java +++ b/src/test/java/gov/nih/nci/bento/IndexControllerTest.java @@ -1,25 +1,28 @@ package gov.nih.nci.bento; import gov.nih.nci.bento.controller.IndexController; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; import static org.junit.Assert.assertNotNull; @RunWith(SpringRunner.class) -@WebMvcTest(IndexController.class) public class IndexControllerTest { - @Autowired private MockMvc mockMvc; + @Before + public void setup() { + this.mockMvc = MockMvcBuilders.standaloneSetup(new IndexController()).build(); + } + /** * Confirm that the "/ping" endpoint accept GET requests and verify the following within the response: * Http Status Code is 200 (OK) @@ -54,4 +57,93 @@ public void pingEndpointTestPOST() throws Exception { assertNotNull(result); } + /** + * Confirm that the "/ping" endpoint does NOT accept PUT requests and verify the following within the response: + * Http Status Code is 405 (METHOD NOT ALLOWED) + * + * @throws Exception + */ + @Test + public void pingEndpointTestPUT() throws Exception { + MvcResult result = this.mockMvc.perform(MockMvcRequestBuilders.put("/ping")) + .andExpect(MockMvcResultMatchers.status().isMethodNotAllowed()) + .andReturn(); + //assert method to satisfy codacy requirement, this statement will not be reached if the test fails + assertNotNull(result); + } + + /** + * Confirm that the "/ping" endpoint does NOT accept DELETE requests and verify the following within the response: + * Http Status Code is 405 (METHOD NOT ALLOWED) + * + * @throws Exception + */ + @Test + public void pingEndpointTestDELETE() throws Exception { + MvcResult result = this.mockMvc.perform(MockMvcRequestBuilders.delete("/ping")) + .andExpect(MockMvcResultMatchers.status().isMethodNotAllowed()) + .andReturn(); + //assert method to satisfy codacy requirement, this statement will not be reached if the test fails + assertNotNull(result); + } + + /** + * Confirm that the "/" (root) endpoint accepts GET requests and verify the following within the response: + * Http Status Code is 200 (OK) + * View name is "/index" + * + * @throws Exception + */ + @Test + public void rootEndpointTestGET() throws Exception { + MvcResult result = this.mockMvc.perform(MockMvcRequestBuilders.get("/")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.view().name("/index")) + .andReturn(); + //assert method to satisfy codacy requirement, this statement will not be reached if the test fails + assertNotNull(result); + } + + /** + * Confirm that the "/" (root) endpoint accepts POST requests and returns a successful response + * + * @throws Exception + */ + @Test + public void rootEndpointTestPOST() throws Exception { + MvcResult result = this.mockMvc.perform(MockMvcRequestBuilders.post("/")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + //assert method to satisfy codacy requirement, this statement will not be reached if the test fails + assertNotNull(result); + } + + /** + * Confirm that the "/" (root) endpoint accepts PUT requests and returns a successful response + * + * @throws Exception + */ + @Test + public void rootEndpointTestPUT() throws Exception { + MvcResult result = this.mockMvc.perform(MockMvcRequestBuilders.put("/")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + //assert method to satisfy codacy requirement, this statement will not be reached if the test fails + assertNotNull(result); + } + + /** + * Confirm that the "/" (root) endpoint accepts DELETE requests and returns a successful response + * + * @throws Exception + */ + @Test + public void rootEndpointTestDELETE() throws Exception { + MvcResult result = this.mockMvc.perform(MockMvcRequestBuilders.delete("/")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + //assert method to satisfy codacy requirement, this statement will not be reached if the test fails + assertNotNull(result); + } + }