Commit 4480c36e authored by Jakub Balga's avatar Jakub Balga
Browse files

Tests added - not working

parent 680cc5a8
Pipeline #130212 passed with stages
in 3 minutes and 8 seconds
......@@ -4,9 +4,6 @@ import cz.muni.fi.pa165.winery.dto.PersistentDtoBase;
import cz.muni.fi.pa165.winery.persistence.dao.DaoBase;
import cz.muni.fi.pa165.winery.persistence.entities.EntityBase;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import java.util.List;
import java.util.stream.Collectors;
......@@ -78,7 +75,7 @@ public class PersistenceServiceImpl<DTO extends PersistentDtoBase, ENTITY extend
}
protected ENTITY mapToEntity(DTO record) {
var mapper= getModelMapper();
var mapper = getModelMapper();
return mapper.map(record, entityClass);
}
......
package cz.muni.fi.pa165.winery.services.order;
import cz.muni.fi.pa165.winery.dto.order.OrderItemDto;
import cz.muni.fi.pa165.winery.persistence.dao.order.OrderDao;
import cz.muni.fi.pa165.winery.persistence.entities.Order;
import cz.muni.fi.pa165.winery.persistence.dao.order.OrderItemDao;
import cz.muni.fi.pa165.winery.persistence.entities.OrderItem;
import cz.muni.fi.pa165.winery.services.PersistenceServiceImpl;
import cz.muni.fi.pa165.winery.services.wine.WineBottleService;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class OrderItemServiceImpl extends PersistenceServiceImpl<OrderItemDto, Order> implements OrderItemService {
public class OrderItemServiceImpl extends PersistenceServiceImpl<OrderItemDto, OrderItem> implements OrderItemService {
private final WineBottleService wineBottleService;
@Autowired
public OrderItemServiceImpl(OrderDao orderDao, WineBottleService wineBottleService) {
super(OrderItemDto.class, Order.class, orderDao);
public OrderItemServiceImpl(OrderItemDao orderItemDao, WineBottleService wineBottleService) {
super(OrderItemDto.class, OrderItem.class, orderItemDao);
this.wineBottleService = wineBottleService;
}
......@@ -28,4 +30,10 @@ public class OrderItemServiceImpl extends PersistenceServiceImpl<OrderItemDto, O
record.setBottle(wineBottleService.get(record.getBottleId()));
return record;
}
@Override
protected void configureModelMapper(ModelMapper mapper) {
TypeMap<OrderItem, OrderItemDto> typeMapper = mapper.createTypeMap(OrderItem.class, OrderItemDto.class);
typeMapper.addMappings(m -> m.skip(OrderItemDto::setBottle));
}
}
package cz.muni.fi.pa165.winery.services.order;
import cz.muni.fi.pa165.winery.persistence.dao.order.OrderItemDao;
import cz.muni.fi.pa165.winery.persistence.dao.wine.WineBottleDao;
import cz.muni.fi.pa165.winery.persistence.entities.Grape;
import cz.muni.fi.pa165.winery.persistence.entities.OrderItem;
import cz.muni.fi.pa165.winery.persistence.entities.WineBottle;
import cz.muni.fi.pa165.winery.persistence.entities.WineType;
import cz.muni.fi.pa165.winery.services.wine.WineBottleService;
import cz.muni.fi.pa165.winery.services.wine.WineBottleServiceImpl;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import java.math.BigDecimal;
@SpringBootTest
class OrderItemServiceTest {
@Mock
private OrderItemDao repository;
@Mock
private WineBottleDao wineRepository;
private OrderItemService orderItemService;
private WineBottleService wineService;
Grape grape = new Grape();
WineType type = new WineType();
WineBottle bottle = new WineBottle();
OrderItem item1 = new OrderItem();
OrderItem item12 = new OrderItem();
@BeforeEach
void prepareEntities() {
wineService = new WineBottleServiceImpl(wineRepository);
orderItemService = new OrderItemServiceImpl(repository, wineService);
grape.setName("Pálava");
type.setName("Pálava pozdní sběr");
type.setGrape(grape);
bottle.setId(1);
bottle.setName("Pálava pozdní sběr 0.7");
bottle.setPrice(new BigDecimal(200));
bottle.setVolume(new BigDecimal(0.70));
bottle.setStock(10);
bottle.setGrape(grape);
bottle.setType(type);
item1.setId(1);
item1.setQuantity(new BigDecimal(8));
item1.setBottle(bottle);
item12.setId(12);
item12.setQuantity(new BigDecimal(10));
item12.setBottle(bottle);
}
@BeforeEach
void mockRepositoryMethods() {
Mockito.when(repository.get(1)).thenReturn(item1);
Mockito.when(repository.get(12)).thenReturn(item12);
Mockito.when(wineRepository.get(1)).thenReturn(bottle);
}
@Test
void testGet() {
Assertions.assertEquals(new BigDecimal(8), orderItemService.get(1, false).getQuantity());
//Assertions.assertNull(orderItemService.get(1, false).getBottle());
Assertions.assertEquals(new BigDecimal(10), orderItemService.get(12, true).getQuantity());
Assertions.assertEquals(bottle.getId(), orderItemService.get(12, true).getBottle().getId());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment