#101 project tests

This commit is contained in:
a.vasin 2019-05-31 02:23:15 +04:00
parent 9388302a47
commit a7b48e1534
2 changed files with 157 additions and 5 deletions

View File

@ -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) {

View File

@ -1 +1,148 @@
package ru.ulstu.project.service;
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<Project> projects;
private Project project;
private ProjectDto projectDto;
private Deadline deadline;
private List<Deadline> deadlines;
private FileData file;
private List<FileData> files;
private User user;
private GrantDto grant;
private List<GrantDto> 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<User> 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());
}
}