Provide mock-like DB manager for integration testing

This commit is contained in:
Riley Schneider
2025-12-08 18:06:43 +01:00
parent e9b2ad0a57
commit 0dd7fd4687

View File

@@ -8,87 +8,186 @@ import java.util.*;
public class SimpleDatabaseManager implements DatabaseManager { public class SimpleDatabaseManager implements DatabaseManager {
private boolean connected = false;
private Map<Integer, Task> temporaryTaskList = new HashMap();
private Map<Integer, Worker> temporaryWorkerList = new HashMap<>();
@Override @Override
public void saveTask(Task task) throws SQLException { public void saveTask(Task task) throws SQLException {
throw new 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 @Override
public void saveTasks(Collection<Task> tasks) throws SQLException { public void saveTasks(Collection<Task> tasks) throws SQLException {
throw new SQLException(); for (Task task : tasks) {
saveTask(task);
}
} }
@Override @Override
public void saveWorker(Worker worker) throws SQLException { public void saveWorker(Worker worker) throws SQLException {
throw new 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 @Override
public void saveWorkers(Collection<Worker> workers) throws SQLException { public void saveWorkers(Collection<Worker> workers) throws SQLException {
throw new SQLException(); for (Worker worker : workers) {
saveWorker(worker);
}
} }
@Override @Override
public void updateTask(int taskId, Task newTaskObject) throws SQLException { public void updateTask(int taskId, Task newTaskObject) throws SQLException {
throw new 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 @Override
public void updateWorker(int workerId, Worker newWorkerObject) throws SQLException { public void updateWorker(int workerId, Worker newWorkerObject) throws SQLException {
throw new 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 @Override
public void deleteTask(int taskId) throws SQLException { public void deleteTask(int taskId) throws SQLException {
throw new 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 @Override
public void deleteWorker(int workerId) throws SQLException { public void deleteWorker(int workerId) throws SQLException {
throw new 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 @Override
public Collection<Task> getTasks() throws SQLException { public Collection<Task> getTasks() throws SQLException {
throw new SQLException(); if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
return temporaryTaskList.values();
} }
@Override @Override
public Collection<Worker> getWorkers() throws SQLException { public Collection<Worker> getWorkers() throws SQLException {
throw new SQLException(); if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
return temporaryWorkerList.values();
} }
@Override @Override
public Task getTaskByTaskId(int taskId) throws SQLException { public Task getTaskByTaskId(int taskId) throws SQLException {
return new Task(0, 0, null, null); 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 @Override
public Worker getWorkerByWorkerId(int workerId) throws SQLException { public Worker getWorkerByWorkerId(int workerId) throws SQLException {
return new Worker(null, 0); 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 @Override
public int getTotalNumberOfTasks() throws SQLException { public int getTotalNumberOfTasks() throws SQLException {
return -1; if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
return temporaryTaskList.size();
} }
@Override @Override
public int getTotalNumberOfWorkers() throws SQLException { public int getTotalNumberOfWorkers() throws SQLException {
return -1; if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
return temporaryWorkerList.size();
} }
public void clearDatabase() throws SQLException { public void clearDatabase() throws SQLException {
throw new SQLException(); if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
temporaryTaskList.clear();
temporaryWorkerList.clear();
} }
@Override @Override
public void connect() throws SQLException { public void connect() throws SQLException {
throw new SQLException(); connected = true;
} }
@Override @Override
public void close() throws SQLException { public void close() throws SQLException {
throw new SQLException(); connected = false;
} }
} }