From 51ed732ae85aba74d0e8dd2be1198eb4ceae365c Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Fri, 7 Jun 2019 20:54:38 +0400 Subject: [PATCH 1/3] #104 fix web element in integration test --- src/test/java/grant/GrantPage.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/grant/GrantPage.java b/src/test/java/grant/GrantPage.java index fe13956..75fbf8a 100644 --- a/src/test/java/grant/GrantPage.java +++ b/src/test/java/grant/GrantPage.java @@ -14,7 +14,7 @@ public class GrantPage extends PageObject { } public String getId() { - return driver.findElement(By.id("id")).getAttribute("value"); + return driver.findElement(By.id("grantId")).getAttribute("value"); } public void setTitle(String name) { From 888fecf687ef69f8c81657abe1deb1108f457ed5 Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Fri, 7 Jun 2019 20:55:59 +0400 Subject: [PATCH 2/3] #104 add unit tests --- .../grant/controller/GrantController.java | 2 +- .../ru/ulstu/grant/service/GrantService.java | 92 ++++---- .../ulstu/grant/service/GrantServiceTest.java | 215 ++++++++++++++++++ 3 files changed, 269 insertions(+), 40 deletions(-) create mode 100644 src/test/java/ru/ulstu/grant/service/GrantServiceTest.java diff --git a/src/main/java/ru/ulstu/grant/controller/GrantController.java b/src/main/java/ru/ulstu/grant/controller/GrantController.java index 1e28a33..48dee8e 100644 --- a/src/main/java/ru/ulstu/grant/controller/GrantController.java +++ b/src/main/java/ru/ulstu/grant/controller/GrantController.java @@ -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 { diff --git a/src/main/java/ru/ulstu/grant/service/GrantService.java b/src/main/java/ru/ulstu/grant/service/GrantService.java index 03ae07f..ea5491c 100644 --- a/src/main/java/ru/ulstu/grant/service/GrantService.java +++ b/src/main/java/ru/ulstu/grant/service/GrantService.java @@ -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 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 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 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 getGrantPapers(List paperIds) { return paperService.findAllSelect(paperIds); } + //++ public List 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 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); - } } diff --git a/src/test/java/ru/ulstu/grant/service/GrantServiceTest.java b/src/test/java/ru/ulstu/grant/service/GrantServiceTest.java new file mode 100644 index 0000000..6e0ea10 --- /dev/null +++ b/src/test/java/ru/ulstu/grant/service/GrantServiceTest.java @@ -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 grants; + private List grantDtos; + private List deadlines; + private List papers; + private PaperDto paperDto; + private List paperDtos; + private Set 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 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()); +// } + + +} \ No newline at end of file From e4f5bede2aa1cab56c82c4ac24081250b70f9902 Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Fri, 7 Jun 2019 22:39:20 +0400 Subject: [PATCH 3/3] #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