From 9a93a1cf5fbb9e1e782e3960511fcdf0452dc854 Mon Sep 17 00:00:00 2001 From: nhkhai Date: Wed, 20 Mar 2024 15:22:24 +0800 Subject: [PATCH 1/2] Update .gitignore Ignore the log file. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 549e00a..5748e90 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,4 @@ build/ ### VS Code ### .vscode/ +src/main/java/sg/com/smartinventory/logfile From 7a155fa8ffca0272747a1bc5a5bef75636fda354 Mon Sep 17 00:00:00 2001 From: nhkhai Date: Wed, 20 Mar 2024 15:41:40 +0800 Subject: [PATCH 2/2] Created skeleton class files and updated project structure. --- .../sg/com/smartinventory/DataLoader.java | 72 ------------ .../controllers/CustomerController.java | 2 +- .../controllers/ProductController.java | 34 ++++++ .../controllers/ReviewController.java | 34 ++++++ .../com/smartinventory/entities/Customer.java | 1 + .../com/smartinventory/entities/Product.java | 1 + .../com/smartinventory/entities/Review.java | 1 + .../exceptions/CustomerNotFoundException.java | 7 ++ .../exceptions/ProductNotFoundException.java | 7 ++ .../exceptions/ReviewNotFoundException.java | 7 ++ .../repositories/CustomerRepository.java | 2 +- .../serviceImpls/ProductServiceImpl.java | 24 ++++ .../serviceImpls/ReviewServiceImpl.java | 24 ++++ .../services/ProductService.java | 7 ++ .../services/ReviewService.java | 7 ++ .../smartinventory/utility/DataLoader.java | 72 ++++++++++++ .../SmartInventoryApplicationTests.java | 1 + .../CustomerControllerTest.java | 3 +- .../controllers/ProductControllerTest.java | 51 +++++++++ .../controllers/ReviewControllerTest.java | 51 +++++++++ .../smartinventory/entities/CustomerTest.java | 24 ++++ .../smartinventory/entities/ProductTest.java | 24 ++++ .../smartinventory/entities/ReviewTest.java | 24 ++++ .../integration/IntegrationTest.java | 103 ++++++++++++++++++ .../CustomerServiceImplTest.java | 5 +- .../smartinventory/useCases/UseCaseTest.java | 103 ++++++++++++++++++ .../utility/DataLoaderTest.java | 32 ++++++ 27 files changed, 647 insertions(+), 76 deletions(-) delete mode 100644 src/main/java/sg/com/smartinventory/DataLoader.java create mode 100644 src/main/java/sg/com/smartinventory/controllers/ProductController.java create mode 100644 src/main/java/sg/com/smartinventory/controllers/ReviewController.java create mode 100644 src/main/java/sg/com/smartinventory/exceptions/CustomerNotFoundException.java create mode 100644 src/main/java/sg/com/smartinventory/exceptions/ProductNotFoundException.java create mode 100644 src/main/java/sg/com/smartinventory/exceptions/ReviewNotFoundException.java create mode 100644 src/main/java/sg/com/smartinventory/serviceImpls/ProductServiceImpl.java create mode 100644 src/main/java/sg/com/smartinventory/serviceImpls/ReviewServiceImpl.java create mode 100644 src/main/java/sg/com/smartinventory/services/ProductService.java create mode 100644 src/main/java/sg/com/smartinventory/services/ReviewService.java create mode 100644 src/main/java/sg/com/smartinventory/utility/DataLoader.java rename src/test/java/sg/com/smartinventory/{ => controllers}/CustomerControllerTest.java (98%) create mode 100644 src/test/java/sg/com/smartinventory/controllers/ProductControllerTest.java create mode 100644 src/test/java/sg/com/smartinventory/controllers/ReviewControllerTest.java create mode 100644 src/test/java/sg/com/smartinventory/entities/CustomerTest.java create mode 100644 src/test/java/sg/com/smartinventory/entities/ProductTest.java create mode 100644 src/test/java/sg/com/smartinventory/entities/ReviewTest.java create mode 100644 src/test/java/sg/com/smartinventory/integration/IntegrationTest.java rename src/test/java/sg/com/smartinventory/{ => services}/CustomerServiceImplTest.java (97%) create mode 100644 src/test/java/sg/com/smartinventory/useCases/UseCaseTest.java create mode 100644 src/test/java/sg/com/smartinventory/utility/DataLoaderTest.java diff --git a/src/main/java/sg/com/smartinventory/DataLoader.java b/src/main/java/sg/com/smartinventory/DataLoader.java deleted file mode 100644 index 4d33d6f..0000000 --- a/src/main/java/sg/com/smartinventory/DataLoader.java +++ /dev/null @@ -1,72 +0,0 @@ -package sg.com.smartinventory; - -// import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import jakarta.annotation.PostConstruct; -import sg.com.smartinventory.entities.Customer; -import sg.com.smartinventory.entities.Product; -import sg.com.smartinventory.entities.Review; -import sg.com.smartinventory.repositories.CustomerRepository; -import sg.com.smartinventory.repositories.ProductRepository; -import sg.com.smartinventory.repositories.ReviewRepository; - -@Component -public class DataLoader { - // Inject all repositories. - private CustomerRepository customerRepository; - private ProductRepository productRepository; - private ReviewRepository reviewRepository; - - // @Autowired - public DataLoader(CustomerRepository customerRepository, ProductRepository productRepository, - ReviewRepository reviewRepository) { - this.customerRepository = customerRepository; - this.productRepository = productRepository; - this.reviewRepository = reviewRepository; - } - - @PostConstruct - public void loadData() { - // Clear all data. - customerRepository.deleteAll(); - productRepository.deleteAll(); - reviewRepository.deleteAll(); - - // Create fake data. - customerRepository - .save(new Customer("John", "Doe", "USA", "123 Main St", 123456, 12345678, "john.doe@example.com", 101)); - customerRepository.save( - new Customer("Alice", "Smith", "Canada", "456 Maple Ave", 543210, 98765432, "alice.smith@example.com", - 102)); - customerRepository.save( - new Customer("Michael", "Johnson", "UK", "789 Oak Rd", 567890, 98761234, "michael.johnson@example.com", - 103)); - customerRepository.save( - new Customer("Emily", "Brown", "Australia", "321 Elm St", 135790, 45678912, "emily.brown@example.com", - 104)); - customerRepository.save( - new Customer("David", "Wilson", "Germany", "654 Pine Rd", 987655, 36985214, "david.wilson@example.com", - 105)); - - productRepository.save( - new Product("Electronics", "Smartphone", "High-end smartphone with advanced features", 999.99, 100, - 101)); - productRepository.save( - new Product("Clothing", "Men's T-Shirt", "Comfortable cotton t-shirt for everyday wear", 29.99, 500, - 102)); - productRepository.save(new Product("Home & Kitchen", "Coffee Maker", - "Automatic coffee maker with programmable settings", 49.99, 50, 103)); - productRepository - .save(new Product("Beauty", "Perfume", "Elegant fragrance with floral and citrus notes", 79.99, 200, - 104)); - productRepository.save(new Product("Books", "Science Fiction Novel", - "Bestselling sci-fi novel set in a dystopian future", 14.99, 300, 105)); - - reviewRepository.save(new Review("Electronics", "Great smartphone with excellent features.", 5, 101, 201)); - reviewRepository.save(new Review("Clothing", "Very comfortable t-shirt, fits perfectly.", 4, 102, 202)); - reviewRepository.save(new Review("Home & Kitchen", "Makes delicious coffee, easy to use.", 4, 103, 203)); - reviewRepository.save(new Review("Beauty", "Lovely fragrance, long-lasting.", 5, 104, 204)); - reviewRepository.save(new Review("Books", "Intriguing plot, couldn't put it down.", 5, 105, 205)); - } -} \ No newline at end of file diff --git a/src/main/java/sg/com/smartinventory/controllers/CustomerController.java b/src/main/java/sg/com/smartinventory/controllers/CustomerController.java index 3349324..3de76c3 100644 --- a/src/main/java/sg/com/smartinventory/controllers/CustomerController.java +++ b/src/main/java/sg/com/smartinventory/controllers/CustomerController.java @@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.RestController; import jakarta.validation.Valid; + import sg.com.smartinventory.entities.Customer; import sg.com.smartinventory.services.CustomerService; @@ -24,7 +25,6 @@ public CustomerController(CustomerService customerService) { // CREATE. @PostMapping("") public ResponseEntity createCustomer(@Valid @RequestBody Customer customer) { - // if(bindingResult.hasErrors()) { // return new ResponseEntity<>(HttpStatus.BAD_REQUEST); // } diff --git a/src/main/java/sg/com/smartinventory/controllers/ProductController.java b/src/main/java/sg/com/smartinventory/controllers/ProductController.java new file mode 100644 index 0000000..07f49a0 --- /dev/null +++ b/src/main/java/sg/com/smartinventory/controllers/ProductController.java @@ -0,0 +1,34 @@ +package sg.com.smartinventory.controllers; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import jakarta.validation.Valid; +import sg.com.smartinventory.entities.Product; +import sg.com.smartinventory.services.ProductService; + +@RestController +@RequestMapping("/products") +public class ProductController { + private ProductService productService; + + // @Autowired + public ProductController(ProductService productService) { + this.productService = productService; + } + + // CREATE. + @PostMapping("") + public ResponseEntity createProduct(@Valid @RequestBody Product product) { + // if(bindingResult.hasErrors()) { + // return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + // } + + Product newProduct = productService.createProduct(product); + return new ResponseEntity<>(newProduct, HttpStatus.CREATED); + } +} \ No newline at end of file diff --git a/src/main/java/sg/com/smartinventory/controllers/ReviewController.java b/src/main/java/sg/com/smartinventory/controllers/ReviewController.java new file mode 100644 index 0000000..11d627f --- /dev/null +++ b/src/main/java/sg/com/smartinventory/controllers/ReviewController.java @@ -0,0 +1,34 @@ +package sg.com.smartinventory.controllers; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import jakarta.validation.Valid; +import sg.com.smartinventory.entities.Review; +import sg.com.smartinventory.services.ReviewService; + +@RestController +@RequestMapping("/reviews") +public class ReviewController { + private ReviewService reviewService; + + // @Autowired + public ReviewController(ReviewService reviewService) { + this.reviewService = reviewService; + } + + // CREATE. + @PostMapping("") + public ResponseEntity createReview(@Valid @RequestBody Review review) { + // if(bindingResult.hasErrors()) { + // return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + // } + + Review newReview = reviewService.createReview(review); + return new ResponseEntity<>(newReview, HttpStatus.CREATED); + } +} \ No newline at end of file diff --git a/src/main/java/sg/com/smartinventory/entities/Customer.java b/src/main/java/sg/com/smartinventory/entities/Customer.java index 46bd745..3eea3eb 100644 --- a/src/main/java/sg/com/smartinventory/entities/Customer.java +++ b/src/main/java/sg/com/smartinventory/entities/Customer.java @@ -9,6 +9,7 @@ import jakarta.validation.constraints.Digits; import jakarta.validation.constraints.Email; import jakarta.validation.constraints.NotBlank; + import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; diff --git a/src/main/java/sg/com/smartinventory/entities/Product.java b/src/main/java/sg/com/smartinventory/entities/Product.java index 944ba16..ba16780 100644 --- a/src/main/java/sg/com/smartinventory/entities/Product.java +++ b/src/main/java/sg/com/smartinventory/entities/Product.java @@ -6,6 +6,7 @@ import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.Table; + import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; diff --git a/src/main/java/sg/com/smartinventory/entities/Review.java b/src/main/java/sg/com/smartinventory/entities/Review.java index 0423a5b..f65ae12 100644 --- a/src/main/java/sg/com/smartinventory/entities/Review.java +++ b/src/main/java/sg/com/smartinventory/entities/Review.java @@ -6,6 +6,7 @@ import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.Table; + import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; diff --git a/src/main/java/sg/com/smartinventory/exceptions/CustomerNotFoundException.java b/src/main/java/sg/com/smartinventory/exceptions/CustomerNotFoundException.java new file mode 100644 index 0000000..1f63ede --- /dev/null +++ b/src/main/java/sg/com/smartinventory/exceptions/CustomerNotFoundException.java @@ -0,0 +1,7 @@ +package sg.com.smartinventory.exceptions; + +public class CustomerNotFoundException extends RuntimeException { + public CustomerNotFoundException(Long id) { + super("Could not find customer with id: " + id + ". "); + } +} \ No newline at end of file diff --git a/src/main/java/sg/com/smartinventory/exceptions/ProductNotFoundException.java b/src/main/java/sg/com/smartinventory/exceptions/ProductNotFoundException.java new file mode 100644 index 0000000..635259f --- /dev/null +++ b/src/main/java/sg/com/smartinventory/exceptions/ProductNotFoundException.java @@ -0,0 +1,7 @@ +package sg.com.smartinventory.exceptions; + +public class ProductNotFoundException extends RuntimeException { + public ProductNotFoundException(String id) { + super("Could not find product with id: " + id + ". "); + } +} \ No newline at end of file diff --git a/src/main/java/sg/com/smartinventory/exceptions/ReviewNotFoundException.java b/src/main/java/sg/com/smartinventory/exceptions/ReviewNotFoundException.java new file mode 100644 index 0000000..a86d991 --- /dev/null +++ b/src/main/java/sg/com/smartinventory/exceptions/ReviewNotFoundException.java @@ -0,0 +1,7 @@ +package sg.com.smartinventory.exceptions; + +public class ReviewNotFoundException extends RuntimeException { + public ReviewNotFoundException(String id) { + super("Could not find review with id: " + id + ". "); + } +} \ No newline at end of file diff --git a/src/main/java/sg/com/smartinventory/repositories/CustomerRepository.java b/src/main/java/sg/com/smartinventory/repositories/CustomerRepository.java index 4b9d4d1..ef75332 100644 --- a/src/main/java/sg/com/smartinventory/repositories/CustomerRepository.java +++ b/src/main/java/sg/com/smartinventory/repositories/CustomerRepository.java @@ -4,7 +4,7 @@ import sg.com.smartinventory.entities.Customer; -//CustomerRepository simply extends JpaRepository, giving us a lot of default methods to access db without the hardwork +// CustomerRepository simply extends JpaRepository, giving us a lot of default methods to access db without the hard work. public interface CustomerRepository extends JpaRepository { } \ No newline at end of file diff --git a/src/main/java/sg/com/smartinventory/serviceImpls/ProductServiceImpl.java b/src/main/java/sg/com/smartinventory/serviceImpls/ProductServiceImpl.java new file mode 100644 index 0000000..aa6eef9 --- /dev/null +++ b/src/main/java/sg/com/smartinventory/serviceImpls/ProductServiceImpl.java @@ -0,0 +1,24 @@ +package sg.com.smartinventory.serviceImpls; + +import org.springframework.stereotype.Service; + +import sg.com.smartinventory.entities.Product; +import sg.com.smartinventory.repositories.ProductRepository; +import sg.com.smartinventory.services.ProductService; + +@Service +public class ProductServiceImpl implements ProductService { + private ProductRepository productRepository; + + // @Autowired + public ProductServiceImpl(ProductRepository productRepository) { + this.productRepository = productRepository; + } + + @Override + public Product createProduct(Product product) { + Product newProduct = productRepository.save(product); + + return newProduct; + } +} \ No newline at end of file diff --git a/src/main/java/sg/com/smartinventory/serviceImpls/ReviewServiceImpl.java b/src/main/java/sg/com/smartinventory/serviceImpls/ReviewServiceImpl.java new file mode 100644 index 0000000..d623ccc --- /dev/null +++ b/src/main/java/sg/com/smartinventory/serviceImpls/ReviewServiceImpl.java @@ -0,0 +1,24 @@ +package sg.com.smartinventory.serviceImpls; + +import org.springframework.stereotype.Service; + +import sg.com.smartinventory.entities.Review; +import sg.com.smartinventory.repositories.ReviewRepository; +import sg.com.smartinventory.services.ReviewService; + +@Service +public class ReviewServiceImpl implements ReviewService { + private ReviewRepository reviewRepository; + + // @Autowired + public ReviewServiceImpl(ReviewRepository reviewRepository) { + this.reviewRepository = reviewRepository; + } + + @Override + public Review createReview(Review review) { + Review newReview = reviewRepository.save(review); + + return newReview; + } +} \ No newline at end of file diff --git a/src/main/java/sg/com/smartinventory/services/ProductService.java b/src/main/java/sg/com/smartinventory/services/ProductService.java new file mode 100644 index 0000000..d55ae43 --- /dev/null +++ b/src/main/java/sg/com/smartinventory/services/ProductService.java @@ -0,0 +1,7 @@ +package sg.com.smartinventory.services; + +import sg.com.smartinventory.entities.Product; + +public interface ProductService { + Product createProduct(Product product); +} \ No newline at end of file diff --git a/src/main/java/sg/com/smartinventory/services/ReviewService.java b/src/main/java/sg/com/smartinventory/services/ReviewService.java new file mode 100644 index 0000000..ccb4c12 --- /dev/null +++ b/src/main/java/sg/com/smartinventory/services/ReviewService.java @@ -0,0 +1,7 @@ +package sg.com.smartinventory.services; + +import sg.com.smartinventory.entities.Review; + +public interface ReviewService { + Review createReview(Review review); +} \ No newline at end of file diff --git a/src/main/java/sg/com/smartinventory/utility/DataLoader.java b/src/main/java/sg/com/smartinventory/utility/DataLoader.java new file mode 100644 index 0000000..3962362 --- /dev/null +++ b/src/main/java/sg/com/smartinventory/utility/DataLoader.java @@ -0,0 +1,72 @@ +package sg.com.smartinventory.utility; + +// import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import jakarta.annotation.PostConstruct; + +import sg.com.smartinventory.entities.Customer; +import sg.com.smartinventory.entities.Product; +import sg.com.smartinventory.entities.Review; +import sg.com.smartinventory.repositories.CustomerRepository; +import sg.com.smartinventory.repositories.ProductRepository; +import sg.com.smartinventory.repositories.ReviewRepository; + +@Component +public class DataLoader { + // Inject all repositories. + private CustomerRepository customerRepository; + private ProductRepository productRepository; + private ReviewRepository reviewRepository; + + // @Autowired + public DataLoader(CustomerRepository customerRepository, ProductRepository productRepository, + ReviewRepository reviewRepository) { + this.customerRepository = customerRepository; + this.productRepository = productRepository; + this.reviewRepository = reviewRepository; + } + + @PostConstruct + public void loadData() { + // Clear all data. + customerRepository.deleteAll(); + productRepository.deleteAll(); + reviewRepository.deleteAll(); + + // Create fake data. + customerRepository.save(new Customer("John", "Doe", "USA", "123 Main St", 123456, 12345678, + "john.doe@example.com", 101)); + + customerRepository.save(new Customer("Alice", "Smith", "Canada", "456 Maple Ave", 543210, 98765432, + "alice.smith@example.com", 102)); + customerRepository.save(new Customer("Michael", "Johnson", "UK", "789 Oak Rd", 567890, 98761234, + "michael.johnson@example.com", 103)); + customerRepository.save(new Customer("Emily", "Brown", "Australia", "321 Elm St", 135790, 45678912, + "emily.brown@example.com", 104)); + customerRepository.save(new Customer("David", "Wilson", "Germany", "654 Pine Rd", 987655, 36985214, + "david.wilson@example.com", 105)); + + productRepository.save(new Product("Electronics", "Smartphone", + "High-end smartphone with advanced features. ", 999.99, 100, 101)); + productRepository.save(new Product("Clothing", "Men's T-Shirt", + "Comfortable cotton t-shirt for everyday wear. ", 29.99, 500, 102)); + productRepository.save(new Product("Home & Kitchen", "Coffee Maker", + "Automatic coffee maker with programmable settings. ", 49.99, 50, 103)); + productRepository.save(new Product("Beauty", "Perfume", + "Elegant fragrance with floral and citrus notes. ", 79.99, 200, 104)); + productRepository.save(new Product("Books", "Science Fiction Novel", + "Bestselling sci-fi novel set in a dystopian future. ", 14.99, 300, 105)); + + reviewRepository.save(new Review("Electronics", "Great smartphone with excellent features. ", + 5, 101, 201)); + reviewRepository.save(new Review("Clothing", "Very comfortable t-shirt, fits perfectly. ", + 4, 102, 202)); + reviewRepository.save(new Review("Home & Kitchen", "Makes delicious coffee, easy to use. ", + 4, 103, 203)); + reviewRepository.save(new Review("Beauty", "Lovely fragrance, long-lasting. ", + 5, 104, 204)); + reviewRepository.save(new Review("Books", "Intriguing plot, couldn't put it down. ", + 5, 105, 205)); + } +} \ No newline at end of file diff --git a/src/test/java/sg/com/smartinventory/SmartInventoryApplicationTests.java b/src/test/java/sg/com/smartinventory/SmartInventoryApplicationTests.java index 329dc5e..fdac9f6 100644 --- a/src/test/java/sg/com/smartinventory/SmartInventoryApplicationTests.java +++ b/src/test/java/sg/com/smartinventory/SmartInventoryApplicationTests.java @@ -1,6 +1,7 @@ package sg.com.smartinventory; import org.junit.jupiter.api.Test; + import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest diff --git a/src/test/java/sg/com/smartinventory/CustomerControllerTest.java b/src/test/java/sg/com/smartinventory/controllers/CustomerControllerTest.java similarity index 98% rename from src/test/java/sg/com/smartinventory/CustomerControllerTest.java rename to src/test/java/sg/com/smartinventory/controllers/CustomerControllerTest.java index 52e472a..fd99903 100644 --- a/src/test/java/sg/com/smartinventory/CustomerControllerTest.java +++ b/src/test/java/sg/com/smartinventory/controllers/CustomerControllerTest.java @@ -1,9 +1,10 @@ -package sg.com.smartinventory; +package sg.com.smartinventory.controllers; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; diff --git a/src/test/java/sg/com/smartinventory/controllers/ProductControllerTest.java b/src/test/java/sg/com/smartinventory/controllers/ProductControllerTest.java new file mode 100644 index 0000000..81ce9da --- /dev/null +++ b/src/test/java/sg/com/smartinventory/controllers/ProductControllerTest.java @@ -0,0 +1,51 @@ +package sg.com.smartinventory.controllers; + +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.RequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import sg.com.smartinventory.entities.Product; + +@SpringBootTest +@AutoConfigureMockMvc +public class ProductControllerTest { + @Autowired + private MockMvc mockMvc; + + @Autowired + private ObjectMapper objectMapper; + + @DisplayName("Create product") + @Test + public void createProductTest() throws Exception { + // Step 1: Create a Product object + Product newProduct = Product.builder().category("Electronics").name("Smartphone") + .description("High-end smartphone with advanced features. ") + .price(999.99).stockQuantity(100).reviewId(101).build(); + + // Step 2: Convert the Java object to JSON using ObjectMapper. + String newProductAsJSON = objectMapper.writeValueAsString(newProduct); + + // Step 3: Build the request. + RequestBuilder request = MockMvcRequestBuilders.post("/products") + .contentType(MediaType.APPLICATION_JSON) + .content(newProductAsJSON); + + // Step 4: Perform the request and get the response and assert. + mockMvc.perform(request).andExpect(status().isCreated()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.category").value("Electronics")) + .andExpect(jsonPath("$.name").value("Smartphone")); + } +} \ No newline at end of file diff --git a/src/test/java/sg/com/smartinventory/controllers/ReviewControllerTest.java b/src/test/java/sg/com/smartinventory/controllers/ReviewControllerTest.java new file mode 100644 index 0000000..b73ebf5 --- /dev/null +++ b/src/test/java/sg/com/smartinventory/controllers/ReviewControllerTest.java @@ -0,0 +1,51 @@ +package sg.com.smartinventory.controllers; + +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.RequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import sg.com.smartinventory.entities.Review; + +@SpringBootTest +@AutoConfigureMockMvc +public class ReviewControllerTest { + @Autowired + private MockMvc mockMvc; + + @Autowired + private ObjectMapper objectMapper; + + @DisplayName("Create review") + @Test + public void createReviewTest() throws Exception { + // Step 1: Create a Review object + Review newReview = Review.builder().category("Electronics") + .reviewContent("Great smartphone with excellent features. ").rating(5).customerId(101) + .productId(201).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).andExpect(status().isCreated()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.category").value("Electronics")) + .andExpect(jsonPath("$.rating").value(5)); + } +} \ No newline at end of file diff --git a/src/test/java/sg/com/smartinventory/entities/CustomerTest.java b/src/test/java/sg/com/smartinventory/entities/CustomerTest.java new file mode 100644 index 0000000..350c448 --- /dev/null +++ b/src/test/java/sg/com/smartinventory/entities/CustomerTest.java @@ -0,0 +1,24 @@ +package sg.com.smartinventory.entities; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import org.mockito.InjectMocks; +import org.mockito.Mock; + +import sg.com.smartinventory.repositories.CustomerRepository; +import sg.com.smartinventory.serviceImpls.CustomerServiceImpl; + +public class CustomerTest { + @Mock + private CustomerRepository customerRepository; + + @InjectMocks + CustomerServiceImpl customerService; + + @DisplayName("Customer Test") + @Test + public void customerTest() throws Exception { + + } +} \ No newline at end of file diff --git a/src/test/java/sg/com/smartinventory/entities/ProductTest.java b/src/test/java/sg/com/smartinventory/entities/ProductTest.java new file mode 100644 index 0000000..0dcbce6 --- /dev/null +++ b/src/test/java/sg/com/smartinventory/entities/ProductTest.java @@ -0,0 +1,24 @@ +package sg.com.smartinventory.entities; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import org.mockito.InjectMocks; +import org.mockito.Mock; + +import sg.com.smartinventory.repositories.ProductRepository; +// import sg.com.smartinventory.serviceImpls.ProductServiceImpl; + +public class ProductTest { + @Mock + private ProductRepository productRepository; + + // @InjectMocks + // ProductServiceImpl productService; + + @DisplayName("Product Test") + @Test + public void productTest() throws Exception { + + } +} \ No newline at end of file diff --git a/src/test/java/sg/com/smartinventory/entities/ReviewTest.java b/src/test/java/sg/com/smartinventory/entities/ReviewTest.java new file mode 100644 index 0000000..b24d7ec --- /dev/null +++ b/src/test/java/sg/com/smartinventory/entities/ReviewTest.java @@ -0,0 +1,24 @@ +package sg.com.smartinventory.entities; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import org.mockito.InjectMocks; +import org.mockito.Mock; + +import sg.com.smartinventory.repositories.ReviewRepository; +// import sg.com.smartinventory.serviceImpls.ReviewServiceImpl; + +public class ReviewTest { + @Mock + private ReviewRepository reviewRepository; + + // @InjectMocks + // ReviewServiceImpl reviewService; + + @DisplayName("Review Test") + @Test + public void reviewTest() throws Exception { + + } +} \ 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 new file mode 100644 index 0000000..41966c3 --- /dev/null +++ b/src/test/java/sg/com/smartinventory/integration/IntegrationTest.java @@ -0,0 +1,103 @@ +package sg.com.smartinventory.integration; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import org.mockito.InjectMocks; +import org.mockito.Mock; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.RequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import sg.com.smartinventory.entities.Customer; +import sg.com.smartinventory.repositories.CustomerRepository; +import sg.com.smartinventory.serviceImpls.CustomerServiceImpl; + +@SpringBootTest +@AutoConfigureMockMvc +public class IntegrationTest { + @Mock + private CustomerRepository customerRepository; + + @InjectMocks + CustomerServiceImpl customerService; + + @Autowired + private MockMvc mockMvc; + + @Autowired + private ObjectMapper objectMapper; + + @DisplayName("Integration Test 1") + @Test + public void createCustomerTest() { + // 1. Setup. + Customer testObject1 = Customer.builder().firstName("Jackie").lastName("Chan").country("Hong Kong") + .address("123 HK St") + .postalCode(654321).mobileNumber(87654321).email("jackie.chan@example.com") + .reviewId(110).build(); + + // mock the save method of the customer repository + when((customerRepository.save(testObject1))).thenReturn(testObject1); + + // 2. Execution. + Customer savedObject1 = customerService.createCustomer(testObject1); + + // 3. Assertion. + assertEquals(testObject1, savedObject1, "The saved object should be the same as the new object."); + + // Verify that the save method of the customer repository is called once. + verify(customerRepository, times(1)).save(testObject1); + } + + @DisplayName("Integration Test 2") + @Test + public void runIntegratedTest() throws Exception { + // Step 1: Create the test objects. + Customer testObject1 = Customer.builder().firstName("Jackie").lastName("Chan").country("Hong Kong") + .address("123 HK St") + .postalCode(654321).mobileNumber(87654321).email("jackie.chan@example.com") + .reviewId(110).build(); + Customer testObject2 = Customer.builder().firstName("Jackie").lastName("Chang").country("Hong Kong") + .address("123 HK St") + .postalCode(654321).mobileNumber(87654321).email("jackie.chang@example.com") + .reviewId(110).build(); + + // Step 2: Convert the Java objects to JSON using ObjectMapper. + String testObject1AsJSON = objectMapper.writeValueAsString(testObject1); + String testObject2AsJSON = objectMapper.writeValueAsString(testObject2); + + // Step 3: Build the request. + 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).andExpect(status().isCreated()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.firstName").value("Jackie")) + .andExpect(jsonPath("$.lastName").value("Chan")); + mockMvc.perform(request2).andExpect(status().isCreated()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.firstName").value("Jackie")) + .andExpect(jsonPath("$.lastName").value("Chang")); + } +} \ No newline at end of file diff --git a/src/test/java/sg/com/smartinventory/CustomerServiceImplTest.java b/src/test/java/sg/com/smartinventory/services/CustomerServiceImplTest.java similarity index 97% rename from src/test/java/sg/com/smartinventory/CustomerServiceImplTest.java rename to src/test/java/sg/com/smartinventory/services/CustomerServiceImplTest.java index 3acbf20..1bdcfed 100644 --- a/src/test/java/sg/com/smartinventory/CustomerServiceImplTest.java +++ b/src/test/java/sg/com/smartinventory/services/CustomerServiceImplTest.java @@ -1,13 +1,16 @@ -package sg.com.smartinventory; +package sg.com.smartinventory.services; import static org.junit.jupiter.api.Assertions.assertEquals; + import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import org.junit.jupiter.api.Test; + import org.mockito.InjectMocks; import org.mockito.Mock; + import org.springframework.boot.test.context.SpringBootTest; import sg.com.smartinventory.entities.Customer; diff --git a/src/test/java/sg/com/smartinventory/useCases/UseCaseTest.java b/src/test/java/sg/com/smartinventory/useCases/UseCaseTest.java new file mode 100644 index 0000000..4592a44 --- /dev/null +++ b/src/test/java/sg/com/smartinventory/useCases/UseCaseTest.java @@ -0,0 +1,103 @@ +package sg.com.smartinventory.useCases; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import org.mockito.InjectMocks; +import org.mockito.Mock; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.RequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import sg.com.smartinventory.entities.Customer; +import sg.com.smartinventory.repositories.CustomerRepository; +import sg.com.smartinventory.serviceImpls.CustomerServiceImpl; + +@SpringBootTest +@AutoConfigureMockMvc +public class UseCaseTest { + @Mock + private CustomerRepository customerRepository; + + @InjectMocks + CustomerServiceImpl customerService; + + @Autowired + private MockMvc mockMvc; + + @Autowired + private ObjectMapper objectMapper; + + @DisplayName("Use Case Test 1") + @Test + public void createCustomerTest() { + // 1. Setup. + Customer testObject1 = Customer.builder().firstName("Jackie").lastName("Chan").country("Hong Kong") + .address("123 HK St") + .postalCode(654321).mobileNumber(87654321).email("jackie.chan@example.com") + .reviewId(110).build(); + + // mock the save method of the customer repository + when((customerRepository.save(testObject1))).thenReturn(testObject1); + + // 2. Execution. + Customer savedObject1 = customerService.createCustomer(testObject1); + + // 3. Assertion. + assertEquals(testObject1, savedObject1, "The saved object should be the same as the new object."); + + // Verify that the save method of the customer repository is called once. + verify(customerRepository, times(1)).save(testObject1); + } + + @DisplayName("Use Case Test 2") + @Test + public void runIntegratedTest() throws Exception { + // Step 1: Create the test objects. + Customer testObject1 = Customer.builder().firstName("Jackie").lastName("Chan").country("Hong Kong") + .address("123 HK St") + .postalCode(654321).mobileNumber(87654321).email("jackie.chan@example.com") + .reviewId(110).build(); + Customer testObject2 = Customer.builder().firstName("Jackie").lastName("Chang").country("Hong Kong") + .address("123 HK St") + .postalCode(654321).mobileNumber(87654321).email("jackie.chang@example.com") + .reviewId(110).build(); + + // Step 2: Convert the Java objects to JSON using ObjectMapper. + String testObject1AsJSON = objectMapper.writeValueAsString(testObject1); + String testObject2AsJSON = objectMapper.writeValueAsString(testObject2); + + // Step 3: Build the request. + 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).andExpect(status().isCreated()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.firstName").value("Jackie")) + .andExpect(jsonPath("$.lastName").value("Chan")); + mockMvc.perform(request2).andExpect(status().isCreated()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.firstName").value("Jackie")) + .andExpect(jsonPath("$.lastName").value("Chang")); + } +} \ No newline at end of file diff --git a/src/test/java/sg/com/smartinventory/utility/DataLoaderTest.java b/src/test/java/sg/com/smartinventory/utility/DataLoaderTest.java new file mode 100644 index 0000000..1def923 --- /dev/null +++ b/src/test/java/sg/com/smartinventory/utility/DataLoaderTest.java @@ -0,0 +1,32 @@ +package sg.com.smartinventory.utility; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import org.mockito.InjectMocks; +import org.mockito.Mock; + +import sg.com.smartinventory.repositories.CustomerRepository; +// import sg.com.smartinventory.serviceImpls.CustomerServiceImpl; +import sg.com.smartinventory.repositories.ProductRepository; +// import sg.com.smartinventory.serviceImpls.ProductServiceImpl; +import sg.com.smartinventory.repositories.ReviewRepository; +// import sg.com.smartinventory.serviceImpls.ReviewServiceImpl; + +public class DataLoaderTest { + @Mock + private CustomerRepository customerRepository; + private ProductRepository productRepository; + private ReviewRepository reviewRepository; + + // @InjectMocks + // CustomerServiceImpl customerService; + // ProductServiceImpl productService; + // ReviewServiceImpl reviewService; + + @DisplayName("DataLoader Test") + @Test + public void dataLoaderTest() throws Exception { + + } +} \ No newline at end of file