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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
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.assertNull;
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;
class UserServiceTest {
private UserService userService;
private UserDAO mockUserDAO;
@BeforeEach
void setupUserService() {
mockUserDAO = mock(UserDAO.class);
CupboardService mockCupboardService = mock(CupboardService.class);
userService = new UserService(mockUserDAO, mockCupboardService);
}
@Test
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
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
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
void testGetUserBlank() throws IOException {
// Setup
String username = "Jelly";
String password = "Fish";
User user = User.create(username, password);
// Mock
when(mockUserDAO.getUser("notReal")).thenReturn(user);
// Analyze
assertNull(userService.getUser(username));
}
@Test void testGetUserCount() throws IOException {
// 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);
when(mockUserDAO.getUserCount()).thenReturn(1);
// Invoke
mockUserDAO.addUser(user);
int userCount = userService.getUserCount();
// Analyze
assertEquals(1, userCount);
}
@Test
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
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
void testDeleteUser() throws IOException {
// Setup
String username = "Jelly";
String password = "Fish";
User user = User.create(username, password);
// Mock
when(mockUserDAO.deleteUser(user.getUsername())).thenReturn(true);
// Analyze
assertTrue(userService.deleteUser(user.getUsername()));
}
}
|