diff --git a/.gradle/8.14/fileHashes/fileHashes.lock b/.gradle/8.14/fileHashes/fileHashes.lock index 3bdaec1a..59980652 100644 Binary files a/.gradle/8.14/fileHashes/fileHashes.lock and b/.gradle/8.14/fileHashes/fileHashes.lock differ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index a4beab63..ec8e1775 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/src/main/java/hhn/temp/project/AssignmentManager.java b/src/main/java/hhn/temp/project/AssignmentManager.java index ec8d4510..756b1eeb 100644 --- a/src/main/java/hhn/temp/project/AssignmentManager.java +++ b/src/main/java/hhn/temp/project/AssignmentManager.java @@ -27,7 +27,7 @@ public class AssignmentManager { if (!workerMap.containsKey(workerId) || name == null || description == null) { throw new IllegalArgumentException("WorkerId must exist and name or description can't be null"); } - Task task = new Task(++taskIdCounter, workerId, name, description, this); + Task task = new Task(++taskIdCounter, workerId, name, description); taskMap.put(taskIdCounter, task); return taskIdCounter; } diff --git a/src/main/java/hhn/temp/project/Task.java b/src/main/java/hhn/temp/project/Task.java index b5ee9cc9..f4d015bb 100644 --- a/src/main/java/hhn/temp/project/Task.java +++ b/src/main/java/hhn/temp/project/Task.java @@ -5,10 +5,8 @@ public class Task { String description; int taskId; int workerId; - AssignmentManager manager; TaskState state; - public Task(int taskId, int workerId, String name, String description, AssignmentManager manager) { - this.manager = manager; + public Task(int taskId, int workerId, String name, String description) { this.name = name; this.description = description; this.taskId = taskId; diff --git a/src/main/java/hhn/temp/project/provider/Database.java b/src/main/java/hhn/temp/project/provider/Database.java index be2d5c74..1f9a84b4 100644 --- a/src/main/java/hhn/temp/project/provider/Database.java +++ b/src/main/java/hhn/temp/project/provider/Database.java @@ -4,6 +4,26 @@ import java.io.IOException; import java.nio.file.Path; import java.sql.SQLException; +/** + * A database interface with basic database connectivity + * @author Riley Schneider + */ public interface Database { - public void connect() throws SQLException, IOException; + /** + * Creates a connection to the database + * @throws SQLException Thrown if database error occurs + */ + void connect() throws SQLException; + + /** + * Closes a connection to the database + * @throws SQLException Thrown if database error occurs + */ + void close() throws SQLException; + + /** + * Clears the entire tables in the database and reset primary key counter to each table + * @throws SQLException Thrown if database error occurs + */ + void clearDatabase() throws SQLException; } diff --git a/src/main/java/hhn/temp/project/provider/DatabaseManager.java b/src/main/java/hhn/temp/project/provider/DatabaseManager.java index 272943a9..a29a05cd 100644 --- a/src/main/java/hhn/temp/project/provider/DatabaseManager.java +++ b/src/main/java/hhn/temp/project/provider/DatabaseManager.java @@ -1,10 +1,115 @@ package hhn.temp.project.provider; +import hhn.temp.project.Task; +import hhn.temp.project.Worker; + +import java.sql.SQLException; import java.util.Collection; -public interface DatabaseManager extends Database { - public void saveObjects(Collection objects); - public Collection getObjects(); - public void saveObject(E object); - public E getObject(int id); +/** + * Simple Database Manager to handle simple command such as insertion, deletion, updating of Task and Worker objects. + * @author Riley Schneider + */ +public interface DatabaseManager extends Database { + /** + * Saves a Task object into the database + * @param task Task to be saved into the database + * @throws SQLException Thrown if database error occurs + */ + void saveTask(Task task) throws SQLException; + + /** + * Saves a collection of Task objects into the database + * @param tasks Tasks to be saved into the database + * @throws SQLException Thrown if database error occurs + */ + void saveTasks(Collection tasks) throws SQLException; + + /** + * Saves a Worker object into the database + * @param worker Worker to be saved into the database + * @throws SQLException Thrown if database error occurs + */ + void saveWorker(Worker worker) throws SQLException; + + /** + * Saves a collection Worker objects into the database + * @param workers Workers to be saved into the database + * @throws SQLException Thrown if database error occurs + */ + void saveWorkers(Collection workers) throws SQLException; + + /** + * Updates a Task object that already exists in the database + * @param taskId The ID of the given task + * @param newTaskObject The new Task object that will overwrite the existent task on database + * @throws SQLException Thrown if database error occurs + */ + void updateTask(int taskId, Task newTaskObject) throws SQLException; + + /** + * Updates a Worker object that already exists in the database + * @param workerId The ID of the given worker + * @param newWorkerObject The new Worker object that will overwrite the existent task on database + * @throws SQLException Thrown if database error occurs + */ + void updateWorker(int workerId, Worker newWorkerObject) throws SQLException; + + /** + * Deletes a task from the database + * @param taskId The ID of the given task that needs to be deleted + * @throws SQLException Thrown if database error occurs + */ + void deleteTask(int taskId) throws SQLException; + + /** + * Deletes a worker from the database + * @param workerId The ID of the given worker that needs to be deleted + * @throws SQLException Thrown if database error occurs + */ + void deleteWorker(int workerId) throws SQLException; + + /** + * Gets a collection of all tasks available in the database + * @return A collection of tasks (empty if no task available) + * @throws SQLException Thrown if database error occurs + */ + Collection getTasks() throws SQLException; + + /** + * Gets a collection of all workers available in the database + * @return A collection of workers (empty if no task available) + * @throws SQLException Thrown if database error occurs + */ + Collection getWorkers() throws SQLException; + + /** + * Gets a task given by its ID from the database + * @param taskId The task ID + * @return A task from the database (NULL if no task found) + * @throws SQLException Thrown if database error occurs + */ + Task getTaskByTaskId(int taskId) throws SQLException; + + /** + * Gets a worker given by its ID from the database + * @param workerId The worker ID + * @return A worker from the database (NULL if no worker found) + * @throws SQLException Thrown if database error occurs + */ + Worker getWorkerByWorkerId(int workerId) throws SQLException; + + /** + * Gets the total number of available tasks in the database. + * @return The total number of tasks (0 = zero entries; -1 = error) + * @throws SQLException Thrown if database error occurs + */ + int getTotalNumberOfTasks() throws SQLException; + + /** + * Gets the total number of available workers in the database. + * @return The total number of workers (0 = zero entries; -1 = error) + * @throws SQLException Thrown if database error occurs + */ + int getTotalNumberOfWorkers() throws SQLException; } \ No newline at end of file diff --git a/src/main/java/hhn/temp/project/provider/SimpleDatabaseManager.java b/src/main/java/hhn/temp/project/provider/SimpleDatabaseManager.java index 941e74e4..e53ff173 100644 --- a/src/main/java/hhn/temp/project/provider/SimpleDatabaseManager.java +++ b/src/main/java/hhn/temp/project/provider/SimpleDatabaseManager.java @@ -1,41 +1,193 @@ package hhn.temp.project.provider; -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; -import java.util.Collection; -import java.util.List; +import hhn.temp.project.Task; +import hhn.temp.project.Worker; -public class SimpleDatabaseManager implements DatabaseManager { +import java.sql.*; +import java.util.*; - private Connection connection; +public class SimpleDatabaseManager implements DatabaseManager { + + private boolean connected = false; + private Map temporaryTaskList = new HashMap(); + private Map temporaryWorkerList = new HashMap<>(); @Override - public void saveObjects(Collection objects) { + public void saveTask(Task task) throws SQLException { + if (!connected) { + throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); + } + if (!temporaryTaskList.containsKey(task.getTaskId())) { + temporaryTaskList.put(task.getTaskId(), task); + } else { + throw new SQLException("[INSERTION FAILED] Task with the same Task ID already exists."); + } } @Override - public Collection getObjects() { - return List.of(); + public void saveTasks(Collection tasks) throws SQLException { + for (Task task : tasks) { + saveTask(task); + } } @Override - public void saveObject(E object) { + public void saveWorker(Worker worker) throws SQLException { + if (!connected) { + throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); + } + if (!temporaryWorkerList.containsKey(worker.getId())) { + temporaryWorkerList.put(worker.getId(), worker); + } else { + throw new SQLException("[INSERTION FAILED] Task with the same Task ID already exists."); + } } @Override - public E getObject(int id) { - return null; + public void saveWorkers(Collection workers) throws SQLException { + for (Worker worker : workers) { + saveWorker(worker); + } } @Override - public void connect() throws SQLException, IOException { - connection = DriverManager.getConnection("jdbc:mysql://sql7.freesqldatabase.com/sql7810540?user=sql7810540&password=mXdJCFtDZz"); + public void updateTask(int taskId, Task newTaskObject) throws SQLException { + if (!connected) { + throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); + } + + if (temporaryTaskList.containsKey(taskId) && newTaskObject.getTaskId() == taskId) { + temporaryTaskList.remove(taskId); + temporaryTaskList.put(taskId, newTaskObject); + return; + } + + throw new SQLException("[UPDATE FAILED] Task ID not found OR Task ID does not equal New Task ID."); + } + + @Override + public void updateWorker(int workerId, Worker newWorkerObject) throws SQLException { + if (!connected) { + throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); + } + + if (temporaryWorkerList.containsKey(workerId) && newWorkerObject.getId() == workerId) { + temporaryWorkerList.remove(workerId); + temporaryWorkerList.put(workerId, newWorkerObject); + return; + } + + throw new SQLException("[UPDATE FAILED] Worker ID not found OR Worker ID does not equal New Worker ID."); + } + + @Override + public void deleteTask(int taskId) throws SQLException { + if (!connected) { + throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); + } + + if (temporaryTaskList.containsKey(taskId)) { + temporaryTaskList.remove(taskId); + return; + } + + throw new SQLException("[DELETION FAILED] Could not find Task ID. Nothing to delete."); + } + + @Override + public void deleteWorker(int workerId) throws SQLException { + if (!connected) { + throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); + } + + if (temporaryWorkerList.containsKey(workerId)) { + temporaryWorkerList.remove(workerId); + return; + } + + throw new SQLException("[DELETION FAILED] Could not find Worker ID. Nothing to delete."); + } + + @Override + public Collection getTasks() throws SQLException { + if (!connected) { + throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); + } + + return temporaryTaskList.values(); + } + + @Override + public Collection getWorkers() throws SQLException { + if (!connected) { + throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); + } + + return temporaryWorkerList.values(); + } + + @Override + public Task getTaskByTaskId(int taskId) throws SQLException { + if (!connected) { + throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); + } + + if (temporaryTaskList.containsKey(taskId)) { + return temporaryTaskList.get(taskId); + } + + throw new SQLException("[SELECTION FAILED] Could not find Task ID. Nothing to get."); + } + + @Override + public Worker getWorkerByWorkerId(int workerId) throws SQLException { + if (!connected) { + throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); + } + + if (temporaryWorkerList.containsKey(workerId)) { + return temporaryWorkerList.get(workerId); + } + + throw new SQLException("[SELECTION FAILED] Could not find Worker ID. Nothing to get."); + } + + @Override + public int getTotalNumberOfTasks() throws SQLException { + if (!connected) { + throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); + } + + return temporaryTaskList.size(); + } + + @Override + public int getTotalNumberOfWorkers() throws SQLException { + if (!connected) { + throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); + } + + return temporaryWorkerList.size(); + } + + public void clearDatabase() throws SQLException { + if (!connected) { + throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); + } + + temporaryTaskList.clear(); + temporaryWorkerList.clear(); + } + + @Override + public void connect() throws SQLException { + connected = true; + } + + @Override + public void close() throws SQLException { + connected = false; } } diff --git a/src/main/java/hhn/temp/project/provider/sql/createTaskTable.sql b/src/main/java/hhn/temp/project/provider/sql/createTaskTable.sql new file mode 100644 index 00000000..bc1ab8f5 --- /dev/null +++ b/src/main/java/hhn/temp/project/provider/sql/createTaskTable.sql @@ -0,0 +1,8 @@ +CREATE TABLE Task ( + id INT AUTO_INCREMENT PRIMARY KEY, + taskid INT, + name VARCHAR(255) NOT NULL, + description TEXT, + workerid INT, + taskstate INT +); \ No newline at end of file diff --git a/src/main/java/hhn/temp/project/provider/sql/deleteTaskTable.sql b/src/main/java/hhn/temp/project/provider/sql/deleteTaskTable.sql new file mode 100644 index 00000000..179a1d92 --- /dev/null +++ b/src/main/java/hhn/temp/project/provider/sql/deleteTaskTable.sql @@ -0,0 +1 @@ +DELETE FROM Task WHERE taskid = ? \ No newline at end of file diff --git a/src/main/java/hhn/temp/project/provider/sql/insertTaskTable.sql b/src/main/java/hhn/temp/project/provider/sql/insertTaskTable.sql new file mode 100644 index 00000000..aafc7901 --- /dev/null +++ b/src/main/java/hhn/temp/project/provider/sql/insertTaskTable.sql @@ -0,0 +1 @@ +INSERT INTO Task (taskid, name, description, workerid, taskstate) VALUES (?, ?, ?, ?, ?) \ No newline at end of file diff --git a/src/main/java/hhn/temp/project/provider/sql/selectTaskTable.sql b/src/main/java/hhn/temp/project/provider/sql/selectTaskTable.sql new file mode 100644 index 00000000..34d1e37a --- /dev/null +++ b/src/main/java/hhn/temp/project/provider/sql/selectTaskTable.sql @@ -0,0 +1 @@ +SELECT * FROM Task WHERE workerid = ? \ No newline at end of file diff --git a/src/main/java/hhn/temp/project/provider/sql/updateTaskTable.sql b/src/main/java/hhn/temp/project/provider/sql/updateTaskTable.sql new file mode 100644 index 00000000..e013b304 --- /dev/null +++ b/src/main/java/hhn/temp/project/provider/sql/updateTaskTable.sql @@ -0,0 +1 @@ +UPDATE Task SET name = ?, description = ?, taskstate = ? WHERE taskid = ? \ No newline at end of file diff --git a/test/hhn/temp/project/DatabaseBadCasesTest.java b/test/hhn/temp/project/DatabaseBadCasesTest.java index c9d45048..bda06a3f 100644 --- a/test/hhn/temp/project/DatabaseBadCasesTest.java +++ b/test/hhn/temp/project/DatabaseBadCasesTest.java @@ -6,34 +6,41 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import java.io.IOException; import java.sql.SQLException; import static org.junit.jupiter.api.Assertions.*; public class DatabaseBadCasesTest { - private DatabaseManager databaseManager; - - public class TestClass { - private int id; - private String dataString; - private int dataInteger; - - public TestClass(int id, String dataString, int dataInteger) { - this.id = id; - this.dataString = dataString; - this.dataInteger = dataInteger; - } - } + private DatabaseManager databaseManager; @BeforeEach - public void setup() { - databaseManager = new SimpleDatabaseManager<>(); + public void setup() throws SQLException { + databaseManager = new SimpleDatabaseManager(); + databaseManager.connect(); + databaseManager.clearDatabase(); } @Test - @DisplayName("Assert connection failed") - public void assertConnectionFailed() { - assertThrows(SQLException.class, () -> databaseManager.connect()); + @DisplayName("Inserting Tasks with the same ID") + public void insertTasksWithTheSameId() throws SQLException { + Task task = new Task(10, 10, "ABC", "XYZ"); + Task taskFaker = new Task(10, 5, "ABC!", "XYZ!"); + + databaseManager.saveTask(task); + + databaseManager.saveTask(taskFaker); + } + + @Test + @DisplayName("Inserting Workers with the same ID") + public void insertWorkersWithTheSameId() throws SQLException { + Worker worker = new Worker("Worker-1", 1); + Worker workerFaker = new Worker("Worker-100", 1); + + databaseManager.saveWorker(worker); + + databaseManager.saveWorker(workerFaker); } } \ No newline at end of file diff --git a/test/hhn/temp/project/DatabaseGoodCasesTest.java b/test/hhn/temp/project/DatabaseGoodCasesTest.java index 4f776ea7..b72b00be 100644 --- a/test/hhn/temp/project/DatabaseGoodCasesTest.java +++ b/test/hhn/temp/project/DatabaseGoodCasesTest.java @@ -6,51 +6,81 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + import java.io.IOException; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; public class DatabaseGoodCasesTest { - private DatabaseManager databaseManager; - - public class TestClass { - private int id; - private String dataString; - private int dataInteger; - - public TestClass(int id, String dataString, int dataInteger) { - this.id = id; - this.dataString = dataString; - this.dataInteger = dataInteger; - } - } + private DatabaseManager databaseManager; @BeforeEach - public void setup() { - databaseManager = new SimpleDatabaseManager<>(); + public void setup() throws SQLException { + databaseManager = new SimpleDatabaseManager(); + databaseManager.connect(); + databaseManager.clearDatabase(); } @Test - @DisplayName("Assert that the TestClass could be inserted into the database") - public void assertGetTestClass() throws SQLException, IOException { - TestClass testClass = new TestClass(1, "Hello World", 123); + @DisplayName("Inserting test task into the database and receiving back the same test task") + public void insertTaskAndReceiveItBackTest() throws SQLException { + Task task = new Task(123, 10, "Hello World", "Doing Something, hell yeah!"); - databaseManager.connect(); - databaseManager.getObject(1); + databaseManager.saveTask(task); + + Task reTask = databaseManager.getTaskByTaskId(task.getTaskId()); + + assertNotNull(reTask); + assertEquals(task.getTaskId(), reTask.getTaskId()); + assertEquals(task.getDescription(), reTask.getDescription()); + assertEquals(task.getName(), reTask.getName()); + assertEquals(task.getWorkerId(), reTask.getWorkerId()); + + assertEquals(1, databaseManager.getTotalNumberOfTasks()); } @Test - @DisplayName("Assert that the TestClass could be inserted into the database") - public void assertInsertTestClass() throws SQLException, IOException { - TestClass testClass = new TestClass(1, "Hello World", 123); + @DisplayName("Inserting test worker into the database and receiving back the same test worker") + public void insertWorkerAndReceivedItBackTest() throws SQLException { + Worker worker = new Worker("Worker-01", 12345678); - databaseManager.connect(); - databaseManager.saveObject(testClass); + databaseManager.saveWorker(worker); + + Worker reWorker = databaseManager.getWorkerByWorkerId(worker.getId()); + + assertNotNull(reWorker); + + assertEquals(worker.getId(), reWorker.getId()); + assertEquals(worker.getName(), reWorker.getName()); } @Test - @DisplayName("Assert connecting to database") - public void assertConnectToDatabase() throws SQLException, IOException { - databaseManager.connect(); + @DisplayName("Clearing the database (Task and Worker) test") + public void clearDatabaseTest() throws SQLException, InterruptedException { + Task task1 = new Task(10, 5, "Hello", "World"); + Task task2 = new Task(9, 4, "Hochschule", "Heilbronn"); + Task task3 = new Task(8, 9, "Gangnam", "Style"); + Collection tasks = List.of(task1, task2, task3); + + Worker worker1 = new Worker("ABC", 2); + Worker worker2 = new Worker("XYZ", 9); + Collection workers = List.of(worker1, worker2); + + databaseManager.saveTasks(tasks); + databaseManager.saveWorkers(workers); + + Thread.sleep(500); + + assertEquals(tasks.size(), databaseManager.getTotalNumberOfTasks()); + assertEquals(workers.size(), databaseManager.getTotalNumberOfWorkers()); + + databaseManager.clearDatabase(); + + assertEquals(0, databaseManager.getTotalNumberOfTasks()); + assertEquals(0, databaseManager.getTotalNumberOfWorkers()); } } \ No newline at end of file