aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java
blob: e57c5a3c85b74d2afb285e74b4378254e5a2687f (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.ufund.api.ufundapi.service;

import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.ufund.api.ufundapi.DuplicateKeyException;
import com.ufund.api.ufundapi.model.User;
import com.ufund.api.ufundapi.persistence.UserDAO;

public class UserServiceTest {

    private UserService userService;
    private UserDAO mockUserDAO;
    private CupboardService mockCupboardService;


    @BeforeEach
    public void setupUserService() {
        mockUserDAO = mock(UserDAO.class);
        mockCupboardService = mock(CupboardService.class);
        userService = new UserService(mockUserDAO, mockCupboardService);
    }

    @Test
    public void testCreateUser() throws IOException, DuplicateKeyException {
        // Setup
        String username = "Jelly";
        String password = "Fish";
        User user = User.create(username, password);

        // Mock
        when(mockUserDAO.getUser(username)).thenReturn(null);
        when(mockUserDAO.addUser(any())).thenReturn(user);

        // Invoke

        // Analyze
        assertEquals(user, userService.createUser(username, password));
    }

    @Test
    public void testCreateUserDuplicate() throws IOException, DuplicateKeyException {
        // Setup
        String username = "Jelly";
        String password = "Fish";
        User user = User.create(username, password);

        // Mock
        when(mockUserDAO.getUser(username)).thenReturn(User.create("Phil", "Phil"));
        when(mockUserDAO.addUser(any())).thenReturn(user);

        // Analyze
        assertThrows(DuplicateKeyException.class, () -> userService.createUser(username, password));
    }

    @Test
    public void testGetUser() throws IOException, DuplicateKeyException {
        // Setup
        String username = "Jelly";
        String password = "Fish";
        User user = User.create(username, password);

        // Mock
        when(mockUserDAO.getUser(username)).thenReturn(user);

        // Analyze
        assertEquals(user, userService.getUser(username));
    }

    @Test
    public void testUpdateUser() throws IOException, DuplicateKeyException {
        // Setup
        String username = "Jelly";
        String password = "Fish";
        User oldUser = User.create(username, password);

        String newUsername = "Jelly";
        String newPassword = "Dog";
        User newUser = User.create(newUsername, newPassword);

        // Mock
        when(mockUserDAO.updateUser(newUser)).thenReturn(newUser);

        // Analyze
        assertEquals(newUser, userService.updateUser(newUser, oldUser.getUsername()));
    }

    @Test
    public void testUpdateUserDifferentUsernames() throws IOException, DuplicateKeyException {
        // Setup
        String username = "Jelly";
        String password = "Fish";
        User oldUser = User.create(username, password);

        String newUsername = "Cat";
        String newPassword = "Fish";
        User newUser = User.create(newUsername, newPassword);

        // Mock
        when(mockUserDAO.updateUser(newUser)).thenReturn(newUser);

        // Analyze
        assertThrows(IllegalArgumentException.class, () -> userService.updateUser(newUser, oldUser.getUsername()));
    }

    @Test
    public void testDeleteUser() throws IOException, DuplicateKeyException {
        // Setup
        String username = "Jelly";
        String password = "Fish";
        User user = User.create(username, password);

        // Mock
        when(mockUserDAO.deleteUser(username)).thenReturn(true);

        // Analyze
        assertTrue(userService.deleteUser(username));
    }
    
}