diff options
Diffstat (limited to 'ufund-api/src/test/java/com/ufund/api/ufundapi/persistence')
3 files changed, 74 insertions, 36 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 index f786a8c..d83e825 100644 --- 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 @@ -4,6 +4,7 @@ import java.io.File;  import java.io.IOException;  import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse;  import static org.junit.jupiter.api.Assertions.assertNotEquals;  import static org.junit.jupiter.api.Assertions.assertNotNull;  import static org.junit.jupiter.api.Assertions.assertNull; @@ -20,44 +21,42 @@ import com.ufund.api.ufundapi.model.Need.GoalType;  @Tag("Persistence-tier")  public class CupboardFileDAOTest { -    private CupboardFileDAO cupboardFileDao; -    private Need[] testNeeds; -    private ObjectMapper mockObjectMapper; +	private CupboardFileDAO cupboardFileDao; +	private Need[] testNeeds;      @BeforeEach -    public void setupCupboardFileDao() throws IOException { -        mockObjectMapper = mock(ObjectMapper.class); -        testNeeds = new Need[]{ -        	new Need("one", 0, 100, Need.GoalType.MONETARY), -			new Need("two", 1, 100, Need.GoalType.MONETARY), -			new Need("three", 2, 100, Need.GoalType.MONETARY) +	public void setupCupboardFileDao() throws IOException { +        ObjectMapper mockObjectMapper = mock(ObjectMapper.class); +		testNeeds = new Need[] { +				new Need("one", 0, 100, Need.GoalType.MONETARY), +				new Need("two", 1, 100, Need.GoalType.MONETARY), +				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); +		// 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() { +	@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); @@ -79,6 +78,15 @@ public class CupboardFileDAOTest {  	}  	@Test +	public void deleteNeedTestFail() throws IOException { +		Need undeletedNeed = cupboardFileDao.getNeed(0); +		assertNotNull(undeletedNeed); + +		boolean nullNeed = cupboardFileDao.deleteNeed(20); +		assertFalse(nullNeed); +	} + +	@Test  	public void updateNeedTest() throws IOException {  		Need[] needs = cupboardFileDao.getNeeds();  		Need unupdatedNeed = needs[needs.length - 1]; @@ -91,4 +99,16 @@ public class CupboardFileDAOTest {  		assertNotEquals(actualNeed, unupdatedNeed);  	} +	@Test +	public void updateNeedTestFail() throws IOException { +		Need[] needs = cupboardFileDao.getNeeds(); +		Need unupdatedNeed = needs[needs.length - 1]; +		assertNotNull(unupdatedNeed); + +		Need updatedNeed = new Need("sequin sea urchin hats", 20, 100, GoalType.PHYSICAL); + +		Need actualNeed = cupboardFileDao.updateNeed(updatedNeed); +		assertNull(actualNeed); +	} +  } diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserAuthFileDAOTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserAuthFileDAOTest.java index f7db747..a4842c5 100644 --- a/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserAuthFileDAOTest.java +++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserAuthFileDAOTest.java @@ -2,6 +2,7 @@ package com.ufund.api.ufundapi.persistence;  import java.io.File;  import java.io.IOException; +import java.time.LocalDateTime;  import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;  import static org.junit.jupiter.api.Assertions.assertEquals; @@ -18,22 +19,21 @@ import com.ufund.api.ufundapi.model.UserAuth;  public class UserAuthFileDAOTest {      private UserAuthFIleDAO userAuthFIleDAO; -    private ObjectMapper mockObjectMapper;      private UserAuth[] userAuths;      @BeforeEach      public void setupUserAuthFileDAO() throws IOException { -        mockObjectMapper = mock(ObjectMapper.class); +        ObjectMapper mockObjectMapper = mock(ObjectMapper.class);          userAuths = new UserAuth[]{ -        	new UserAuth("123", "Phil", null), -            new UserAuth("456", "Bob", null), -            new UserAuth("789", "Steve", null) +        	new UserAuth("123", "Phil", LocalDateTime.MAX), +            new UserAuth("456", "Bob", LocalDateTime.MAX), +            new UserAuth("789", "Steve", LocalDateTime.MAX)  		};          // 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"),UserAuth[].class)) +                .readValue(new File("doesnt_matter.txt"),UserAuth[].class))                  .thenReturn(userAuths);          userAuthFIleDAO = new UserAuthFIleDAO(mockObjectMapper, "doesnt_matter.txt");      } @@ -43,18 +43,18 @@ public class UserAuthFileDAOTest {          String key = "123";          UserAuth auth = userAuthFIleDAO.getUserAuth(key); -        assertEquals(auth, userAuths[0]); +        assertEquals(userAuths[0], auth);      }      @Test -    public void addUserAuthTest() throws IOException { +    public void addUserAuthTest() {          UserAuth auth = new UserAuth("999", "Fish", null);          assertDoesNotThrow(() -> userAuthFIleDAO.addUserAuth(auth));      }      @Test -    public void removeUserAuthTest() throws IOException { +    public void removeUserAuthTest() {          String key = "123";          assertDoesNotThrow(() -> userAuthFIleDAO.removeUserAuth(key)); diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserFileDAOTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserFileDAOTest.java index 9361188..2ee0fc0 100644 --- a/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserFileDAOTest.java +++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserFileDAOTest.java @@ -39,6 +39,24 @@ public class UserFileDAOTest {          userFileDAO = new UserFileDAO("doesnt_matter.txt",mockObjectMapper);      } +	@Test +	public void addUsersTest() throws IOException { +		User user = User.create("Name", "Pass"); + +		User addedUser = userFileDAO.addUser(user); + +		assertEquals(addedUser, user); +	} + +	@Test +	public void addUsersTestFail() throws IOException { +		User user = User.create("bob", "test"); + +		User existingUser = userFileDAO.addUser(user); + +		assertEquals(existingUser, testUsers[0]); +	} +      @Test      public void getUsersTest() {          User[] users = userFileDAO.getUsers();  | 
