-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathUserControllerTest.java
More file actions
110 lines (94 loc) · 3.6 KB
/
UserControllerTest.java
File metadata and controls
110 lines (94 loc) · 3.6 KB
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
package com.bravo.user.controller;
import com.bravo.user.App;
import com.bravo.user.dao.model.User;
import com.bravo.user.model.dto.UserReadDto;
import com.bravo.user.model.filter.UserFilter;
import com.bravo.user.service.UserService;
import com.bravo.user.utility.PageUtil;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import javax.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ContextConfiguration(classes = {App.class})
@ExtendWith(SpringExtension.class)
@SpringBootTest()
@AutoConfigureMockMvc
class UserControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private UserService userService;
private List<UserReadDto> users;
@BeforeEach
public void beforeEach(){
final List<Integer> ids = IntStream
.range(1, 10)
.boxed()
.collect(Collectors.toList());
this.users = ids.stream()
.map(id -> createUserReadDto(Integer.toString(id)))
.collect(Collectors.toList());
}
@Test
void getRetrieveWithName() throws Exception {
when(userService
.retrieveByName(anyString(), any(PageRequest.class), any(HttpServletResponse.class)))
.thenReturn(users);
final ResultActions result = this.mockMvc
.perform(get("/user/retrieve?name=lucy"))
.andExpect(status().isOk());
for(int i = 0; i < users.size(); i++){
result.andExpect(jsonPath(String.format("$[%d].id", i)).value(users.get(i).getId()));
}
final PageRequest pageRequest = PageUtil.createPageRequest();
verify(userService).retrieveByName(
eq("lucy"), eq(pageRequest), any(HttpServletResponse.class)
);
}
@Test
void getRetrieveWithNameEmpty() throws Exception {
this.mockMvc.perform(get("/user/retrieve?name="))
.andExpect(status().isBadRequest());
}
@Test
void getRetrieveWithNameSpecialCharacters() throws Exception {
this.mockMvc.perform(get("/user/retrieve?name=^"))
.andExpect(status().isBadRequest());
}
@Test
void getRetrieveWithNameSpace() throws Exception {
this.mockMvc.perform(get("/user/retrieve?name=Sa w"))
.andExpect(status().isBadRequest());
}
@Test
void getRetrieveWithNameMissing() throws Exception {
this.mockMvc.perform(get("/user/retrieve"))
.andExpect(status().isBadRequest());
}
private UserReadDto createUserReadDto(final String id){
final UserReadDto user = new UserReadDto();
user.setId(id);
return user;
}
}