Skip to content

Commit

Permalink
Менеджер задач - ФЗ-5
Browse files Browse the repository at this point in the history
Изменение структуры проекта по тестам, добавление тестов для Managers
  • Loading branch information
OlgaPegushina committed Nov 10, 2024
1 parent 06d0867 commit c168d1b
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 105 deletions.
2 changes: 1 addition & 1 deletion java-kanban.iml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
Expand Down
4 changes: 1 addition & 3 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import model.Epic;
import model.Status;
import model.Subtask;
import model.Task;
import service.HistoryManager;
import service.InMemoryTaskManager;

import service.Managers;
import service.TaskManager;

Expand Down
4 changes: 2 additions & 2 deletions src/service/InMemoryHistoryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ private void removeHistoryFirst() {

@Override
public List<Task> getHistory() {
return history;
return new ArrayList<>(history);
}

@Override
public void addInHistory(Task task) {
if (task != null && SIZE_HISTORY != 0) {
if (task != null) {
removeHistoryFirst();
history.add(task);
}
Expand Down
12 changes: 6 additions & 6 deletions src/service/InMemoryTaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ public class InMemoryTaskManager implements TaskManager {
private final HistoryManager inMemoryHistoryManager = Managers.getDefaultHistory();

@Override
public ArrayList<Task> getAllTasks() {
public List<Task> getAllTasks() {
return new ArrayList<>(tasks.values());
}

@Override
public ArrayList<Subtask> getAllSubtasks() {
public List<Subtask> getAllSubtasks() {
return new ArrayList<>(subtasks.values());
}

@Override
public ArrayList<Epic> getAllEpics() {
public List<Epic> getAllEpics() {
return new ArrayList<>(epics.values());
}

Expand Down Expand Up @@ -167,8 +167,8 @@ public void deleteEpic(int epicId) {
}

@Override
public ArrayList<Subtask> getEpicSubtasks(int epicId) {
ArrayList<Subtask> epicSubtasks = new ArrayList<>();
public List<Subtask> getEpicSubtasks(int epicId) {
List<Subtask> epicSubtasks = new ArrayList<>();
Epic epic = epics.get(epicId);
if (epic != null) {
for (Integer subtaskId : epic.getSubtaskIds()) {
Expand All @@ -180,7 +180,7 @@ public ArrayList<Subtask> getEpicSubtasks(int epicId) {

@Override
public void updateStatusEpic(int epicId) {
ArrayList<Subtask> epicSubtasks = getEpicSubtasks(epicId);
List<Subtask> epicSubtasks = getEpicSubtasks(epicId);
int countNew = 0;
int countDone = 0;
if (epicSubtasks.isEmpty()) {
Expand Down
9 changes: 4 additions & 5 deletions src/service/TaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import model.Subtask;
import model.Task;

import java.util.ArrayList;
import java.util.List;

public interface TaskManager {
ArrayList<Task> getAllTasks();
List<Task> getAllTasks();

ArrayList<Subtask> getAllSubtasks();
List<Subtask> getAllSubtasks();

ArrayList<Epic> getAllEpics();
List<Epic> getAllEpics();

List<Task> getHistory();

Expand Down Expand Up @@ -46,7 +45,7 @@ public interface TaskManager {

void deleteEpic(int epicId);

ArrayList<Subtask> getEpicSubtasks(int epicId);
List<Subtask> getEpicSubtasks(int epicId);

void updateStatusEpic(int epicId);
}
21 changes: 21 additions & 0 deletions test/model/EpicTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package model;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class EpicTest {

@Test
void epicTest() {
Epic epic1 = new Epic("Эпическая задача - 1",
"Описание эпической задачи - 1");
Epic epic2 = new Epic("Эпическая задача - 2",
"Описание эпической задачи - 2");
epic2.setId(epic1.getId());

assertEquals(epic1.getId(), epic2.getId(), "Id Эпиков не совпадают.");
assertEquals(epic1, epic2, "Эпики не совпадают.");
}
}

21 changes: 21 additions & 0 deletions test/model/SubtaskTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package model;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class SubtaskTest {

@Test
void epicTest() {
Subtask subtask1 = new Subtask( "Подзадача - 1",
"Описание подзадачи - 1, эпической задачи - 1", 1);
Subtask subtask2 = new Subtask("Подзадача - 2",
"Описание подзадачи - 2, эпической задачи - 1", 1);
subtask2.setId(subtask1.getId());

assertEquals(subtask1.getId(), subtask2.getId(), "Id Подзадач не совпадают.");
assertEquals(subtask1, subtask2, "Подзадачи не совпадают.");
}
}

18 changes: 18 additions & 0 deletions test/model/TaskTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package model;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TaskTest {

@Test
void taskTest() {
Task task1 = new Task("Просто задача - 1", "Описание простой задачи - 1");
Task task2 = new Task("Просто задача - 2", "Описание простой задачи - 1");
task2.setId(task1.getId());

assertEquals(task1.getId(), task2.getId(), "Id Задач не совпадают.");
assertEquals(task1, task2, "Задачи не совпадают.");
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package test;
package service;

import model.Epic;
import model.Status;
import model.Subtask;
import model.Task;

import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import service.Managers;
import service.TaskManager;

import static org.junit.jupiter.api.Assertions.*;

Expand All @@ -27,7 +24,7 @@ void setUp() {
}

@Test
void addTaskInHistory() {
void addTaskInHistoryTest() {
final int taskId = manager.addNewTask(task);

manager.getTask(taskId);
Expand All @@ -39,7 +36,7 @@ void addTaskInHistory() {
}

@Test
void addEpicInHistory() {
void addEpicInHistoryTest() {
final int epicId = manager.addNewEpic(epic);

manager.getEpic(epicId);
Expand All @@ -51,7 +48,7 @@ void addEpicInHistory() {
}

@Test
void addSubtaskInHistory() {
void addSubtaskInHistoryTest() {
final int epicId = manager.addNewEpic(epic);
Subtask subtask = new Subtask("Подзадача - 1",
"Описание подзадачи - 1, эпической задачи - 1", epicId);
Expand All @@ -67,7 +64,7 @@ void addSubtaskInHistory() {
}

@Test
void getHistory() {
void getHistoryTest() {
final int epicId = manager.addNewEpic(epic);
Subtask subtask = new Subtask("Подзадача - 1",
"Описание подзадачи - 1, эпической задачи - 1", epicId);
Expand Down
20 changes: 20 additions & 0 deletions test/service/ManagersTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package service;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;

public class ManagersTest {

@Test
void TaskManagerGetDefaultTest() {
final TaskManager manager = Managers.getDefault();
assertNotNull(manager, "Объект TaskManager не создан");
}

@Test
void HistoryManagerGetDefaultHistory() {
final HistoryManager inMemoryHistoryManager = Managers.getDefaultHistory();
assertNotNull(inMemoryHistoryManager, "Объект HistoryManager не создан");
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,78 @@
package test;
package service;

import model.Epic;
import model.Status;
import model.Subtask;

import org.junit.jupiter.api.Test;
import model.Task;
import org.junit.jupiter.api.BeforeEach;
import service.Managers;
import service.TaskManager;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class EpicAndSubtaskTest {
public class TaskManagerTest {
private TaskManager manager;
private Task task;
private Epic epic;

@BeforeEach
void setUp() {
task = new Task("Просто задача - 1", "Описание простой задачи - 1");
epic = new Epic("Эпическая задача - 1",
"Описание эпической задачи - 1");
manager = Managers.getDefault();
}

@Test
void addNewEpic() {
void addNewTaskTest() {
final int taskId = manager.addNewTask(task);
final Task savedTask = manager.getTask(taskId);

assertNotNull(savedTask, "Задача не найдена.");
assertEquals(task, savedTask, "Задачи не совпадают.");

final List<Task> tasks = manager.getAllTasks();

assertNotNull(tasks, "Задачи не возвращаются.");
assertEquals(1, tasks.size(), "Неверное количество задач.");
assertEquals(task, tasks.get(0), "Задачи не совпадают.");
}

@Test
void updateTaskTest() {
final int taskId = manager.addNewTask(task);
final Task savedTask = manager.getTask(taskId);

savedTask.setStatus(Status.DONE);
manager.updateTask(savedTask);

assertNotNull(savedTask, "Задача не найдена.");
assertEquals(task, savedTask, "Задачи не совпадают.");
assertEquals(task.getStatus(), savedTask.getStatus(), "Статусы задач не совпадают");
}

@Test
void deleteTaskTest() {
manager.addNewTask(task);
manager.deleteTask(task.getId());

assertTrue(manager.getAllTasks().isEmpty(), "Задача не удалилась");
assertEquals(0, manager.getAllTasks().size(), "Задача не удалилась");
}

@Test
void deleteAllTasksTest() {
manager.addNewTask(task);
manager.addNewTask(task);
manager.deleteAllTasks();

assertTrue(manager.getAllTasks().isEmpty(), "Задачи не удалились");
assertEquals(0, manager.getAllTasks().size(), "Задачи не удалились");
}

@Test
void addNewEpicTest() {
final int epicId = manager.addNewEpic(epic);
final Epic savedEpic = manager.getEpic(epicId);

Expand All @@ -41,7 +87,7 @@ void addNewEpic() {
}

@Test
void addNewSubtask() {
void addNewSubtaskTest() {
final int epicId = manager.addNewEpic(epic);
Subtask subtask = new Subtask("Подзадача - 1",
"Описание подзадачи - 1, эпической задачи - 1", epicId);
Expand Down Expand Up @@ -164,4 +210,3 @@ void updateStatusEpicTest() {
assertEquals(Status.IN_PROGRESS, epic.getStatus(), "Неверный статус IN_PROGRESS");
}
}

Loading

0 comments on commit c168d1b

Please sign in to comment.