aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/CupboardFileDAOTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/CupboardFileDAOTest.java')
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/CupboardFileDAOTest.java93
1 files changed, 93 insertions, 0 deletions
diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/CupboardFileDAOTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/CupboardFileDAOTest.java
new file mode 100644
index 0000000..7888084
--- /dev/null
+++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/CupboardFileDAOTest.java
@@ -0,0 +1,93 @@
+package com.ufund.api.ufundapi.persistence;
+
+import java.io.File;
+import java.io.IOException;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.ufund.api.ufundapi.model.Need;
+import com.ufund.api.ufundapi.model.Need.GoalType;
+
+@Tag("Persistence-tier")
+public class CupboardFileDAOTest {
+ CupboardFileDAO cupboardFileDao;
+ Need[] testNeeds;
+ ObjectMapper mockObjectMapper;
+
+ @BeforeEach
+ public void setupCupboardFileDao() throws IOException {
+ mockObjectMapper = mock(ObjectMapper.class);
+ testNeeds = new Need[3];
+ testNeeds[0] = new Need("one", 0, 100, Need.GoalType.MONETARY);
+ testNeeds[1] = new Need("two", 1, 100, Need.GoalType.MONETARY);
+ testNeeds[2] = new Need("three", 2, 100, Need.GoalType.MONETARY);
+ // When the object mapper is supposed to read from the file
+ // the mock object mapper will return the hero array above
+ when(mockObjectMapper
+ .readValue(new File("doesnt_matter.txt"),Need[].class))
+ .thenReturn(testNeeds);
+ cupboardFileDao = new CupboardFileDAO("doesnt_matter.txt",mockObjectMapper);
+ }
+
+ @Test
+ public void getNeedsTest() {
+ Need[] needs = cupboardFileDao.getNeeds();
+ assertEquals(needs.length,testNeeds.length);
+ assertEquals(needs[0].getName(), testNeeds[0].getName());
+ }
+
+ @Test
+ public void getNeedTest() {
+ Need need1 = cupboardFileDao.getNeed(0);
+
+ assertEquals(testNeeds[0], need1);
+ }
+
+ @Test
+ public void createNeedTest() throws IOException {
+ Need newNeed = new Need("sea urchin hats", 3, 100, GoalType.PHYSICAL);
+
+
+ Need actualNeed = cupboardFileDao.addNeed(newNeed);
+
+ assertNotNull(actualNeed);
+
+ assertEquals(actualNeed.getName(), newNeed.getName());
+ }
+
+ @Test
+ public void deleteNeedTest() throws IOException {
+ Need undeletedNeed = cupboardFileDao.getNeed(0);
+ assertNotNull(undeletedNeed);
+
+ boolean isDeleted = cupboardFileDao.deleteNeed(0);
+ assertTrue(isDeleted);
+
+ Need deletedNeed = cupboardFileDao.getNeed(0);
+ assertNull(deletedNeed);
+ }
+
+ @Test
+ public void updateNeedTest() throws IOException {
+ Need[] needs = cupboardFileDao.getNeeds();
+ Need unupdatedNeed = needs[needs.length - 1];
+ assertNotNull(unupdatedNeed);
+
+ Need updatedNeed = new Need("sequin sea urchin hats", 2, 100, GoalType.PHYSICAL);
+
+ Need actualNeed = cupboardFileDao.updateNeed(updatedNeed);
+ assertEquals(actualNeed, updatedNeed);
+ assertNotEquals(actualNeed, unupdatedNeed);
+ }
+
+}