From c5b7729f6662a4bfce81f85dd787861cb9cdb7e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EC=84=B8=EC=A4=80?= <74056843+sejoon00@users.noreply.github.com> Date: Thu, 15 Aug 2024 16:06:59 +0900 Subject: [PATCH] =?UTF-8?q?[feate][#51]=20=EB=B6=84=EA=B8=B0=20=EB=A6=AC?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=A1=B0=ED=9A=8C=20api=20=EC=99=84?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/SeasonControllerTest.java | 91 +++++++++++++++++++ src/test/resources/season-test-data.sql | 30 ++++++ 2 files changed, 121 insertions(+) create mode 100644 src/test/java/com/server/bbo_gak/domain/recruit/controller/SeasonControllerTest.java create mode 100644 src/test/resources/season-test-data.sql diff --git a/src/test/java/com/server/bbo_gak/domain/recruit/controller/SeasonControllerTest.java b/src/test/java/com/server/bbo_gak/domain/recruit/controller/SeasonControllerTest.java new file mode 100644 index 0000000..ef52c3c --- /dev/null +++ b/src/test/java/com/server/bbo_gak/domain/recruit/controller/SeasonControllerTest.java @@ -0,0 +1,91 @@ +package com.server.bbo_gak.domain.recruit.controller; + +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.server.bbo_gak.domain.recruit.dto.response.SeasonGetResponse; +import com.server.bbo_gak.domain.recruit.entity.Season; +import com.server.bbo_gak.domain.user.entity.User; +import com.server.bbo_gak.domain.user.entity.UserRepository; +import com.server.bbo_gak.domain.user.entity.UserRole; +import com.server.bbo_gak.global.AbstractRestDocsTests; +import com.server.bbo_gak.global.RestDocsFactory; +import com.server.bbo_gak.global.security.PrincipalDetails; +import java.util.List; +import java.util.Optional; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpMethod; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.transaction.annotation.Transactional; + +@Transactional +@SpringBootTest +@ActiveProfiles("test") +@Sql("/season-test-data.sql") +public class SeasonControllerTest extends AbstractRestDocsTests { + + private static final String DEFAULT_URL = "/api/v1/seasons"; + @Autowired + private UserRepository userRepository; + @Autowired + private RestDocsFactory restDocsFactory; + private List responses; + + @BeforeEach + void setUp() { + User user = userRepository.findById(1L).get(); + Season season1 = new Season("2024 하반기", user); + Season season2 = new Season("2025 상반기", user); + + responses = List.of( + SeasonGetResponse.from(season1), + SeasonGetResponse.from(season2) + ); + } + + @Nested + class 분기리스트_조회 { + + @Test + public void 분기_없으면_기본생성_성공() throws Exception { + mockMvc.perform( + restDocsFactory.createRequest(DEFAULT_URL, null, HttpMethod.GET, objectMapper)) + .andExpect(status().isOk()) + .andDo( + restDocsFactory.getSuccessResourceList("[GET] 분기 리스트 조회 성공", "분기 리스트 조회", "Season", + List.of(), responses)); + } + + @Test + public void 분기_있으면_DB에서_조회_성공() throws Exception { + UserDetails userDetails = PrincipalDetails.ofJwt(2L, UserRole.USER); + Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, null, + userDetails.getAuthorities()); + SecurityContextHolder.getContext().setAuthentication(authentication); + + User mockUser = User.builder() + .id(2L) + .role(UserRole.USER) + .build(); + + when(userRepository.findById(2L)).thenReturn(Optional.of(mockUser)); + + mockMvc.perform( + restDocsFactory.createRequest(DEFAULT_URL, null, HttpMethod.GET, objectMapper)) + .andExpect(status().isOk()) + .andDo( + restDocsFactory.getSuccessResourceList("[GET] 분기 리스트 조회 성공", "분기 리스트 조회", "Season", + List.of(), responses)); + } + } + +} diff --git a/src/test/resources/season-test-data.sql b/src/test/resources/season-test-data.sql new file mode 100644 index 0000000..1d37444 --- /dev/null +++ b/src/test/resources/season-test-data.sql @@ -0,0 +1,30 @@ +delete +from notification; + +delete +from recruit_schedule; + +delete +from recruit; + +delete +from recruit_season; + +delete +from users; + +INSERT INTO users (deleted, created_at, update_at, user_id, dtype, email, login_id, name, password, role) +VALUES (false, '2024-07-24 21:27:20.000000', '2024-07-24 21:27:21.000000', 1, 'AuthTestUser', 'email', 'test', 'test', + 'test123', 'USER'), + (false, '2024-07-24 21:27:20.000000', '2024-07-24 21:27:21.000000', 2, 'AuthTestUser', 'email', 'test', 'test', + 'test123', 'USER'); + +INSERT INTO recruit_season (recruit_season_id, name, user_id) +VALUES (1, '2024 상반기', 2), + (2, '2024 하반기', 2), + (3, '2025 상반기', 2); + + + + +