-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathUserControllerIntegrationTest.java
189 lines (168 loc) · 6.23 KB
/
UserControllerIntegrationTest.java
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.lambdaschool.usermodel.controllers;
import com.lambdaschool.usermodel.UserModelApplication;
import io.restassured.module.mockmvc.RestAssuredMockMvc;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
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.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = UserModelApplication.class)
@AutoConfigureMockMvc
public class UserControllerIntegrationTest
{
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Before
public void setUp() throws Exception
{
RestAssuredMockMvc.webAppContextSetup(webApplicationContext);
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.build();
}
@After
public void tearDown() throws Exception
{
}
@Test
public void whenMeasuredResponseTime() throws
Exception
{
long time = System.currentTimeMillis();
this.mockMvc.perform(get("/users/users"))
.andDo(print());
long responseTime = (System.currentTimeMillis() - time);
assertTrue("timestamp",
(responseTime < 5000L));
}
@Test
public void getAllUsers() throws
Exception
{
this.mockMvc.perform(get("/users/users"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("cinnamon")));
}
@Test
public void getUserLikeName() throws
Exception
{
this.mockMvc.perform(get("/users/user/name/like/{userName}",
"kitty"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("misskitty")));
}
@Test
public void getUserById() throws
Exception
{
this.mockMvc.perform(get("/users/user/{userid}",
4))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("admin")));
}
@Test
public void getUserByIdNotFound() throws
Exception
{
this.mockMvc.perform(get("/users/user/{userid}",
100))
.andDo(print())
.andExpect(status().is4xxClientError())
.andExpect(content().string(containsString("ResourceNotFoundException")));
}
@Test
public void getUserByName() throws
Exception
{
this.mockMvc.perform(get("/users/user/name/{userName}",
"admin"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("admin")));
}
@Test
public void getUserByNameNotFound() throws
Exception
{
this.mockMvc.perform(get("/users/user/name/{userName}",
"rabbit"))
.andDo(print())
.andExpect(status().is4xxClientError())
.andExpect(content().string(containsString("ResourceNotFoundException")));
}
@Test
public void givenPostAUser() throws
Exception
{
mockMvc.perform(MockMvcRequestBuilders.post("/users/user")
.content("{\"username\": \"Ginger\", \"password\": \"EATEATEAT\", \"primaryemail\" : \"[email protected]\"}")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isCreated())
.andExpect(MockMvcResultMatchers.header()
.exists("location"));
}
@Test
public void givenPutAUser() throws
Exception
{
mockMvc.perform(MockMvcRequestBuilders.put("/users/user/11")
.content("{\"username\": \"stumps\", \"password\": \"EATEATEAT\", \"primaryemail\" : \"[email protected]\"}")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk());
}
@Test
public void deleteUserById() throws
Exception
{
mockMvc.perform(MockMvcRequestBuilders.delete("/users/user/{id}",
13))
.andDo(print())
.andExpect(status().is2xxSuccessful());
}
@Test
public void deleteUserByIdNotFound() throws
Exception
{
mockMvc.perform(MockMvcRequestBuilders.delete("/users/user/{id}",
100))
.andDo(print())
.andExpect(status().is4xxClientError());
}
@Test
public void UpdateUser() throws
Exception
{
mockMvc.perform(MockMvcRequestBuilders.patch("/users/user/{userid}",
7)
.content("{\"password\": \"EATEATEAT\"}")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk());
}
}