From 10b2081c77f9438d4019cf7520c25804e284ed02 Mon Sep 17 00:00:00 2001 From: Poy Date: Sun, 24 Mar 2024 12:46:31 +0700 Subject: [PATCH 1/2] create cart test controller --- .../com/kampus/kbazaar/cart/CartService.java | 7 +++- .../kbazaar/cart/CartControllerTest.java | 40 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartService.java b/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartService.java index 962ab35..c5d8cc6 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartService.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartService.java @@ -69,8 +69,13 @@ public ApplyCodeResponse applyCode(String username, ApplyCodeRequest request) { public CreateCartResponse createCart(String userName, CartRequest cartRequest) { CreateCartResponse response = new CreateCartResponse(); Optional shopper = shopperRepository.findByUsername(userName); + Optional checkShopperHaveCart = cartRepository.findByShopperId(shopper.get().getId()); + productRepository + .findBySku(cartRequest.getSku()) + .orElseThrow(() -> new NotFoundException("Product not found")); + if (shopper.isPresent()) { Cart newCart = new Cart(); if (checkShopperHaveCart.isEmpty()) { @@ -122,7 +127,7 @@ public CreateCartResponse createCart(String userName, CartRequest cartRequest) { return response; } else { - throw new BadRequestException("Product not found"); + throw new NotFoundException("User not found"); } } } diff --git a/kbazaar/src/test/java/com/kampus/kbazaar/cart/CartControllerTest.java b/kbazaar/src/test/java/com/kampus/kbazaar/cart/CartControllerTest.java index 4481dfb..86ff7bc 100644 --- a/kbazaar/src/test/java/com/kampus/kbazaar/cart/CartControllerTest.java +++ b/kbazaar/src/test/java/com/kampus/kbazaar/cart/CartControllerTest.java @@ -5,6 +5,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import com.kampus.kbazaar.exceptions.NotFoundException; import com.kampus.kbazaar.security.JwtAuthFilter; import java.math.BigDecimal; import java.util.ArrayList; @@ -83,6 +84,45 @@ public void createCart_ReturnsCreated() throws Exception { .andExpect(status().isCreated()); } + @Test + public void createCart_ReturnsFailedUserNotFound() throws Exception { + + CartRequest cartRequest = new CartRequest(); + cartRequest.setSku("MOBILE-APPLE-IPHONE-12-PRO"); + cartRequest.setQty(1); + String username = "TechNinja1"; + + Exception exception = new NotFoundException("User not found"); + + when(cartService.createCart(username, cartRequest)).thenThrow(exception); + mockMvc.perform( + post("/api/v1/carts/{username}/items", username) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content("{\"sku\":\"MOBILE-APPLE-IPHONE-12-PRO\",\"qty\":1}")) + .andExpect(status().isNotFound()); + } + + @Test + public void createCart_ReturnsFailedProductNotFound() throws Exception { + + CartRequest cartRequest = new CartRequest(); + cartRequest.setSku("MOBILE-APPLE-IPHONE-12-PRO-MAXMAX"); + cartRequest.setQty(1); + String username = "TechNinja"; + + Exception exception = new NotFoundException("Product not found"); + + when(cartService.createCart(username, cartRequest)).thenThrow(exception); + mockMvc.perform( + post("/api/v1/carts/{username}/items", username) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content( + "{\"sku\":\"MOBILE-APPLE-IPHONE-12-PRO-MAXMAX\",\"qty\":1}")) + .andExpect(status().isNotFound()); + } + // @Test // public void applyCodeIfSuccess_ShouldReturnOk() throws Exception { // String username = "TechNinja"; From 996281b1811e097a4ec6ebe46bf374dcbeac62e8 Mon Sep 17 00:00:00 2001 From: robot Date: Sun, 24 Mar 2024 05:51:09 +0000 Subject: [PATCH 2/2] =?UTF-8?q?[skip=20actions]=20=F0=9F=A4=96=20change=20?= =?UTF-8?q?docker=20image=20version=20to=20ae41d8532c2a968eaa1d425d513380d?= =?UTF-8?q?a85262151?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- infra/gitops/dev/deployment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/infra/gitops/dev/deployment.yml b/infra/gitops/dev/deployment.yml index b295b39..d56ef3d 100644 --- a/infra/gitops/dev/deployment.yml +++ b/infra/gitops/dev/deployment.yml @@ -15,7 +15,7 @@ spec: spec: containers: - name: kbazaar-api - image: ghcr.io/kbtg-kampus-classnest-se-java/workshop-group-2:bdf1b3108b1f2b27a2d8b2c59ce868e391a8cdc7 + image: ghcr.io/kbtg-kampus-classnest-se-java/workshop-group-2:ae41d8532c2a968eaa1d425d513380da85262151 imagePullPolicy: Always ports: - containerPort: 8080