-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostControllerTest.java
More file actions
238 lines (203 loc) · 10.6 KB
/
PostControllerTest.java
File metadata and controls
238 lines (203 loc) · 10.6 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
package com.devpick.domain.community.controller;
import com.devpick.domain.community.dto.PostCreateRequest;
import com.devpick.domain.community.dto.PostDetailResponse;
import com.devpick.domain.community.dto.PostListResponse;
import com.devpick.domain.community.dto.PostSummaryResponse;
import com.devpick.domain.community.dto.PostUpdateRequest;
import com.devpick.domain.community.service.CommunityLikeService;
import com.devpick.domain.community.service.PostService;
import com.devpick.domain.user.entity.Job;
import com.devpick.domain.user.entity.Level;
import com.devpick.global.common.exception.DevpickException;
import com.devpick.global.common.exception.ErrorCode;
import com.devpick.global.common.exception.GlobalExceptionHandler;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.time.Instant;
import java.util.List;
import java.util.UUID;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
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.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ExtendWith(MockitoExtension.class)
class PostControllerTest {
private MockMvc mockMvc;
private ObjectMapper objectMapper;
@Mock
private PostService postService;
@Mock
private CommunityLikeService communityLikeService;
@InjectMocks
private PostController postController;
private UUID userId;
private UUID postId;
private PostDetailResponse detailResponse;
@BeforeEach
void setUp() {
objectMapper = new ObjectMapper();
mockMvc = MockMvcBuilders
.standaloneSetup(postController)
.setControllerAdvice(new GlobalExceptionHandler())
.setCustomArgumentResolvers(new AuthenticationPrincipalArgumentResolver())
.build();
userId = UUID.randomUUID();
postId = UUID.randomUUID();
SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(
userId, null,
List.of(new SimpleGrantedAuthority("ROLE_USER")))
);
detailResponse = new PostDetailResponse(
postId, "Test Post", "Test Content", Level.JUNIOR,
userId, "tester", null, null, null, 2L,
Instant.now(), Instant.now(), List.of()
);
}
@AfterEach
void tearDown() {
SecurityContextHolder.clearContext();
}
@Test
@DisplayName("POST /posts - 게시글 작성 성공 시 201과 응답 반환")
void createPost_success_returns201() throws Exception {
PostCreateRequest request = new PostCreateRequest("Test Post", "Test Content", Level.JUNIOR);
given(postService.createPost(eq(userId), any())).willReturn(detailResponse);
mockMvc.perform(post("/posts")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.success").value(true))
.andExpect(jsonPath("$.data.title").value("Test Post"))
.andExpect(jsonPath("$.data.answerCount").value(2));
}
@Test
@DisplayName("POST /posts - 유효성 검사 실패 시 400 반환")
void createPost_validationFails_returns400() throws Exception {
PostCreateRequest request = new PostCreateRequest("", "content", Level.JUNIOR);
mockMvc.perform(post("/posts")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.success").value(false));
}
@Test
@DisplayName("GET /posts - 목록 조회 성공 시 200과 목록 반환")
void getPosts_success_returns200() throws Exception {
PostSummaryResponse summary = new PostSummaryResponse(
postId, "Test Post", Level.JUNIOR, userId, "tester", Job.BACKEND, null, Instant.now(), 2L, "content preview", null);
PostListResponse listResponse = new PostListResponse(List.of(summary), 0, 20, 1L, 1);
given(postService.getPosts(any(), any())).willReturn(listResponse);
mockMvc.perform(get("/posts"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.success").value(true))
.andExpect(jsonPath("$.data.posts[0].title").value("Test Post"))
.andExpect(jsonPath("$.data.posts[0].authorJob").value("BACKEND"))
.andExpect(jsonPath("$.data.totalElements").value(1));
}
@Test
@DisplayName("GET /posts - 목록 응답에 authorJob 필드 포함")
void getPosts_responseIncludesAuthorJob() throws Exception {
PostSummaryResponse summary = new PostSummaryResponse(
postId, "Test Post", Level.JUNIOR, userId, "tester", Job.FRONTEND, null, Instant.now(), 0L, "content preview", null);
PostListResponse listResponse = new PostListResponse(List.of(summary), 0, 20, 1L, 1);
given(postService.getPosts(any(), any())).willReturn(listResponse);
mockMvc.perform(get("/posts"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.posts[0].authorJob").value("FRONTEND"));
}
@Test
@DisplayName("GET /posts - authorJob이 null이어도 정상 반환")
void getPosts_nullAuthorJob_returns200() throws Exception {
PostSummaryResponse summary = new PostSummaryResponse(
postId, "Test Post", Level.JUNIOR, userId, "tester", null, null, Instant.now(), 0L, "content preview", null);
PostListResponse listResponse = new PostListResponse(List.of(summary), 0, 20, 1L, 1);
given(postService.getPosts(any(), any())).willReturn(listResponse);
mockMvc.perform(get("/posts"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.posts[0].authorJob").doesNotExist());
}
@Test
@DisplayName("GET /posts/{postId} - 상세 조회 성공 시 200과 상세 반환")
void getPostDetail_success_returns200() throws Exception {
given(postService.getPostDetail(postId)).willReturn(detailResponse);
mockMvc.perform(get("/posts/" + postId))
.andExpect(status().isOk())
.andExpect(jsonPath("$.success").value(true))
.andExpect(jsonPath("$.data.content").value("Test Content"))
.andExpect(jsonPath("$.data.authorNickname").value("tester"));
}
@Test
@DisplayName("GET /posts/{postId} - 게시글 없으면 404 반환")
void getPostDetail_notFound_returns404() throws Exception {
given(postService.getPostDetail(postId))
.willThrow(new DevpickException(ErrorCode.COMMUNITY_POST_NOT_FOUND));
mockMvc.perform(get("/posts/" + postId))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.success").value(false));
}
@Test
@DisplayName("PUT /posts/{postId} - 수정 성공 시 200 반환")
void updatePost_success_returns200() throws Exception {
PostUpdateRequest request = new PostUpdateRequest("Updated", "Updated Content", Level.SENIOR);
PostDetailResponse updated = new PostDetailResponse(
postId, "Updated", "Updated Content", Level.SENIOR,
userId, "tester", null, null, null, 0L, Instant.now(), Instant.now(), List.of());
given(postService.updatePost(eq(userId), eq(postId), any())).willReturn(updated);
mockMvc.perform(put("/posts/" + postId)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.success").value(true))
.andExpect(jsonPath("$.data.title").value("Updated"));
}
@Test
@DisplayName("PUT /posts/{postId} - 권한 없으면 403 반환")
void updatePost_unauthorized_returns403() throws Exception {
PostUpdateRequest request = new PostUpdateRequest("Updated", "Content", Level.JUNIOR);
given(postService.updatePost(eq(userId), eq(postId), any()))
.willThrow(new DevpickException(ErrorCode.COMMUNITY_UNAUTHORIZED_POST_ACTION));
mockMvc.perform(put("/posts/" + postId)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isForbidden())
.andExpect(jsonPath("$.success").value(false));
}
@Test
@DisplayName("DELETE /posts/{postId} - 삭제 성공 시 204 반환")
void deletePost_success_returns204() throws Exception {
mockMvc.perform(delete("/posts/" + postId))
.andExpect(status().isNoContent());
verify(postService).deletePost(userId, postId);
}
@Test
@DisplayName("DELETE /posts/{postId} - 권한 없으면 403 반환")
void deletePost_unauthorized_returns403() throws Exception {
doThrow(new DevpickException(ErrorCode.COMMUNITY_UNAUTHORIZED_POST_ACTION))
.when(postService).deletePost(userId, postId);
mockMvc.perform(delete("/posts/" + postId))
.andExpect(status().isForbidden())
.andExpect(jsonPath("$.success").value(false));
}
}