-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestaurantIntegrationTest.java
More file actions
353 lines (300 loc) · 13.4 KB
/
RestaurantIntegrationTest.java
File metadata and controls
353 lines (300 loc) · 13.4 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
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
package com.sparta.test.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.*;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class RestaurantIntegrationTest {
@Autowired
private TestRestTemplate restTemplate;
private HttpHeaders headers;
private ObjectMapper mapper = new ObjectMapper();
private final List<RestaurantDto> registeredRestaurants = new ArrayList<>();
@BeforeEach
public void setup() {
headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
}
@Nested
@DisplayName("음식점 3개 등록 및 조회")
class RegisterRestaurants {
@Test
@Order(1)
@DisplayName("음식점1 등록")
void test1() throws JsonProcessingException {
// given
RestaurantDto restaurantRequest = RestaurantDto.builder()
.id(null)
.name("쉐이크쉑 청담점")
.minOrderPrice(1_000)
.deliveryFee(0)
.build();
String requestBody = mapper.writeValueAsString(restaurantRequest);
HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
// when
ResponseEntity<RestaurantDto> response = restTemplate.postForEntity(
"/restaurant/register",
request,
RestaurantDto.class);
// then
assertEquals(HttpStatus.OK, response.getStatusCode());
RestaurantDto restaurantResponse = response.getBody();
assertNotNull(restaurantResponse);
assertTrue(restaurantResponse.id > 0);
assertEquals(restaurantRequest.name, restaurantResponse.name);
assertEquals(restaurantRequest.minOrderPrice, restaurantResponse.minOrderPrice);
assertEquals(restaurantRequest.deliveryFee, restaurantResponse.deliveryFee);
// 음식점 등록 성공 시, registeredRestaurants 에 추가
registeredRestaurants.add(restaurantResponse);
}
@Test
@Order(2)
@DisplayName("음식점2 등록")
void test2() throws JsonProcessingException {
// given
RestaurantDto restaurantRequest = RestaurantDto.builder()
.id(null)
.name("자담치킨 강남점")
.minOrderPrice(100_000)
.deliveryFee(10_000)
.build();
String requestBody = mapper.writeValueAsString(restaurantRequest);
HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
// when
ResponseEntity<RestaurantDto> response = restTemplate.postForEntity(
"/restaurant/register",
request,
RestaurantDto.class);
// then
assertEquals(HttpStatus.OK, response.getStatusCode());
RestaurantDto restaurantResponse = response.getBody();
assertNotNull(restaurantResponse);
assertTrue(restaurantResponse.id > 0);
assertEquals(restaurantRequest.name, restaurantResponse.name);
assertEquals(restaurantRequest.minOrderPrice, restaurantResponse.minOrderPrice);
assertEquals(restaurantRequest.deliveryFee, restaurantResponse.deliveryFee);
// 음식점 등록 성공 시, registeredRestaurants 에 추가
registeredRestaurants.add(restaurantResponse);
}
@Test
@Order(3)
@DisplayName("음식점3 등록")
void test3() throws JsonProcessingException {
// given
RestaurantDto restaurantRequest = RestaurantDto.builder()
.id(null)
.name("마라하오 위례점")
.minOrderPrice(100000)
.deliveryFee(10000)
.build();
String requestBody = mapper.writeValueAsString(restaurantRequest);
HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
// when
ResponseEntity<RestaurantDto> response = restTemplate.postForEntity(
"/restaurant/register",
request,
RestaurantDto.class);
// then
assertEquals(HttpStatus.OK, response.getStatusCode());
RestaurantDto restaurantResponse = response.getBody();
assertNotNull(restaurantResponse);
assertTrue(restaurantResponse.id > 0);
assertEquals(restaurantRequest.name, restaurantResponse.name);
assertEquals(restaurantRequest.minOrderPrice, restaurantResponse.minOrderPrice);
assertEquals(restaurantRequest.deliveryFee, restaurantResponse.deliveryFee);
// 음식점 등록 성공 시, registeredRestaurants 에 추가
registeredRestaurants.add(restaurantResponse);
}
@Test
@Order(4)
@DisplayName("등록된 모든 음식점 조회")
void test4() {
// when
ResponseEntity<RestaurantDto[]> response = restTemplate.getForEntity(
"/restaurants",
RestaurantDto[].class
);
// then
assertEquals(HttpStatus.OK, response.getStatusCode());
RestaurantDto[] responseRestaurants = response.getBody();
assertNotNull(responseRestaurants);
assertEquals(registeredRestaurants.size(), responseRestaurants.length);
for (RestaurantDto responseRestaurant : responseRestaurants) {
RestaurantDto registerRestaurant = registeredRestaurants.stream()
.filter(restaurant -> responseRestaurant.getId().equals(restaurant.getId()))
.findAny()
.orElse(null);
assertNotNull(registerRestaurant);
assertEquals(registerRestaurant.getName(), responseRestaurant.getName());
assertEquals(registerRestaurant.getDeliveryFee(), responseRestaurant.getDeliveryFee());
assertEquals(registerRestaurant.getMinOrderPrice(), responseRestaurant.getMinOrderPrice());
}
}
}
@Nested
@DisplayName("최소주문 가격")
class MinOrderPrice {
@Test
@DisplayName("1,000원 미만 에러")
void test1() throws JsonProcessingException {
// given
RestaurantDto restaurantRequest = RestaurantDto.builder()
.id(null)
.name("쉐이크쉑 청담점")
.minOrderPrice(500)
.deliveryFee(1000)
.build();
String requestBody = mapper.writeValueAsString(restaurantRequest);
HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
// when
ResponseEntity<RestaurantDto> response = restTemplate.postForEntity(
"/restaurant/register",
request,
RestaurantDto.class);
// then
assertTrue(
response.getStatusCode() == HttpStatus.BAD_REQUEST
|| response.getStatusCode() == HttpStatus.INTERNAL_SERVER_ERROR
);
}
@Test
@DisplayName("100,000원 초과 에러")
void test2() throws JsonProcessingException {
// given
RestaurantDto restaurantRequest = RestaurantDto.builder()
.id(null)
.name("쉐이크쉑 청담점")
.minOrderPrice(100100)
.deliveryFee(1000)
.build();
String requestBody = mapper.writeValueAsString(restaurantRequest);
HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
// when
ResponseEntity<RestaurantDto> response = restTemplate.postForEntity(
"/restaurant/register",
request,
RestaurantDto.class);
// then
assertTrue(
response.getStatusCode() == HttpStatus.BAD_REQUEST
|| response.getStatusCode() == HttpStatus.INTERNAL_SERVER_ERROR
);
}
@Test
@DisplayName("100원 단위로 입력 안 됨 에러")
void test3() throws JsonProcessingException {
// given
RestaurantDto restaurantRequest = RestaurantDto.builder()
.id(null)
.name("쉐이크쉑 청담점")
.minOrderPrice(2220)
.deliveryFee(1000)
.build();
String requestBody = mapper.writeValueAsString(restaurantRequest);
HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
// when
ResponseEntity<RestaurantDto> response = restTemplate.postForEntity(
"/restaurant/register",
request,
RestaurantDto.class);
// then
assertTrue(
response.getStatusCode() == HttpStatus.BAD_REQUEST
|| response.getStatusCode() == HttpStatus.INTERNAL_SERVER_ERROR
);
}
}
@Nested
@DisplayName("기본 배달비")
class DeliveryFee {
@Test
@DisplayName("0원 미만 에러")
void test2() throws JsonProcessingException {
// given
RestaurantDto restaurantRequest = RestaurantDto.builder()
.id(null)
.name("쉐이크쉑 청담점")
.minOrderPrice(5000)
.deliveryFee(-500)
.build();
String requestBody = mapper.writeValueAsString(restaurantRequest);
HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
// when
ResponseEntity<RestaurantDto> response = restTemplate.postForEntity(
"/restaurant/register",
request,
RestaurantDto.class);
// then
assertTrue(
response.getStatusCode() == HttpStatus.BAD_REQUEST
|| response.getStatusCode() == HttpStatus.INTERNAL_SERVER_ERROR
);
}
@Test
@DisplayName("10,000원 초과 에러")
void test3() throws JsonProcessingException {
// given
RestaurantDto restaurantRequest = RestaurantDto.builder()
.id(null)
.name("쉐이크쉑 청담점")
.minOrderPrice(5000)
.deliveryFee(20000)
.build();
String requestBody = mapper.writeValueAsString(restaurantRequest);
HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
// when
ResponseEntity<RestaurantDto> response = restTemplate.postForEntity(
"/restaurant/register",
request,
RestaurantDto.class);
// then
assertTrue(
response.getStatusCode() == HttpStatus.BAD_REQUEST
|| response.getStatusCode() == HttpStatus.INTERNAL_SERVER_ERROR
);
}
@Test
@DisplayName("500원 단위로 입력 안 됨 에러")
void test4() throws JsonProcessingException {
// given
RestaurantDto restaurantRequest = RestaurantDto.builder()
.id(null)
.name("쉐이크쉑 청담점")
.minOrderPrice(5000)
.deliveryFee(2200)
.build();
String requestBody = mapper.writeValueAsString(restaurantRequest);
HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
// when
ResponseEntity<RestaurantDto> response = restTemplate.postForEntity(
"/restaurant/register",
request,
RestaurantDto.class);
// then
assertTrue(
response.getStatusCode() == HttpStatus.BAD_REQUEST
|| response.getStatusCode() == HttpStatus.INTERNAL_SERVER_ERROR
);
}
}
@Getter
@Setter
@Builder
static class RestaurantDto {
private Long id;
private String name;
private int minOrderPrice;
private int deliveryFee;
}
}