Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Charat-Ninjar committed Mar 24, 2024
2 parents 22834df + 20aca1a commit e0b3dcb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 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-4:2813dfdc69068a041c985c2d480b4217196acfe4
image: ghcr.io/kbtg-kampus-classnest-se-java/workshop-group-4:90755c3cb236cd9d33ca8a736f7b980a3041eada
imagePullPolicy: Always
ports:
- containerPort: 8080
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
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.MockMvcResultMatchers.status;

import com.kampus.kbazaar.product.Product;
import com.kampus.kbazaar.security.JwtAuthFilter;
import org.junit.jupiter.api.BeforeEach;
import com.kampus.kbazaar.shopper.Shopper;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc(addFilters = false)
@WebMvcTest(
controllers = CartController.class,
excludeFilters =
Expand All @@ -32,17 +40,49 @@ public class CartControllerTest {

@MockBean private CartService cartService;

@InjectMocks private CartController cartController;

@BeforeEach
public void setup() {
MockitoAnnotations.openMocks(this);
this.mockMvc = MockMvcBuilders.standaloneSetup(cartController).build();
}

@Test
public void getCart_ReturnsOk() throws Exception {
mockMvc.perform(get("/api/v1/carts").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

@Test
public void testAddProduct() throws Exception {
// Arrange
Shopper shopper = new Shopper();
shopper.setEmail("[email protected]");
shopper.setId(1L);
shopper.setUsername("janedoe");

Cart cart = new Cart();
Product product = new Product();
product.setId(1L);
product.setPrice(new BigDecimal(100));
product.setName("IPHONE 15");
product.setSku("IPHONE_15");

CartProduct cartProduct = new CartProduct();
cartProduct.setId(1L);
cartProduct.setProduct(product);
cartProduct.setQuantity(1);

cart.setCartProducts(new ArrayList<>(Arrays.asList(cartProduct)));
cart.setId(1L);
cart.setPromotions(new HashSet<>());
cart.setShopper(shopper);
when(cartService.addProductByUsernameAndProductSku(
Mockito.<String>any(), Mockito.<String>any(), Mockito.<Integer>any()))
.thenReturn(cart);

String requestBody =
"{\"cartId\":1,\"items\":[{\"productSku\": \"IPHONE_15\", \"quantity\":\"1\"}],\"promotionCodes\":[],\"totalCost\":0,\"entireCartPromotionDiscount\":0,\"finalTotalCost"
+ "\":0}";
mockMvc.perform(
MockMvcRequestBuilders.post("/api/v1/carts/janedoe/items")
.contentType(MediaType.APPLICATION_JSON)
.content(requestBody))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.jsonPath("$.items[0].name").value("IPHONE 15"));
}
}

0 comments on commit e0b3dcb

Please sign in to comment.