Skip to content

Commit 912d153

Browse files
committed
[Feat]: 투표 게시글 생성 테스트 구현
1 parent 1d50d98 commit 912d153

File tree

1 file changed

+65
-4
lines changed

1 file changed

+65
-4
lines changed

back/src/test/java/com/back/domain/post/controller/PostControllerTest.java

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.back.domain.post.controller;
22

3+
import com.back.domain.poll.dto.PollRequest;
34
import com.back.domain.post.dto.PostRequest;
45
import com.back.domain.post.entity.Post;
56
import com.back.domain.post.enums.PostCategory;
@@ -30,9 +31,12 @@
3031
import org.springframework.test.web.servlet.MockMvc;
3132

3233
import java.time.LocalDateTime;
34+
import java.util.ArrayList;
35+
import java.util.List;
3336
import java.util.UUID;
3437

3538
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
39+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
3640
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
3741
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3842

@@ -113,7 +117,7 @@ class CreatePost {
113117
@Test
114118
@DisplayName("성공 - 정상 요청")
115119
void success() throws Exception {
116-
PostRequest request = new PostRequest("테스트 게시글", "테스트 내용입니다.", PostCategory.CHAT, false);
120+
PostRequest request = new PostRequest("테스트 게시글", "테스트 내용입니다.", PostCategory.CHAT, false, null);
117121

118122
mockMvc.perform(post("/api/v1/posts")
119123
.contentType(MediaType.APPLICATION_JSON)
@@ -127,7 +131,7 @@ void success() throws Exception {
127131
@Test
128132
@DisplayName("실패 - 유효성 검사 실패 (빈 제목)")
129133
void failEmptyTitle() throws Exception {
130-
PostRequest request = new PostRequest("", "내용", PostCategory.SCENARIO, false);
134+
PostRequest request = new PostRequest("", "내용", PostCategory.SCENARIO, false, null);
131135

132136
mockMvc.perform(post("/api/v1/posts")
133137
.contentType(MediaType.APPLICATION_JSON)
@@ -136,6 +140,36 @@ void failEmptyTitle() throws Exception {
136140
.andExpect(jsonPath("$.code").value(ErrorCode.INVALID_INPUT_VALUE.getCode()))
137141
.andExpect(jsonPath("$.message").exists());
138142
}
143+
144+
@Test
145+
@DisplayName("성공 - 투표 게시글 생성")
146+
void successPollPost() throws Exception {
147+
String requestJson = """
148+
{
149+
"title": "글 제목",
150+
"content": "글 내용 123456789",
151+
"category": "POLL",
152+
"poll": {
153+
"options": [
154+
{ "index": 1, "text": "첫 번째 옵션" },
155+
{ "index": 2, "text": "두 번째 옵션" },
156+
{ "index": 3, "text": "세 번째 옵션" }
157+
]
158+
}
159+
}
160+
""";
161+
162+
mockMvc.perform(post("/api/v1/posts")
163+
.contentType(MediaType.APPLICATION_JSON)
164+
.content(requestJson))
165+
.andExpect(status().isCreated())
166+
.andExpect(jsonPath("$.postId").exists())
167+
.andExpect(jsonPath("$.title").value("글 제목"))
168+
.andExpect(jsonPath("$.content").value("글 내용 123456789"))
169+
.andExpect(jsonPath("$.author").exists())
170+
.andExpect(jsonPath("$.category").value("POLL"))
171+
.andExpect(jsonPath("$.polls.options.size()").value(3));
172+
}
139173
}
140174

141175
@Nested
@@ -164,6 +198,33 @@ void success() throws Exception {
164198
.andExpect(jsonPath("$.category").value("CHAT"));
165199
}
166200

201+
@Test
202+
@DisplayName("성공 - 투표 게시글 조회")
203+
void successPollPost() throws Exception {
204+
Post pollPost = Post.builder()
205+
.title("투표 게시글")
206+
.content("투표 내용입니다.")
207+
.category(PostCategory.POLL)
208+
.voteContent("""
209+
{
210+
"options": [
211+
{"index": 1, "text": "첫 번째 옵션"},
212+
{"index": 2, "text": "두 번째 옵션"}
213+
]
214+
}
215+
""")
216+
.user(testUser)
217+
.build();
218+
postRepository.save(pollPost);
219+
220+
mockMvc.perform(get("/api/v1/posts/{postId}", pollPost.getId()))
221+
.andExpect(status().isOk())
222+
.andExpect(jsonPath("$.title").value("투표 게시글"))
223+
.andExpect(jsonPath("$.content").value("투표 내용입니다."))
224+
.andExpect(jsonPath("$.category").value("POLL"))
225+
.andExpect(jsonPath("$.polls.options.length()").value(2));
226+
}
227+
167228
@Test
168229
@DisplayName("실패 - 존재하지 않는 게시글 ID")
169230
void failNotFound() throws Exception {
@@ -252,7 +313,7 @@ void createPost() {
252313
@Test
253314
@DisplayName("성공 - 본인 게시글 수정")
254315
void success() throws Exception {
255-
PostRequest updateRequest = new PostRequest("수정된 제목", "수정된 내용", PostCategory.CHAT, false);
316+
PostRequest updateRequest = new PostRequest("수정된 제목", "수정된 내용", PostCategory.CHAT, false, null);
256317

257318
mockMvc.perform(put("/api/v1/posts/{postId}", savedPost.getId())
258319
.contentType(MediaType.APPLICATION_JSON)
@@ -272,7 +333,7 @@ void failUnauthorizedUser() throws Exception {
272333
.build();
273334
postRepository.save(otherPost);
274335

275-
PostRequest updateRequest = new PostRequest("수정 시도", "수정 시도 내용", PostCategory.CHAT, false);
336+
PostRequest updateRequest = new PostRequest("수정 시도", "수정 시도 내용", PostCategory.CHAT, false, null);
276337

277338
mockMvc.perform(put("/api/v1/posts/{postId}", otherPost.getId())
278339
.contentType(MediaType.APPLICATION_JSON)

0 commit comments

Comments
 (0)