From 9fc4fa11e4574e9b0ada9ffebab22ca68e345977 Mon Sep 17 00:00:00 2001 From: "nhkhai@gmail.com" Date: Sat, 30 Mar 2024 08:36:40 +0800 Subject: [PATCH 1/3] Create DataLoader.java Added a Dataloader class to skip post construct behavior in test case. This empty class takes precedence over the application class of the same class name. --- .../java/sg/com/smartinventory/utility/DataLoader.java | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/test/java/sg/com/smartinventory/utility/DataLoader.java diff --git a/src/test/java/sg/com/smartinventory/utility/DataLoader.java b/src/test/java/sg/com/smartinventory/utility/DataLoader.java new file mode 100644 index 0000000..ed8e71d --- /dev/null +++ b/src/test/java/sg/com/smartinventory/utility/DataLoader.java @@ -0,0 +1,8 @@ +package sg.com.smartinventory.utility; + +import org.springframework.context.annotation.Primary; + +@Primary +public class DataLoader { + +} \ No newline at end of file From 949705c4e778569ea4aceffbb15bf6b54f25507b Mon Sep 17 00:00:00 2001 From: "nhkhai@gmail.com" Date: Sat, 30 Mar 2024 09:03:09 +0800 Subject: [PATCH 2/3] Update ReviewController.java Fixed syntax. --- .../controllers/ReviewController.java | 92 +++++++++---------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/src/main/java/sg/com/smartinventory/controllers/ReviewController.java b/src/main/java/sg/com/smartinventory/controllers/ReviewController.java index ef22913..320e79e 100644 --- a/src/main/java/sg/com/smartinventory/controllers/ReviewController.java +++ b/src/main/java/sg/com/smartinventory/controllers/ReviewController.java @@ -23,59 +23,59 @@ @RequestMapping("/reviews") @CrossOrigin(origins = "http://localhost:5173") public class ReviewController { - private ReviewService reviewService; + private ReviewService reviewService; - // @Autowired - public ReviewController(ReviewService reviewService) { - this.reviewService = reviewService; - } + // @Autowired + public ReviewController(ReviewService reviewService) { + this.reviewService = reviewService; + } - // - - - NO POST HERE, due to data integrity with Customer FK - - - - // @PostMapping("")... + // - - - NO POST HERE, due to data integrity with Customer FK - - - + // @PostMapping("")... - // READ (GET ALL). - @GetMapping("") - public ResponseEntity> getAllReviews() { - ArrayList allReviews = reviewService.getAllReviews(); - return new ResponseEntity<>(allReviews, HttpStatus.OK); - } + // READ (GET ALL). + @GetMapping("") + public ResponseEntity> getAllReviews() { + ArrayList allReviews = reviewService.getAllReviews(); + return new ResponseEntity<>(allReviews, HttpStatus.OK); + } - // READ (GET ONE). - @GetMapping("/{id}") - public ResponseEntity getReview(@PathVariable long id) { - Review foundReview = reviewService.getReview(id); - return new ResponseEntity<>(foundReview, HttpStatus.OK); - } + // READ (GET ONE). + @GetMapping("/{id}") + public ResponseEntity getReview(@PathVariable long id) { + Review foundReview = reviewService.getReview(id); + return new ResponseEntity<>(foundReview, HttpStatus.OK); + } - // Read (Get ratings) - @GetMapping("/ratings") - public ArrayList getRatings(@Valid @RequestParam int rating) { - return reviewService.getRatings(rating); - } + // Read (Get ratings) + @GetMapping("/ratings") + public ArrayList getRatings(@Valid @RequestParam int rating) { + return reviewService.getRatings(rating); + } - // READ (Search by customer Id) - @GetMapping("/customers/{id}") - public ArrayList searchReviewByCustomerId(@PathVariable long id) { - return reviewService.searchCustomerReviews(id); - } + // READ (Search by customer Id) + @GetMapping("/customers/{id}") + public ArrayList searchReviewByCustomerId(@PathVariable long id) { + return reviewService.searchCustomerReviews(id); + } - // READ (Search by product Id) - @GetMapping("/products/{productId}") - public ArrayList searchReviewByProductId(@PathVariable long productId) { - return reviewService.searchProductReviews(productId); - } + // READ (Search by product Id) + @GetMapping("/products/{productId}") + public ArrayList searchReviewByProductId(@PathVariable long productId) { + return reviewService.searchProductReviews(productId); + } - // UPDATE. - @PutMapping("/{id}") - public ResponseEntity updateReview(@PathVariable long id, @RequestBody Review review) { - Review updatedReview = reviewService.updateReview(id, review); - return new ResponseEntity<>(updatedReview, HttpStatus.OK); - } + // UPDATE. + @PutMapping("/{id}") + public ResponseEntity updateReview(@PathVariable long id, @RequestBody Review review) { + Review updatedReview = reviewService.updateReview(id, review); + return new ResponseEntity<>(updatedReview, HttpStatus.OK); + } - // DELETE. - @DeleteMapping("/{id}") - public ResponseEntity deleteReview(@PathVariable long id) { - reviewService.deleteReview(id); - return new ResponseEntity<>(HttpStatus.NO_CONTENT); - } + // DELETE. + @DeleteMapping("/{id}") + public ResponseEntity deleteReview(@PathVariable long id) { + reviewService.deleteReview(id); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } } \ No newline at end of file From c49ac14b6ff3d6e7f3525d012c554408d0632ced Mon Sep 17 00:00:00 2001 From: "nhkhai@gmail.com" Date: Sat, 30 Mar 2024 09:03:38 +0800 Subject: [PATCH 3/3] Update ReviewControllerTest.java Fixed test case. --- .../controllers/ReviewControllerTest.java | 61 +++++++++++++++++-- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/src/test/java/sg/com/smartinventory/controllers/ReviewControllerTest.java b/src/test/java/sg/com/smartinventory/controllers/ReviewControllerTest.java index 75fda21..98860d3 100644 --- a/src/test/java/sg/com/smartinventory/controllers/ReviewControllerTest.java +++ b/src/test/java/sg/com/smartinventory/controllers/ReviewControllerTest.java @@ -34,6 +34,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import sg.com.smartinventory.entities.Customer; +import sg.com.smartinventory.entities.Product; import sg.com.smartinventory.entities.Review; import sg.com.smartinventory.repositories.ReviewRepository; import sg.com.smartinventory.serviceImpls.ReviewServiceImpl; @@ -85,11 +86,61 @@ void teardown() { // <<<<<<< SIS-39 @DisplayName("Get Review Test by Id") public void getReviewTest() throws Exception { - // Setup - // for now, setup is created in DataLoader. need fix + test_logger.info("Starting test: " + getCurrentMethodName() + ". "); + + // Step 1: Create the test objects. + Customer newCustomer = Customer.builder().firstName("Jackie").lastName("Chan").country("Hong Kong") + .address("123 HK St").postalCode(654321).phoneNumber(87654321) + .email("jackie.chan@example.com").build(); + + Product newProduct = Product.builder().category("Electronics").name("Smartphone") + .description("High-end smartphone with advanced features. ") + .price(999.99).stockQuantity(100).build(); + + Review newReview = Review.builder().category("Electronics") + .reviewContent("Fast delivery, product works as expected. ").rating(4) + .customer(newCustomer) + .product(newProduct).build(); + + // Step 2: Convert the Java object to JSON using ObjectMapper. + String newCustomerAsJSON = objectMapper.writeValueAsString(newCustomer); + String newProductAsJSON = objectMapper.writeValueAsString(newProduct); + String newReviewAsJSON = objectMapper.writeValueAsString(newReview); + + // Step 3: Build the request. + RequestBuilder request = MockMvcRequestBuilders.post("/customers") + .contentType(MediaType.APPLICATION_JSON) + .content(newCustomerAsJSON); + + RequestBuilder request2 = MockMvcRequestBuilders.post("/products") + .contentType(MediaType.APPLICATION_JSON) + .content(newProductAsJSON); + + RequestBuilder request3 = MockMvcRequestBuilders + .post("/customers/{id}/products/{productId}/reviews", "1", "1") + .contentType(MediaType.APPLICATION_JSON) + // .sessionAttr("review", newReview) + // .param("id", "1") + .content(newReviewAsJSON); + + // Step 4: Perform the request and get the response and assert. + mockMvc.perform(request) + .andDo(print()) + .andExpect(status().isCreated()); + + mockMvc.perform(request2) + .andDo(print()) + .andExpect(status().isCreated()); + + mockMvc.perform(request3).andDo(print()) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + // .andExpect(jsonPath("$[0].id").exists()) + .andExpect(jsonPath("$.category").value("Electronics")) + .andExpect(jsonPath("$.rating").value(4)); // Build the GET request - RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/reviews/2"); + RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/reviews/1"); // ======= // = = = DATA INTEGRITY VIOLATION EXCEPTION = = = // public void createReviewTest() throws Exception { // test_logger.info("Starting test: " + getCurrentMethodName() + ". "); @@ -129,7 +180,9 @@ public void getReviewTest() throws Exception { .andDo(print()) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.id").value(2)); + .andExpect(jsonPath("$.id").value(1)); + + test_logger.info("Ending test: " + getCurrentMethodName() + ". "); } // <<<<<<< SIS-39 // =======