/
EventControllerTests.java
393 lines (360 loc) · 20.3 KB
/
EventControllerTests.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package me.brucehan.restfulhan.events;
import me.brucehan.restfulhan.accounts.Account;
import me.brucehan.restfulhan.accounts.AccountRepository;
import me.brucehan.restfulhan.accounts.AccountRole;
import me.brucehan.restfulhan.accounts.AccountService;
import me.brucehan.restfulhan.common.AppProperties;
import me.brucehan.restfulhan.common.BaseTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.MediaTypes;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.security.oauth2.common.util.Jackson2JsonParser;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.ResultActions;
import java.time.LocalDateTime;
import java.util.Set;
import java.util.stream.IntStream;
import static org.springframework.restdocs.headers.HeaderDocumentation.*;
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel;
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.payload.PayloadDocumentation.*;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
public class EventControllerTests extends BaseTest {
@Autowired EventRepository eventRepository;
@Autowired AccountService accountService;
@Autowired AccountRepository accountRepository;
@Autowired AppProperties appProperties;
@BeforeEach
public void setUp() {
this.eventRepository.deleteAll();
this.accountRepository.deleteAll();
}
@Test
@DisplayName("정상적으로 이벤트를 생성하는 테스트")
public void createEvent() throws Exception {
EventDto event = EventDto.builder()
.name("Spring")
.description("REST API Development with Spring")
.beginEnrollmentDateTime(LocalDateTime.of(2022, 11, 4, 11, 47))
.closeEnrollmentDateTime(LocalDateTime.of(2022, 11, 5, 11, 50))
.beginEventDateTime(LocalDateTime.of(2022, 11, 6, 9, 18))
.endEventDateTime(LocalDateTime.of(2022, 11, 7, 4, 23))
.basePrice(100)
.maxPrice(200)
.limitOfEnrollment(100)
.location("hong-dae")
.build();
// // eventRepository.save(event)가 호출되면 event를 리턴하라
// Mockito.when(eventRepository.save(event)).thenReturn(event);
mockMvc.perform(post("/api/events/")
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
.contentType(MediaType.APPLICATION_JSON_VALUE) // APPLICATION_JSON_UTF8
.accept(MediaTypes.HAL_JSON) // 이 스펙이 준하는 응답을 보내겠다
.content(objectMapper.writeValueAsString(event))) // bean으로 등록 안 했지만 있는 것처럼 쓸 수 있음
.andDo(print()) // 실제 콘솔에서 어떤 요청과 응답을 받았는지 확인 가능
.andExpect(status().isCreated())
.andExpect(jsonPath("$.id").exists()) // "id"가 있는지 테스트
.andExpect(header().exists(HttpHeaders.LOCATION)) // type safe 하게 작성됨
.andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaTypes.HAL_JSON_VALUE))
.andExpect(jsonPath("free").value(false)) // type safe 하게 작성됨
.andExpect(jsonPath("offline").value(true)) // type safe 하게 작성됨
.andExpect(jsonPath("eventStatus").value(EventStatus.DRAFT.name()))
.andExpect(jsonPath("_links.self").exists())
.andExpect(jsonPath("_links.query-events").exists())
.andExpect(jsonPath("_links.update-event").exists())
.andDo(document("create-events",
links(
linkWithRel("self").description("link to self"),
linkWithRel("query-events").description("link to query events"),
linkWithRel("update-event").description("link to update an existing event"),
linkWithRel("profile").description("link to update an existing event")
),
requestHeaders(
headerWithName(HttpHeaders.ACCEPT).description("accept header"),
headerWithName(HttpHeaders.CONTENT_TYPE).description("content type header")
),
requestFields(
fieldWithPath("name").description("Name of new event"),
fieldWithPath("description").description("description of new event"),
fieldWithPath("beginEnrollmentDateTime").description("date time of begin of new event"),
fieldWithPath("closeEnrollmentDateTime").description("date time of close of new event"),
fieldWithPath("beginEventDateTime").description("date time of begin of new event"),
fieldWithPath("endEventDateTime").description("date time of end of new event"),
fieldWithPath("location").description("location of new event"),
fieldWithPath("basePrice").description("base price of new event"),
fieldWithPath("maxPrice").description("max price of new event"),
fieldWithPath("limitOfEnrollment").description("limit of enrollment")
),
responseHeaders(
headerWithName(HttpHeaders.LOCATION).description("Location header"), // 새로 생성된 이벤트를 조회할 수 있는 URL
headerWithName(HttpHeaders.CONTENT_TYPE).description("Content type") // 어떤 컨텐트 타입이다
),
relaxedResponseFields( // relaxedResponseFields : 문서의 일부분, 즉 _links에 해당하는 거는
fieldWithPath("id").description("identifier of new event"),
fieldWithPath("name").description("Name of new event"),
fieldWithPath("description").description("description of new event"),
fieldWithPath("beginEnrollmentDateTime").description("date time of begin of new event"),
fieldWithPath("closeEnrollmentDateTime").description("date time of close of new event"),
fieldWithPath("beginEventDateTime").description("date time of begin of new event"),
fieldWithPath("endEventDateTime").description("date time of end of new event"),
fieldWithPath("location").description("location of new event"),
fieldWithPath("basePrice").description("base price of new event"),
fieldWithPath("maxPrice").description("max price of new event"),
fieldWithPath("limitOfEnrollment").description("limit of enrollment"),
fieldWithPath("free").description("it tells if this event is free or not"),
fieldWithPath("offline").description("it tells if this event is offline event or not"),
fieldWithPath("eventStatus").description("event status"),
/* _link관련된 것들은 없앨 방법을 찾는 것도 추천? */
fieldWithPath("_links.self.href").description("link to self"),
fieldWithPath("_links.query-events.href").description("link to query event list"),
fieldWithPath("_links.update-event.href").description("link to update existing event"),
fieldWithPath("_links.profile.href").description("link to profile")
)
));
}
private String getBearerToken() throws Exception {
return "Bearer " + getAccessToken(true);
}
private String getBearerToken(boolean needToCreateAccount) throws Exception {
return "Bearer " + getAccessToken(needToCreateAccount);
}
private String getAccessToken(boolean needToCreateAccount) throws Exception {
// given
if(needToCreateAccount) {
createAccount();
}
Account bruce = Account.builder()
.email(appProperties.getUserUsername())
.password(appProperties.getUserPassword())
.roles(Set.of(AccountRole.ADMIN, AccountRole.USER))
.build();
this.accountService.saveAccount(bruce);
// 인증 서버가 등록되면 /oauth/token이라는 요청을 처리할 수 있는 핸들러가 적용됨
ResultActions perform = this.mockMvc.perform(post("/oauth/token")
.with(httpBasic(appProperties.getClientId(), appProperties.getClientSecret()))
.param("username", appProperties.getUserUsername())
.param("password", appProperties.getUserPassword())
.param("grant_type", "password"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("access_token").exists());
var responseBody = perform.andReturn().getResponse().getContentAsString();
Jackson2JsonParser parser = new Jackson2JsonParser();
return parser.parseMap(responseBody).get("access_token").toString();
}
private Account createAccount() {
Account keesun = Account.builder()
.email(appProperties.getUserUsername())
.password(appProperties.getUserPassword())
.roles(Set.of(AccountRole.ADMIN, AccountRole.USER))
.build();
return this.accountService.saveAccount(keesun);
}
@Test
@DisplayName("입력 받을 수 없는 값을 사용한 경우에 에러가 발생하는 테스트")
public void createEvent_Bad_Request() throws Exception {
Event event = Event.builder()
.id(100)
.name("Spring")
.description("REST API Development with Spring")
.beginEnrollmentDateTime(LocalDateTime.of(2022, 11, 5, 11, 47))
.closeEnrollmentDateTime(LocalDateTime.of(2022, 11, 5, 11, 50))
.beginEventDateTime(LocalDateTime.of(2022, 11, 6, 9, 18))
.endEventDateTime(LocalDateTime.of(2022, 11, 7, 4, 23))
.basePrice(100)
.maxPrice(200)
.limitOfEnrollment(100)
.location("hong-dae")
.free(true)
.offline(false)
.eventStatus(EventStatus.PUBLISHED)
.build();
// // eventRepository.save(event)가 호출되면 event를 리턴하라
// Mockito.when(eventRepository.save(event)).thenReturn(event);
mockMvc.perform(post("/api/events/")
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
.contentType(MediaType.APPLICATION_JSON_VALUE) // APPLICATION_JSON_UTF8
.accept(MediaTypes.HAL_JSON) // 이 스펙이 준하는 응답을 보내겠다
.content(objectMapper.writeValueAsString(event))) // bean으로 등록 안 했지만 있는 것처럼 쓸 수 있음
.andDo(print()) // 실제 콘솔에서 어떤 요청과 응답을 받았는지 확인 가능
.andExpect(status().isBadRequest());
}
@Test
@DisplayName("입력 값이 비어있는 경우에 에러가 발생하는 테스트")
public void createEvent_Bad_Request_Empty_Input() throws Exception {
EventDto eventDto = EventDto.builder().build();
this.mockMvc.perform(post("/api/events")
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(this.objectMapper.writeValueAsString(eventDto)))
.andExpect(status().isBadRequest());
}
@Test
@DisplayName("입력 값이 잘못된 경우에 에러가 발생하는 테스트")
public void createEvent_Bad_Request_Wrong_Input() throws Exception {
EventDto eventDto = EventDto.builder()
.name("Spring")
.description("REST API Development with Spring")
.beginEnrollmentDateTime(LocalDateTime.of(2022, 11, 3, 11, 47))
.closeEnrollmentDateTime(LocalDateTime.of(2022, 11, 4, 11, 50))
.beginEventDateTime(LocalDateTime.of(2022, 11, 5, 9, 18))
.endEventDateTime(LocalDateTime.of(2022, 11, 6, 4, 23))
.basePrice(10000)
.maxPrice(200)
.limitOfEnrollment(100)
.location("hong-dae")
.build();
this.mockMvc.perform(post("/api/events/")
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(this.objectMapper.writeValueAsString(eventDto)))
.andDo(print())
.andExpect(status().isBadRequest())
.andExpect(jsonPath("errors[0].objectName").exists())
// .andExpect(jsonPath("content[0].field").exists()) // 어떤 필드에서 발생한 에러인지
.andExpect(jsonPath("errors[0].defaultMessage").exists()) // 기본 메시지는 무엇이고
.andExpect(jsonPath("errors[0].code").exists()) // 에러 코드는 무엇이었는지
// .andExpect(jsonPath("content[0].rejectedValue").exists()) // 입력을 거절당한 그 값이 무엇인지
.andExpect(jsonPath("_links.index").exists());
}
@Test
@DisplayName("30개의 이벤트를 10개씩 두번째 페이지 조회하기")
public void queryEventsWithAuthentication() throws Exception {
// given
IntStream.range(0, 30).forEach(this::generateEvent);
// when & then
this.mockMvc.perform(get("/api/events")
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
.param("page", "1")
.param("size", "10")
.param("sort", "name,DESC"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("page").exists())
.andExpect(jsonPath("_embedded.eventList[0]._links.self").exists())
.andExpect(jsonPath("_links.self").exists())
.andExpect(jsonPath("_links.profile").exists())
.andExpect(jsonPath("_links.create-event").exists())
.andDo(document("query-events"));
}
@Test
@DisplayName("기존의 이벤트를 하나 조회하기")
public void getEvent() throws Exception {
// given
Account account = this.createAccount();
Event event = this.generateEvent(100, account);
// when & then
this.mockMvc.perform(get("/api/events/{id}", event.getId()))
.andExpect(status().isOk())
.andExpect(jsonPath("name").exists())
.andExpect(jsonPath("id").exists())
.andExpect(jsonPath("_links.self").exists())
.andExpect(jsonPath("_links.profile").exists())
.andDo(document("get-an-event"));
}
@Test
@DisplayName("없는 이벤트를 조회했을 때 404 응답받기")
public void getEvent404() throws Exception {
// when & then
this.mockMvc.perform(get("/api/events/11883"))
.andExpect(status().isNotFound());
}
@Test
@DisplayName("이벤트를 정상적으로 수정하기")
public void updateEvent() throws Exception {
// given
Account account = this.createAccount();
Event event = this.generateEvent(200, account);
EventDto eventDto = this.modelMapper.map(event, EventDto.class);
String eventName = "Updated Event";
eventDto.setName(eventName);
// when & then
this.mockMvc.perform(put("/api/events/{id}", event.getId())
.header(HttpHeaders.AUTHORIZATION, getBearerToken(false))
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(this.objectMapper.writeValueAsString(eventDto)))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("name").value(eventName))
.andExpect(jsonPath("_links.self").exists())
.andDo(document("update-event"));
}
@Test
@DisplayName("입력값이 잘못된 경우에 이벤트 수정 실패")
public void updateEvent_Bad_Request() throws Exception {
// given
Event event = this.generateEvent(200);
event.setBasePrice(30000);
event.setMaxPrice(150);
EventDto eventDto = this.modelMapper.map(event, EventDto.class);
// when & then
this.mockMvc.perform(put("/api/events/{id}", event.getId())
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(this.objectMapper.writeValueAsString(eventDto)))
.andDo(print())
.andExpect(status().isBadRequest());
}
@Test
@DisplayName("존재하지 않는 이벤트 이벤트 수정 실패")
public void updateEvent_Not_Found() throws Exception {
// given
Event event = this.generateEvent(200);
EventDto eventDto = this.modelMapper.map(event, EventDto.class);
// when & then
this.mockMvc.perform(put("/api/events/112343")
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
.contentType(MediaType.APPLICATION_JSON)
.content(this.objectMapper.writeValueAsString(eventDto)))
.andDo(print())
.andExpect(status().isNotFound());
}
@Test
@DisplayName("입력값이 비어있는 경우에 이벤트 수정 실패")
public void updateEvent_Bad_Request_Empty_Input() throws Exception {
// given
Event event = this.generateEvent(200);
EventDto eventDto = new EventDto();
// when & then
this.mockMvc.perform(put("/api/events/{id}", event.getId())
.header(HttpHeaders.AUTHORIZATION, getBearerToken(true))
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(this.objectMapper.writeValueAsString(eventDto)))
.andDo(print())
.andExpect(status().isBadRequest());
}
private Event generateEvent(int index, Account account) {
Event event = buildEvent(index);
event.setManager(account);
return this.eventRepository.save(event);
}
private Event generateEvent(int index) {
Event event = buildEvent(index);
return this.eventRepository.save(event);
}
private Event buildEvent(int index) {
return Event.builder()
.name("event " + index)
.description("test event")
.beginEnrollmentDateTime(LocalDateTime.of(2022, 11, 4, 11, 47))
.closeEnrollmentDateTime(LocalDateTime.of(2022, 11, 5, 11, 50))
.beginEventDateTime(LocalDateTime.of(2022, 11, 6, 9, 18))
.endEventDateTime(LocalDateTime.of(2022, 11, 7, 4, 23))
.basePrice(100)
.maxPrice(200)
.limitOfEnrollment(100)
.location("hong-dae")
.free(false)
.offline(true)
.eventStatus(EventStatus.DRAFT)
.build();
}
}