summaryrefslogtreecommitdiff
path: root/src/test/java/design/persistence/JSONPersonalDatabaseTest.java
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/test/java/design/persistence/JSONPersonalDatabaseTest.java62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/test/java/design/persistence/JSONPersonalDatabaseTest.java b/src/test/java/design/persistence/JSONPersonalDatabaseTest.java
new file mode 100644
index 0000000..21cc366
--- /dev/null
+++ b/src/test/java/design/persistence/JSONPersonalDatabaseTest.java
@@ -0,0 +1,62 @@
+package design.persistence;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+
+import design.model.Golfer;
+
+/** Unit Tests for the JSON Personal Database Singleton
+ * @author Willem Dalton
+ **/
+@Tag("Persistence-tier")
+public class JSONPersonalDatabaseTest {
+ Path tempDB;
+
+ @BeforeEach
+ void clearDB() throws IOException
+ {
+ tempDB = Files.createTempFile("testdb", ".json");
+ Files.writeString(tempDB, "[]");
+ JSONPersonalDatabase.testInstance(tempDB.toString());
+ }
+
+ @Test
+ void testInstance()
+ {
+ JSONPersonalDatabase instance = JSONPersonalDatabase.instance(); // makes new instance
+ assertNotNull(instance);
+ JSONPersonalDatabase instance2 = JSONPersonalDatabase.instance(); // instance already exists
+ assertNotNull(instance2);
+ }
+
+ @Test
+ void testAddRemove() throws IOException
+ {
+ JSONPersonalDatabase instance = JSONPersonalDatabase.testInstance(tempDB.toString()); // makes new instance
+ Golfer testGolfer = new Golfer("Jamie Doe", "joe_cool", "12345");
+ instance.addGolfer(testGolfer);
+ assertEquals(testGolfer, instance.getGolfer("joe_cool"));
+ instance.removeGolfer(testGolfer);
+ assertEquals(null, instance.getGolfer("joe_cool"));
+ }
+
+ @Test
+ void testUpdateGolfer() throws IOException
+ {
+ JSONPersonalDatabase instance = JSONPersonalDatabase.testInstance(tempDB.toString()); // makes new instance
+ Golfer testGolfer = new Golfer("Jamie Doe", "joe_cool", "12345");
+ instance.addGolfer(testGolfer);
+ testGolfer.setUsername("joe_super_cool");
+ instance.updateGolfer(testGolfer);
+ assertEquals(testGolfer, instance.getGolfer("joe_super_cool"));
+ }
+}