diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 00000000..8b4ddb1f --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,11 @@ +### Overview + +N/A + +### Change Details (Specifics) + +N/A + +### Related Ticket(s) + +N/A diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..2c7ec1e7 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,39 @@ +name: Test + +on: + workflow_dispatch: + push: + branches: + - "*.*.*" + - "main" + - "master" + pull_request: + branches: + - "*" + +permissions: + contents: read + +jobs: + test: + name: Test Changes + runs-on: ubuntu-latest + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + with: + submodules: true + + - name: Setup Java + uses: actions/setup-java@v4 + with: + java-version: "17" + distribution: "temurin" + cache: "maven" + + - name: Run Tests + run: mvn test + + - name: Coveralls GitHub Action + uses: coverallsapp/github-action@v2 + continue-on-error: true diff --git a/README.md b/README.md index 43c5420d..6403b0f9 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ [![Codacy Badge](https://app.codacy.com/project/badge/Grade/197ca1f70b6a47618332548b6da480c1)](https://www.codacy.com/gh/CBIIT/bento-backend?utm_source=github.com&utm_medium=referral&utm_content=CBIIT/bento-backend&utm_campaign=Badge_Grade) +[![Test](https://github.com/CBIIT/INS-WebService/actions/workflows/test.yml/badge.svg)](https://github.com/CBIIT/INS-WebService/actions/workflows/test.yml) +[![Coverage Status](https://coveralls.io/repos/github/CBIIT/INS-WebService/badge.svg)](https://coveralls.io/github/CBIIT/INS-WebService) # Bento Backend Framework Configuration Guide This is the user documentation for the Bento Backend. diff --git a/pom.xml b/pom.xml index 2eca163a..3f50c5c5 100644 --- a/pom.xml +++ b/pom.xml @@ -386,6 +386,25 @@ + + org.jacoco + jacoco-maven-plugin + 0.8.10 + + + + prepare-agent + + + + report + test + + report + + + + 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 33b31efb..00000000 --- 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 93fdb3b5..00000000 --- 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 11f78427..05634a8b 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); + } + } diff --git a/src/test/java/gov/nih/nci/bento/utility/StrUtilTest.java b/src/test/java/gov/nih/nci/bento/utility/StrUtilTest.java deleted file mode 100644 index bb435aee..00000000 --- a/src/test/java/gov/nih/nci/bento/utility/StrUtilTest.java +++ /dev/null @@ -1,51 +0,0 @@ -package gov.nih.nci.bento.utility; - -import org.junit.Test; - -import static org.hamcrest.Matchers.is; -import static org.hamcrest.MatcherAssert.assertThat; - -public class StrUtilTest { - - @Test - public void getBoolText_Test() { - assertThat(StrUtil.getBoolText(null), is("")); - assertThat(StrUtil.getBoolText("TESTTESTTEST true TESTTESTTEST"), is("true")); - assertThat(StrUtil.getBoolText("TEST_FALSE_TEST"), is("")); - assertThat(StrUtil.getBoolText("TRUEFALSETESTTEST"), is("")); - assertThat(StrUtil.getBoolText("TESTTESTTESTTESTTESTTEST true"), is("true")); - assertThat(StrUtil.getBoolText("true"), is("true")); - assertThat(StrUtil.getBoolText("false"), is("false")); - assertThat(StrUtil.getBoolText(" false "), is("false")); - assertThat(StrUtil.getBoolText(" FALse "), is("false")); - assertThat(StrUtil.getBoolText(" tRue "), is("true")); - } - - @Test - public void getIntText_Test() { - assertThat(StrUtil.getIntText(null), is("")); - assertThat(StrUtil.getIntText("TESTTESTTEST 000 TESTTESTTEST"), is("000")); - assertThat(StrUtil.getIntText("55TEST_FALSE_TEST"), is("")); - assertThat(StrUtil.getIntText("TRUEFALSETESTTEST"), is("")); - assertThat(StrUtil.getIntText("TESTTESTTESTTESTTESTTEST 1"), is("1")); - assertThat(StrUtil.getIntText("1234"), is("1234")); - assertThat(StrUtil.getIntText("98 "), is("98")); - assertThat(StrUtil.getIntText(" 5 "), is("5")); - } - -/* @Test - public void getToken_Test() { - assertThat(StrUtil.getToken(null), is("")); - assertThat(StrUtil.getToken("Bearer "), is("")); - assertThat(StrUtil.getToken("Bearer ssss "), is("")); - assertThat(StrUtil.getToken("Bearer"), is("")); - assertThat(StrUtil.getToken("Bearer xxxx"), is("xxxx")); - assertThat(StrUtil.getToken("XXBearer xxxx"), is("")); - assertThat(StrUtil.getToken("XXX xxxx"), is("")); - assertThat(StrUtil.getToken("Bearerxxxx"), is("")); - assertThat(StrUtil.getToken("Bearer 1234"), is("1234")); - assertThat(StrUtil.getToken("Bearer 1*23/4"), is("1*23/4")); - assertThat(StrUtil.getToken("bearer 1*23/4"), is("")); - }*/ - -} \ No newline at end of file