Skip to content

Commit 68a7798

Browse files
committed
[Feat]: 투표 관련 테스트 추가
1 parent cb1c0e9 commit 68a7798

File tree

2 files changed

+225
-4
lines changed

2 files changed

+225
-4
lines changed
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
package com.back.domain.poll.controller;
2+
3+
import com.back.domain.poll.entity.PollVote;
4+
import com.back.domain.poll.repository.PollVoteRepository;
5+
import com.back.domain.post.entity.Post;
6+
import com.back.domain.post.enums.PostCategory;
7+
import com.back.domain.post.repository.PostRepository;
8+
import com.back.domain.user.entity.Gender;
9+
import com.back.domain.user.entity.Mbti;
10+
import com.back.domain.user.entity.Role;
11+
import com.back.domain.user.entity.User;
12+
import com.back.domain.user.repository.UserRepository;
13+
import com.back.global.security.CustomUserDetails;
14+
import org.junit.jupiter.api.BeforeEach;
15+
import org.junit.jupiter.api.DisplayName;
16+
import org.junit.jupiter.api.Nested;
17+
import org.junit.jupiter.api.Test;
18+
import org.springframework.beans.factory.annotation.Autowired;
19+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
20+
import org.springframework.boot.test.context.SpringBootTest;
21+
import org.springframework.http.MediaType;
22+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
23+
import org.springframework.security.core.context.SecurityContextHolder;
24+
import org.springframework.test.context.jdbc.Sql;
25+
import org.springframework.test.context.jdbc.SqlConfig;
26+
import org.springframework.test.web.servlet.MockMvc;
27+
28+
import java.time.LocalDateTime;
29+
import java.util.List;
30+
import java.util.UUID;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
34+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
35+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
36+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
37+
38+
@SpringBootTest
39+
@AutoConfigureMockMvc(addFilters = false)
40+
@SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED)
41+
@Sql(
42+
statements = {
43+
"SET REFERENTIAL_INTEGRITY FALSE",
44+
"TRUNCATE TABLE POLL_VOTES",
45+
"TRUNCATE TABLE COMMENTS",
46+
"TRUNCATE TABLE POST",
47+
"TRUNCATE TABLE USERS",
48+
"ALTER TABLE POLL_VOTES ALTER COLUMN ID RESTART WITH 1",
49+
"ALTER TABLE COMMENTS ALTER COLUMN ID RESTART WITH 1",
50+
"ALTER TABLE POST ALTER COLUMN ID RESTART WITH 1",
51+
"ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 1",
52+
"SET REFERENTIAL_INTEGRITY TRUE"
53+
},
54+
executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD
55+
)
56+
class PollVoteControllerIntegrationTest {
57+
58+
@Autowired
59+
private MockMvc mockMvc;
60+
61+
@Autowired
62+
private UserRepository userRepository;
63+
64+
@Autowired
65+
private PostRepository postRepository;
66+
67+
@Autowired
68+
private PollVoteRepository pollVoteRepository;
69+
70+
private User testUser;
71+
private Post pollPost;
72+
73+
@BeforeEach
74+
void setUp() {
75+
String uid1 = UUID.randomUUID().toString().substring(0, 5);
76+
77+
testUser = userRepository.save(User.builder()
78+
.email("testuser" + uid1 + "@example.com")
79+
.nickname("nickname" + uid1)
80+
.username("tester" + uid1)
81+
.password("password")
82+
.gender(Gender.M)
83+
.role(Role.USER)
84+
.mbti(Mbti.ISFJ)
85+
.birthdayAt(LocalDateTime.of(2000, 1, 1, 0, 0))
86+
.build());
87+
88+
userRepository.save(testUser);
89+
90+
String voteContent = """
91+
{
92+
"pollUid": "11111111-1111-1111-1111-111111111111",
93+
"options": [
94+
{"index":1,"text":"첫 번째 옵션"},
95+
{"index":2,"text":"두 번째 옵션"},
96+
{"index":3,"text":"세 번째 옵션"}
97+
]
98+
}
99+
""";
100+
101+
pollPost = postRepository.save(Post.builder()
102+
.title("테스트 투표 게시글")
103+
.content("내용")
104+
.category(PostCategory.POLL)
105+
.user(testUser)
106+
.voteContent(voteContent)
107+
.build());
108+
109+
CustomUserDetails cs = new CustomUserDetails(testUser);
110+
SecurityContextHolder.getContext().setAuthentication(
111+
new UsernamePasswordAuthenticationToken(cs, null, cs.getAuthorities())
112+
);
113+
}
114+
115+
@Nested
116+
@DisplayName("투표 참여")
117+
class participateVote {
118+
119+
@Test
120+
@DisplayName("성공 - 투표하기")
121+
void vote_post_success() throws Exception {
122+
String requestJson = """
123+
{
124+
"choice": [1, 2]
125+
}
126+
""";
127+
128+
mockMvc.perform(post("/api/v1/posts/{postId}/polls", pollPost.getId())
129+
.contentType(MediaType.APPLICATION_JSON)
130+
.content(requestJson))
131+
.andExpect(status().isOk());
132+
133+
List<PollVote> votes = pollVoteRepository.findByPostId(pollPost.getId());
134+
assertThat(votes).hasSize(1);
135+
assertThat(votes.getFirst().getUser().getId()).isEqualTo(testUser.getId());
136+
}
137+
138+
@Test
139+
@DisplayName("실패 - 중복 투표 방지")
140+
void vote_post_duplicate_fail() throws Exception {
141+
String firstRequest = """
142+
{
143+
"choice": [1, 2]
144+
}
145+
""";
146+
147+
mockMvc.perform(post("/api/v1/posts/{postId}/polls", pollPost.getId())
148+
.contentType(MediaType.APPLICATION_JSON)
149+
.content(firstRequest))
150+
.andExpect(status().isOk());
151+
152+
String secondRequest = """
153+
{
154+
"choice": [1]
155+
}
156+
""";
157+
158+
mockMvc.perform(post("/api/v1/posts/{postId}/polls", pollPost.getId())
159+
.contentType(MediaType.APPLICATION_JSON)
160+
.content(secondRequest))
161+
.andExpect(status().isBadRequest());
162+
163+
List<PollVote> votes = pollVoteRepository.findByPostId(pollPost.getId());
164+
assertThat(votes).hasSize(1);
165+
}
166+
}
167+
168+
@Nested
169+
@DisplayName("투표 조회")
170+
class getVote {
171+
172+
@Test
173+
@DisplayName("성공 - 여러 사용자가 투표한 후 집계 결과 조회")
174+
void get_vote_with_multiple_participants_success() throws Exception {
175+
// given: 첫 번째 사용자 투표
176+
String firstVote = """
177+
{
178+
"choice": [1]
179+
}
180+
""";
181+
182+
mockMvc.perform(post("/api/v1/posts/{postId}/polls", pollPost.getId())
183+
.contentType(MediaType.APPLICATION_JSON)
184+
.content(firstVote))
185+
.andExpect(status().isOk());
186+
187+
// given: 두 번째 사용자 생성 및 투표
188+
String uid2 = UUID.randomUUID().toString().substring(0, 5);
189+
User secondUser = userRepository.save(User.builder()
190+
.email("testuser" + uid2 + "@example.com")
191+
.nickname("nickname" + uid2)
192+
.username("tester" + uid2)
193+
.password("password")
194+
.gender(Gender.M)
195+
.role(Role.USER)
196+
.mbti(Mbti.ISFJ)
197+
.birthdayAt(LocalDateTime.of(2000, 1, 1, 0, 0))
198+
.build());
199+
200+
CustomUserDetails secondUserDetails = new CustomUserDetails(secondUser);
201+
SecurityContextHolder.getContext().setAuthentication(
202+
new UsernamePasswordAuthenticationToken(secondUserDetails, null, secondUserDetails.getAuthorities())
203+
);
204+
205+
String secondVote = """
206+
{
207+
"choice": [1, 2]
208+
}
209+
""";
210+
211+
mockMvc.perform(post("/api/v1/posts/{postId}/polls", pollPost.getId())
212+
.contentType(MediaType.APPLICATION_JSON)
213+
.content(secondVote))
214+
.andExpect(status().isOk());
215+
216+
// when & then: 집계된 투표 결과 조회
217+
mockMvc.perform(get("/api/v1/posts/{postId}/polls", pollPost.getId())
218+
.contentType(MediaType.APPLICATION_JSON))
219+
.andExpect(status().isOk())
220+
.andExpect(jsonPath("$.options[0].voteCount").value(2))
221+
.andExpect(jsonPath("$.options[1].voteCount").value(1))
222+
.andExpect(jsonPath("$.options[2].voteCount").value(0));
223+
}
224+
}
225+
}

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

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

3-
import com.back.domain.poll.dto.PollRequest;
43
import com.back.domain.post.dto.PostRequest;
54
import com.back.domain.post.entity.Post;
65
import com.back.domain.post.enums.PostCategory;
@@ -31,12 +30,9 @@
3130
import org.springframework.test.web.servlet.MockMvc;
3231

3332
import java.time.LocalDateTime;
34-
import java.util.ArrayList;
35-
import java.util.List;
3633
import java.util.UUID;
3734

3835
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
39-
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
4036
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
4137
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
4238

0 commit comments

Comments
 (0)