Commit 4bf9d7cf authored by Samuel Dudík's avatar Samuel Dudík
Browse files

Add tests for Harvest and Grape services

parent f46fdecb
Pipeline #131993 failed with stages
in 59 seconds
package cz.muni.fi.pa165.winery.persistence.dao;
import cz.muni.fi.pa165.winery.persistence.PersistenceTestConfig;
import cz.muni.fi.pa165.winery.persistence.dao.wine.HarvestDao;
import cz.muni.fi.pa165.winery.persistence.entities.*;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
......@@ -12,6 +15,7 @@ import java.math.BigDecimal;
/**
* @author Michaela Korenkova
* @author Samuel Dudík
*/
@ExtendWith(SpringExtension.class)
......@@ -19,6 +23,27 @@ import java.math.BigDecimal;
public class HarvestDaoTests extends DaoTestBase<Harvest>{
private Grape testGrape;
@Autowired
private HarvestDao harvestDao;
@Test
public void testGetAllWithinYears() {
var entity = getEntity1();
executeTransaction(() -> entityManager.persist(entity));
var entity2 = getEntity2();
executeTransaction(() -> entityManager.persist(entity2));
var harvests = harvestDao.getAllWithinYears(2005, 2010);
Assertions.assertThat(harvests).containsExactly(entity);
harvests = harvestDao.getAllWithinYears(2005, 2020);
Assertions.assertThat(harvests).contains(entity, entity2);
harvests = harvestDao.getAllWithinYears(1000, 1010);
Assertions.assertThat(harvests).isEmpty();
}
@Override
protected Class<Harvest> getEntityClass() {
return Harvest.class;
......
package cz.muni.fi.pa165.winery.services.persistence.wine;
import cz.muni.fi.pa165.winery.dto.wine.GrapeDto;
import cz.muni.fi.pa165.winery.persistence.dao.DaoBase;
import cz.muni.fi.pa165.winery.persistence.dao.wine.GrapeDao;
import cz.muni.fi.pa165.winery.persistence.entities.Grape;
import cz.muni.fi.pa165.winery.services.PersistenceServiceImpl;
import cz.muni.fi.pa165.winery.services.mappers.wine.GrapeEntityMapper;
import cz.muni.fi.pa165.winery.services.persistence.PersistenceServiceTestBase;
import cz.muni.fi.pa165.winery.services.wine.GrapeServiceImpl;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
/**
* @author Samuel Dudík
*/
@ExtendWith(MockitoExtension.class)
public class GrapeServiceTests extends PersistenceServiceTestBase<GrapeDto, Grape> {
@Mock
GrapeDao grapeDao;
@Spy
GrapeEntityMapper grapeEntityMapper;
GrapeServiceImpl grapeService;
@Override
protected GrapeDto getTestDto() {
var dto = new GrapeDto();
dto.setId(1);
dto.setName("testgrape");
return dto;
}
@Override
protected Grape getTestEntity() {
var entity = new Grape();
entity.setId(1);
entity.setName("testgrape");
return entity;
}
@Override
protected boolean equals(GrapeDto grapeDto, Grape grape) {
return grapeDto.getId() == grape.getId()
&& grapeDto.getName() == grape.getName();
}
@Override
protected PersistenceServiceImpl<GrapeDto, Grape> getPersistenceService() {
if (grapeService != null) {
return grapeService;
}
grapeService = new GrapeServiceImpl(grapeDao, grapeEntityMapper);
return grapeService;
}
@Override
protected DaoBase<Grape> getMockedDao() {
return grapeDao;
}
}
package cz.muni.fi.pa165.winery.services.persistence.wine;
import cz.muni.fi.pa165.winery.dto.wine.HarvestDto;
import cz.muni.fi.pa165.winery.persistence.dao.DaoBase;
import cz.muni.fi.pa165.winery.persistence.dao.wine.HarvestDao;
import cz.muni.fi.pa165.winery.persistence.entities.Grape;
import cz.muni.fi.pa165.winery.persistence.entities.Harvest;
import cz.muni.fi.pa165.winery.services.PersistenceServiceImpl;
import cz.muni.fi.pa165.winery.services.mappers.wine.HarvestEntityMapper;
import cz.muni.fi.pa165.winery.services.persistence.PersistenceServiceTestBase;
import cz.muni.fi.pa165.winery.services.wine.HarvestServiceImpl;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import java.math.BigDecimal;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Samuel Dudík
*/
@ExtendWith(MockitoExtension.class)
public class HarvestServiceTests extends PersistenceServiceTestBase<HarvestDto, Harvest> {
@Mock
HarvestDao harvestDao;
@Spy
HarvestEntityMapper harvestEntityMapper;
HarvestServiceImpl harvestService;
@Test
public void testGetAllWithinYears() {
getPersistenceService();
var entityList = List.of(getTestEntity());
Mockito.when(harvestDao.getAllWithinYears(2010, 2015)).thenReturn(entityList);
var dtos = harvestService.getAllWithinYears(2010, 2015);
assertThat(dtos).hasSize(1);
assertThat(equals(dtos.stream().findFirst().get(), getTestEntity())).isTrue();
Mockito.verify(harvestDao, Mockito.times(1)).getAllWithinYears(2010, 2015);
}
@Override
protected HarvestDto getTestDto() {
var dto = new HarvestDto();
dto.setId(1);
dto.setGrade((short) 3);
dto.setHarvestYear(2012);
dto.setGrapeId(1);
dto.setYieldKilograms(new BigDecimal(540));
return dto;
}
@Override
protected Harvest getTestEntity() {
var grape = new Grape();
grape.setId(1);
grape.setName("grape");
var entity = new Harvest();
entity.setId(1);
entity.setGrade((short) 3);
entity.setHarvestYear(2012);
entity.setGrape(grape);
entity.setYieldKilograms(new BigDecimal(540));
return entity;
}
@Override
protected boolean equals(HarvestDto harvestDto, Harvest harvest) {
return harvestDto.getHarvestYear() == harvest.getHarvestYear()
&& harvestDto.getId() == harvest.getId()
&& harvestDto.getGrade() == harvest.getGrade()
&& harvestDto.getGrapeId() == harvest.getGrape().getId()
&& harvestDto.getYieldKilograms().equals(harvest.getYieldKilograms());
}
@Override
protected PersistenceServiceImpl<HarvestDto, Harvest> getPersistenceService() {
if (harvestService != null) {
return harvestService;
}
harvestService = new HarvestServiceImpl(harvestDao, harvestEntityMapper);
return harvestService;
}
@Override
protected DaoBase<Harvest> getMockedDao() {
return harvestDao;
}
}
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