Skip to content

Commit

Permalink
Refactor cart service and controller
Browse files Browse the repository at this point in the history
Add unit test for cart service
  • Loading branch information
ce-pong committed Mar 31, 2024
1 parent 627cfa6 commit 938305b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ public CartController(CartService cartService) {
}

@GetMapping("/carts")
public ResponseEntity<List<CartResponse>> getCart() { // NOSONAR
// return ResponseEntity.ok().build();

public ResponseEntity<List<CartResponse>> getCart() {
return new ResponseEntity<>(cartService.getAll(), HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,9 @@ public List<CartResponse> getAll(){
if(carts.isEmpty()){
throw new NotFoundException("Cart not found");
}

// CartResponse cartResponse = new CartResponse();
List<CartResponse> cartResponseList = new ArrayList<CartResponse>();
List<CartResponse> cartResponseList = new ArrayList<>();
for (Cart cart: carts){
List<CartItem> cartItems = cartItemService.findByUsername(cart.getUsername());
//Calculate for each username
// BigDecimal discount = 0;
// for (CartItem cartItem: cartItems){
//
// }
CartResponse cartResponse = new CartResponse(
cart.getUsername(),
cartItems,
Expand Down
63 changes: 63 additions & 0 deletions kbazaar/src/test/java/com/kampus/kbazaar/cart/CartServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.kampus.kbazaar.cart;

import com.kampus.kbazaar.cartItem.CartItem;
import com.kampus.kbazaar.cartItem.CartItemService;
import com.kampus.kbazaar.exceptions.NotFoundException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;


import java.math.BigDecimal;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;

class CartServiceTest {

@Mock private CartRepository cartRepository;
@Mock private CartItemService cartItemService;

@InjectMocks private CartService cartService;



@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

@Test
void shouldBeThrowNotFoundExceptionWhenCardEmpty(){
//Arrange
when(cartRepository.findAll()).thenReturn(List.of());

//Act & Assert
assertThrows(NotFoundException.class,() -> cartService.getAll());

}

@Test
void shouldBeAbleToListCartResponse(){
//Arrange
String username = "TechNinja";
Cart cart = new Cart();
cart.setUsername(username);
when(cartRepository.findAll()).thenReturn(List.of(cart));

CartItem cartItem = new CartItem(1L,"TechNinja","BEV-COCA-COLA","Coca-Cola",BigDecimal.valueOf(10),BigDecimal.valueOf(10),BigDecimal.valueOf(10),"FIXEDAMOUNT10");

when(cartItemService.findByUsername(username)).thenReturn(List.of(cartItem));

//Act
List<CartResponse> actualResult = cartService.getAll();

//Assert
assertEquals(username,actualResult.get(0).getUsername());


}
}

0 comments on commit 938305b

Please sign in to comment.