From e4f5bede2aa1cab56c82c4ac24081250b70f9902 Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Fri, 7 Jun 2019 22:39:20 +0400 Subject: [PATCH] #104 add 3 more tests --- .../ru/ulstu/grant/service/GrantService.java | 36 ++----------- .../ulstu/grant/service/GrantServiceTest.java | 52 ++++++++++++------- 2 files changed, 39 insertions(+), 49 deletions(-) diff --git a/src/main/java/ru/ulstu/grant/service/GrantService.java b/src/main/java/ru/ulstu/grant/service/GrantService.java index ea5491c..99387c7 100644 --- a/src/main/java/ru/ulstu/grant/service/GrantService.java +++ b/src/main/java/ru/ulstu/grant/service/GrantService.java @@ -6,6 +6,7 @@ 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; @@ -76,13 +77,11 @@ public class GrantService extends BaseService { this.pingService = pingService; } - //++ public GrantDto getExistGrantById(Integer id) { GrantDto grantDto = new GrantDto(findById(id)); return grantDto; } - //++ public List findAll() { return grantRepository.findAll(); } @@ -91,7 +90,6 @@ public class GrantService extends BaseService { return convert(findAll(), GrantDto::new); } - //++ @Transactional public Grant create(GrantDto grantDto) throws IOException { Grant newGrant = copyFromDto(new Grant(), grantDto); @@ -164,7 +162,6 @@ public class GrantService extends BaseService { return grant.getId(); } - //++ @Transactional public boolean delete(Integer grantId) throws IOException { Grant grant = findById(grantId); @@ -175,30 +172,10 @@ public class GrantService extends BaseService { return false; } - //++ public List 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.createFromGrant(grant); -// grantNotificationService.sendCreateNotification(grant); -// -// return grant; -// } - public boolean save(GrantDto grantDto, Errors errors) throws IOException { grantDto.setName(grantDto.getTitle()); filterEmptyDeadlines(grantDto); @@ -278,26 +255,24 @@ public class GrantService extends BaseService { .collect(toList()); } - //++ public List getGrantPapers(List paperIds) { return paperService.findAllSelect(paperIds); } - //++ public List getAllUncompletedPapers() { return paperService.findAllNotCompleted(); } - public void attachPaper(GrantDto grantDto) { + public List attachPaper(GrantDto grantDto) { if (!grantDto.getPaperIds().isEmpty()) { grantDto.getPapers().clear(); grantDto.setPapers(getGrantPapers(grantDto.getPaperIds())); } else { grantDto.getPapers().clear(); } + return grantDto.getPapers(); } - //++ public GrantDto removeDeadline(GrantDto grantDto, Integer deadlineId) { if (grantDto.getDeadlines().get(deadlineId).getId() != null) { grantDto.getRemovedDeadlineIds().add(grantDto.getDeadlines().get(deadlineId).getId()); @@ -329,10 +304,11 @@ public class GrantService extends BaseService { .collect(toList()); } - public void filterEmptyDeadlines(GrantDto grantDto) { + public List filterEmptyDeadlines(GrantDto grantDto) { grantDto.setDeadlines(grantDto.getDeadlines().stream() .filter(dto -> dto.getDate() != null || !StringUtils.isEmpty(dto.getDescription())) .collect(Collectors.toList())); + return grantDto.getDeadlines(); } @Transactional @@ -354,12 +330,10 @@ 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)); diff --git a/src/test/java/ru/ulstu/grant/service/GrantServiceTest.java b/src/test/java/ru/ulstu/grant/service/GrantServiceTest.java index 6e0ea10..99d7ef6 100644 --- a/src/test/java/ru/ulstu/grant/service/GrantServiceTest.java +++ b/src/test/java/ru/ulstu/grant/service/GrantServiceTest.java @@ -28,6 +28,7 @@ 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; @@ -103,7 +104,6 @@ public class GrantServiceTest { paperDto = new PaperDto(paperWithId); paperDtos.add(paperDto); - authors = new HashSet<>(); author = leader; authors.add(author); @@ -184,7 +184,7 @@ public class GrantServiceTest { } @Test - public void removeDeadline() throws IOException { + public void removeDeadline() { GrantDto newGrantDto = new GrantDto(); newGrantDto.getRemovedDeadlineIds().add(ID); grantDto.getDeadlines().add(deadline); @@ -193,23 +193,39 @@ public class GrantServiceTest { assertEquals(newGrantDto.getRemovedDeadlineIds(), result.getRemovedDeadlineIds()); } + @Test + public void findById() { + when(grantRepository.findOne(ID)).thenReturn(grantWithId); + Grant findGrant = grantService.findById(ID); -// @Test -// public void findAllDto() { -// when(grantRepository.findAll()).thenReturn(grants); -// List list = convert(grants, GrantDto::new); -// grantDtos.clear(); -// //List 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 result = ; -// assertEquals(grantDtos, grantService.findAllDto()); -// } + assertEquals(grantWithId.getId(), findGrant.getId()); + } + @Test + public void attachPaper() { + when(grantRepository.findOne(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.findOne(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)); + } } \ No newline at end of file