aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserAuthFileDAOTest.java
blob: f7db747346dc0ae12d5f98dfb3d458ba22020fdd (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
package com.ufund.api.ufundapi.persistence;

import java.io.File;
import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
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.UserAuth;

@Tag("Persistence-tier")
public class UserAuthFileDAOTest {

    private UserAuthFIleDAO userAuthFIleDAO;
    private ObjectMapper mockObjectMapper;
    private UserAuth[] userAuths;

    @BeforeEach
    public void setupUserAuthFileDAO() throws IOException {

        mockObjectMapper = mock(ObjectMapper.class);
        userAuths = new UserAuth[]{
        	new UserAuth("123", "Phil", null),
            new UserAuth("456", "Bob", null),
            new UserAuth("789", "Steve", null)
		};
        // 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))
                .thenReturn(userAuths);
        userAuthFIleDAO = new UserAuthFIleDAO(mockObjectMapper, "doesnt_matter.txt");
    }

    @Test
    public void getUserAuthTest() {
        String key = "123";
        UserAuth auth = userAuthFIleDAO.getUserAuth(key);

        assertEquals(auth, userAuths[0]);
    }

    @Test
    public void addUserAuthTest() throws IOException {
        UserAuth auth = new UserAuth("999", "Fish", null);

        assertDoesNotThrow(() -> userAuthFIleDAO.addUserAuth(auth));
    }

    @Test
    public void removeUserAuthTest() throws IOException {
        String key = "123";

        assertDoesNotThrow(() -> userAuthFIleDAO.removeUserAuth(key));
    }
    
}