#104 add unit tests
This commit is contained in:
parent
51ed732ae8
commit
888fecf687
@ -50,7 +50,7 @@ public class GrantController {
|
||||
@GetMapping("/grant")
|
||||
public void getGrant(ModelMap modelMap, @RequestParam(value = "id") Integer id) {
|
||||
if (id != null && id > 0) {
|
||||
GrantDto grantDto = grantService.findOneDto(id);
|
||||
GrantDto grantDto = grantService.getExistGrantById(id);
|
||||
attachPaper(grantDto);
|
||||
modelMap.put("grantDto", grantDto);
|
||||
} else {
|
||||
|
@ -1,11 +1,11 @@
|
||||
package ru.ulstu.grant.service;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.validation.Errors;
|
||||
import ru.ulstu.deadline.model.Deadline;
|
||||
import ru.ulstu.deadline.service.DeadlineService;
|
||||
import ru.ulstu.file.model.FileDataDto;
|
||||
import ru.ulstu.file.service.FileService;
|
||||
@ -17,7 +17,6 @@ import ru.ulstu.paper.model.Paper;
|
||||
import ru.ulstu.paper.model.PaperDto;
|
||||
import ru.ulstu.paper.service.PaperService;
|
||||
import ru.ulstu.ping.service.PingService;
|
||||
import ru.ulstu.project.model.Project;
|
||||
import ru.ulstu.project.model.ProjectDto;
|
||||
import ru.ulstu.project.service.ProjectService;
|
||||
import ru.ulstu.timeline.service.EventService;
|
||||
@ -77,6 +76,13 @@ public class GrantService extends BaseService {
|
||||
this.pingService = pingService;
|
||||
}
|
||||
|
||||
//++
|
||||
public GrantDto getExistGrantById(Integer id) {
|
||||
GrantDto grantDto = new GrantDto(findById(id));
|
||||
return grantDto;
|
||||
}
|
||||
|
||||
//++
|
||||
public List<Grant> findAll() {
|
||||
return grantRepository.findAll();
|
||||
}
|
||||
@ -85,17 +91,14 @@ public class GrantService extends BaseService {
|
||||
return convert(findAll(), GrantDto::new);
|
||||
}
|
||||
|
||||
public GrantDto findOneDto(Integer id) {
|
||||
return new GrantDto(grantRepository.findOne(id));
|
||||
}
|
||||
|
||||
//++
|
||||
@Transactional
|
||||
public Integer create(GrantDto grantDto) throws IOException {
|
||||
public Grant create(GrantDto grantDto) throws IOException {
|
||||
Grant newGrant = copyFromDto(new Grant(), grantDto);
|
||||
newGrant = grantRepository.save(newGrant);
|
||||
eventService.createFromObject(newGrant, Collections.emptyList(), false, "гранта");
|
||||
grantNotificationService.sendCreateNotification(newGrant);
|
||||
return newGrant.getId();
|
||||
return newGrant;
|
||||
}
|
||||
|
||||
private Grant copyFromDto(Grant grant, GrantDto grantDto) throws IOException {
|
||||
@ -106,9 +109,11 @@ public class GrantService extends BaseService {
|
||||
grant.setProject(projectService.findById(grantDto.getProject().getId()));
|
||||
}
|
||||
grant.setDeadlines(deadlineService.saveOrCreate(grantDto.getDeadlines()));
|
||||
grant.setFiles(fileService.saveOrCreate(grantDto.getFiles().stream()
|
||||
.filter(f -> !f.isDeleted())
|
||||
.collect(toList())));
|
||||
if (!grant.getFiles().isEmpty()) {
|
||||
grant.setFiles(fileService.saveOrCreate(grantDto.getFiles().stream()
|
||||
.filter(f -> !f.isDeleted())
|
||||
.collect(toList())));
|
||||
}
|
||||
grant.getAuthors().clear();
|
||||
if (grantDto.getAuthorIds() != null && !grantDto.getAuthorIds().isEmpty()) {
|
||||
grantDto.getAuthorIds().forEach(authorIds -> grant.getAuthors().add(userService.findById(authorIds)));
|
||||
@ -123,6 +128,7 @@ public class GrantService extends BaseService {
|
||||
return grant;
|
||||
}
|
||||
|
||||
|
||||
public void createProject(GrantDto grantDto) throws IOException {
|
||||
grantDto.setProject(
|
||||
new ProjectDto(projectService.save(new ProjectDto(grantDto.getTitle()))));
|
||||
@ -130,7 +136,7 @@ public class GrantService extends BaseService {
|
||||
|
||||
@Transactional
|
||||
public Integer update(GrantDto grantDto) throws IOException {
|
||||
Grant grant = grantRepository.findOne(grantDto.getId());
|
||||
Grant grant = findById(grantDto.getId());
|
||||
Set<User> oldAuthors = new HashSet<>(grant.getAuthors());
|
||||
User oldLeader = grant.getLeader();
|
||||
for (FileDataDto file : grantDto.getFiles().stream()
|
||||
@ -158,34 +164,40 @@ public class GrantService extends BaseService {
|
||||
return grant.getId();
|
||||
}
|
||||
|
||||
//++
|
||||
@Transactional
|
||||
public void delete(Integer grantId) throws IOException {
|
||||
Grant grant = grantRepository.findOne(grantId);
|
||||
grantRepository.delete(grant);
|
||||
public boolean delete(Integer grantId) throws IOException {
|
||||
Grant grant = findById(grantId);
|
||||
if (grant != null) {
|
||||
grantRepository.delete(grant);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//++
|
||||
public List<Grant.GrantStatus> getGrantStatuses() {
|
||||
return Arrays.asList(Grant.GrantStatus.values());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Grant create(String title, Project projectId, Date deadlineDate, User user, Paper paper) {
|
||||
Grant grant = new Grant();
|
||||
grant.setTitle(title);
|
||||
grant.setComment("Комментарий к гранту 1");
|
||||
grant.setProject(projectId);
|
||||
grant.setStatus(APPLICATION);
|
||||
grant.getDeadlines().add(new Deadline(deadlineDate, "первый дедлайн"));
|
||||
grant.getAuthors().add(user);
|
||||
grant.setLeader(user);
|
||||
grant.getPapers().add(paper);
|
||||
grant = grantRepository.save(grant);
|
||||
|
||||
eventService.createFromObject(grant, Collections.emptyList(), false, "гранта");
|
||||
grantNotificationService.sendCreateNotification(grant);
|
||||
|
||||
return grant;
|
||||
}
|
||||
// @Transactional
|
||||
// public Grant create(String title, Project projectId, Date deadlineDate, User user, Paper paper) {
|
||||
// Grant grant = new Grant();
|
||||
// grant.setTitle(title);
|
||||
// grant.setComment("Комментарий к гранту 1");
|
||||
// grant.setProject(projectId);
|
||||
// grant.setStatus(APPLICATION);
|
||||
// grant.getDeadlines().add(new Deadline(deadlineDate, "первый дедлайн"));
|
||||
// grant.getAuthors().add(user);
|
||||
// grant.setLeader(user);
|
||||
// grant.getPapers().add(paper);
|
||||
// grant = grantRepository.save(grant);
|
||||
//
|
||||
// eventService.createFromGrant(grant);
|
||||
// grantNotificationService.sendCreateNotification(grant);
|
||||
//
|
||||
// return grant;
|
||||
// }
|
||||
|
||||
public boolean save(GrantDto grantDto, Errors errors) throws IOException {
|
||||
grantDto.setName(grantDto.getTitle());
|
||||
@ -266,10 +278,12 @@ public class GrantService extends BaseService {
|
||||
.collect(toList());
|
||||
}
|
||||
|
||||
//++
|
||||
public List<PaperDto> getGrantPapers(List<Integer> paperIds) {
|
||||
return paperService.findAllSelect(paperIds);
|
||||
}
|
||||
|
||||
//++
|
||||
public List<PaperDto> getAllUncompletedPapers() {
|
||||
return paperService.findAllNotCompleted();
|
||||
}
|
||||
@ -283,11 +297,13 @@ public class GrantService extends BaseService {
|
||||
}
|
||||
}
|
||||
|
||||
public void removeDeadline(GrantDto grantDto, Integer deadlineId) {
|
||||
//++
|
||||
public GrantDto removeDeadline(GrantDto grantDto, Integer deadlineId) {
|
||||
if (grantDto.getDeadlines().get(deadlineId).getId() != null) {
|
||||
grantDto.getRemovedDeadlineIds().add(grantDto.getDeadlines().get(deadlineId).getId());
|
||||
}
|
||||
grantDto.getDeadlines().remove((int) deadlineId);
|
||||
return grantDto;
|
||||
}
|
||||
|
||||
private List<User> getCompletedPapersAuthors(Paper.PaperType type) {
|
||||
@ -315,7 +331,7 @@ public class GrantService extends BaseService {
|
||||
|
||||
public void filterEmptyDeadlines(GrantDto grantDto) {
|
||||
grantDto.setDeadlines(grantDto.getDeadlines().stream()
|
||||
.filter(dto -> dto.getDate() != null || !org.springframework.util.StringUtils.isEmpty(dto.getDescription()))
|
||||
.filter(dto -> dto.getDate() != null || !StringUtils.isEmpty(dto.getDescription()))
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
@ -338,16 +354,14 @@ public class GrantService extends BaseService {
|
||||
return grantRepository.findAllActive();
|
||||
}
|
||||
|
||||
//+
|
||||
public Grant findById(Integer id) {
|
||||
return grantRepository.findOne(id);
|
||||
}
|
||||
|
||||
//+
|
||||
@Transactional
|
||||
public void ping(int grantId) throws IOException {
|
||||
pingService.addPing(findById(grantId));
|
||||
}
|
||||
|
||||
public Grant findGrantById(Integer grantId) {
|
||||
return grantRepository.findOne(grantId);
|
||||
}
|
||||
}
|
||||
|
215
src/test/java/ru/ulstu/grant/service/GrantServiceTest.java
Normal file
215
src/test/java/ru/ulstu/grant/service/GrantServiceTest.java
Normal file
@ -0,0 +1,215 @@
|
||||
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.mockito.runners.MockitoJUnitRunner;
|
||||
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 static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.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<GrantDto> grantDtos;
|
||||
private List<Deadline> deadlines;
|
||||
private List<Paper> papers;
|
||||
private PaperDto paperDto;
|
||||
private List<PaperDto> paperDtos;
|
||||
private Set<User> authors;
|
||||
private GrantDto grantDto;
|
||||
private Deadline deadline;
|
||||
private User leader;
|
||||
private User author;
|
||||
|
||||
private Grant grantWithId;
|
||||
private Paper paperWithId;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
grants = new ArrayList<>();
|
||||
grantDtos = 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);
|
||||
|
||||
papers = new ArrayList<>();
|
||||
paperWithId = new Paper();
|
||||
paperWithId.setId(ID);
|
||||
paperWithId.setTitle(TITLE);
|
||||
papers.add(paperWithId);
|
||||
paperDto = new PaperDto(paperWithId);
|
||||
paperDtos.add(paperDto);
|
||||
|
||||
|
||||
authors = new HashSet<>();
|
||||
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);
|
||||
grantDtos.add(grantDto);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getExistGrantById() {
|
||||
when(grantRepository.findOne(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.findOne(ID)).thenReturn(grantWithId);
|
||||
assertTrue(grantService.delete(grantWithId.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeDeadline() throws IOException {
|
||||
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 findAllDto() {
|
||||
// when(grantRepository.findAll()).thenReturn(grants);
|
||||
// List<Grant> list = convert(grants, GrantDto::new);
|
||||
// grantDtos.clear();
|
||||
// //List<GrantDto> listDto = new ArrayList<>();
|
||||
// list.forEach(grant -> {
|
||||
// GrantDto grantDto = new GrantDto(grant);
|
||||
// grantDtos.add(grantDto);
|
||||
// });
|
||||
//
|
||||
//
|
||||
// //grantDtos.forEach(grantDto -> grantDto.setTitle(StringUtils.abbreviate(grantDto.getTitle(), MAX_DISPLAY_SIZE)));
|
||||
// //List<GrantDto> result = ;
|
||||
// assertEquals(grantDtos, grantService.findAllDto());
|
||||
// }
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user