Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write all unit-test #22

Merged
merged 10 commits into from
Jun 6, 2024
9 changes: 4 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.learning</groupId>
<artifactId>SpringSecurity</artifactId>
<artifactId>YasMiniShop</artifactId>
<version>1.0.0</version>
<name>SpringSecurity</name>
<description>SpringSecurity</description>
<name>YasMini</name>
<description>YasMini CarShop</description>
<properties>
<java.version>21</java.version>
<projectlombok-lombok.version>1.18.30</projectlombok-lombok.version>
Expand Down Expand Up @@ -198,10 +198,9 @@
</executions>
<configuration>
<excludes>
<exclude>**/common/**</exclude>
<exclude>**/dto/**</exclude>
<exclude>**/entity/**</exclude>
<exclude>**/mapper/**</exclude>
<exclude>**/configs/**</exclude>
</excludes>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.persistence.*;
import lombok.*;
import lombok.experimental.FieldDefaults;

@Entity
@Table(name = "t_notification")
Expand All @@ -10,6 +11,7 @@
@Builder
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Notification extends AuditEntity<String>{
@Id
@GeneratedValue(strategy = GenerationType.UUID)
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package com.learning.yasminishop.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.learning.yasminishop.cart.CartItemRepository;
import com.learning.yasminishop.cart.dto.request.CartItemRequest;
import com.learning.yasminishop.common.entity.CartItem;
import com.learning.yasminishop.common.entity.Product;
import com.learning.yasminishop.common.entity.User;
import com.learning.yasminishop.product.ProductRepository;
import com.learning.yasminishop.user.UserRepository;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import java.math.BigDecimal;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@Slf4j
@AutoConfigureMockMvc
@TestPropertySource("/test.properties")
class CartControllerTest {

@Autowired
private MockMvc mockMvc;

@Autowired
private ProductRepository productRepository;

@Autowired
private CartItemRepository cartItemRepository;

@Autowired
private UserRepository userRepository;

private CartItemRequest cartItemRequest;
private Product product;
private User user;
private CartItem cartItem;

@BeforeEach
void setUp() {
cartItemRequest = CartItemRequest.builder()
.productId("product-1")
.quantity(2)
.build();
product = Product.builder()
.name("Product 1")
.description("Product 1 description")
.price(BigDecimal.valueOf(100))
.quantity(10L)
.slug("product-1")
.sku("sku-1")
.isFeatured(true)
.isAvailable(true)
.build();
user = User.builder()
.email("[email protected]")
.password("password")
.build();
cartItem = CartItem.builder()
.product(product)
.quantity(2)
.price(BigDecimal.valueOf(200))
.build();
}

@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD)
@WithMockUser(username = "[email protected]")
void createCart_validRequest_success() throws Exception {
// GIVEN
ObjectMapper objectMapper = new ObjectMapper();
Product savedProduct = productRepository.save(product);
userRepository.save(user);
cartItemRequest.setProductId(savedProduct.getId());
String requestJson = objectMapper.writeValueAsString(cartItemRequest);

// WHEN THEN
mockMvc.perform(MockMvcRequestBuilders.post("/carts")
.contentType(MediaType.APPLICATION_JSON)
.content(requestJson))
.andExpect(status().isCreated())
.andExpect(jsonPath("internalCode").value(1000))
.andExpect(jsonPath("result.product.name").value("Product 1"));
}

@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD)
@WithMockUser(username = "[email protected]")
void getAllCarts_validRequest_success() throws Exception {
// GIVEN
Product savedProduct = productRepository.save(product);
User savedUser = userRepository.save(user);
cartItem.setProduct(savedProduct);
cartItem.setUser(savedUser);
cartItemRepository.save(cartItem);


// WHEN THEN
mockMvc.perform(MockMvcRequestBuilders.get("/carts"))
.andExpect(status().isOk())
.andExpect(jsonPath("internalCode").value(1000))
.andExpect(jsonPath("result[0].product.name").value("Product 1"));
}

@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD)
@WithMockUser(username = "[email protected]")
void updateCart_validRequest_success() throws Exception {
// GIVEN
Product savedProduct = productRepository.save(product);
User savedUser = userRepository.save(user);
cartItem.setProduct(savedProduct);
cartItem.setUser(savedUser);
CartItem savedCartItem = cartItemRepository.save(cartItem);
String requestJson = "{\"quantity\": 5}";

// WHEN THEN
mockMvc.perform(MockMvcRequestBuilders.put("/carts/" + savedCartItem.getId())
.contentType(MediaType.APPLICATION_JSON)
.content(requestJson))
.andExpect(status().isOk())
.andExpect(jsonPath("internalCode").value(1000))
.andExpect(jsonPath("result.quantity").value(5));
}

@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD)
@WithMockUser(username = "[email protected]")
void deleteCart_validRequest_success() throws Exception {
// GIVEN
Product savedProduct = productRepository.save(product);
User savedUser = userRepository.save(user);
cartItem.setProduct(savedProduct);
cartItem.setUser(savedUser);
CartItem savedCartItem = cartItemRepository.save(cartItem);
String requestJson = "{\"ids\": [\"" + savedCartItem.getId() + "\"]}";

// WHEN THEN
mockMvc.perform(MockMvcRequestBuilders.delete("/carts")
.contentType(MediaType.APPLICATION_JSON)
.content(requestJson))
.andExpect(status().isOk())
.andExpect(jsonPath("internalCode").value(1000))
.andExpect(jsonPath("message").value("Cart items deleted successfully"));
}

@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD)
@WithMockUser(username = "[email protected]")
void getCartItemsByIds_validRequest_success() throws Exception {
// GIVEN
Product savedProduct = productRepository.save(product);
User savedUser = userRepository.save(user);
cartItem.setProduct(savedProduct);
cartItem.setUser(savedUser);
CartItem savedCartItem = cartItemRepository.save(cartItem);

// WHEN THEN
mockMvc.perform(MockMvcRequestBuilders.get("/carts/get-by-ids")
.param("ids", savedCartItem.getId()))
.andExpect(status().isOk())
.andExpect(jsonPath("internalCode").value(1000))
.andExpect(jsonPath("result[0].product.name").value("Product 1"));
}

}
Loading
Loading