From 284a3f51e9539c27db16825145815006c1d510ab Mon Sep 17 00:00:00 2001 From: ASH Date: Thu, 23 May 2019 12:07:34 +0400 Subject: [PATCH 1/4] #104 mod tests filter and create tasks --- .../java/ru/ulstu/students/model/TaskDto.java | 20 ++ .../students/service/TaskServiceTest.java | 181 ++++++++++++++++++ 2 files changed, 201 insertions(+) create mode 100644 src/test/java/ru/ulstu/students/service/TaskServiceTest.java diff --git a/src/main/java/ru/ulstu/students/model/TaskDto.java b/src/main/java/ru/ulstu/students/model/TaskDto.java index 8d52e01..1d0eac0 100644 --- a/src/main/java/ru/ulstu/students/model/TaskDto.java +++ b/src/main/java/ru/ulstu/students/model/TaskDto.java @@ -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,25 @@ 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() diff --git a/src/test/java/ru/ulstu/students/service/TaskServiceTest.java b/src/test/java/ru/ulstu/students/service/TaskServiceTest.java new file mode 100644 index 0000000..e00570c --- /dev/null +++ b/src/test/java/ru/ulstu/students/service/TaskServiceTest.java @@ -0,0 +1,181 @@ +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.deadline.model.Deadline; +import ru.ulstu.deadline.service.DeadlineService; +import ru.ulstu.students.model.Task; +import ru.ulstu.students.model.TaskDto; +import ru.ulstu.students.model.TaskFilterDto; +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.Collections; +import java.util.Date; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class TaskServiceTest { + + @Mock + TaskRepository taskRepository; + + @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 List tasks; + private List tags; + private Task task; + private TaskFilterDto filterDto; + private TaskDto taskDto; + private List tasksDto; + private Tag tag; + private Tag tagNull; + private Deadline deadline; + private List deadlines; + + + @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); + + tags = new ArrayList<>(); + tags.add(tag); + + tasks.add(task); + taskDto = new TaskDto(task); + + } + + @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 update() { + } + + @Test + public void delete() { + } + + @Test + public void save() { + } + + @Test + public void copyMainPart() { + } + + @Test + public void copyTaskWithNewDates() { + } + + @Test + public void copyTaskWithNewYear() { + } + + @Test + public void generateYearTasks() { + } + + @Test + public void checkRepeatingTags() { + } + + @Test + public void getTaskStatuses() { + } + + @Test + public void getTags() { + } + + @Test + public void findTasksByTag() { + } + + @Test + public void createPeriodTask() { + } +} \ No newline at end of file From ef521123dd157ea0381d2b6699be4ac9569e295e Mon Sep 17 00:00:00 2001 From: ASH Date: Thu, 23 May 2019 14:31:34 +0400 Subject: [PATCH 2/4] #104 mod tests check repeatings --- .../java/ru/ulstu/students/model/TaskDto.java | 8 +++- .../ulstu/students/service/TaskService.java | 10 ++-- .../students/service/TaskServiceTest.java | 46 +++++++++++-------- 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/main/java/ru/ulstu/students/model/TaskDto.java b/src/main/java/ru/ulstu/students/model/TaskDto.java index 1d0eac0..2df691f 100644 --- a/src/main/java/ru/ulstu/students/model/TaskDto.java +++ b/src/main/java/ru/ulstu/students/model/TaskDto.java @@ -138,8 +138,12 @@ public class TaskDto { @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + 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) && diff --git a/src/main/java/ru/ulstu/students/service/TaskService.java b/src/main/java/ru/ulstu/students/service/TaskService.java index 2be44cb..379314d 100644 --- a/src/main/java/ru/ulstu/students/service/TaskService.java +++ b/src/main/java/ru/ulstu/students/service/TaskService.java @@ -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())); diff --git a/src/test/java/ru/ulstu/students/service/TaskServiceTest.java b/src/test/java/ru/ulstu/students/service/TaskServiceTest.java index e00570c..19eff12 100644 --- a/src/test/java/ru/ulstu/students/service/TaskServiceTest.java +++ b/src/test/java/ru/ulstu/students/service/TaskServiceTest.java @@ -7,11 +7,13 @@ 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.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; @@ -21,9 +23,12 @@ import java.io.IOException; import java.util.ArrayList; 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) @@ -32,6 +37,9 @@ public class TaskServiceTest { @Mock TaskRepository taskRepository; + @Mock + SchedulerRepository schedulerRepository; + @Mock private TagService tagService; @@ -50,6 +58,7 @@ public class TaskServiceTest { 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 tasks; private List tags; @@ -61,6 +70,7 @@ public class TaskServiceTest { private Tag tagNull; private Deadline deadline; private List deadlines; + private Set repeatingTags; @Before @@ -83,12 +93,17 @@ public class TaskServiceTest { deadline.setId(ID); deadlines.add(deadline); + task.setDeadlines(deadlines); + tags = new ArrayList<>(); tags.add(tag); tasks.add(task); taskDto = new TaskDto(task); + repeatingTags = new HashSet<>(); + repeatingTags.add(tag); + } @Test @@ -132,35 +147,26 @@ public class TaskServiceTest { } @Test - public void update() { - } + public void delete() throws IOException { + when(taskRepository.exists(ID)).thenReturn(true); + when(taskRepository.findOne(ID)).thenReturn(task); + when(schedulerRepository.findOneByTask(task)).thenReturn(null); - @Test - public void delete() { - } - - @Test - public void save() { - } - - @Test - public void copyMainPart() { - } - - @Test - public void copyTaskWithNewDates() { - } - - @Test - public void copyTaskWithNewYear() { + assertTrue(taskService.delete(ID)); } @Test public void generateYearTasks() { + when(taskService.checkRepeatingTags(false)).thenReturn(repeatingTags); + } @Test public void checkRepeatingTags() { + when(tagService.findById(ID)).thenReturn(tag); + when(taskRepository.findAllYear(DateUtils.clearTime(DateUtils.addYears(new Date(), -1)))).thenReturn(tasks); + + assertEquals(tag, taskService.checkRepeatingTags(false)); } @Test From 82e454a50b8b209d0e0eb771e62ea3a864c0dbcc Mon Sep 17 00:00:00 2001 From: ASH Date: Thu, 23 May 2019 16:17:04 +0400 Subject: [PATCH 3/4] #104 mod tests generate task --- .../ulstu/students/service/TaskService.java | 4 ++- .../students/service/TaskServiceTest.java | 26 ++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/main/java/ru/ulstu/students/service/TaskService.java b/src/main/java/ru/ulstu/students/service/TaskService.java index 379314d..d080d37 100644 --- a/src/main/java/ru/ulstu/students/service/TaskService.java +++ b/src/main/java/ru/ulstu/students/service/TaskService.java @@ -186,7 +186,7 @@ public class TaskService { } @Transactional - public void generateYearTasks() { + public List generateYearTasks() { Set tags = checkRepeatingTags(false); List tasks = new ArrayList<>(); tags.forEach(tag -> { @@ -202,7 +202,9 @@ public class TaskService { Task newTask = copyTaskWithNewYear(task); taskRepository.save(newTask); }); + return tasks; } + return null; } diff --git a/src/test/java/ru/ulstu/students/service/TaskServiceTest.java b/src/test/java/ru/ulstu/students/service/TaskServiceTest.java index 19eff12..1ba04c4 100644 --- a/src/test/java/ru/ulstu/students/service/TaskServiceTest.java +++ b/src/test/java/ru/ulstu/students/service/TaskServiceTest.java @@ -21,6 +21,7 @@ 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; @@ -157,16 +158,35 @@ public class TaskServiceTest { @Test public void generateYearTasks() { - when(taskService.checkRepeatingTags(false)).thenReturn(repeatingTags); + 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.setDescription(deadline.getDescription()); + newDeadline.setDate(DateUtils.addYears(deadline.getDate(), 1)); + newTask.setDeadlines(Arrays.asList(newDeadline)); + + when(taskRepository.save(newTask)).thenReturn(task); + + assertEquals(Arrays.asList(task), taskService.generateYearTasks()); } @Test public void checkRepeatingTags() { - when(tagService.findById(ID)).thenReturn(tag); + when(tagService.getTags()).thenReturn(tags); + tasks.get(0).setTags(tags); when(taskRepository.findAllYear(DateUtils.clearTime(DateUtils.addYears(new Date(), -1)))).thenReturn(tasks); - assertEquals(tag, taskService.checkRepeatingTags(false)); + assertEquals(new HashSet(Arrays.asList(tag)), taskService.checkRepeatingTags(false)); } @Test From ab17b9f1521e498d7bea148935e48cfa675f1b5e Mon Sep 17 00:00:00 2001 From: ASH Date: Thu, 23 May 2019 16:45:09 +0400 Subject: [PATCH 4/4] #104 mod tests period task --- .../ulstu/students/service/TaskService.java | 3 +- .../students/service/TaskServiceTest.java | 47 +++++++++++-------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/main/java/ru/ulstu/students/service/TaskService.java b/src/main/java/ru/ulstu/students/service/TaskService.java index d080d37..e89aedf 100644 --- a/src/main/java/ru/ulstu/students/service/TaskService.java +++ b/src/main/java/ru/ulstu/students/service/TaskService.java @@ -249,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; } } diff --git a/src/test/java/ru/ulstu/students/service/TaskServiceTest.java b/src/test/java/ru/ulstu/students/service/TaskServiceTest.java index 1ba04c4..8edfc77 100644 --- a/src/test/java/ru/ulstu/students/service/TaskServiceTest.java +++ b/src/test/java/ru/ulstu/students/service/TaskServiceTest.java @@ -10,6 +10,7 @@ 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; @@ -26,7 +27,6 @@ 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; @@ -64,14 +64,12 @@ public class TaskServiceTest { private List tasks; private List tags; private Task task; - private TaskFilterDto filterDto; + private Task taskForSchedule; private TaskDto taskDto; - private List tasksDto; private Tag tag; - private Tag tagNull; private Deadline deadline; private List deadlines; - private Set repeatingTags; + private Scheduler scheduler; @Before @@ -102,8 +100,14 @@ public class TaskServiceTest { tasks.add(task); taskDto = new TaskDto(task); - repeatingTags = new HashSet<>(); - repeatingTags.add(tag); + taskForSchedule = new Task(); + taskForSchedule.setTitle(TITLE); + taskForSchedule.setDescription(DESCR); + + scheduler = new Scheduler(); + scheduler.setDate(new Date()); + scheduler.setTask(taskForSchedule); + } @@ -171,8 +175,10 @@ public class TaskServiceTest { 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); @@ -189,19 +195,22 @@ public class TaskServiceTest { assertEquals(new HashSet(Arrays.asList(tag)), taskService.checkRepeatingTags(false)); } - @Test - public void getTaskStatuses() { - } - - @Test - public void getTags() { - } - - @Test - public void findTasksByTag() { - } - @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)); } } \ No newline at end of file