diff --git a/src/test/java/sg/com/smartinventory/controllers/CustomerControllerTest.java b/src/test/java/sg/com/smartinventory/controllers/CustomerControllerTest.java index 98b3ab7..210fa73 100644 --- a/src/test/java/sg/com/smartinventory/controllers/CustomerControllerTest.java +++ b/src/test/java/sg/com/smartinventory/controllers/CustomerControllerTest.java @@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; @@ -84,6 +85,7 @@ public void createCustomerTest() throws Exception { // Step 4: Perform the request and get the response and assert. mockMvc.perform(request) + .andDo(print()) .andExpect(status().isCreated()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.firstName").value("Jackie")) diff --git a/src/test/java/sg/com/smartinventory/controllers/ProductControllerTest.java b/src/test/java/sg/com/smartinventory/controllers/ProductControllerTest.java index 35aa14d..ed5fe1e 100644 --- a/src/test/java/sg/com/smartinventory/controllers/ProductControllerTest.java +++ b/src/test/java/sg/com/smartinventory/controllers/ProductControllerTest.java @@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; @@ -84,6 +85,7 @@ public void createProductTest() throws Exception { // Step 4: Perform the request and get the response and assert. mockMvc.perform(request) + .andDo(print()) .andExpect(status().isCreated()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.category").value("Electronics")) diff --git a/src/test/java/sg/com/smartinventory/controllers/ReviewControllerTest.java b/src/test/java/sg/com/smartinventory/controllers/ReviewControllerTest.java index 4d13242..1b3afc0 100644 --- a/src/test/java/sg/com/smartinventory/controllers/ReviewControllerTest.java +++ b/src/test/java/sg/com/smartinventory/controllers/ReviewControllerTest.java @@ -4,7 +4,9 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.when; + import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; @@ -12,8 +14,10 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; + import org.mockito.InjectMocks; import org.mockito.Mock; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -75,7 +79,7 @@ void teardown() { private ReviewServiceImpl reviewService; @Test -//<<<<<<< SIS-39 + // <<<<<<< SIS-39 @DisplayName("Get Review Test by Id") public void getReviewTest() throws Exception { // Setup @@ -83,166 +87,172 @@ public void getReviewTest() throws Exception { // Build the GET request RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/reviews/2"); -//======= // = = = DATA INTEGRITY VIOLATION EXCEPTION = = = - // public void createReviewTest() throws Exception { - // test_logger.info("Starting test: " + getCurrentMethodName() + ". "); - - // // Step 1: Create a Review object. - // Review newReview = Review.builder().category("Electronics") - // .reviewContent("Great smartphone with excellent features. ").rating(5).customerId(1) - // .productId(2).build(); - - // // Step 2: Convert the Java object to JSON using ObjectMapper. - // String newReviewAsJSON = objectMapper.writeValueAsString(newReview); - - // // Step 3: Build the request. - // RequestBuilder request = MockMvcRequestBuilders.post("/reviews") - // .contentType(MediaType.APPLICATION_JSON) - // .content(newReviewAsJSON); - - // // Step 4: Perform the request and get the response and assert. - // mockMvc.perform(request) - // // MvcResult Result = mockMvc.perform(request) - // // .andDo(print -> test_logger.error("Starting Request: createReviewTest. ")) - // .andExpect(status().isCreated()) - // .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - // .andExpect(jsonPath("$.category").value("Electronics")) - // .andExpect(jsonPath("$.rating").value(5)); - // // .andDo(print -> test_logger.error("Customer's id: {}", - // // print.getResponse().getContentAsString())) - // // .andDo(print -> test_logger.error("Starting Request: createReviewTest. ")) - // // .andReturn(); -// >>>>>>> main + // ======= // = = = DATA INTEGRITY VIOLATION EXCEPTION = = = + // public void createReviewTest() throws Exception { + // test_logger.info("Starting test: " + getCurrentMethodName() + ". "); + + // // Step 1: Create a Review object. + // Review newReview = Review.builder().category("Electronics") + // .reviewContent("Great smartphone with excellent features. + // ").rating(5).customerId(1) + // .productId(2).build(); + + // // Step 2: Convert the Java object to JSON using ObjectMapper. + // String newReviewAsJSON = objectMapper.writeValueAsString(newReview); + + // // Step 3: Build the request. + // RequestBuilder request = MockMvcRequestBuilders.post("/reviews") + // .contentType(MediaType.APPLICATION_JSON) + // .content(newReviewAsJSON); + + // // Step 4: Perform the request and get the response and assert. + // mockMvc.perform(request) + // // MvcResult Result = mockMvc.perform(request) + // // .andDo(print -> test_logger.error("Starting Request: createReviewTest. ")) + // .andExpect(status().isCreated()) + // .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + // .andExpect(jsonPath("$.category").value("Electronics")) + // .andExpect(jsonPath("$.rating").value(5)); + // // .andDo(print -> test_logger.error("Customer's id: {}", + // // print.getResponse().getContentAsString())) + // // .andDo(print -> test_logger.error("Starting Request: createReviewTest. ")) + // // .andReturn(); + // >>>>>>> main // when(reviewService.getReview(2L)).thenReturn(testReview); // Perform the request (Execute + Assert) mockMvc.perform(requestBuilder) + .andDo(print()) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.id").value(2)); } -//<<<<<<< SIS-39 -// ======= + // <<<<<<< SIS-39 + // ======= // @DisplayName("Get All Reviews") // @Test // public void getAllReviewsTest() throws Exception { - // test_logger.info("Starting test: " + getCurrentMethodName() + ". "); - - // // Step 1: Create a Review objects. - // Review newReview = Review.builder().category("Electronics") - // .reviewContent("Great smartphone with excellent features. ").rating(5).customerId(1) - // .productId(2).build(); - - // // Step 2: Convert the Java object to JSON using ObjectMapper. - // String newReviewAsJSON = objectMapper.writeValueAsString(newReview); - - // // Step 3: Build the request. - // RequestBuilder request = MockMvcRequestBuilders.post("/reviews") - // .contentType(MediaType.APPLICATION_JSON) - // .content(newReviewAsJSON); - - // // Step 4: Perform the request and get the response and assert. - // mockMvc.perform(request) - // // MvcResult Result = mockMvc.perform(request) - // // .andDo(print -> test_logger.error("Starting Request: createReviewTest. ")) - // .andExpect(status().isCreated()) - // .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - // .andExpect(jsonPath("$.category").value("Electronics")) - // .andExpect(jsonPath("$.rating").value(5)); - - // test_logger.info("Ending test: " + getCurrentMethodName() + ". "); + // test_logger.info("Starting test: " + getCurrentMethodName() + ". "); + + // // Step 1: Create a Review objects. + // Review newReview = Review.builder().category("Electronics") + // .reviewContent("Great smartphone with excellent features. + // ").rating(5).customerId(1) + // .productId(2).build(); + + // // Step 2: Convert the Java object to JSON using ObjectMapper. + // String newReviewAsJSON = objectMapper.writeValueAsString(newReview); + + // // Step 3: Build the request. + // RequestBuilder request = MockMvcRequestBuilders.post("/reviews") + // .contentType(MediaType.APPLICATION_JSON) + // .content(newReviewAsJSON); + + // // Step 4: Perform the request and get the response and assert. + // mockMvc.perform(request) + // // MvcResult Result = mockMvc.perform(request) + // // .andDo(print -> test_logger.error("Starting Request: createReviewTest. ")) + // .andExpect(status().isCreated()) + // .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + // .andExpect(jsonPath("$.category").value("Electronics")) + // .andExpect(jsonPath("$.rating").value(5)); + + // test_logger.info("Ending test: " + getCurrentMethodName() + ". "); // } // @DisplayName("Get One Review") // @Test // public void getOneReviewTest() throws Exception { - // test_logger.info("Starting test: " + getCurrentMethodName() + ". "); - - // // Step 1: Create a Review object. - // Review newReview = Review.builder().category("Electronics") - // .reviewContent("Great smartphone with excellent features. ").rating(5).customerId(1) - // .productId(2).build(); - - // // Step 2: Convert the Java object to JSON using ObjectMapper. - // String newReviewAsJSON = objectMapper.writeValueAsString(newReview); - - // // Step 3: Build the request. - // RequestBuilder request = MockMvcRequestBuilders.post("/reviews") - // .contentType(MediaType.APPLICATION_JSON) - // .content(newReviewAsJSON); - - // // Step 4: Perform the request and get the response and assert. - // mockMvc.perform(request) - // // MvcResult Result = mockMvc.perform(request) - // // .andDo(print -> test_logger.error("Starting Request: createReviewTest. ")) - // .andExpect(status().isCreated()) - // .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - // .andExpect(jsonPath("$.category").value("Electronics")) - // .andExpect(jsonPath("$.rating").value(5)); - - // test_logger.info("Ending test: " + getCurrentMethodName() + ". "); + // test_logger.info("Starting test: " + getCurrentMethodName() + ". "); + + // // Step 1: Create a Review object. + // Review newReview = Review.builder().category("Electronics") + // .reviewContent("Great smartphone with excellent features. + // ").rating(5).customerId(1) + // .productId(2).build(); + + // // Step 2: Convert the Java object to JSON using ObjectMapper. + // String newReviewAsJSON = objectMapper.writeValueAsString(newReview); + + // // Step 3: Build the request. + // RequestBuilder request = MockMvcRequestBuilders.post("/reviews") + // .contentType(MediaType.APPLICATION_JSON) + // .content(newReviewAsJSON); + + // // Step 4: Perform the request and get the response and assert. + // mockMvc.perform(request) + // // MvcResult Result = mockMvc.perform(request) + // // .andDo(print -> test_logger.error("Starting Request: createReviewTest. ")) + // .andExpect(status().isCreated()) + // .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + // .andExpect(jsonPath("$.category").value("Electronics")) + // .andExpect(jsonPath("$.rating").value(5)); + + // test_logger.info("Ending test: " + getCurrentMethodName() + ". "); // } // @DisplayName("Update Review") // @Test // public void updateReviewTest() throws Exception { - // test_logger.info("Starting test: " + getCurrentMethodName() + ". "); - - // // Step 1: Create a Review object. - // Review newReview = Review.builder().category("Electronics") - // .reviewContent("Great smartphone with excellent features. ").rating(5).customerId(1) - // .productId(2).build(); - - // // Step 2: Convert the Java object to JSON using ObjectMapper. - // String newReviewAsJSON = objectMapper.writeValueAsString(newReview); - - // // Step 3: Build the request. - // RequestBuilder request = MockMvcRequestBuilders.post("/reviews") - // .contentType(MediaType.APPLICATION_JSON) - // .content(newReviewAsJSON); - - // // Step 4: Perform the request and get the response and assert. - // mockMvc.perform(request) - // // MvcResult Result = mockMvc.perform(request) - // // .andDo(print -> test_logger.error("Starting Request: createReviewTest. ")) - // .andExpect(status().isCreated()) - // .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - // .andExpect(jsonPath("$.category").value("Electronics")) - // .andExpect(jsonPath("$.rating").value(5)); - - // test_logger.info("Ending test: " + getCurrentMethodName() + ". "); + // test_logger.info("Starting test: " + getCurrentMethodName() + ". "); + + // // Step 1: Create a Review object. + // Review newReview = Review.builder().category("Electronics") + // .reviewContent("Great smartphone with excellent features. + // ").rating(5).customerId(1) + // .productId(2).build(); + + // // Step 2: Convert the Java object to JSON using ObjectMapper. + // String newReviewAsJSON = objectMapper.writeValueAsString(newReview); + + // // Step 3: Build the request. + // RequestBuilder request = MockMvcRequestBuilders.post("/reviews") + // .contentType(MediaType.APPLICATION_JSON) + // .content(newReviewAsJSON); + + // // Step 4: Perform the request and get the response and assert. + // mockMvc.perform(request) + // // MvcResult Result = mockMvc.perform(request) + // // .andDo(print -> test_logger.error("Starting Request: createReviewTest. ")) + // .andExpect(status().isCreated()) + // .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + // .andExpect(jsonPath("$.category").value("Electronics")) + // .andExpect(jsonPath("$.rating").value(5)); + + // test_logger.info("Ending test: " + getCurrentMethodName() + ". "); // } // @DisplayName("Delete Review") // @Test // public void deleteReviewTest() throws Exception { - // test_logger.info("Starting test: " + getCurrentMethodName() + ". "); - - // // Step 1: Create a Review object. - // Review newReview = Review.builder().category("Electronics") - // .reviewContent("Great smartphone with excellent features. ").rating(5).customerId(1) - // .productId(2).build(); - - // // Step 2: Convert the Java object to JSON using ObjectMapper. - // String newReviewAsJSON = objectMapper.writeValueAsString(newReview); - - // // Step 3: Build the request. - // RequestBuilder request = MockMvcRequestBuilders.post("/reviews") - // .contentType(MediaType.APPLICATION_JSON) - // .content(newReviewAsJSON); - - // // Step 4: Perform the request and get the response and assert. - // mockMvc.perform(request) - // // MvcResult Result = mockMvc.perform(request) - // // .andDo(print -> test_logger.error("Starting Request: createReviewTest. ")) - // .andExpect(status().isCreated()) - // .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - // .andExpect(jsonPath("$.category").value("Electronics")) - // .andExpect(jsonPath("$.rating").value(5)); - - // test_logger.info("Ending test: " + getCurrentMethodName() + ". "); + // test_logger.info("Starting test: " + getCurrentMethodName() + ". "); + + // // Step 1: Create a Review object. + // Review newReview = Review.builder().category("Electronics") + // .reviewContent("Great smartphone with excellent features. + // ").rating(5).customerId(1) + // .productId(2).build(); + + // // Step 2: Convert the Java object to JSON using ObjectMapper. + // String newReviewAsJSON = objectMapper.writeValueAsString(newReview); + + // // Step 3: Build the request. + // RequestBuilder request = MockMvcRequestBuilders.post("/reviews") + // .contentType(MediaType.APPLICATION_JSON) + // .content(newReviewAsJSON); + + // // Step 4: Perform the request and get the response and assert. + // mockMvc.perform(request) + // // MvcResult Result = mockMvc.perform(request) + // // .andDo(print -> test_logger.error("Starting Request: createReviewTest. ")) + // .andExpect(status().isCreated()) + // .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + // .andExpect(jsonPath("$.category").value("Electronics")) + // .andExpect(jsonPath("$.rating").value(5)); + + // test_logger.info("Ending test: " + getCurrentMethodName() + ". "); // } -// >>>>>>> main -} + // >>>>>>> main +} \ No newline at end of file diff --git a/src/test/java/sg/com/smartinventory/integration/IntegrationTest.java b/src/test/java/sg/com/smartinventory/integration/IntegrationTest.java index 145f396..7d2083d 100644 --- a/src/test/java/sg/com/smartinventory/integration/IntegrationTest.java +++ b/src/test/java/sg/com/smartinventory/integration/IntegrationTest.java @@ -9,6 +9,7 @@ import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; @@ -131,11 +132,14 @@ public void runIntegratedTest() throws Exception { // Step 4: Perform the request and get the response and assert. mockMvc.perform(request) + .andDo(print()) .andExpect(status().isCreated()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.firstName").value("Jackie")) .andExpect(jsonPath("$.lastName").value("Chan")); + mockMvc.perform(request2) + .andDo(print()) .andExpect(status().isCreated()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.firstName").value("Jackie")) diff --git a/src/test/java/sg/com/smartinventory/useCases/UseCaseTest.java b/src/test/java/sg/com/smartinventory/useCases/UseCaseTest.java index da9fe02..319c1cc 100644 --- a/src/test/java/sg/com/smartinventory/useCases/UseCaseTest.java +++ b/src/test/java/sg/com/smartinventory/useCases/UseCaseTest.java @@ -9,6 +9,7 @@ import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; @@ -113,6 +114,7 @@ public void runIntegratedTest() throws Exception { Customer testObject1 = Customer.builder().firstName("Jackie").lastName("Chan").country("Hong Kong") .address("123 HK St").postalCode(654321).phoneNumber(87654321) .email("jackie.chan@example.com").build(); + Customer testObject2 = Customer.builder().firstName("Jackie").lastName("Chang").country("Hong Kong") .address("123 HK St").postalCode(654321).phoneNumber(87654321) .email("jackie.chang@example.com").build(); @@ -125,17 +127,21 @@ public void runIntegratedTest() throws Exception { RequestBuilder request = MockMvcRequestBuilders.post("/customers") .contentType(MediaType.APPLICATION_JSON) .content(testObject1AsJSON); + RequestBuilder request2 = MockMvcRequestBuilders.post("/customers") .contentType(MediaType.APPLICATION_JSON) .content(testObject2AsJSON); // Step 4: Perform the request and get the response and assert. mockMvc.perform(request) + .andDo(print()) .andExpect(status().isCreated()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.firstName").value("Jackie")) .andExpect(jsonPath("$.lastName").value("Chan")); + mockMvc.perform(request2) + .andDo(print()) .andExpect(status().isCreated()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.firstName").value("Jackie"))