Heavily refactored DatabaseManager.java and SimpleDatabaseManager.java. Fully refactored DatabaseGoodCasesTest.java and DatabaseBadCasesTest.java.
This commit is contained in:
@@ -4,8 +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;
|
||||
public void close() throws SQLException;
|
||||
public void clearDatabase() throws SQLException;
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
@@ -1,16 +1,115 @@
|
||||
package hhn.temp.project.provider;
|
||||
|
||||
import jdk.jshell.spi.ExecutionControl;
|
||||
import hhn.temp.project.Task;
|
||||
import hhn.temp.project.Worker;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
|
||||
public interface DatabaseManager<E> extends Database {
|
||||
public void saveObjects(Collection<E> objects) throws SQLException;
|
||||
public Collection<E> getObjects() throws SQLException;
|
||||
public void saveObject(E object) throws SQLException;
|
||||
public E getObject(int taskId) throws SQLException;
|
||||
public void deleteObject(int taskId) throws SQLException;
|
||||
public E updateObject(int taskId, E object) throws SQLException;
|
||||
/**
|
||||
* 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<Task> 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<Worker> 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<Task> 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<Worker> 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;
|
||||
}
|
||||
@@ -1,152 +1,94 @@
|
||||
package hhn.temp.project.provider;
|
||||
|
||||
import hhn.temp.project.Task;
|
||||
import hhn.temp.project.TaskState;
|
||||
import hhn.temp.project.Worker;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
public class SimpleDatabaseManager<E> implements DatabaseManager<E> {
|
||||
public class SimpleDatabaseManager implements DatabaseManager {
|
||||
|
||||
private Connection connection;
|
||||
|
||||
private enum QueryMode {
|
||||
SELECT, INSERT, DELETE, UPDATE
|
||||
@Override
|
||||
public void saveTask(Task task) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
private int taskStateToInt(TaskState taskState) {
|
||||
return (taskState == TaskState.FINISHED) ? 1 : 0;
|
||||
@Override
|
||||
public void saveTasks(Collection<Task> tasks) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
private TaskState intToTaskState(int i) {
|
||||
if (i < 0 || i > 1) {
|
||||
throw new IllegalArgumentException("Integer may only bet 0 or 1");
|
||||
@Override
|
||||
public void saveWorker(Worker worker) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
return (i == 1) ? TaskState.FINISHED : TaskState.IN_PROGRESS;
|
||||
@Override
|
||||
public void saveWorkers(Collection<Worker> workers) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
private String getQuery(QueryMode query) {
|
||||
String queryString = null;
|
||||
|
||||
switch (query) {
|
||||
case INSERT -> queryString = "INSERT INTO Task (taskid, name, description, workerid, taskstate) VALUES (?, ?, ?, ?, ?)";
|
||||
case UPDATE -> queryString = "UPDATE Task SET name = ?, description = ?, taskstate = ? WHERE taskid = ?";
|
||||
case DELETE -> queryString = "DELETE FROM Task WHERE taskid = ?";
|
||||
case SELECT -> queryString = "SELECT * FROM Task WHERE workerid = ?";
|
||||
@Override
|
||||
public void updateTask(int taskId, Task newTaskObject) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
return queryString;
|
||||
@Override
|
||||
public void updateWorker(int workerId, Worker newWorkerObject) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteTask(int taskId) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteWorker(int workerId) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Task> getTasks() throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Worker> getWorkers() throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Task getTaskByTaskId(int taskId) throws SQLException {
|
||||
return new Task(0, 0, null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Worker getWorkerByWorkerId(int workerId) throws SQLException {
|
||||
return new Worker(null, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTotalNumberOfTasks() throws SQLException {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTotalNumberOfWorkers() throws SQLException {
|
||||
return -1;
|
||||
}
|
||||
|
||||
public void clearDatabase() throws SQLException {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
statement.executeUpdate("TRUNCATE TABLE Task;");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveObjects(Collection<E> objects) throws SQLException {
|
||||
for (E obj : objects) {
|
||||
saveObject(obj);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Collection<E> getObjects() throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveObject(E object) throws SQLException {
|
||||
String query = getQuery(QueryMode.INSERT);
|
||||
|
||||
if (object instanceof Task task) {
|
||||
PreparedStatement ps = connection.prepareStatement(query);
|
||||
ps.setInt(1, task.getTaskId());
|
||||
ps.setString(2, task.getName());
|
||||
ps.setString(3, task.getDescription());
|
||||
ps.setInt(4, task.getWorkerId());
|
||||
ps.setInt(5, taskStateToInt(task.getTaskState()));
|
||||
|
||||
ps.executeUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E getObject(int taskId) throws SQLException {
|
||||
String query = getQuery(QueryMode.SELECT);
|
||||
Task task = null;
|
||||
|
||||
PreparedStatement ps = connection.prepareStatement(query);
|
||||
ps.setInt(1, taskId);
|
||||
|
||||
ResultSet resultSet = ps.executeQuery();
|
||||
|
||||
if (resultSet.next()) {
|
||||
|
||||
task = new Task(
|
||||
resultSet.getInt("taskid"),
|
||||
resultSet.getInt("workerid"),
|
||||
resultSet.getString("name"),
|
||||
resultSet.getString("description")
|
||||
);
|
||||
task.setTaskState(intToTaskState(resultSet.getInt("taskstate")));
|
||||
}
|
||||
|
||||
return (E) task;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteObject(int taskId) throws SQLException {
|
||||
String query = getQuery(QueryMode.DELETE);
|
||||
|
||||
try (PreparedStatement ps = connection.prepareStatement(query)) {
|
||||
ps.setInt(1, taskId);
|
||||
ps.executeUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E updateObject(int taskId, E object) throws SQLException {
|
||||
String query = getQuery(QueryMode.UPDATE);
|
||||
|
||||
if (object instanceof Task task) {
|
||||
|
||||
if (taskId != task.getTaskId()) {
|
||||
throw new IllegalArgumentException("Task ID does not equals Task ID from Task!");
|
||||
}
|
||||
|
||||
PreparedStatement ps = connection.prepareStatement(query);
|
||||
ps.setString(1, task.getName());
|
||||
ps.setString(2, task.getDescription());
|
||||
ps.setInt(3, taskStateToInt(task.getTaskState()));
|
||||
ps.setInt(4, task.getTaskId());
|
||||
|
||||
int updated = ps.executeUpdate();
|
||||
|
||||
if (updated == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (E) task;
|
||||
}
|
||||
|
||||
return null;
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect() throws SQLException {
|
||||
connection = DriverManager.getConnection("jdbc:mysql://sql7.freesqldatabase.com/sql7810540?user=sql7810540&password=mXdJCFtDZz");
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws SQLException {
|
||||
if (connection != null) {
|
||||
connection.close();
|
||||
}
|
||||
throw new SQLException();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,36 +13,34 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class DatabaseBadCasesTest {
|
||||
|
||||
private DatabaseManager<Task> 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() throws SQLException, IOException {
|
||||
databaseManager = new SimpleDatabaseManager<>();
|
||||
public void setup() throws SQLException {
|
||||
databaseManager = new SimpleDatabaseManager();
|
||||
databaseManager.connect();
|
||||
databaseManager.clearDatabase();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Gets an object that may not be existent")
|
||||
public void assertObjectShouldNotBeExistent() throws SQLException {
|
||||
assertNull(databaseManager.getObject(999));
|
||||
@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("Updates an object that does not exist")
|
||||
public void assertUpdateNonExistentObject() throws SQLException {
|
||||
assertNull(databaseManager.updateObject(123, new Task(123, 2, "", "")));
|
||||
@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);
|
||||
}
|
||||
}
|
||||
@@ -12,103 +12,75 @@ 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<Task> databaseManager;
|
||||
private DatabaseManager databaseManager;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() throws SQLException, IOException {
|
||||
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 {
|
||||
@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.saveTask(task);
|
||||
|
||||
Task task = new Task(1, 1, "hi", "hi");
|
||||
Task reTask = databaseManager.getTaskByTaskId(task.getTaskId());
|
||||
|
||||
databaseManager.saveObject(task);
|
||||
assertNotNull(reTask);
|
||||
assertEquals(task.getTaskId(), reTask.getTaskId());
|
||||
assertEquals(task.getDescription(), reTask.getDescription());
|
||||
assertEquals(task.getName(), reTask.getName());
|
||||
assertEquals(task.getWorkerId(), reTask.getWorkerId());
|
||||
|
||||
Task taskNew = databaseManager.getObject(1);
|
||||
|
||||
assertEquals(task.getTaskId(), taskNew.getTaskId());
|
||||
assertEquals(task.getName(), taskNew.getName());
|
||||
assertEquals(task.getDescription(), taskNew.getDescription());
|
||||
assertEquals(task.getWorkerId(), taskNew.getWorkerId());
|
||||
assertEquals(task.getTaskState(), taskNew.getTaskState());
|
||||
|
||||
databaseManager.close();
|
||||
assertEquals(1, databaseManager.getTotalNumberOfTasks());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Assert that an inserted and deleted task is not there anymore")
|
||||
public void assertDeleteTest() throws SQLException {
|
||||
@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.saveWorker(worker);
|
||||
|
||||
Task task = new Task(9, 1, "hi", "hi");
|
||||
Worker reWorker = databaseManager.getWorkerByWorkerId(worker.getId());
|
||||
|
||||
databaseManager.saveObject(task);
|
||||
assertNotNull(reWorker);
|
||||
|
||||
databaseManager.deleteObject(9);
|
||||
|
||||
databaseManager.getObject(9);
|
||||
|
||||
databaseManager.close();
|
||||
assertEquals(worker.getId(), reWorker.getId());
|
||||
assertEquals(worker.getName(), reWorker.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Assert that a simple Task could be inserted into the database")
|
||||
public void assertInsertTask() throws SQLException {
|
||||
@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<Task> tasks = List.of(task1, task2, task3);
|
||||
|
||||
Task testTask = new Task(1, 1, "Hello World", "Description");
|
||||
Worker worker1 = new Worker("ABC", 2);
|
||||
Worker worker2 = new Worker("XYZ", 9);
|
||||
Collection<Worker> workers = List.of(worker1, worker2);
|
||||
|
||||
databaseManager.connect();
|
||||
databaseManager.saveObject(testTask);
|
||||
databaseManager.close();
|
||||
}
|
||||
databaseManager.saveTasks(tasks);
|
||||
databaseManager.saveWorkers(workers);
|
||||
|
||||
@Test
|
||||
@DisplayName("Assert that multiple Tasks could be inserted into the database")
|
||||
public void assertInsertMultipleTasks() throws SQLException {
|
||||
Thread.sleep(500);
|
||||
|
||||
Task testTaskZero = new Task(1, 1, "Hello", "Description");
|
||||
Task testTaskOne = new Task(2, 2, "World", "Description");
|
||||
Task testTaskTwo = new Task(3, 3, "Hello World", "Description");
|
||||
assertEquals(tasks.size(), databaseManager.getTotalNumberOfTasks());
|
||||
assertEquals(workers.size(), databaseManager.getTotalNumberOfWorkers());
|
||||
|
||||
Collection<Task> tasks = new ArrayList<>();
|
||||
tasks.add(testTaskZero);
|
||||
tasks.add(testTaskOne);
|
||||
tasks.add(testTaskTwo);
|
||||
databaseManager.clearDatabase();
|
||||
|
||||
databaseManager.connect();
|
||||
databaseManager.saveObjects(tasks);
|
||||
databaseManager.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Assert insert object and update it")
|
||||
public void assertInsertUpdateTask() throws SQLException {
|
||||
|
||||
int taskId = 1;
|
||||
|
||||
Task testTask = new Task(taskId, 1, "Hello World", "Description");
|
||||
Task newTask = new Task(taskId, 1, "No World", "Description");
|
||||
|
||||
databaseManager.connect();
|
||||
databaseManager.saveObject(testTask);
|
||||
newTask = databaseManager.updateObject(testTask.getTaskId(), newTask);
|
||||
|
||||
assertNotNull(testTask);
|
||||
|
||||
assertNotEquals(testTask.getDescription(), newTask.getDescription());
|
||||
assertEquals(testTask.getTaskId(), newTask.getTaskId());
|
||||
|
||||
databaseManager.close();
|
||||
assertEquals(0, databaseManager.getTotalNumberOfTasks());
|
||||
assertEquals(0, databaseManager.getTotalNumberOfWorkers());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user