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 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 fe4e1e0..95e7170 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartService.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartService.java @@ -73,8 +73,13 @@ public CreateCartResponse createCart(String userName, CartRequest cartRequest) { CreateCartResponse response = new CreateCartResponse(); response.setItems(new ArrayList<>()); 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()) { @@ -130,7 +135,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";