Merge branch '104-students-mod-tests' into 'dev'

Resolve "Реализовать модульные тесты"

See merge request romanov73/ng-tracker!107
This commit is contained in:
Anton Romanov 2019-05-24 06:37:03 +00:00
commit c9ecae9842
3 changed files with 251 additions and 6 deletions

View File

@ -10,6 +10,7 @@ import ru.ulstu.tags.model.Tag;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
@ -135,6 +136,29 @@ public class TaskDto {
this.tags = tags;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TaskDto taskDto = (TaskDto) o;
return Objects.equals(id, taskDto.id) &&
Objects.equals(title, taskDto.title) &&
Objects.equals(description, taskDto.description) &&
status == taskDto.status &&
Objects.equals(deadlines, taskDto.deadlines) &&
Objects.equals(tagIds, taskDto.tagIds) &&
Objects.equals(tags, taskDto.tags);
}
@Override
public int hashCode() {
return Objects.hash(id, title, description, status, deadlines, createDate, updateDate, tagIds, tags);
}
public String getTagsString() {
return StringUtils.abbreviate(tags
.stream()

View File

@ -108,7 +108,7 @@ public class TaskService {
}
@Transactional
public void delete(Integer taskId) throws IOException {
public boolean delete(Integer taskId) throws IOException {
if (taskRepository.exists(taskId)) {
Task scheduleTask = taskRepository.findOne(taskId);
Scheduler sch = schedulerRepository.findOneByTask(scheduleTask);
@ -116,7 +116,9 @@ public class TaskService {
schedulerRepository.delete(sch.getId());
}
taskRepository.delete(taskId);
return true;
}
return false;
}
@ -128,14 +130,14 @@ public class TaskService {
}
}
public void copyMainPart(Task newTask, Task task) {
private void copyMainPart(Task newTask, Task task) {
newTask.setTitle(task.getTitle());
newTask.setTags(tagService.saveOrCreate(task.getTags()));
newTask.setCreateDate(new Date());
newTask.setStatus(Task.TaskStatus.LOADED_FROM_KIAS);
}
public Task copyTaskWithNewDates(Task task) {
private Task copyTaskWithNewDates(Task task) {
Task newTask = new Task();
copyMainPart(newTask, task);
Calendar cal1 = DateUtils.getCalendar(newTask.getCreateDate());
@ -157,7 +159,7 @@ public class TaskService {
}).collect(Collectors.toList());
}
public Task copyTaskWithNewYear(Task task) {
private Task copyTaskWithNewYear(Task task) {
Task newTask = new Task();
copyMainPart(newTask, task);
newTask.setDeadlines(newYearDeadlines(task.getDeadlines()));
@ -184,7 +186,7 @@ public class TaskService {
}
@Transactional
public void generateYearTasks() {
public List<Task> generateYearTasks() {
Set<Tag> tags = checkRepeatingTags(false);
List<Task> tasks = new ArrayList<>();
tags.forEach(tag -> {
@ -200,7 +202,9 @@ public class TaskService {
Task newTask = copyTaskWithNewYear(task);
taskRepository.save(newTask);
});
return tasks;
}
return null;
}
@ -245,8 +249,9 @@ public class TaskService {
}
@Transactional
public void createPeriodTask(Scheduler scheduler) {
public Task createPeriodTask(Scheduler scheduler) {
Task newTask = copyTaskWithNewDates(scheduler.getTask());
taskRepository.save(newTask);
return newTask;
}
}

View File

@ -0,0 +1,216 @@
package ru.ulstu.students.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 org.springframework.data.domain.Sort;
import ru.ulstu.core.util.DateUtils;
import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.deadline.service.DeadlineService;
import ru.ulstu.students.model.Scheduler;
import ru.ulstu.students.model.Task;
import ru.ulstu.students.model.TaskDto;
import ru.ulstu.students.model.TaskFilterDto;
import ru.ulstu.students.repository.SchedulerRepository;
import ru.ulstu.students.repository.TaskRepository;
import ru.ulstu.tags.model.Tag;
import ru.ulstu.tags.service.TagService;
import ru.ulstu.timeline.service.EventService;
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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class TaskServiceTest {
@Mock
TaskRepository taskRepository;
@Mock
SchedulerRepository schedulerRepository;
@Mock
private TagService tagService;
@Mock
DeadlineService deadlineService;
@Mock
EventService eventService;
@InjectMocks
TaskService taskService;
private final static Sort SORT = new Sort(Sort.Direction.DESC, "createDate");
private final static Integer ID = 1;
private final static Task.TaskStatus STATUS = Task.TaskStatus.IN_WORK;
private final static String TITLE = "title";
private final static String DESCR = "descr";
private final static String TAG = "tag";
private final static Date YEAR = DateUtils.clearTime(DateUtils.addYears(new Date(), 0));
private List<Task> tasks;
private List<Tag> tags;
private Task task;
private Task taskForSchedule;
private TaskDto taskDto;
private Tag tag;
private Deadline deadline;
private List<Deadline> deadlines;
private Scheduler scheduler;
@Before
public void setUp() throws Exception {
tasks = new ArrayList<>();
task = new Task();
task.setId(ID);
task.setTitle(TITLE);
task.setDescription(DESCR);
tag = new Tag();
tag.setId(ID);
tag.setTagName(TAG);
deadlines = new ArrayList<>();
deadline = new Deadline(new Date(), DESCR);
deadline.setId(ID);
deadlines.add(deadline);
task.setDeadlines(deadlines);
tags = new ArrayList<>();
tags.add(tag);
tasks.add(task);
taskDto = new TaskDto(task);
taskForSchedule = new Task();
taskForSchedule.setTitle(TITLE);
taskForSchedule.setDescription(DESCR);
scheduler = new Scheduler();
scheduler.setDate(new Date());
scheduler.setTask(taskForSchedule);
}
@Test
public void findAll() {
when(taskRepository.findAll(SORT)).thenReturn(tasks);
assertEquals(Collections.singletonList(task), taskService.findAll());
}
@Test
public void filter() {
when(tagService.findById(ID)).thenReturn(tag);
when(taskRepository.filterNew(STATUS, tag)).thenReturn(tasks);
TaskFilterDto taskFilterDto = new TaskFilterDto();
taskFilterDto.setTag(ID);
taskFilterDto.setOrder("new");
taskFilterDto.setStatus(STATUS);
assertEquals(Collections.singletonList(taskDto), taskService.filter(taskFilterDto));
}
@Test
public void create() throws IOException {
when(tagService.saveOrCreate(new ArrayList<>())).thenReturn(tags);
when(deadlineService.saveOrCreate(new ArrayList<>())).thenReturn(deadlines);
when(taskRepository.save(new Task())).thenReturn(task);
eventService.createFromTask(new Task());
taskDto.setTags(tags);
taskDto.setDeadlines(deadlines);
Task newTask = new Task();
task.setId(ID);
task.setTitle(TITLE);
task.setDescription(DESCR);
task.setTags(tags);
task.setDeadlines(deadlines);
assertEquals(task.getId(), taskService.create(taskDto));
}
@Test
public void delete() throws IOException {
when(taskRepository.exists(ID)).thenReturn(true);
when(taskRepository.findOne(ID)).thenReturn(task);
when(schedulerRepository.findOneByTask(task)).thenReturn(null);
assertTrue(taskService.delete(ID));
}
@Test
public void generateYearTasks() {
when(tagService.getTags()).thenReturn(tags);
tasks.get(0).setTags(tags);
when(taskRepository.findAllYear(DateUtils.clearTime(DateUtils.addYears(new Date(), -1)))).thenReturn(tasks);
tasks.get(0).setCreateDate(DateUtils.clearTime(DateUtils.addYears(new Date(), -1)));
when(taskRepository.findByTag(tag)).thenReturn(tasks);
Task newTask = new Task();
newTask.setTitle(tasks.get(0).getTitle());
newTask.setTags(tasks.get(0).getTags());
newTask.setCreateDate(new Date());
newTask.setStatus(Task.TaskStatus.LOADED_FROM_KIAS);
Deadline newDeadline = new Deadline();
newDeadline.setId(ID);
newDeadline.setDescription(deadline.getDescription());
newDeadline.setDate(DateUtils.addYears(deadline.getDate(), 1));
when(deadlineService.create(newDeadline)).thenReturn(newDeadline);
newTask.setDeadlines(Arrays.asList(newDeadline));
when(taskRepository.save(newTask)).thenReturn(task);
assertEquals(Arrays.asList(task), taskService.generateYearTasks());
}
@Test
public void checkRepeatingTags() {
when(tagService.getTags()).thenReturn(tags);
tasks.get(0).setTags(tags);
when(taskRepository.findAllYear(DateUtils.clearTime(DateUtils.addYears(new Date(), -1)))).thenReturn(tasks);
assertEquals(new HashSet<Tag>(Arrays.asList(tag)), taskService.checkRepeatingTags(false));
}
@Test
public void createPeriodTask() {
Task newTask = new Task();
newTask.setTitle(scheduler.getTask().getTitle());
newTask.setTags(scheduler.getTask().getTags());
newTask.setCreateDate(new Date());
Deadline newDeadline = new Deadline();
newDeadline.setId(ID);
newDeadline.setDescription(deadline.getDescription());
newDeadline.setDate(DateUtils.addYears(deadline.getDate(), 1));
when(deadlineService.create(newDeadline)).thenReturn(newDeadline);
newTask.setDeadlines(Arrays.asList(newDeadline));
when(taskRepository.save(newTask)).thenReturn(taskForSchedule);
assertEquals(taskForSchedule, taskService.createPeriodTask(scheduler));
}
}