-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoteApiIntegrationTest.java
More file actions
316 lines (269 loc) · 12.5 KB
/
VoteApiIntegrationTest.java
File metadata and controls
316 lines (269 loc) · 12.5 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
package com.ject.vs.vote.adapter.web;
import com.ject.vs.user.domain.User;
import com.ject.vs.user.domain.UserRepository;
import com.ject.vs.vote.domain.Vote;
import com.ject.vs.vote.domain.VoteOption;
import com.ject.vs.vote.domain.VoteOptionRepository;
import com.ject.vs.vote.domain.VoteRepository;
import com.ject.vs.vote.domain.VoteStatus;
import com.ject.vs.vote.domain.VoteType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
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.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
@Transactional
@DisplayName("Vote API 통합 테스트")
class VoteApiIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private VoteRepository voteRepository;
@Autowired
private VoteOptionRepository voteOptionRepository;
@Autowired
private UserRepository userRepository;
private User testUser;
@BeforeEach
void setUp() {
// 테스트용 사용자 생성
testUser = userRepository.save(User.createWithSub("test-sub-" + System.currentTimeMillis()));
}
private void authenticateAs(Long userId) {
UsernamePasswordAuthenticationToken authentication =
new UsernamePasswordAuthenticationToken(userId, null, AuthorityUtils.NO_AUTHORITIES);
SecurityContextHolder.getContext().setAuthentication(authentication);
}
private void clearAuthentication() {
SecurityContextHolder.clearContext();
}
@Nested
@DisplayName("POST /api/votes - 투표 생성")
class CreateVote {
@Test
@DisplayName("인증된 사용자가 투표 생성 시 DB에 저장된다")
void 인증된_사용자가_투표_생성시_DB에_저장된다() throws Exception {
// given
authenticateAs(testUser.getId());
String requestBody = """
{
"type": "GENERAL",
"title": "테스트 투표 제목",
"content": "테스트 투표 내용",
"thumbnailUrl": "https://example.com/thumb.png",
"duration": "HOURS_12",
"optionA": "선택지 A",
"optionB": "선택지 B"
}
""";
// when
MvcResult result = mockMvc.perform(post("/api/votes")
.contentType(MediaType.APPLICATION_JSON)
.content(requestBody))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.voteId").isNumber())
.andExpect(jsonPath("$.status").value("ONGOING"))
.andExpect(jsonPath("$.endAt").isNotEmpty())
.andReturn();
// then - DB에 저장 확인
List<Vote> votes = voteRepository.findAll();
assertThat(votes).hasSize(1);
Vote savedVote = votes.get(0);
assertThat(savedVote.getType()).isEqualTo(VoteType.GENERAL);
assertThat(savedVote.getTitle()).isEqualTo("테스트 투표 제목");
assertThat(savedVote.getContent()).isEqualTo("테스트 투표 내용");
assertThat(savedVote.getThumbnailUrl()).isEqualTo("https://example.com/thumb.png");
assertThat(savedVote.getEndAt()).isNotNull();
// VoteOption도 저장 확인
List<VoteOption> options = voteOptionRepository.findByVoteIdOrderByPosition(savedVote.getId());
assertThat(options).hasSize(2);
assertThat(options.get(0).getLabel()).isEqualTo("선택지 A");
assertThat(options.get(1).getLabel()).isEqualTo("선택지 B");
}
@Test
@DisplayName("몰입형 투표 생성 시 imageUrl도 함께 저장된다")
void 몰입형_투표_생성시_imageUrl도_저장된다() throws Exception {
// given
authenticateAs(testUser.getId());
String requestBody = """
{
"type": "IMMERSIVE",
"title": "몰입형 투표",
"content": "몰입형 내용",
"thumbnailUrl": "https://example.com/thumb.png",
"imageUrl": "https://example.com/image.png",
"duration": "HOURS_24",
"optionA": "A",
"optionB": "B"
}
""";
// when
mockMvc.perform(post("/api/votes")
.contentType(MediaType.APPLICATION_JSON)
.content(requestBody))
.andExpect(status().isCreated());
// then
Vote savedVote = voteRepository.findAll().get(0);
assertThat(savedVote.getType()).isEqualTo(VoteType.IMMERSIVE);
assertThat(savedVote.getImageUrl()).isEqualTo("https://example.com/image.png");
}
@Test
@DisplayName("인증되지 않은 사용자는 403 Forbidden")
void 인증되지_않은_사용자는_403() throws Exception {
// given
clearAuthentication();
String requestBody = """
{
"type": "GENERAL",
"title": "테스트",
"thumbnailUrl": "https://example.com/thumb.png",
"duration": "HOURS_12",
"optionA": "A",
"optionB": "B"
}
""";
// when & then
mockMvc.perform(post("/api/votes")
.contentType(MediaType.APPLICATION_JSON)
.content(requestBody))
.andExpect(status().isForbidden());
// DB에 저장되지 않음
assertThat(voteRepository.findAll()).isEmpty();
}
@Test
@DisplayName("다양한 duration으로 투표 생성 시 endAt이 정확히 계산된다")
void 다양한_duration으로_투표_생성시_endAt_계산_확인() throws Exception {
// given
authenticateAs(testUser.getId());
String[] durations = {"HOURS_1", "HOURS_6", "HOURS_12", "HOURS_24"};
for (String duration : durations) {
String requestBody = String.format("""
{
"type": "GENERAL",
"title": "투표 %s",
"thumbnailUrl": "https://example.com/thumb.png",
"duration": "%s",
"optionA": "A",
"optionB": "B"
}
""", duration, duration);
// when
mockMvc.perform(post("/api/votes")
.contentType(MediaType.APPLICATION_JSON)
.content(requestBody))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.endAt").isNotEmpty());
}
// then
List<Vote> votes = voteRepository.findAll();
assertThat(votes).hasSize(4);
// 모든 투표의 endAt이 설정되어 있는지 확인
for (Vote vote : votes) {
assertThat(vote.getEndAt()).isNotNull();
}
}
}
@Nested
@DisplayName("GET /api/votes/{voteId} - 투표 상세 조회")
class GetVoteDetail {
@Test
@DisplayName("투표 상세 조회 시 endAt이 응답에 포함된다")
void 투표_상세_조회시_endAt_포함() throws Exception {
// given - 투표 생성
authenticateAs(testUser.getId());
String createRequest = """
{
"type": "GENERAL",
"title": "상세 조회 테스트",
"content": "내용",
"thumbnailUrl": "https://example.com/thumb.png",
"duration": "HOURS_12",
"optionA": "A",
"optionB": "B"
}
""";
MvcResult createResult = mockMvc.perform(post("/api/votes")
.contentType(MediaType.APPLICATION_JSON)
.content(createRequest))
.andExpect(status().isCreated())
.andReturn();
// voteId 추출
String responseBody = createResult.getResponse().getContentAsString();
Long voteId = voteRepository.findAll().get(0).getId();
// when & then - 상세 조회
mockMvc.perform(get("/api/votes/{voteId}", voteId))
.andExpect(status().isOk())
.andExpect(jsonPath("$.voteId").value(voteId))
.andExpect(jsonPath("$.title").value("상세 조회 테스트"))
.andExpect(jsonPath("$.endAt").isNotEmpty())
.andExpect(jsonPath("$.status").value("ONGOING"));
}
}
@Nested
@DisplayName("전체 플로우 테스트")
class FullFlowTest {
@Test
@DisplayName("투표 생성 → 조회 → DB 저장 전체 플로우")
void 투표_생성_조회_DB_저장_전체_플로우() throws Exception {
// 1. 투표 생성
authenticateAs(testUser.getId());
String createRequest = """
{
"type": "GENERAL",
"title": "전체 플로우 테스트",
"content": "테스트 내용입니다",
"thumbnailUrl": "https://example.com/thumb.png",
"duration": "HOURS_24",
"optionA": "찬성",
"optionB": "반대"
}
""";
mockMvc.perform(post("/api/votes")
.contentType(MediaType.APPLICATION_JSON)
.content(createRequest))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.voteId").isNumber())
.andExpect(jsonPath("$.status").value("ONGOING"))
.andExpect(jsonPath("$.endAt").isNotEmpty());
// 2. DB 저장 확인
List<Vote> votes = voteRepository.findAll();
assertThat(votes).hasSize(1);
Vote savedVote = votes.get(0);
assertThat(savedVote.getTitle()).isEqualTo("전체 플로우 테스트");
assertThat(savedVote.getContent()).isEqualTo("테스트 내용입니다");
assertThat(savedVote.getEndAt()).isNotNull();
assertThat(savedVote.getStatus()).isEqualTo(VoteStatus.ONGOING);
// 3. VoteOption 저장 확인
List<VoteOption> options = voteOptionRepository.findByVoteIdOrderByPosition(savedVote.getId());
assertThat(options).hasSize(2);
assertThat(options.get(0).getLabel()).isEqualTo("찬성");
assertThat(options.get(1).getLabel()).isEqualTo("반대");
// 4. 투표 상세 조회
mockMvc.perform(get("/api/votes/{voteId}", savedVote.getId()))
.andExpect(status().isOk())
.andExpect(jsonPath("$.title").value("전체 플로우 테스트"))
.andExpect(jsonPath("$.endAt").isNotEmpty())
.andExpect(jsonPath("$.options[0].label").value("찬성"))
.andExpect(jsonPath("$.options[1].label").value("반대"));
}
}
}