From a7b48e1534eb191a26f6543436209a0fd8bd8835 Mon Sep 17 00:00:00 2001 From: "a.vasin" Date: Fri, 31 May 2019 02:23:15 +0400 Subject: [PATCH] #101 project tests --- .../ulstu/project/service/ProjectService.java | 13 +- .../project/service/ProjectServiceTest.java | 149 +++++++++++++++++- 2 files changed, 157 insertions(+), 5 deletions(-) diff --git a/src/main/java/ru/ulstu/project/service/ProjectService.java b/src/main/java/ru/ulstu/project/service/ProjectService.java index e77cc88..80bd52f 100644 --- a/src/main/java/ru/ulstu/project/service/ProjectService.java +++ b/src/main/java/ru/ulstu/project/service/ProjectService.java @@ -90,9 +90,13 @@ public class ProjectService { } @Transactional - public void delete(Integer projectId) throws IOException { - Project project = projectRepository.findOne(projectId); - projectRepository.delete(project); + public boolean delete(Integer projectId) throws IOException { + if (projectRepository.exists(projectId)) { + Project project = projectRepository.findOne(projectId); + projectRepository.delete(project); + return true; + } + return false; } private Project copyFromDto(Project project, ProjectDto projectDto) throws IOException { @@ -122,11 +126,12 @@ public class ProjectService { } } - public void removeDeadline(ProjectDto projectDto, Integer deadlineId) { + public ProjectDto removeDeadline(ProjectDto projectDto, Integer deadlineId) { if (deadlineId != null) { projectDto.getRemovedDeadlineIds().add(deadlineId); } projectDto.getDeadlines().remove((int) deadlineId); + return projectDto; } public Project findById(Integer id) { diff --git a/src/test/java/ru/ulstu/project/service/ProjectServiceTest.java b/src/test/java/ru/ulstu/project/service/ProjectServiceTest.java index 1f2debe..7c4f5fa 100644 --- a/src/test/java/ru/ulstu/project/service/ProjectServiceTest.java +++ b/src/test/java/ru/ulstu/project/service/ProjectServiceTest.java @@ -1 +1,148 @@ -package ru.ulstu.project.service; \ No newline at end of file +package ru.ulstu.project.service; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import ru.ulstu.deadline.model.Deadline; +import ru.ulstu.deadline.service.DeadlineService; +import ru.ulstu.file.model.FileData; +import ru.ulstu.file.service.FileService; +import ru.ulstu.grant.model.GrantDto; +import ru.ulstu.grant.service.GrantService; +import ru.ulstu.project.model.Project; +import ru.ulstu.project.model.ProjectDto; +import ru.ulstu.project.repository.ProjectRepository; +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.*; + +import static junit.framework.TestCase.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class ProjectServiceTest { + + @Mock + ProjectRepository projectRepository; + + @Mock + DeadlineService deadlineService; + + @Mock + EventService eventService; + + @Mock + FileService fileService; + + @Mock + UserService userService; + + @Mock + GrantService grantService; + + @InjectMocks + ProjectService projectService; + + private final static String TITLE = "title"; + private final static String DESCR = "descr"; + private final static Integer ID = 1; + private final static Integer INDEX = 0; + private final static String NAME = "name"; + + private List projects; + private Project project; + private ProjectDto projectDto; + private Deadline deadline; + private List deadlines; + private FileData file; + private List files; + private User user; + private GrantDto grant; + private List grants; + + @Before + public void setUp() throws Exception { + projects = new ArrayList<>(); + project = new Project(); + + projects.add(project); + projectDto = new ProjectDto(project); + + deadlines = new ArrayList<>(); + deadline = new Deadline(new Date(), DESCR); + deadline.setId(ID); + deadlines.add(deadline); + + user = new User(); + user.setFirstName(NAME); + + grants = new ArrayList<>(); + grant = new GrantDto(); + grant.setId(ID); + grants.add(grant); + } + + @Test + public void findAll() { + when(projectRepository.findAll()).thenReturn(projects); + assertEquals(projects, projectService.findAll()); + } + + @Test + public void create() throws IOException { + when(deadlineService.saveOrCreate(new ArrayList<>())).thenReturn(deadlines); + when(projectRepository.save(new Project())).thenReturn(project); + eventService.createFromProject(new Project()); + + projectDto.setTitle(TITLE); + projectDto.setDeadlines(deadlines); + + project.setId(ID); + project.setTitle(TITLE); + project.setDescription(DESCR); + project.setDeadlines(deadlines); + project.setFiles(files); + + assertEquals(project.getId(), (projectService.create(projectDto)).getId()); + } + + @Test + public void delete() throws IOException { + when(projectRepository.exists(ID)).thenReturn(true); + when(projectRepository.findOne(ID)).thenReturn(project); + + assertTrue(projectService.delete(ID)); + } + + @Test + public void getProjectExecutors() { + List executors = Collections.singletonList(user); + when(userService.findAll()).thenReturn(executors); + + assertEquals(executors, projectService.getProjectExecutors(projectDto)); + } + + @Test + public void findById() { + when(projectRepository.findOne(ID)).thenReturn(project); + assertEquals(project, projectService.findById(ID)); + } + + @Test + public void removeDeadline() throws IOException { + ProjectDto newProjectDto = new ProjectDto(); + newProjectDto.getRemovedDeadlineIds().add(INDEX); + projectDto.getDeadlines().add(deadline); + ProjectDto result = projectService.removeDeadline(projectDto, INDEX); + + assertEquals(newProjectDto.getDeadlines(), result.getDeadlines()); + assertEquals(newProjectDto.getRemovedDeadlineIds(), result.getRemovedDeadlineIds()); + } +} \ No newline at end of file