Skip to content

Commit

Permalink
Merge pull request #27 from nhkhai/SIS-24
Browse files Browse the repository at this point in the history
Create Test DataLoader.java.
  • Loading branch information
nhkhai authored Mar 30, 2024
2 parents 05f3284 + c49ac14 commit 1ce7223
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArrayList<Review>> getAllReviews() {
ArrayList<Review> allReviews = reviewService.getAllReviews();
return new ResponseEntity<>(allReviews, HttpStatus.OK);
}
// READ (GET ALL).
@GetMapping("")
public ResponseEntity<ArrayList<Review>> getAllReviews() {
ArrayList<Review> allReviews = reviewService.getAllReviews();
return new ResponseEntity<>(allReviews, HttpStatus.OK);
}

// READ (GET ONE).
@GetMapping("/{id}")
public ResponseEntity<Review> getReview(@PathVariable long id) {
Review foundReview = reviewService.getReview(id);
return new ResponseEntity<>(foundReview, HttpStatus.OK);
}
// READ (GET ONE).
@GetMapping("/{id}")
public ResponseEntity<Review> getReview(@PathVariable long id) {
Review foundReview = reviewService.getReview(id);
return new ResponseEntity<>(foundReview, HttpStatus.OK);
}

// Read (Get ratings)
@GetMapping("/ratings")
public ArrayList<Review> getRatings(@Valid @RequestParam int rating) {
return reviewService.getRatings(rating);
}
// Read (Get ratings)
@GetMapping("/ratings")
public ArrayList<Review> getRatings(@Valid @RequestParam int rating) {
return reviewService.getRatings(rating);
}

// READ (Search by customer Id)
@GetMapping("/customers/{id}")
public ArrayList<Review> searchReviewByCustomerId(@PathVariable long id) {
return reviewService.searchCustomerReviews(id);
}
// READ (Search by customer Id)
@GetMapping("/customers/{id}")
public ArrayList<Review> searchReviewByCustomerId(@PathVariable long id) {
return reviewService.searchCustomerReviews(id);
}

// READ (Search by product Id)
@GetMapping("/products/{productId}")
public ArrayList<Review> searchReviewByProductId(@PathVariable long productId) {
return reviewService.searchProductReviews(productId);
}
// READ (Search by product Id)
@GetMapping("/products/{productId}")
public ArrayList<Review> searchReviewByProductId(@PathVariable long productId) {
return reviewService.searchProductReviews(productId);
}

// UPDATE.
@PutMapping("/{id}")
public ResponseEntity<Review> updateReview(@PathVariable long id, @RequestBody Review review) {
Review updatedReview = reviewService.updateReview(id, review);
return new ResponseEntity<>(updatedReview, HttpStatus.OK);
}
// UPDATE.
@PutMapping("/{id}")
public ResponseEntity<Review> updateReview(@PathVariable long id, @RequestBody Review review) {
Review updatedReview = reviewService.updateReview(id, review);
return new ResponseEntity<>(updatedReview, HttpStatus.OK);
}

// DELETE.
@DeleteMapping("/{id}")
public ResponseEntity<Review> deleteReview(@PathVariable long id) {
reviewService.deleteReview(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
// DELETE.
@DeleteMapping("/{id}")
public ResponseEntity<Review> deleteReview(@PathVariable long id) {
reviewService.deleteReview(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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("[email protected]").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() + ". ");
Expand Down Expand Up @@ -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
// =======
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/sg/com/smartinventory/utility/DataLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package sg.com.smartinventory.utility;

import org.springframework.context.annotation.Primary;

@Primary
public class DataLoader {

}

0 comments on commit 1ce7223

Please sign in to comment.