-
Notifications
You must be signed in to change notification settings - Fork 0
test: 콘텐츠 통합 테스트 작성 #272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
test: 콘텐츠 통합 테스트 작성 #272
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
395 changes: 395 additions & 0 deletions
395
.../com/codeit/team5/mopl/content/controller/ContentCollectionControllerIntegrationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,395 @@ | ||
| package com.codeit.team5.mopl.content.controller; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.BDDMockito.given; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication; | ||
| import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
|
||
| import com.codeit.team5.mopl.TestcontainersConfiguration; | ||
| import com.codeit.team5.mopl.global.support.security.IntegrationTestSecuritySupport; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.springframework.batch.core.JobParameters; | ||
| import org.springframework.batch.core.JobParametersInvalidException; | ||
| import org.springframework.batch.core.launch.JobLauncher; | ||
| 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.context.annotation.Import; | ||
| import org.springframework.security.core.Authentication; | ||
| import org.springframework.test.context.ActiveProfiles; | ||
| import org.springframework.test.context.bean.override.mockito.MockitoBean; | ||
| import org.springframework.test.web.servlet.MockMvc; | ||
|
|
||
| /** | ||
| * ContentCollectionController 통합 테스트. | ||
| * | ||
| * <p>실제 시큐리티 필터체인, 요청 검증·타입 변환(컨버터), 컨트롤러 로직을 모두 주입하여 수집 | ||
| * 엔드포인트의 인증/인가(401/403), 요청 검증(400), Job 실행 경로(202/500)를 한곳에서 검증한다.</p> | ||
| * | ||
| * <p>실제 배치 Job은 외부 API(TMDB/SportsDB)를 호출하므로, JobLauncher와 Job 빈만 목으로 대체하여 | ||
| * "요청이 정상적으로 Job 실행 경로까지 도달하는지(202)"와 "실행 실패 시 500"만 검증한다. | ||
| * </p> | ||
| */ | ||
| @SpringBootTest | ||
| @AutoConfigureMockMvc | ||
| @ActiveProfiles("test") | ||
| @Import(TestcontainersConfiguration.class) | ||
| class ContentCollectionControllerIntegrationTest { | ||
|
|
||
| private static final String MOVIES_URL = "/api/admin/contents/collect/tmdb/movies"; | ||
| private static final String TV_URL = "/api/admin/contents/collect/tmdb/tv"; | ||
| private static final String SPORTS_URL = "/api/admin/contents/collect/sports"; | ||
| private static final String SPORTS_DAY_URL = "/api/admin/contents/collect/sports/day"; | ||
|
|
||
| @Autowired | ||
| private MockMvc mockMvc; | ||
|
|
||
| // 실제 Job 빈은 그대로 두고(배치 JobRegistry가 getName()으로 등록하므로 목으로 대체하면 NPE 발생), | ||
| // JobLauncher만 목으로 대체해 실제 배치 실행(외부 API 호출)을 차단한다. | ||
| @MockitoBean(name = "asyncJobLauncher") | ||
| private JobLauncher asyncJobLauncher; | ||
|
|
||
| // --- 인증 헬퍼 --- | ||
|
|
||
| private Authentication adminAuth() { | ||
| return IntegrationTestSecuritySupport.adminAuthentication(); | ||
| } | ||
|
|
||
| private Authentication userAuth() { | ||
| return IntegrationTestSecuritySupport.userAuthentication(); | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("POST /tmdb/movies - TMDB 영화 수집") | ||
| class CollectTmdbMovies { | ||
|
|
||
| @Test | ||
| @DisplayName("ADMIN이 유효한 페이지 범위로 요청하면 202를 반환하고 JobParameters에 startPage/endPage가 실제로 담겨 전달된다") | ||
| void success_returnsAccepted() throws Exception { | ||
| // given | ||
| ArgumentCaptor<JobParameters> paramsCaptor = ArgumentCaptor.forClass(JobParameters.class); | ||
|
|
||
| // when & then | ||
| mockMvc.perform(post(MOVIES_URL) | ||
| .param("startPage", "1") | ||
| .param("endPage", "5") | ||
| .with(csrf()) | ||
| .with(authentication(adminAuth()))) | ||
| .andExpect(status().isAccepted()); | ||
|
|
||
| verify(asyncJobLauncher).run(any(), paramsCaptor.capture()); | ||
| JobParameters params = paramsCaptor.getValue(); | ||
| assertThat(params.getString("startPage")).isEqualTo("1"); | ||
| assertThat(params.getString("endPage")).isEqualTo("5"); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("USER 권한이면 403을 반환한다") | ||
| void asUser_returnsForbidden() throws Exception { | ||
| // when & then | ||
| mockMvc.perform(post(MOVIES_URL) | ||
| .param("startPage", "1") | ||
| .param("endPage", "5") | ||
| .with(csrf()) | ||
| .with(authentication(userAuth()))) | ||
| .andExpect(status().isForbidden()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("인증 없이 요청하면 401을 반환한다") | ||
| void unauthenticated_returnsUnauthorized() throws Exception { | ||
| // when & then | ||
| mockMvc.perform(post(MOVIES_URL) | ||
| .param("startPage", "1") | ||
| .param("endPage", "5") | ||
| .with(csrf())) | ||
| .andExpect(status().isUnauthorized()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("페이지 파라미터가 누락되면 400을 반환한다") | ||
| void missingParams_returnsBadRequest() throws Exception { | ||
| // when & then | ||
| mockMvc.perform(post(MOVIES_URL) | ||
| .with(csrf()) | ||
| .with(authentication(adminAuth()))) | ||
| .andExpect(status().isBadRequest()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("startPage가 0 이하면 400을 반환한다") | ||
| void invalidStartPage_returnsBadRequest() throws Exception { | ||
| // when & then | ||
| mockMvc.perform(post(MOVIES_URL) | ||
| .param("startPage", "0") | ||
| .param("endPage", "5") | ||
| .with(csrf()) | ||
| .with(authentication(adminAuth()))) | ||
| .andExpect(status().isBadRequest()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("endPage가 startPage보다 작으면 400을 반환한다") | ||
| void endPageLessThanStartPage_returnsBadRequest() throws Exception { | ||
| // when & then | ||
| mockMvc.perform(post(MOVIES_URL) | ||
| .param("startPage", "5") | ||
| .param("endPage", "3") | ||
| .with(csrf()) | ||
| .with(authentication(adminAuth()))) | ||
| .andExpect(status().isBadRequest()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Job 실행 자체가 실패하면 500을 반환한다") | ||
| void jobLaunchFails_returnsInternalServerError() throws Exception { | ||
| // given | ||
| given(asyncJobLauncher.run(any(), any())) | ||
| .willThrow(new JobParametersInvalidException("잘못된 Job 파라미터")); | ||
|
|
||
| // when & then | ||
| mockMvc.perform(post(MOVIES_URL) | ||
| .param("startPage", "1") | ||
| .param("endPage", "5") | ||
| .with(csrf()) | ||
| .with(authentication(adminAuth()))) | ||
| .andExpect(status().isInternalServerError()); | ||
| } | ||
|
plzslp marked this conversation as resolved.
|
||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("POST /tmdb/tv - TMDB TV 시리즈 수집") | ||
| class CollectTmdbTvSeries { | ||
|
|
||
| @Test | ||
| @DisplayName("ADMIN이 유효한 페이지 범위로 요청하면 202를 반환하고 JobParameters에 startPage/endPage가 실제로 담겨 전달된다") | ||
| void success_returnsAccepted() throws Exception { | ||
| // given | ||
| ArgumentCaptor<JobParameters> paramsCaptor = ArgumentCaptor.forClass(JobParameters.class); | ||
|
|
||
| // when & then | ||
| mockMvc.perform(post(TV_URL) | ||
| .param("startPage", "2") | ||
| .param("endPage", "10") | ||
| .with(csrf()) | ||
| .with(authentication(adminAuth()))) | ||
| .andExpect(status().isAccepted()); | ||
|
|
||
| verify(asyncJobLauncher).run(any(), paramsCaptor.capture()); | ||
| JobParameters params = paramsCaptor.getValue(); | ||
| assertThat(params.getString("startPage")).isEqualTo("2"); | ||
| assertThat(params.getString("endPage")).isEqualTo("10"); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("USER 권한이면 403을 반환한다") | ||
| void asUser_returnsForbidden() throws Exception { | ||
| // when & then | ||
| mockMvc.perform(post(TV_URL) | ||
| .param("startPage", "2") | ||
| .param("endPage", "10") | ||
| .with(csrf()) | ||
| .with(authentication(userAuth()))) | ||
| .andExpect(status().isForbidden()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("인증 없이 요청하면 401을 반환한다") | ||
| void unauthenticated_returnsUnauthorized() throws Exception { | ||
| // when & then | ||
| mockMvc.perform(post(TV_URL) | ||
| .param("startPage", "2") | ||
| .param("endPage", "10") | ||
| .with(csrf())) | ||
| .andExpect(status().isUnauthorized()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("페이지 파라미터가 누락되면 400을 반환한다") | ||
| void missingParams_returnsBadRequest() throws Exception { | ||
| // when & then | ||
| mockMvc.perform(post(TV_URL) | ||
| .with(csrf()) | ||
| .with(authentication(adminAuth()))) | ||
| .andExpect(status().isBadRequest()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("endPage가 startPage보다 작으면 400을 반환한다") | ||
| void endPageLessThanStartPage_returnsBadRequest() throws Exception { | ||
| // when & then | ||
| mockMvc.perform(post(TV_URL) | ||
| .param("startPage", "10") | ||
| .param("endPage", "5") | ||
| .with(csrf()) | ||
| .with(authentication(adminAuth()))) | ||
| .andExpect(status().isBadRequest()); | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| @Nested | ||
| @DisplayName("POST /sports - SportsDB 경기 수집") | ||
| class CollectSportsEvents { | ||
|
|
||
| @Test | ||
| @DisplayName("ADMIN이 유효한 리그·시즌으로 요청하면 202를 반환하고 JobParameters에 leagueId/season이 실제로 담겨 전달된다") | ||
| void success_returnsAccepted() throws Exception { | ||
| // given | ||
| ArgumentCaptor<JobParameters> paramsCaptor = ArgumentCaptor.forClass(JobParameters.class); | ||
|
|
||
| // when & then | ||
| mockMvc.perform(post(SPORTS_URL) | ||
| .param("league", "EPL") | ||
| .param("season", "2023-2024") | ||
| .with(csrf()) | ||
| .with(authentication(adminAuth()))) | ||
| .andExpect(status().isAccepted()); | ||
|
|
||
| verify(asyncJobLauncher).run(any(), paramsCaptor.capture()); | ||
| JobParameters params = paramsCaptor.getValue(); | ||
| assertThat(params.getString("leagueId")).isEqualTo("4328"); | ||
| assertThat(params.getString("season")).isEqualTo("2023-2024"); | ||
| } | ||
|
plzslp marked this conversation as resolved.
|
||
|
|
||
| @Test | ||
| @DisplayName("USER 권한이면 403을 반환한다") | ||
| void asUser_returnsForbidden() throws Exception { | ||
| // when & then | ||
| mockMvc.perform(post(SPORTS_URL) | ||
| .param("league", "EPL") | ||
| .param("season", "2023-2024") | ||
| .with(csrf()) | ||
| .with(authentication(userAuth()))) | ||
| .andExpect(status().isForbidden()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("인증 없이 요청하면 401을 반환한다") | ||
| void unauthenticated_returnsUnauthorized() throws Exception { | ||
| // when & then | ||
| mockMvc.perform(post(SPORTS_URL) | ||
| .param("league", "EPL") | ||
| .param("season", "2023-2024") | ||
| .with(csrf())) | ||
| .andExpect(status().isUnauthorized()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("league 파라미터가 누락되면 400을 반환한다") | ||
| void missingLeague_returnsBadRequest() throws Exception { | ||
| // when & then | ||
| mockMvc.perform(post(SPORTS_URL) | ||
| .param("season", "2023-2024") | ||
| .with(csrf()) | ||
| .with(authentication(adminAuth()))) | ||
| .andExpect(status().isBadRequest()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("유효하지 않은 리그 값이면 400을 반환한다") | ||
| void invalidLeague_returnsBadRequest() throws Exception { | ||
| // when & then | ||
| mockMvc.perform(post(SPORTS_URL) | ||
| .param("league", "INVALID_LEAGUE") | ||
| .param("season", "2023-2024") | ||
| .with(csrf()) | ||
| .with(authentication(adminAuth()))) | ||
| .andExpect(status().isBadRequest()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("시즌 형식이 YYYY-YYYY가 아니면 400을 반환한다") | ||
| void invalidSeasonFormat_returnsBadRequest() throws Exception { | ||
| // when & then | ||
| mockMvc.perform(post(SPORTS_URL) | ||
| .param("league", "EPL") | ||
| .param("season", "2023/2024") | ||
| .with(csrf()) | ||
| .with(authentication(adminAuth()))) | ||
| .andExpect(status().isBadRequest()); | ||
| } | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("POST /sports/day - SportsDB 일별 경기 수집") | ||
| class CollectSportsEventsByDay { | ||
|
|
||
| @Test | ||
| @DisplayName("ADMIN이 유효한 날짜로 요청하면 202를 반환하고 JobParameters에 date가 실제로 담겨 전달된다") | ||
| void success_returnsAccepted() throws Exception { | ||
| // given | ||
| ArgumentCaptor<JobParameters> paramsCaptor = ArgumentCaptor.forClass(JobParameters.class); | ||
|
|
||
| // when & then | ||
| mockMvc.perform(post(SPORTS_DAY_URL) | ||
| .param("date", "2024-12-26") | ||
| .with(csrf()) | ||
| .with(authentication(adminAuth()))) | ||
| .andExpect(status().isAccepted()); | ||
|
|
||
| verify(asyncJobLauncher).run(any(), paramsCaptor.capture()); | ||
| JobParameters params = paramsCaptor.getValue(); | ||
| assertThat(params.getString("date")).isEqualTo("2024-12-26"); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("USER 권한이면 403을 반환한다") | ||
| void asUser_returnsForbidden() throws Exception { | ||
| // when & then | ||
| mockMvc.perform(post(SPORTS_DAY_URL) | ||
| .param("date", "2024-12-26") | ||
| .with(csrf()) | ||
| .with(authentication(userAuth()))) | ||
| .andExpect(status().isForbidden()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("인증 없이 요청하면 401을 반환한다") | ||
| void unauthenticated_returnsUnauthorized() throws Exception { | ||
| // when & then | ||
| mockMvc.perform(post(SPORTS_DAY_URL) | ||
| .param("date", "2024-12-26") | ||
| .with(csrf())) | ||
| .andExpect(status().isUnauthorized()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("date 파라미터가 누락되면 400을 반환한다") | ||
| void missingDate_returnsBadRequest() throws Exception { | ||
| // when & then | ||
| mockMvc.perform(post(SPORTS_DAY_URL) | ||
| .with(csrf()) | ||
| .with(authentication(adminAuth()))) | ||
| .andExpect(status().isBadRequest()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("날짜 형식이 YYYY-MM-DD가 아니면 400을 반환한다") | ||
| void invalidDateFormat_returnsBadRequest() throws Exception { | ||
| // when & then | ||
| mockMvc.perform(post(SPORTS_DAY_URL) | ||
| .param("date", "2024/12/26") | ||
| .with(csrf()) | ||
| .with(authentication(adminAuth()))) | ||
| .andExpect(status().isBadRequest()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("실존하지 않는 날짜면 400을 반환한다") | ||
| void nonExistentDate_returnsBadRequest() throws Exception { | ||
| // when & then | ||
| mockMvc.perform(post(SPORTS_DAY_URL) | ||
| .param("date", "2024-02-31") | ||
| .with(csrf()) | ||
| .with(authentication(adminAuth()))) | ||
| .andExpect(status().isBadRequest()); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.