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
|
package com.ufund.api.ufundapi.controller;
import java.io.IOException;
import java.util.Map;
import static java.util.Map.entry;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.mockito.ArgumentMatchers.any;
import org.mockito.Mockito;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import com.ufund.api.ufundapi.service.AuthService;
@RequestMapping("auth")
public class AuthControllerTest {
private AuthController authController;
private AuthService mockAuthService;
private Map<String, String> authMap;
@BeforeEach
private void setupAuthController() {
mockAuthService = mock(AuthService.class);
authController = new AuthController(mockAuthService);
authMap = Map.ofEntries(
entry("Bob", "123")
);
}
@Test
public void testLogin() throws IllegalAccessException, IOException {
// Setup
String key = "123";
// Mock
when(mockAuthService.login(any(), any())).thenReturn(key);
// Invoke
ResponseEntity<String> response = authController.login(authMap);
// Analyze
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(key, response.getBody());
}
@Test
public void testLoginUnauthorized() throws IllegalAccessException, IOException {
// Mock
when(mockAuthService.login(any(), any())).thenThrow(IllegalAccessException.class);
// Invoke
ResponseEntity<String> response = authController.login(authMap);
// Analyze
assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
}
@Test
public void testLoginIOException() throws IllegalAccessException, IOException {
// Mock
when(mockAuthService.login(any(), any())).thenThrow(IOException.class);
// Invoke
ResponseEntity<String> response = authController.login(authMap);
// Analyze
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
}
@Test
public void testLogout() throws IllegalAccessException, IOException {
// Setup
String key = "123";
// Invoke
ResponseEntity<Object> response = authController.logout(key);
// Analyze
assertEquals(HttpStatus.OK, response.getStatusCode());
}
@Test
public void testLogoutIOException() throws IllegalAccessException, IOException {
// Setup
String key = "123";
// Mock
doThrow(new IOException()).when(mockAuthService).logout(key);
// Invoke
ResponseEntity<Object> response = authController.logout(key);
// Analyze
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
}
}
|