Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
naruepracht-s committed Mar 24, 2024
2 parents a570aa2 + 996281b commit ad9355c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion infra/gitops/dev/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,13 @@ public CreateCartResponse createCart(String userName, CartRequest cartRequest) {
CreateCartResponse response = new CreateCartResponse();
response.setItems(new ArrayList<>());
Optional<Shopper> shopper = shopperRepository.findByUsername(userName);

Optional<Cart> 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()) {
Expand Down Expand Up @@ -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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down

0 comments on commit ad9355c

Please sign in to comment.