aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java
blob: 5adabf1f2fe6efdf198fe1401b9a4575d73179f6 (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
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;


    @BeforeEach
    public void setupUserService() {
        mockUserDAO = mock(UserDAO.class);
        CupboardService 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 {
        // 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 {
        // 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 {
        // 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 {
        // 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 {
        // 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));
    }
    
}