Skip to content

Commit

Permalink
Add unit test for get all carts
Browse files Browse the repository at this point in the history
  • Loading branch information
Patpum Hakaew authored and Patpum Hakaew committed Mar 31, 2024
1 parent 9016007 commit 6c0e12d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 43 deletions.
31 changes: 0 additions & 31 deletions kbazaar/src/main/java/com/kampus/kbazaar/cart/CartService.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,4 @@ public List<CartResponse> getAllCart() {

return carts;
}

// public List<CartResponse> getAllCart() {
// List<Cart> carts = cartRepository.findAll();
// List<CartResponse> cartResponseDto = new ArrayList<>();
// for (Cart cart : carts) {
// List<CartItem> cartItems = cartItemRepository.findByUsername(cart.getUsername());
// List<CartItemDto> cartItemDtos = new ArrayList<>();
// for (CartItem cartItem : cartItems) {
// cartItemDtos.add(
// new CartItemDto(
// cartItem.getId(),
// cartItem.getUsername(),
// cartItem.getSku(),
// cartItem.getPrice(),
// cartItem.getQuantity(),
// cartItem.getDiscount(),
// cartItem.getPromotionCodes()));
// }
//
// CartResponseDto cartResponse =
// new CartResponseDto(
// cart.getUsername(),
// cartItemDtos,
// cart.getDiscount(),
// cart.getTotalDiscount(),
// cart.getSubtotal(),
// cart.getGrandTotal());
// }
//
// return cartResponseDto;
// }
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.kampus.kbazaar.cart;

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.kampus.kbazaar.promotion.PromotionService;
import com.kampus.kbazaar.security.JwtAuthFilter;
import java.math.BigDecimal;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -48,16 +51,29 @@ public void getCart_ReturnsOk() throws Exception {
.andExpect(status().isOk());
}

// @Test
// public void getCards_ReturnsCorrectResponse() throws Exception {
// mockMvc.perform(get("/api/v1/carts").contentType(MediaType.APPLICATION_JSON))
// .andDo(print())
// .andExpect(jsonPath("$.username").value("TechNinja"))
// .andExpect(jsonPath("$.items").isArray())
// .andExpect(jsonPath("$.discount").value(0))
// .andExpect(jsonPath("$.totalDiscount").value(0))
// .andExpect(jsonPath("$.subtotal").value(1))
// .andExpect(jsonPath("$.grandTotal").value(1))
// .andExpect(status().isOk());
// }
@Test
public void getCards_ReturnsCorrectResponse() throws Exception {
when(cartService.getAllCart())
.thenReturn(
List.of(
CartResponse.builder()
.username("TechNinja")
.discount(BigDecimal.valueOf(0))
.totalDiscount(BigDecimal.valueOf(0))
.subtotal(BigDecimal.valueOf(1))
.grandTotal(BigDecimal.valueOf(1))
.items(List.of())
.build()));

mockMvc.perform(get("/api/v1/carts").contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(jsonPath("$.length()").value(1))
.andExpect(jsonPath("$[0].username").value("TechNinja"))
.andExpect(jsonPath("$[0].items").isArray())
.andExpect(jsonPath("$[0].discount").value(0))
.andExpect(jsonPath("$[0].totalDiscount").value(0))
.andExpect(jsonPath("$[0].subtotal").value(1))
.andExpect(jsonPath("$[0].grandTotal").value(1))
.andExpect(status().isOk());
}
}

0 comments on commit 6c0e12d

Please sign in to comment.