aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/CupboardFileDAOTest.java
blob: acb759ac6a3b99ba2ba214257db8c141a2068e5e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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.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;
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 {
	private CupboardFileDAO cupboardFileDao;
	private Need[] testNeeds;

    @BeforeEach
	public void setupCupboardFileDao() throws IOException {
        ObjectMapper mockObjectMapper = mock(ObjectMapper.class);
		testNeeds = new Need[] {
				new Need("one", "Atlantis", 0, 100, Need.GoalType.MONETARY, false),
				new Need("two", "Atlantis", 1, 100, Need.GoalType.MONETARY, false),
				new Need("three", "Atlantis", 2, 100, Need.GoalType.MONETARY, false)
		};
		// 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", "Atlantis", 100, GoalType.PHYSICAL, false);

		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 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];
		assertNotNull(unupdatedNeed);

		Need updatedNeed = new Need("sequin sea urchin hats", "Atlantis", 100, GoalType.PHYSICAL, false);

		Need actualNeed = cupboardFileDao.updateNeed(updatedNeed);
		assertEquals(actualNeed, updatedNeed);
		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", "Atlantis", 5, 100, GoalType.PHYSICAL, false);

		Need actualNeed = cupboardFileDao.updateNeed(updatedNeed);
		assertNull(actualNeed);
	}

}