diff --git a/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/cookie/CookieUtilsTest.java b/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/cookie/CookieUtilsTest.java index 31af7b3b..93fec143 100644 --- a/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/cookie/CookieUtilsTest.java +++ b/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/cookie/CookieUtilsTest.java @@ -1,7 +1,6 @@ package com.inhabas.api.auth.domain.oauth2.cookie; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; @@ -22,22 +21,28 @@ public class CookieUtilsTest { + private static final String COOKIE_NAME = "myCookie"; + private static final String COOKIE_CONTENTS = "hello"; + private static final int COOKIE_MAX_AGE = 180; + @DisplayName("request 에서 쿠키를 꺼낸다.") @Test public void resolveCookieFromRequest() { // given - MockHttpServletRequest request = new MockHttpServletRequest(); - Cookie cookie = new Cookie("myCookie", "hello"); - cookie.setMaxAge(180); - request.setCookies(cookie); + MockHttpServletRequest request = + createRequestWithCookie(COOKIE_NAME, COOKIE_CONTENTS, COOKIE_MAX_AGE); // when - Optional myCookie = CookieUtils.resolveCookie(request, "myCookie"); + Optional myCookie = CookieUtils.resolveCookie(request, COOKIE_NAME); // then - assertThat(myCookie.isPresent()).isTrue(); - assertThat(myCookie.get().getValue()).isEqualTo("hello"); - assertThat(myCookie.get().getMaxAge()).isEqualTo(180); + assertThat(myCookie) + .isPresent() + .hasValueSatisfying( + cookie -> { + assertThat(cookie.getValue()).isEqualTo(COOKIE_CONTENTS); + assertThat(cookie.getMaxAge()).isEqualTo(COOKIE_MAX_AGE); + }); } @DisplayName("response 에 쿠키를 저장한다.") @@ -45,19 +50,20 @@ public void resolveCookieFromRequest() { public void saveCookieToResponse() { // given MockHttpServletResponse response = new MockHttpServletResponse(); - String cookieName = "myCookie"; - String cookieContents = "hello"; // when - CookieUtils.setCookie(response, cookieName, cookieContents, 180); + CookieUtils.setCookie(response, COOKIE_NAME, COOKIE_CONTENTS, COOKIE_MAX_AGE); // then - Cookie resolvedCookie = response.getCookie(cookieName); - assert resolvedCookie != null; - - assertThat(resolvedCookie.getName()).isEqualTo(cookieName); - assertThat(resolvedCookie.getValue()).isEqualTo(cookieContents); - assertThat(resolvedCookie.getMaxAge()).isEqualTo(180); + Cookie resolvedCookie = response.getCookie(COOKIE_NAME); + assertThat(resolvedCookie) + .isNotNull() + .satisfies( + cookie -> { + assertThat(cookie.getName()).isEqualTo(COOKIE_NAME); + assertThat(cookie.getValue()).isEqualTo(COOKIE_CONTENTS); + assertThat(cookie.getMaxAge()).isEqualTo(COOKIE_MAX_AGE); + }); } @DisplayName("request 에서 쿠키를 지운다.") @@ -65,52 +71,35 @@ public void saveCookieToResponse() { public void removeCookieOfRequest() { // given MockHttpServletResponse response = new MockHttpServletResponse(); - MockHttpServletRequest request = new MockHttpServletRequest(); - Cookie cookie = new Cookie("myCookie", "hello"); - cookie.setMaxAge(180); - request.setCookies(cookie); + MockHttpServletRequest request = + createRequestWithCookie(COOKIE_NAME, COOKIE_CONTENTS, COOKIE_MAX_AGE); // when - CookieUtils.deleteCookie(request, response, "myCookie"); + CookieUtils.deleteCookie(request, response, COOKIE_NAME); // then - Cookie deletedCookie = response.getCookie("myCookie"); - assert deletedCookie != null; - assertThat(deletedCookie.getMaxAge()).isEqualTo(0); - assertThat(deletedCookie.getValue()).isEqualTo(""); + Cookie deletedCookie = response.getCookie(COOKIE_NAME); + assertThat(deletedCookie) + .isNotNull() + .satisfies( + cookie -> { + assertThat(cookie.getMaxAge()).isEqualTo(0); + assertThat(cookie.getValue()).isEmpty(); + }); } @DisplayName("성공적으로 serialize 한다.") @Test public void serializingTest() - throws InvocationTargetException, InstantiationException, IllegalAccessException, - NoSuchMethodException { - // reflection - Constructor constructor = - OAuth2AuthorizationRequest.Builder.class.getDeclaredConstructor( - AuthorizationGrantType.class); - constructor.setAccessible(true); - - // given - OAuth2AuthorizationRequest.Builder builder = - (OAuth2AuthorizationRequest.Builder) - constructor.newInstance(AuthorizationGrantType.AUTHORIZATION_CODE); - OAuth2AuthorizationRequest request = - builder - .authorizationUri("https://kauth.kakao.com/oauth/authorize") - .clientId("1234") - .redirectUri("http://localhost/api/login/oauth2/code/kakao") - .scopes(Set.of("gender", "profile_image", "account_email", "profile_nickname")) - .state("state1934") - .additionalParameters(java.util.Map.of()) - .attributes(java.util.Map.of("registration_id", "kakao")) - .build(); + throws NoSuchMethodException, InvocationTargetException, InstantiationException, + IllegalAccessException { + OAuth2AuthorizationRequest request = createOAuth2AuthorizationRequest(); // when String serializedRequest = CookieUtils.serialize(request); // then - assertTrue(Base64.isBase64(serializedRequest)); + assertThat(serializedRequest).matches(Base64::isBase64); } @DisplayName("성공적으로 deserialize 한다.") @@ -118,36 +107,46 @@ public void serializingTest() public void deserializingTest() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + OAuth2AuthorizationRequest originalRequest = createOAuth2AuthorizationRequest(); + String serializedRequest = CookieUtils.serialize(originalRequest); + Cookie cookie = new Cookie("base64", serializedRequest); + // when + OAuth2AuthorizationRequest deserializedRequest = + CookieUtils.deserialize(cookie, OAuth2AuthorizationRequest.class); + + // then + assertThat(deserializedRequest).usingRecursiveComparison().isEqualTo(originalRequest); + } + + private MockHttpServletRequest createRequestWithCookie(String name, String value, int maxAge) { + MockHttpServletRequest request = new MockHttpServletRequest(); + Cookie cookie = new Cookie(name, value); + cookie.setMaxAge(maxAge); + request.setCookies(cookie); + return request; + } + + private static OAuth2AuthorizationRequest createOAuth2AuthorizationRequest() + throws NoSuchMethodException, InvocationTargetException, InstantiationException, + IllegalAccessException { // reflection Constructor constructor = OAuth2AuthorizationRequest.Builder.class.getDeclaredConstructor( AuthorizationGrantType.class); constructor.setAccessible(true); - // given OAuth2AuthorizationRequest.Builder builder = (OAuth2AuthorizationRequest.Builder) constructor.newInstance(AuthorizationGrantType.AUTHORIZATION_CODE); - OAuth2AuthorizationRequest originalRequest = - builder - .authorizationUri("https://kauth.kakao.com/oauth/authorize") - .clientId("1234") - .redirectUri("http://localhost/api/login/oauth2/code/kakao") - .scopes(Set.of("gender", "profile_image", "account_email", "profile_nickname")) - .state("state1934") - .additionalParameters(java.util.Map.of()) - .attributes(java.util.Map.of("registration_id", "kakao")) - .build(); - - String serializedRequest = CookieUtils.serialize(originalRequest); - Cookie cookie = new Cookie("base64", serializedRequest); - - // when - OAuth2AuthorizationRequest deserializedRequest = - CookieUtils.deserialize(cookie, OAuth2AuthorizationRequest.class); - - // then - assertThat(deserializedRequest).usingRecursiveComparison().isEqualTo(originalRequest); + return builder + .authorizationUri("https://kauth.kakao.com/oauth/authorize") + .clientId("1234") + .redirectUri("http://localhost/api/login/oauth2/code/kakao") + .scopes(Set.of("gender", "profile_image", "account_email", "profile_nickname")) + .state("state1934") + .additionalParameters(java.util.Map.of()) + .attributes(java.util.Map.of("registration_id", "kakao")) + .build(); } } diff --git a/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/cookie/HttpCookieOAuth2AuthorizationRequestRepositoryTest.java b/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/cookie/HttpCookieOAuth2AuthorizationRequestRepositoryTest.java index 1c8677a7..5c4410bd 100644 --- a/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/cookie/HttpCookieOAuth2AuthorizationRequestRepositoryTest.java +++ b/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/cookie/HttpCookieOAuth2AuthorizationRequestRepositoryTest.java @@ -3,7 +3,6 @@ import static com.inhabas.api.auth.domain.oauth2.cookie.HttpCookieOAuth2AuthorizationRequestRepository.OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME; import static com.inhabas.api.auth.domain.oauth2.cookie.HttpCookieOAuth2AuthorizationRequestRepository.REDIRECT_URL_PARAM_COOKIE_NAME; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; @@ -19,11 +18,19 @@ import org.apache.commons.codec.binary.Base64; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; public class HttpCookieOAuth2AuthorizationRequestRepositoryTest { + private HttpCookieOAuth2AuthorizationRequestRepository repository; + + @BeforeEach + public void setUp() { + repository = new HttpCookieOAuth2AuthorizationRequestRepository(); + } + private final HttpCookieOAuth2AuthorizationRequestRepository httpCookieOAuth2AuthorizationRequestRepository = new HttpCookieOAuth2AuthorizationRequestRepository(); @@ -62,8 +69,7 @@ public void saveAuthorizationRequestNullTest() { MockHttpServletResponse response = new MockHttpServletResponse(); // when - httpCookieOAuth2AuthorizationRequestRepository.saveAuthorizationRequest( - null, request, response); + repository.saveAuthorizationRequest(null, request, response); // then assertThat(response.getCookies()) @@ -83,14 +89,13 @@ public void saveAuthorizationRequestTest() throws NoSuchMethodException { OAuth2AuthorizationRequest oAuth2AuthorizationRequest = this.createOAuth2AuthorizationRequest(); // when - httpCookieOAuth2AuthorizationRequestRepository.saveAuthorizationRequest( - oAuth2AuthorizationRequest, request, response); + repository.saveAuthorizationRequest(oAuth2AuthorizationRequest, request, response); // then // 쿠키 한가지 존재하는지 확인. Cookie savedCookie = response.getCookie(OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME); - assert savedCookie != null; - assertTrue(Base64.isBase64(savedCookie.getValue())); + assertThat(savedCookie).isNotNull(); + assertThat(Base64.isBase64(savedCookie.getValue())).isTrue(); } @DisplayName("OAuth2AuthorizationRequest 를 쿠키로 저장할 때, redirect_url 도 쿠키로 저장한다.") @@ -104,8 +109,7 @@ public void saveAuthorizationRequestWithRedirectUrlTest() throws NoSuchMethodExc request.setParameter(REDIRECT_URL_PARAM_COOKIE_NAME, "/index.html"); // when - httpCookieOAuth2AuthorizationRequestRepository.saveAuthorizationRequest( - oAuth2AuthorizationRequest, request, response); + repository.saveAuthorizationRequest(oAuth2AuthorizationRequest, request, response); // then // 쿠키 두가지 존재하는 지 확인 @@ -133,13 +137,13 @@ public void removeAuthorizationRequestTest() throws NoSuchMethodException { // when OAuth2AuthorizationRequest returnedRequest = - httpCookieOAuth2AuthorizationRequestRepository.removeAuthorizationRequest( - request, response); + repository.removeAuthorizationRequest(request, response); // then Cookie cookie = response.getCookie(OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME); - assert cookie != null; - assertTrue(cookie.getValue().isBlank() && cookie.getMaxAge() == 0); + assertThat(cookie).isNotNull(); + assertThat(cookie.getValue()).isBlank(); + assertThat(cookie.getMaxAge()).isEqualTo(0); } @DisplayName("OAuth2AuthorizationRequest 를 성공적으로 쿠키에서 삭제한다. (redirectUrl 쿠키도 삭제된다.)") @@ -150,7 +154,7 @@ public void removeAuthorizationRequestTotally() { MockHttpServletResponse response = new MockHttpServletResponse(); // when - httpCookieOAuth2AuthorizationRequestRepository.clearCookies(request, response); + repository.clearCookies(request, response); // then assertThat(response.getCookies()) @@ -189,8 +193,8 @@ private OAuth2AuthorizationRequest createOAuth2AuthorizationRequest() .attributes(java.util.Map.of("registration_id", "kakao")) .build(); - } catch (InvocationTargetException | InstantiationException | IllegalAccessException ignored) { - return null; + } catch (InvocationTargetException | InstantiationException | IllegalAccessException e) { + throw new RuntimeException("Failed to create OAuth2AuthorizationRequest", e); } } } diff --git a/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/handler/Oauth2AuthenticationFailureHandlerTest.java b/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/handler/Oauth2AuthenticationFailureHandlerTest.java index b1440535..497ade15 100644 --- a/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/handler/Oauth2AuthenticationFailureHandlerTest.java +++ b/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/handler/Oauth2AuthenticationFailureHandlerTest.java @@ -38,53 +38,59 @@ public class Oauth2AuthenticationFailureHandlerTest { @Mock private AuthProperties.OAuth2 oauth2Utils; + private static final String VALID_REDIRECT_URL = "https://www.inhabas.com"; + private static final String INVALID_REDIRECT_URL = "https://www.unauthorized_url.com"; + private static final String ERROR_CODE = OAuth2ErrorCodes.INVALID_REQUEST; + @BeforeEach public void setUp() { given(authProperties.getOauth2()).willReturn(oauth2Utils); } + private MockHttpServletRequest createRequestWithCookie(String cookieValue) { + MockHttpServletRequest request = new MockHttpServletRequest(); + Cookie redirectCookie = new Cookie(REDIRECT_URL_PARAM_COOKIE_NAME, cookieValue); + request.setCookies(redirectCookie); + return request; + } + + private AuthenticationException createAuthenticationException(String errorCode) { + return new OAuth2AuthenticationException(errorCode); + } + @DisplayName("FailureHandler 호출 시, 허락된 defaultURL 로 정상적으로 리다이렉트 된다.") @Test public void redirectToDefaultTest() throws IOException { // given - String errorCode = OAuth2ErrorCodes.INVALID_REQUEST; - MockHttpServletRequest request = new MockHttpServletRequest(); + MockHttpServletRequest request = createRequestWithCookie(VALID_REDIRECT_URL); MockHttpServletResponse response = new MockHttpServletResponse(); - AuthenticationException authenticationException = new OAuth2AuthenticationException(errorCode); - - Cookie redirectCookie = new Cookie(REDIRECT_URL_PARAM_COOKIE_NAME, "https://www.inhabas.com"); - request.setCookies(redirectCookie); + AuthenticationException authenticationException = new OAuth2AuthenticationException(ERROR_CODE); - given(oauth2Utils.getDefaultRedirectUri()).willReturn("https://www.inhabas.com"); + given(oauth2Utils.getDefaultRedirectUri()).willReturn(VALID_REDIRECT_URL); // when oauth2AuthenticationFailureHandler.onAuthenticationFailure( request, response, authenticationException); // then - assertThat(response.getRedirectedUrl()).isEqualTo("https://www.inhabas.com?error=" + errorCode); + assertThat(response.getRedirectedUrl()).isEqualTo(VALID_REDIRECT_URL + "?error=" + ERROR_CODE); } @DisplayName("유효하지 않은 redirect_url 은 허용하지 않는다.") @Test public void validateRedirectUrlTest() throws IOException { // given - String errorCode = OAuth2ErrorCodes.INVALID_REQUEST; - MockHttpServletRequest request = new MockHttpServletRequest(); + MockHttpServletRequest request = createRequestWithCookie(INVALID_REDIRECT_URL); MockHttpServletResponse response = new MockHttpServletResponse(); - AuthenticationException authenticationException = new OAuth2AuthenticationException(errorCode); - - Cookie redirectCookie = - new Cookie(REDIRECT_URL_PARAM_COOKIE_NAME, "https://www.unauthorized_url.com"); - request.setCookies(redirectCookie); + AuthenticationException authenticationException = createAuthenticationException(ERROR_CODE); - given(oauth2Utils.getDefaultRedirectUri()).willReturn("https://www.inhabas.com"); + given(oauth2Utils.getDefaultRedirectUri()).willReturn(VALID_REDIRECT_URL); // when oauth2AuthenticationFailureHandler.onAuthenticationFailure( request, response, authenticationException); // then - assertThat(response.getRedirectedUrl()).isEqualTo("https://www.inhabas.com?error=" + errorCode); + assertThat(response.getRedirectedUrl()).isEqualTo(VALID_REDIRECT_URL + "?error=" + ERROR_CODE); } } diff --git a/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/handler/Oauth2AuthenticationSuccessHandlerTest.java b/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/handler/Oauth2AuthenticationSuccessHandlerTest.java index 2bfaffcc..a10af762 100644 --- a/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/handler/Oauth2AuthenticationSuccessHandlerTest.java +++ b/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/handler/Oauth2AuthenticationSuccessHandlerTest.java @@ -51,6 +51,8 @@ public class Oauth2AuthenticationSuccessHandlerTest { private final Set basicAuthorities = Collections.singleton(new SimpleGrantedAuthority("ROLE_BASIC")); + private static final String VALID_REDIRECT_URL = "https://www.inhabas.com"; + @BeforeEach public void setUp() { given(authProperties.getOauth2()).willReturn(oAuth2Utils); @@ -58,18 +60,25 @@ public void setUp() { new DefaultOAuth2User(basicAuthorities, Map.of("id", 1234, "properties", "blahblah"), "id"); } + private MockHttpServletRequest createRequestWithCookie(String cookieValue) { + MockHttpServletRequest request = new MockHttpServletRequest(); + Cookie redirectCookie = new Cookie(REDIRECT_URL_PARAM_COOKIE_NAME, cookieValue); + request.setCookies(redirectCookie); + return request; + } + + private OAuth2AuthenticationToken createAuthenticationToken() { + return new OAuth2AuthenticationToken(defaultOAuth2User, basicAuthorities, "google"); + } + @DisplayName("SuccessHandler 호출 시, targetURL 로 정상적으로 리다이렉트 된다.") @Test public void redirectToTargetUrlTest() throws IOException { // given - MockHttpServletRequest request = new MockHttpServletRequest(); + MockHttpServletRequest request = createRequestWithCookie(VALID_REDIRECT_URL); MockHttpServletResponse response = new MockHttpServletResponse(); - OAuth2AuthenticationToken authenticationToken = - new OAuth2AuthenticationToken(defaultOAuth2User, basicAuthorities, "google"); - - Cookie redirectCookie = new Cookie(REDIRECT_URL_PARAM_COOKIE_NAME, "https://www.inhabas.com"); - request.setCookies(redirectCookie); + OAuth2AuthenticationToken authenticationToken = createAuthenticationToken(); given(oAuth2Utils.isAuthorizedRedirectUri(any())).willReturn(true); @@ -78,8 +87,7 @@ public void redirectToTargetUrlTest() throws IOException { // then assertThat(response.getRedirectedUrl()) - .contains( - "https://www.inhabas.com", "accessToken", "refreshToken", "expiresIn", "imageUrl"); + .contains(VALID_REDIRECT_URL, "accessToken", "refreshToken", "expiresIn", "imageUrl"); } @DisplayName("인가되지 않은 redirect_url 요청 시, UnauthorizedRedirectUriException 발생") @@ -87,13 +95,9 @@ public void redirectToTargetUrlTest() throws IOException { public void unAuthorizedTargetUrlTest() { // given - MockHttpServletRequest request = new MockHttpServletRequest(); + MockHttpServletRequest request = createRequestWithCookie(VALID_REDIRECT_URL); MockHttpServletResponse response = new MockHttpServletResponse(); - OAuth2AuthenticationToken authenticationToken = - new OAuth2AuthenticationToken(defaultOAuth2User, basicAuthorities, "google"); - - Cookie redirectCookie = new Cookie(REDIRECT_URL_PARAM_COOKIE_NAME, "https://www.inhabas.com"); - request.setCookies(redirectCookie); + OAuth2AuthenticationToken authenticationToken = createAuthenticationToken(); given(oAuth2Utils.isAuthorizedRedirectUri(any())).willReturn(false); @@ -107,20 +111,16 @@ public void unAuthorizedTargetUrlTest() { @Test public void clearCookieAfterHandleOAuth2Authentication() throws IOException { // given - MockHttpServletRequest request = new MockHttpServletRequest(); + MockHttpServletRequest request = createRequestWithCookie(VALID_REDIRECT_URL); MockHttpServletResponse response = new MockHttpServletResponse(); - OAuth2AuthenticationToken authenticationToken = - new OAuth2AuthenticationToken(defaultOAuth2User, basicAuthorities, "google"); - - Cookie redirectCookie = new Cookie(REDIRECT_URL_PARAM_COOKIE_NAME, "https://www.inhabas.com"); - request.setCookies(redirectCookie); + OAuth2AuthenticationToken authenticationToken = createAuthenticationToken(); given(oAuth2Utils.isAuthorizedRedirectUri(any())).willReturn(true); // when successHandler.onAuthenticationSuccess(request, response, authenticationToken); - // when + // then then(requestRepository).should(times(1)).clearCookies(any(), any()); } } diff --git a/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/majorInfo/domain/valueObject/CollegeTest.java b/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/majorInfo/domain/valueObject/CollegeTest.java index cf8ddd24..c0efb470 100644 --- a/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/majorInfo/domain/valueObject/CollegeTest.java +++ b/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/majorInfo/domain/valueObject/CollegeTest.java @@ -12,7 +12,7 @@ public class CollegeTest { @DisplayName("College 타입에 제목을 저장한다.") @Test - public void College_is_OK() { + public void saveValidCollegeName() { // given String collegeString = "사회과학대학"; @@ -26,7 +26,7 @@ public void College_is_OK() { @DisplayName("College 타입에 너무 긴 이름을 저장한다. 20자 이상") @Test - public void College_is_too_long() { + public void throwExceptionWhenSavingTooLongCollegeName() { // given String collegeString = "지금이문장은10자임".repeat(20); @@ -37,14 +37,14 @@ public void College_is_too_long() { @DisplayName("College 은 null 일 수 없습니다.") @Test - public void College_cannot_be_Null() { + public void throwExceptionWhenSavingNullCollegeName() { assertThrows(InvalidInputException.class, () -> new College(null)); } @DisplayName("College 은 빈 문자열일 수 없습니다.") @Test - public void College_cannot_be_Blank() { + public void throwExceptionWhenSavingBlankCollegeName() { assertThrows(InvalidInputException.class, () -> new College("\t")); } diff --git a/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/majorInfo/domain/valueObject/MajorTest.java b/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/majorInfo/domain/valueObject/MajorTest.java index 5f7e74fa..dd72dcec 100644 --- a/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/majorInfo/domain/valueObject/MajorTest.java +++ b/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/majorInfo/domain/valueObject/MajorTest.java @@ -12,7 +12,7 @@ public class MajorTest { @DisplayName("Major 타입에 제목을 저장한다.") @Test - public void Major_is_OK() { + public void saveValidMajorName() { // given String majorString = "컴퓨터공학과"; @@ -26,7 +26,7 @@ public void Major_is_OK() { @DisplayName("Major 타입에 너무 긴 이름을 저장한다. 50자 이상") @Test - public void Major_is_too_long() { + public void throwExceptionWhenSavingTooLongMajorName() { // given String majorString = "지금이문장은10자임".repeat(50); @@ -37,14 +37,14 @@ public void Major_is_too_long() { @DisplayName("Major 은 null 일 수 없습니다.") @Test - public void Major_cannot_be_Null() { + public void throwExceptionWhenSavingNullMajorName() { assertThrows(InvalidInputException.class, () -> new Major(null)); } @DisplayName("Major 은 빈 문자열일 수 없습니다.") @Test - public void Major_cannot_be_Blank() { + public void throwExceptionWhenSavingBlankMajorName() { assertThrows(InvalidInputException.class, () -> new Major("\t")); } diff --git a/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/majorInfo/usecase/MajorInfoServiceTest.java b/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/majorInfo/usecase/MajorInfoServiceTest.java index d23eac87..bbb4c759 100644 --- a/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/majorInfo/usecase/MajorInfoServiceTest.java +++ b/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/majorInfo/usecase/MajorInfoServiceTest.java @@ -9,7 +9,6 @@ import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.times; -import java.util.ArrayList; import java.util.List; import org.springframework.test.util.ReflectionTestUtils; @@ -33,26 +32,25 @@ public class MajorInfoServiceTest { @Mock private MajorInfoRepository majorInfoRepository; + private List createSampleMajorInfos() { + MajorInfo majorInfo1 = createMajorInfo("공과대학", "기계공학과", 1); + MajorInfo majorInfo2 = createMajorInfo("자연과학대학", "수학과", 2); + MajorInfo majorInfo3 = createMajorInfo("경영대학", "경영학과", 3); + return List.of(majorInfo1, majorInfo2, majorInfo3); + } + + private MajorInfo createMajorInfo(String college, String major, int id) { + MajorInfo majorInfo = new MajorInfo(college, major); + ReflectionTestUtils.setField(majorInfo, "id", id); + return majorInfo; + } + @DisplayName("모든 학과 정보를 불러온다.") @Test - public void findAllTest() { + public void getAllMajorInfoTest() { // given - MajorInfo majorInfo1 = new MajorInfo("공과대학", "기계공학과"); - MajorInfo majorInfo2 = new MajorInfo("자연과학대학", "수학과"); - MajorInfo majorInfo3 = new MajorInfo("경영대학", "경영학과"); - ReflectionTestUtils.setField(majorInfo1, "id", 1); - ReflectionTestUtils.setField(majorInfo2, "id", 2); - ReflectionTestUtils.setField(majorInfo3, "id", 3); - List majorInfos = - new ArrayList<>() { - { - add(majorInfo1); - add(majorInfo2); - add(majorInfo3); - } - }; - + List majorInfos = createSampleMajorInfos(); given(majorInfoRepository.findAll()).willReturn(majorInfos); // when @@ -68,7 +66,7 @@ public void findAllTest() { @DisplayName("새로운 학과를 추가한다.") @Test - public void saveMajorInfoTest() { + public void saveNewMajorInfoTest() { // given MajorInfoSaveDto newMajor = new MajorInfoSaveDto("경영대학", "글로벌금융학과");