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

Refactor existing test cases #769

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ databaseChangeLog:
- includeAll:
path: db/changelog/ddl/
- includeAll:
path: db/changelog/data/
# - includeAll:
# path: db/changelog/eventuate-dll/
path: db/changelog/data/
87 changes: 38 additions & 49 deletions cart/src/test/java/com/yas/cart/service/CartServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,78 +1,67 @@
package com.yas.cart.service;

import com.yas.cart.CartApplication;
import com.yas.cart.model.Cart;
import com.yas.cart.model.CartItem;
import com.yas.cart.repository.CartItemRepository;
import com.yas.cart.repository.CartRepository;
import com.yas.cart.viewmodel.CartGetDetailVm;
import com.yas.cart.viewmodel.CartListVm;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;

import java.util.HashSet;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest(classes = CartApplication.class)
class CartServiceTest {
CartRepository cartRepository;
CartItemRepository cartItemRepository;
CartService cartService;
ProductService productService;

CartGetDetailVm cartGetDetailVm;
@Autowired
private CartRepository cartRepository;
@Autowired
private CartItemRepository cartItemRepository;
@MockBean
private ProductService productService;
@Autowired
private CartService cartService;
Cart cart1;
Cart cart2;
List<Cart> carts;

Authentication authentication;

@BeforeEach
void setUp() {
cartRepository = mock(CartRepository.class);
cartItemRepository = mock(CartItemRepository.class);
productService = mock(ProductService.class);
cartService = new CartService(
cartRepository,
cartItemRepository,
productService);
cart1 = cartRepository.save(Cart
.builder().customerId("customer-1").build());
cart2 = cartRepository.save(Cart
.builder().customerId("customer-2").build());

cartGetDetailVm = new CartGetDetailVm(1L, "customerId",null, null);
CartItem cartItem1 = new CartItem();
cartItem1.setProductId(1L);
cartItem1.setQuantity(2);
cartItem1.setCart(cart1);
CartItem cartItem2 = new CartItem();
cartItem2.setProductId(2L);
cartItem2.setQuantity(3);
cartItem2.setCart(cart2);

HashSet<CartItem> cartItemList = new HashSet<>();
cartItemList.add(new CartItem(1L, null, 1L, null, 1));
cartItemList.add(new CartItem(2L, null, 2L, null, 2));
cart1 = new Cart(1L, "customer-1", null,cartItemList);
cart2 = new Cart(2L, "customer-2",null, null);
carts = List.of(cart1, cart2);
cartItemRepository.saveAll(List.of(cartItem1, cartItem2));
}

//Security config
authentication = mock(Authentication.class);
Mockito.when(authentication.getName()).thenReturn("Name");
SecurityContextHolder.getContext().setAuthentication(authentication);
@AfterEach
void tearDown() {
cartItemRepository.deleteAll();
cartRepository.deleteAll();
}

@Test
void getCarts_ExistProductsInDatabase_Success() {
//given
List<CartListVm> cartListVmExpected = List.of(
new CartListVm(1L, "customer-1"),
new CartListVm(2L, "customer-2")
);
when(cartRepository.findAll()).thenReturn(carts);

//when
void getCarts_ExistInDatabase_Success() {
List<CartListVm> cartListVmActual = cartService.getCarts();

//then
assertThat(cartListVmActual).hasSameSizeAs(cartListVmExpected);
assertThat(cartListVmActual.get(0)).isEqualTo(cartListVmExpected.get(0));
assertThat(cartListVmActual.get(1)).isEqualTo(cartListVmExpected.get(1));

assertEquals(2, cartListVmActual.size());
for(CartListVm cartListVm : cartListVmActual) {
assertThat(cartListVm.customerId().startsWith("customer-"));
}
}
}
9 changes: 4 additions & 5 deletions cart/src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

spring.jpa.hibernate.ddl-auto=update
spring.liquibase.enabled=false

# Setting Spring profile
spring.profiles.active=test
springdoc.swagger-ui.enabled=true
springdoc.api-docs.enabled=true

# swagger-ui custom path
springdoc.swagger-ui.path=/swagger-ui.html
springdoc.packagesToScan=com.yas.cart
spring.security.oauth2.resourceserver.jwt.issuer-uri=test
springdoc.oauthflow.authorization-url=test
springdoc.oauthflow.token-url=test
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.yas.order.viewmodel.order;

import lombok.Builder;

import java.math.BigDecimal;

@Builder
public record OrderItemPostVm(
Long productId,
String productName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import com.yas.order.model.enumeration.EPaymentStatus;
import com.yas.order.viewmodel.orderaddress.OrderAddressPostVm;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;

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

@Builder
public record OrderPostVm(
String checkoutId,
String email,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ databaseChangeLog:
- includeAll:
path: db/changelog/ddl/
- includeAll:
path: db/changelog/data/
# - includeAll:
# path: db/changelog/eventuate-dll/
path: db/changelog/data/
90 changes: 88 additions & 2 deletions order/src/test/java/com/yas/order/service/OrderServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,106 @@
package com.yas.order.service;

import com.yas.order.OrderApplication;
import com.yas.order.model.Order;
import com.yas.order.model.OrderItem;
import com.yas.order.repository.OrderItemRepository;
import com.yas.order.repository.OrderRepository;
import com.yas.order.viewmodel.order.OrderItemPostVm;
import com.yas.order.viewmodel.order.OrderPostVm;
import com.yas.order.viewmodel.order.OrderVm;
import com.yas.order.viewmodel.orderaddress.OrderAddressPostVm;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;

import javax.swing.text.html.Option;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import static org.mockito.Mockito.mock;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.*;

@SpringBootTest(classes = OrderApplication.class)
class OrderServiceTest {

@MockBean
private ProductService productService;
@MockBean
private CartService cartService;
@Autowired
private OrderItemRepository orderItemRepository;
@Autowired
private OrderRepository orderRepository;
@Autowired
private OrderService orderService;
private OrderItemPostVm orderItemPostVm;
private OrderAddressPostVm orderAddressPostVm;
private OrderPostVm orderPostVm;


@BeforeEach
void setUp() {
orderItemPostVm = OrderItemPostVm.builder()
.productId(1L).productName("abc")
.quantity(1).productPrice(BigDecimal.TEN)
.discountAmount(BigDecimal.ONE).taxAmount(BigDecimal.ONE).taxPercent(BigDecimal.ONE)
.build();
orderAddressPostVm = OrderAddressPostVm.builder()
.contactName("contactName").phone("phone").addressLine1("addressLine1").addressLine2("addressLine2")
.city("city").zipCode("zipCode").districtId(1L).districtName("districtName")
.stateOrProvinceId(1L).stateOrProvinceName("stateOrProvinceName")
.countryId(1L).countryName("countryName")
.build();

orderPostVm = OrderPostVm.builder()
.checkoutId("1").email("[email protected]")
.orderItemPostVms(Arrays.asList(orderItemPostVm))
.billingAddressPostVm(orderAddressPostVm)
.shippingAddressPostVm(orderAddressPostVm)
.build();
}

@AfterEach
void tearDown() {
orderItemRepository.deleteAll();
orderRepository.deleteAll();
}

@Test
void Test() {
void testCreateOrder_successful() {

OrderVm orderVm = orderService.createOrder(orderPostVm);

Optional<Order> orderOptional = orderRepository.findById(orderVm.id());
assertTrue(orderOptional.isPresent());
Order orderDB = orderOptional.get();
assertEquals("[email protected]", orderDB.getEmail());
assertEquals(1, orderDB.getOrderItems().size());
assertEquals("abc", orderDB.getOrderItems().stream().findFirst().get().getProductName());
}

@Test
void testCreateOrder_RemoteServiceThrowsException_RollbackOrder() {
doThrow(new RuntimeException()).when(productService).subtractProductStockQuantity(any(OrderVm.class));
try {
orderService.createOrder(orderPostVm);
} catch (Exception e) {

}
List<Order> orders = orderRepository.findAll();
assertEquals(0, orders.size());
List<OrderItem> orderItems = orderItemRepository.findAll();
assertEquals(0, orderItems.size());
}

}
15 changes: 7 additions & 8 deletions order/src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
server.servlet.context-path=/v1
server.port=8085

# Setting Spring profile
spring.profiles.active=test

spring.datasource.url=jdbc:h2:mem:testdb;NON_KEYWORDS=VALUE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

spring.jpa.hibernate.ddl-auto=update
spring.liquibase.enabled=false

# Setting Spring profile
spring.profiles.active=test
springdoc.swagger-ui.enabled=true
springdoc.api-docs.enabled=true

# swagger-ui custom path
springdoc.swagger-ui.path=/swagger-ui.html
springdoc.packagesToScan=com.yas.order
spring.security.oauth2.resourceserver.jwt.issuer-uri=test
springdoc.oauthflow.authorization-url=test
springdoc.oauthflow.token-url=test
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ databaseChangeLog:
- includeAll:
path: db/changelog/ddl/
- includeAll:
path: db/changelog/data/
# - includeAll:
# path: db/changelog/eventuate-dll/
path: db/changelog/data/
5 changes: 5 additions & 0 deletions product/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ databaseChangeLog:
- includeAll:
path: db/changelog/ddl/
- includeAll:
path: db/changelog/data/
# - includeAll:
# path: db/changelog/eventuate-dll/
path: db/changelog/data/
19 changes: 0 additions & 19 deletions product/src/test/java/com/yas/product/config/SecurityConfig.java

This file was deleted.

Loading
Loading