223 lines
6.7 KiB
Java
223 lines
6.7 KiB
Java
package ru.ulstu.grant.service;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.junit.Before;
|
|
import org.junit.Test;
|
|
import org.junit.runner.RunWith;
|
|
import org.mockito.InjectMocks;
|
|
import org.mockito.Mock;
|
|
import org.mockito.Mockito;
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
import ru.ulstu.deadline.model.Deadline;
|
|
import ru.ulstu.deadline.service.DeadlineService;
|
|
import ru.ulstu.grant.model.Grant;
|
|
import ru.ulstu.grant.model.GrantDto;
|
|
import ru.ulstu.grant.repository.GrantRepository;
|
|
import ru.ulstu.paper.model.Paper;
|
|
import ru.ulstu.paper.model.PaperDto;
|
|
import ru.ulstu.paper.service.PaperService;
|
|
import ru.ulstu.timeline.service.EventService;
|
|
import ru.ulstu.user.model.User;
|
|
import ru.ulstu.user.service.UserService;
|
|
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.Collections;
|
|
import java.util.Date;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
import java.util.stream.Collectors;
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
import static org.junit.Assert.assertTrue;
|
|
import static org.mockito.Mockito.when;
|
|
|
|
@RunWith(SpringRunner.class)
|
|
public class GrantServiceTest {
|
|
|
|
@Mock
|
|
GrantRepository grantRepository;
|
|
|
|
@Mock
|
|
DeadlineService deadlineService;
|
|
|
|
@Mock
|
|
PaperService paperService;
|
|
|
|
@Mock
|
|
UserService userService;
|
|
|
|
@Mock
|
|
EventService eventService;
|
|
|
|
@Mock
|
|
GrantNotificationService grantNotificationService;
|
|
|
|
@InjectMocks
|
|
GrantService grantService;
|
|
|
|
private final static Integer ID = 1;
|
|
private final static Integer INDEX = 0;
|
|
private final static String TITLE = "Title";
|
|
private final static String COMMENT = "Comment";
|
|
private final static boolean TRUE = true;
|
|
private final static Integer YEAR = 2019;
|
|
private final static Integer MAX_DISPLAY_SIZE = 50;
|
|
|
|
private List<Grant> grants;
|
|
private List<Deadline> deadlines;
|
|
private List<PaperDto> paperDtos;
|
|
private GrantDto grantDto;
|
|
private Deadline deadline;
|
|
private User leader;
|
|
|
|
private Grant grantWithId;
|
|
|
|
|
|
@Before
|
|
public void setUp() throws Exception {
|
|
grants = new ArrayList<>();
|
|
paperDtos = new ArrayList<>();
|
|
grantWithId = new Grant();
|
|
|
|
deadlines = new ArrayList<>();
|
|
deadline = new Deadline(new Date(), COMMENT);
|
|
deadline.setId(ID);
|
|
deadlines.add(deadline);
|
|
|
|
leader = Mockito.mock(User.class);
|
|
|
|
List<Paper> papers = new ArrayList<>();
|
|
Paper paperWithId = new Paper();
|
|
paperWithId.setId(ID);
|
|
paperWithId.setTitle(TITLE);
|
|
papers.add(paperWithId);
|
|
PaperDto paperDto = new PaperDto(paperWithId);
|
|
paperDtos.add(paperDto);
|
|
|
|
Set<User> authors = new HashSet<>();
|
|
User author = leader;
|
|
authors.add(author);
|
|
|
|
grantWithId.setId(ID);
|
|
grantWithId.setTitle(TITLE);
|
|
grantWithId.setComment(COMMENT);
|
|
grantWithId.setDeadlines(deadlines);
|
|
grantWithId.setLeader(leader);
|
|
grantWithId.setPapers(papers);
|
|
grantWithId.setAuthors(authors);
|
|
|
|
grants.add(grantWithId);
|
|
grantDto = new GrantDto(grantWithId);
|
|
}
|
|
|
|
@Test
|
|
public void getExistGrantById() {
|
|
when(grantRepository.getOne(ID)).thenReturn(grantWithId);
|
|
|
|
GrantDto newGrantDto = new GrantDto(grantWithId);
|
|
GrantDto result = grantService.getExistGrantById(ID);
|
|
|
|
assertEquals(newGrantDto.getId(), result.getId());
|
|
}
|
|
|
|
@Test
|
|
public void findAll() {
|
|
when(grantRepository.findAll()).thenReturn(grants);
|
|
|
|
assertEquals(Collections.singletonList(grantWithId), grantService.findAll());
|
|
}
|
|
|
|
@Test
|
|
public void create() throws IOException {
|
|
when(deadlineService.saveOrCreate(new ArrayList<>())).thenReturn(deadlines);
|
|
when(userService.getUserByLogin("admin")).thenReturn(leader);
|
|
when(grantRepository.save(new Grant())).thenReturn(grantWithId);
|
|
|
|
Grant newGrant = new Grant();
|
|
newGrant.setId(ID);
|
|
newGrant.setTitle(TITLE);
|
|
newGrant.setComment(COMMENT);
|
|
newGrant.setDeadlines(deadlines);
|
|
newGrant.setLeader(leader);
|
|
|
|
assertEquals(newGrant, grantService.create(grantDto));
|
|
}
|
|
|
|
@Test
|
|
public void getGrantStatuses() {
|
|
assertEquals(Arrays.asList(Grant.GrantStatus.values()), grantService.getGrantStatuses());
|
|
}
|
|
|
|
@Test
|
|
public void getGrantPapers() {
|
|
when(paperService.findAllSelect(Collections.singletonList(ID))).thenReturn(paperDtos);
|
|
|
|
assertEquals(paperDtos, grantService.getGrantPapers(Collections.singletonList(ID)));
|
|
}
|
|
|
|
@Test
|
|
public void getAllUncompletedPapers() {
|
|
when(paperService.findAllNotCompleted()).thenReturn(paperDtos);
|
|
paperDtos.stream()
|
|
.forEach(paperDto -> {
|
|
paperDto.setTitle(StringUtils.abbreviate(paperDto.getTitle(), MAX_DISPLAY_SIZE));
|
|
});
|
|
|
|
assertEquals(paperDtos, grantService.getAllUncompletedPapers());
|
|
}
|
|
|
|
@Test
|
|
public void delete() throws IOException {
|
|
when(grantRepository.getOne(ID)).thenReturn(grantWithId);
|
|
assertTrue(grantService.delete(grantWithId.getId()));
|
|
}
|
|
|
|
@Test
|
|
public void removeDeadline() {
|
|
GrantDto newGrantDto = new GrantDto();
|
|
newGrantDto.getRemovedDeadlineIds().add(ID);
|
|
grantDto.getDeadlines().add(deadline);
|
|
GrantDto result = grantService.removeDeadline(grantDto, INDEX);
|
|
|
|
assertEquals(newGrantDto.getRemovedDeadlineIds(), result.getRemovedDeadlineIds());
|
|
}
|
|
|
|
@Test
|
|
public void findById() {
|
|
when(grantRepository.getOne(ID)).thenReturn(grantWithId);
|
|
Grant findGrant = grantService.findById(ID);
|
|
|
|
assertEquals(grantWithId.getId(), findGrant.getId());
|
|
}
|
|
|
|
@Test
|
|
public void attachPaper() {
|
|
when(grantRepository.getOne(ID)).thenReturn(grantWithId);
|
|
when(paperService.findAllSelect(Collections.singletonList(ID))).thenReturn(paperDtos);
|
|
GrantDto newGrantDto = new GrantDto(grantWithId);
|
|
|
|
if (!newGrantDto.getPaperIds().isEmpty()) {
|
|
newGrantDto.getPapers().clear();
|
|
newGrantDto.setPapers(paperDtos);
|
|
} else {
|
|
newGrantDto.getPapers().clear();
|
|
}
|
|
|
|
assertEquals(newGrantDto.getPapers(), grantService.attachPaper(grantDto));
|
|
}
|
|
|
|
@Test
|
|
public void filterEmptyDeadlines() {
|
|
when(grantRepository.getOne(ID)).thenReturn(grantWithId);
|
|
GrantDto newGrantDto = new GrantDto(grantWithId);
|
|
|
|
newGrantDto.setDeadlines(newGrantDto.getDeadlines().stream()
|
|
.filter(dto -> dto.getDate() != null || !StringUtils.isEmpty(dto.getDescription()))
|
|
.collect(Collectors.toList()));
|
|
|
|
assertEquals(newGrantDto.getDeadlines(), grantService.filterEmptyDeadlines(grantDto));
|
|
}
|
|
} |