#104 add unit tests

This commit is contained in:
T-Midnight 2019-06-07 20:55:59 +04:00
parent 51ed732ae8
commit 888fecf687
3 changed files with 269 additions and 40 deletions

View File

@ -50,7 +50,7 @@ public class GrantController {
@GetMapping("/grant") @GetMapping("/grant")
public void getGrant(ModelMap modelMap, @RequestParam(value = "id") Integer id) { public void getGrant(ModelMap modelMap, @RequestParam(value = "id") Integer id) {
if (id != null && id > 0) { if (id != null && id > 0) {
GrantDto grantDto = grantService.findOneDto(id); GrantDto grantDto = grantService.getExistGrantById(id);
attachPaper(grantDto); attachPaper(grantDto);
modelMap.put("grantDto", grantDto); modelMap.put("grantDto", grantDto);
} else { } else {

View File

@ -1,11 +1,11 @@
package ru.ulstu.grant.service; package ru.ulstu.grant.service;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.Errors; import org.springframework.validation.Errors;
import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.deadline.service.DeadlineService; import ru.ulstu.deadline.service.DeadlineService;
import ru.ulstu.file.model.FileDataDto; import ru.ulstu.file.model.FileDataDto;
import ru.ulstu.file.service.FileService; 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.model.PaperDto;
import ru.ulstu.paper.service.PaperService; import ru.ulstu.paper.service.PaperService;
import ru.ulstu.ping.service.PingService; import ru.ulstu.ping.service.PingService;
import ru.ulstu.project.model.Project;
import ru.ulstu.project.model.ProjectDto; import ru.ulstu.project.model.ProjectDto;
import ru.ulstu.project.service.ProjectService; import ru.ulstu.project.service.ProjectService;
import ru.ulstu.timeline.service.EventService; import ru.ulstu.timeline.service.EventService;
@ -77,6 +76,13 @@ public class GrantService extends BaseService {
this.pingService = pingService; this.pingService = pingService;
} }
//++
public GrantDto getExistGrantById(Integer id) {
GrantDto grantDto = new GrantDto(findById(id));
return grantDto;
}
//++
public List<Grant> findAll() { public List<Grant> findAll() {
return grantRepository.findAll(); return grantRepository.findAll();
} }
@ -85,17 +91,14 @@ public class GrantService extends BaseService {
return convert(findAll(), GrantDto::new); return convert(findAll(), GrantDto::new);
} }
public GrantDto findOneDto(Integer id) { //++
return new GrantDto(grantRepository.findOne(id));
}
@Transactional @Transactional
public Integer create(GrantDto grantDto) throws IOException { public Grant create(GrantDto grantDto) throws IOException {
Grant newGrant = copyFromDto(new Grant(), grantDto); Grant newGrant = copyFromDto(new Grant(), grantDto);
newGrant = grantRepository.save(newGrant); newGrant = grantRepository.save(newGrant);
eventService.createFromObject(newGrant, Collections.emptyList(), false, "гранта"); eventService.createFromObject(newGrant, Collections.emptyList(), false, "гранта");
grantNotificationService.sendCreateNotification(newGrant); grantNotificationService.sendCreateNotification(newGrant);
return newGrant.getId(); return newGrant;
} }
private Grant copyFromDto(Grant grant, GrantDto grantDto) throws IOException { 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.setProject(projectService.findById(grantDto.getProject().getId()));
} }
grant.setDeadlines(deadlineService.saveOrCreate(grantDto.getDeadlines())); grant.setDeadlines(deadlineService.saveOrCreate(grantDto.getDeadlines()));
grant.setFiles(fileService.saveOrCreate(grantDto.getFiles().stream() if (!grant.getFiles().isEmpty()) {
.filter(f -> !f.isDeleted()) grant.setFiles(fileService.saveOrCreate(grantDto.getFiles().stream()
.collect(toList()))); .filter(f -> !f.isDeleted())
.collect(toList())));
}
grant.getAuthors().clear(); grant.getAuthors().clear();
if (grantDto.getAuthorIds() != null && !grantDto.getAuthorIds().isEmpty()) { if (grantDto.getAuthorIds() != null && !grantDto.getAuthorIds().isEmpty()) {
grantDto.getAuthorIds().forEach(authorIds -> grant.getAuthors().add(userService.findById(authorIds))); grantDto.getAuthorIds().forEach(authorIds -> grant.getAuthors().add(userService.findById(authorIds)));
@ -123,6 +128,7 @@ public class GrantService extends BaseService {
return grant; return grant;
} }
public void createProject(GrantDto grantDto) throws IOException { public void createProject(GrantDto grantDto) throws IOException {
grantDto.setProject( grantDto.setProject(
new ProjectDto(projectService.save(new ProjectDto(grantDto.getTitle())))); new ProjectDto(projectService.save(new ProjectDto(grantDto.getTitle()))));
@ -130,7 +136,7 @@ public class GrantService extends BaseService {
@Transactional @Transactional
public Integer update(GrantDto grantDto) throws IOException { 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()); Set<User> oldAuthors = new HashSet<>(grant.getAuthors());
User oldLeader = grant.getLeader(); User oldLeader = grant.getLeader();
for (FileDataDto file : grantDto.getFiles().stream() for (FileDataDto file : grantDto.getFiles().stream()
@ -158,34 +164,40 @@ public class GrantService extends BaseService {
return grant.getId(); return grant.getId();
} }
//++
@Transactional @Transactional
public void delete(Integer grantId) throws IOException { public boolean delete(Integer grantId) throws IOException {
Grant grant = grantRepository.findOne(grantId); Grant grant = findById(grantId);
grantRepository.delete(grant); if (grant != null) {
grantRepository.delete(grant);
return true;
}
return false;
} }
//++
public List<Grant.GrantStatus> getGrantStatuses() { public List<Grant.GrantStatus> getGrantStatuses() {
return Arrays.asList(Grant.GrantStatus.values()); return Arrays.asList(Grant.GrantStatus.values());
} }
@Transactional // @Transactional
public Grant create(String title, Project projectId, Date deadlineDate, User user, Paper paper) { // public Grant create(String title, Project projectId, Date deadlineDate, User user, Paper paper) {
Grant grant = new Grant(); // Grant grant = new Grant();
grant.setTitle(title); // grant.setTitle(title);
grant.setComment("Комментарий к гранту 1"); // grant.setComment("Комментарий к гранту 1");
grant.setProject(projectId); // grant.setProject(projectId);
grant.setStatus(APPLICATION); // grant.setStatus(APPLICATION);
grant.getDeadlines().add(new Deadline(deadlineDate, "первый дедлайн")); // grant.getDeadlines().add(new Deadline(deadlineDate, "первый дедлайн"));
grant.getAuthors().add(user); // grant.getAuthors().add(user);
grant.setLeader(user); // grant.setLeader(user);
grant.getPapers().add(paper); // grant.getPapers().add(paper);
grant = grantRepository.save(grant); // grant = grantRepository.save(grant);
//
eventService.createFromObject(grant, Collections.emptyList(), false, "гранта"); // eventService.createFromGrant(grant);
grantNotificationService.sendCreateNotification(grant); // grantNotificationService.sendCreateNotification(grant);
//
return grant; // return grant;
} // }
public boolean save(GrantDto grantDto, Errors errors) throws IOException { public boolean save(GrantDto grantDto, Errors errors) throws IOException {
grantDto.setName(grantDto.getTitle()); grantDto.setName(grantDto.getTitle());
@ -266,10 +278,12 @@ public class GrantService extends BaseService {
.collect(toList()); .collect(toList());
} }
//++
public List<PaperDto> getGrantPapers(List<Integer> paperIds) { public List<PaperDto> getGrantPapers(List<Integer> paperIds) {
return paperService.findAllSelect(paperIds); return paperService.findAllSelect(paperIds);
} }
//++
public List<PaperDto> getAllUncompletedPapers() { public List<PaperDto> getAllUncompletedPapers() {
return paperService.findAllNotCompleted(); 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) { if (grantDto.getDeadlines().get(deadlineId).getId() != null) {
grantDto.getRemovedDeadlineIds().add(grantDto.getDeadlines().get(deadlineId).getId()); grantDto.getRemovedDeadlineIds().add(grantDto.getDeadlines().get(deadlineId).getId());
} }
grantDto.getDeadlines().remove((int) deadlineId); grantDto.getDeadlines().remove((int) deadlineId);
return grantDto;
} }
private List<User> getCompletedPapersAuthors(Paper.PaperType type) { private List<User> getCompletedPapersAuthors(Paper.PaperType type) {
@ -315,7 +331,7 @@ public class GrantService extends BaseService {
public void filterEmptyDeadlines(GrantDto grantDto) { public void filterEmptyDeadlines(GrantDto grantDto) {
grantDto.setDeadlines(grantDto.getDeadlines().stream() 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())); .collect(Collectors.toList()));
} }
@ -338,16 +354,14 @@ public class GrantService extends BaseService {
return grantRepository.findAllActive(); return grantRepository.findAllActive();
} }
//+
public Grant findById(Integer id) { public Grant findById(Integer id) {
return grantRepository.findOne(id); return grantRepository.findOne(id);
} }
//+
@Transactional @Transactional
public void ping(int grantId) throws IOException { public void ping(int grantId) throws IOException {
pingService.addPing(findById(grantId)); pingService.addPing(findById(grantId));
} }
public Grant findGrantById(Integer grantId) {
return grantRepository.findOne(grantId);
}
} }

View 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());
// }
}