Skip to content

Commit

Permalink
OrderItemMapperImpl tests adjusted to the new configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
kaizerpwn committed Jun 3, 2024
1 parent d2617f8 commit 0ead1f0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class OrderItemMapperImpl implements Mapper<OrderItem, OrderItemDto> {

public OrderItemMapperImpl(ModelMapper modelMapper) {
this.modelMapper = modelMapper;
this.modelMapper.getConfiguration().setAmbiguityIgnored(true);
this.modelMapper.typeMap(OrderItem.class, OrderItemDto.class).addMappings(mapper -> {
mapper.map(src -> src.getOrder().getOrderId(), OrderItemDto::setOrderId);
mapper.map(src -> src.getProduct().getProductId(), OrderItemDto::setProductId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

import com.ibrahimokic.ordermanagement.domain.dto.OrderItemDto;
import com.ibrahimokic.ordermanagement.domain.entity.OrderItem;
import com.ibrahimokic.ordermanagement.domain.entity.Product;
import com.ibrahimokic.ordermanagement.mapper.impl.OrderItemMapperImpl;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.modelmapper.ModelMapper;

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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.mock;


class OrderItemMapperImplTest {

Expand All @@ -28,38 +26,38 @@ void setUp() {

@Test
void testMapFrom() {
Product product = mock(Product.class);
OrderItemDto orderItemDto = OrderItemDto.builder()
.productId(product.getProductId())
.orderId(1L)
.productId(1L)
.quantity(2)
.itemPrice(new BigDecimal(50))
.build();

OrderItem orderItem = orderItemMapper.mapFrom(orderItemDto);

assertNotNull(orderItem);
assertEquals(orderItemDto.getOrderId(), orderItem.getOrder().getOrderId());
assertEquals(orderItemDto.getProductId(), orderItem.getProduct().getProductId());
assertEquals(orderItemDto.getQuantity(), orderItem.getQuantity());
assertEquals(orderItemDto.getItemPrice(), orderItem.getItemPrice());
}

@Test
void testMapListToEntityList() {
Product product1 = mock(Product.class);
Product product2 = mock(Product.class);
List<OrderItemDto> orderItemDtos = new ArrayList<>();
orderItemDtos.add(OrderItemDto.builder().productId(product1.getProductId()).quantity(2).itemPrice(new BigDecimal(50)).build());
orderItemDtos.add(OrderItemDto.builder().productId(product2.getProductId()).quantity(3).itemPrice(new BigDecimal(60)).build());
orderItemDtos.add(OrderItemDto.builder().orderId(1L).productId(1L).quantity(2).itemPrice(new BigDecimal(50)).build());
orderItemDtos.add(OrderItemDto.builder().orderId(2L).productId(2L).quantity(3).itemPrice(new BigDecimal(60)).build());

List<OrderItem> orderItems = orderItemMapper.mapListToEntityList(orderItemDtos);

assertNotNull(orderItems);
assertEquals(orderItemDtos.size(), orderItems.size());

for (int i = 0; i < orderItemDtos.size(); i++) {
assertEquals(orderItemDtos.get(i).getOrderId(), orderItems.get(i).getOrder().getOrderId());
assertEquals(orderItemDtos.get(i).getProductId(), orderItems.get(i).getProduct().getProductId());
assertEquals(orderItemDtos.get(i).getQuantity(), orderItems.get(i).getQuantity());
assertEquals(orderItemDtos.get(i).getItemPrice(), orderItems.get(i).getItemPrice());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.modelmapper.ModelMapper;

import java.math.BigDecimal;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down

0 comments on commit 0ead1f0

Please sign in to comment.