Skip to content

Commit

Permalink
test: memberService 테스트 코드 리팩토링 및 ssafyInfo null 처리 추가
Browse files Browse the repository at this point in the history
* chore: AuthFixture directory 구조 변경

* refactor: static Fixture에서 method로 instance 반환하도록 리팩토링

* fix: MemberFixture 변경으로 인한 코드 수정

* fix: MemberFixture 리팩토링 변경으로 인한 Comment, Post Fixture 수정

* fix: MemberFixture 변경으로 인한 Recruit 도메인 Fixture 수정

* refactor: MemberFixture 리팩토링으로 인한 MemberService 코드 수정

* fix: MemberService 토큰 저장 메서드 파라미터 간소화

* test: MemberService 코드 리팩토링

* test: metadataConsumer given 설정 추가

* fix: SSAFYInfo Null Pointer Exception 방지를 위한 검증 코드 추가

* feat: member의 학기 및 캠퍼스 정보 null 예외 처리 추가
  • Loading branch information
YongsHub authored Sep 23, 2023
1 parent a5ab3b9 commit 72a658f
Show file tree
Hide file tree
Showing 13 changed files with 611 additions and 519 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ public EnvelopeResponse<CreateMemberTokensResDto> login(
AuthenticatedMember authenticatedMember = memberService.createMemberByOauthIdentifier(postMemberReqDto);
CreateMemberTokensResDto createMemberTokensResDto = authService.createToken(authenticatedMember);

Member member = memberService.saveTokenByMember(
authenticatedMember,
createMemberTokensResDto.getAccessToken(),
createMemberTokensResDto.getRefreshToken());
Member member = memberService.saveTokenByMember(authenticatedMember, createMemberTokensResDto);

cookieProvider.setResponseWithCookies(
response,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.ssafy.ssafsound.domain.member.dto;

import com.ssafy.ssafsound.domain.member.domain.Member;
import com.ssafy.ssafsound.domain.member.exception.MemberErrorInfo;
import com.ssafy.ssafsound.domain.member.exception.MemberException;
import com.ssafy.ssafsound.domain.meta.domain.MetaData;
import lombok.Getter;

Expand All @@ -24,7 +26,14 @@ private SSAFYInfo(int semester, String campus, String certificationState, String
}

public static SSAFYInfo from(Member member) {
return new SSAFYInfo(member.getSemester(), member.getCampus().getName(), member.getCertificationState().name(), member.getMajorTrack() == null ? null : member.getMajorTrack().getName());
if(member.getSemester() == null) throw new MemberException(MemberErrorInfo.MEMBER_INFORMATION_ERROR);
if(member.getCampus().getName() == null) throw new MemberException(MemberErrorInfo.MEMBER_INFORMATION_ERROR);

return new SSAFYInfo(
member.getSemester(),
member.getCampus().getName(),
member.getCertificationState().name(),
member.getMajorTrack() == null ? null : member.getMajorTrack().getName());
}
public static SSAFYInfo of(int semester, String campus, String certificationState, MetaData majorTrack) {
if (majorTrack == null) return new SSAFYInfo(semester, campus, certificationState);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.ssafy.ssafsound.domain.member.service;

import com.ssafy.ssafsound.domain.auth.dto.AuthenticatedMember;
import com.ssafy.ssafsound.domain.auth.service.AuthService;
import com.ssafy.ssafsound.domain.auth.util.ClientUtils;
import com.ssafy.ssafsound.domain.auth.dto.CreateMemberTokensResDto;
import com.ssafy.ssafsound.domain.member.domain.*;
import com.ssafy.ssafsound.domain.member.dto.*;
import com.ssafy.ssafsound.domain.member.exception.MemberErrorInfo;
Expand Down Expand Up @@ -50,14 +49,18 @@ public AuthenticatedMember createMemberByOauthIdentifier(PostMemberReqDto postMe
}

@Transactional
public Member saveTokenByMember(AuthenticatedMember authenticatedMember, String accessToken, String refreshToken) {
public Member saveTokenByMember(
AuthenticatedMember authenticatedMember,
CreateMemberTokensResDto createMemberTokensResDto) {
Member member = memberRepository.findById(authenticatedMember.getMemberId())
.orElseThrow(() -> new MemberException(MemberErrorInfo.MEMBER_NOT_FOUND_BY_ID));

Optional<MemberToken> memberTokenOptional = memberTokenRepository.findById(authenticatedMember.getMemberId());
Optional<MemberToken> memberTokenOptional = memberTokenRepository
.findById(authenticatedMember.getMemberId());

memberTokenOptional.ifPresentOrElse(memberToken -> changeMemberTokens(memberToken, accessToken, refreshToken),
() -> createMemberToken(member, accessToken, refreshToken));
memberTokenOptional.ifPresentOrElse(
memberToken -> changeMemberTokens(memberToken, createMemberTokensResDto),
() -> createMemberToken(member, createMemberTokensResDto));

return member;
}
Expand Down Expand Up @@ -287,23 +290,28 @@ private boolean isGeneralMemberInformation(Member member) {
}

private boolean isSSAFYMemberInformation(Member member) {
return member.getSsafyMember() && member.getNickname() != null && member.getMajor() != null;
return member.getSsafyMember() && member.getNickname() != null && member.getMajor() != null
&& member.getSemester() != null && member.getCampus() != null && member.getCertificationState() != null;
}

private Member getMemberByMemberIdOrThrowException(Long memberId) {
return memberRepository.findById(memberId)
.orElseThrow(() -> new MemberException(MemberErrorInfo.MEMBER_NOT_FOUND_BY_ID));
}

private void changeMemberTokens(MemberToken memberToken, String accessToken, String refreshToken) {
memberToken.changeAccessTokenByLogin(accessToken);
memberToken.changeRefreshTokenByLogin(refreshToken);
private void changeMemberTokens(
MemberToken memberToken,
CreateMemberTokensResDto createMemberTokensResDto) {
memberToken.changeAccessTokenByLogin(createMemberTokensResDto.getAccessToken());
memberToken.changeRefreshTokenByLogin(createMemberTokensResDto.getRefreshToken());
}

private void createMemberToken(Member member, String accessToken, String refreshToken) {
private void createMemberToken(
Member member,
CreateMemberTokensResDto createMemberTokensResDto) {
MemberToken memberToken = MemberToken.builder()
.accessToken(accessToken)
.refreshToken(refreshToken)
.accessToken(createMemberTokensResDto.getAccessToken())
.refreshToken(createMemberTokensResDto.getRefreshToken())
.member(member)
.build();
memberTokenRepository.save(memberToken);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ssafy.ssafsound.domain.auth.controller;

import com.ssafy.ssafsound.domain.auth.fixture.AuthFixture;
import com.ssafy.ssafsound.global.util.fixture.AuthFixture;
import com.ssafy.ssafsound.global.docs.ControllerTest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ssafy.ssafsound.domain.member.controller;

import com.ssafy.ssafsound.domain.member.dto.GetMemberDefaultInfoResDto;
import com.ssafy.ssafsound.domain.member.dto.GetMemberResDto;
import com.ssafy.ssafsound.domain.member.dto.PostMemberInfoReqDto;
import com.ssafy.ssafsound.global.docs.ControllerTest;
Expand All @@ -25,6 +26,8 @@

class MemberControllerTest extends ControllerTest {

private final MemberFixture memberFixture = new MemberFixture();

public ResponseFieldsSnippet getPortfolioSnippet() {
return getEnvelopPatternWithData()
.andWithPrefix("data.portfolioElement.",
Expand Down Expand Up @@ -78,7 +81,7 @@ public ResponseFieldsSnippet getMemberSnippet() {
void getMemberInformationByFirstTry() {

given(memberService.getMemberInformation(any()))
.willReturn(GetMemberResDto.fromGeneralUser(MemberFixture.INITIALIZER_MEMBER));
.willReturn(GetMemberResDto.fromGeneralUser(memberFixture.createInitializerMember()));

restDocs
.cookie(ACCESS_TOKEN)
Expand All @@ -102,7 +105,7 @@ void getMemberInformationByFirstTry() {
void getGeneralMemberInformation() {

given(memberService.getMemberInformation(any()))
.willReturn(GetMemberResDto.fromGeneralUser(MemberFixture.GENERAL_MEMBER));
.willReturn(GetMemberResDto.fromGeneralUser(memberFixture.createGeneralMember()));

restDocs
.cookie(ACCESS_TOKEN)
Expand Down Expand Up @@ -132,7 +135,7 @@ void getGeneralMemberInformation() {
void getSSAFYMemberInformation() {

given(memberService.getMemberInformation(any()))
.willReturn(MemberFixture.CERTIFIED_SSAFY_MEMBER);
.willReturn(memberFixture.createCertifiedSSAFYMemberResDto());

restDocs
.cookie(ACCESS_TOKEN)
Expand All @@ -150,7 +153,7 @@ void getSSAFYMemberInformation() {
void putMemberInformation() {

given(memberService.registerMemberInformation(any(), any()))
.willReturn(GetMemberResDto.fromGeneralUser(MemberFixture.GENERAL_MEMBER));
.willReturn(GetMemberResDto.fromGeneralUser(memberFixture.createGeneralMember()));
given(semesterValidator.isValid(any(), any())).willReturn(true);

PostMemberInfoReqDto postMemberInfoReqDto = PostMemberInfoReqDto.builder()
Expand Down Expand Up @@ -178,7 +181,7 @@ void putMemberInformation() {
void putMemberSSAFYInformation() {

given(memberService.registerMemberInformation(any(), any()))
.willReturn(MemberFixture.UNCERTIFIED_SSAFY_MEMBER);
.willReturn(memberFixture.createUncertifiedSSAFYMemberResDto());
given(semesterValidator.isValid(any(), any())).willReturn(true);
given(semesterConstantProvider.getMAX_SEMESTER()).willReturn(10);
given(semesterConstantProvider.getMIN_SEMESTER()).willReturn(1);
Expand Down Expand Up @@ -210,15 +213,15 @@ void putMemberSSAFYInformation() {
void certificationSSAFY() {

given(memberService.certifySSAFYInformation(any(), any()))
.willReturn(MemberFixture.POST_CERTIFICATION_INFO_RESPONSE);
.willReturn(memberFixture.createPostCertificationInfoResDto());
given(semesterValidator.isValid(any(), any())).willReturn(true);
given(semesterConstantProvider.getMAX_SEMESTER()).willReturn(10);
given(semesterConstantProvider.getMIN_SEMESTER()).willReturn(1);

restDocs
.contentType(MediaType.APPLICATION_JSON_VALUE)
.cookie(ACCESS_TOKEN)
.body(MemberFixture.POST_CERTIFICATION_INFO_REQUEST)
.body(memberFixture.createPostCertificationInfoReqDto())
.when().post("/members/ssafy-certification")
.then().log().all()
.assertThat()
Expand All @@ -241,7 +244,7 @@ void certificationSSAFY() {
void getMyPortfolio() {

given(memberService.getMyPortfolio(any()))
.willReturn(MemberFixture.MY_PORTFOLIO);
.willReturn(memberFixture.createGetMemberPortfolioResDto());

restDocs
.cookie(ACCESS_TOKEN)
Expand All @@ -259,7 +262,7 @@ void getMyPortfolio() {
void getOtherPortfolio() {

given(memberService.getMemberPortfolioById(any()))
.willReturn(MemberFixture.MY_PORTFOLIO);
.willReturn(memberFixture.createGetMemberPortfolioResDto());

restDocs
.when().get("members/{memberId}/portfolio", 99)
Expand All @@ -279,7 +282,7 @@ void putMemberPortfolio() {
restDocs
.cookie(ACCESS_TOKEN)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(MemberFixture.PUT_MEMBER_PORTFOLIO_REQUEST)
.body(memberFixture.createPutMemberPortfolioReqDto())
.when().put("members/portfolio")
.then().log().all()
.assertThat()
Expand All @@ -300,7 +303,7 @@ void putMemberPortfolio() {
void getMemberDefaultInformation() {

given(memberService.getMemberDefaultInfoByMemberId(any()))
.willReturn(MemberFixture.GET_MEMBER_DEFAULT_INFO);
.willReturn(GetMemberDefaultInfoResDto.from(memberFixture.createMember()));

restDocs
.when().get("members/{memberId}/default-information", 99)
Expand Down Expand Up @@ -334,7 +337,7 @@ void patchMemberDefaultInformation() {
restDocs
.cookie(ACCESS_TOKEN)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(MemberFixture.PATCH_MEMBER_DEFAULT_INFO_REQUEST)
.body(memberFixture.createPatchMemberDefaultInfoReqDto())
.when().patch("members/default-information")
.then().log().all()
.assertThat()
Expand All @@ -353,7 +356,7 @@ void patchMemberDefaultInformation() {
void getStatusOfPublicProfile() {

given(memberService.getMemberPublicProfileByMemberId(any()))
.willReturn(MemberFixture.GET_MEMBER_PUBLIC_PROFILE);
.willReturn(memberFixture.createGetMemberPublicProfileResDto());

restDocs
.cookie(ACCESS_TOKEN)
Expand All @@ -374,7 +377,7 @@ void getStatusOfPublicProfile() {
void getOtherStatusOfPublicProfile() {

given(memberService.getMemberPublicProfileByMemberId(any()))
.willReturn(MemberFixture.GET_MEMBER_PUBLIC_PROFILE);
.willReturn(memberFixture.createGetMemberPublicProfileResDto());

restDocs
.cookie(ACCESS_TOKEN)
Expand All @@ -398,7 +401,7 @@ void changePublicProfile() {
restDocs
.cookie(ACCESS_TOKEN)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(MemberFixture.PATCH_MEMBER_PUBLIC_PROFILE_REQUEST)
.body(memberFixture.createPatchMemberPublicProfileReqDto())
.when().patch("members/public-profile")
.then().log().all()
.assertThat()
Expand All @@ -415,11 +418,11 @@ void changePublicProfile() {
void checkNicknamePossible() {

given(memberService.checkNicknamePossible(any()))
.willReturn(MemberFixture.POST_NICKNAME_RESPONSE);
.willReturn(memberFixture.createPostNicknameResDto());

restDocs
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(MemberFixture.POST_NICKNAME_REQUEST)
.body(memberFixture.createPostNicknameReqDto())
.when().post("members/nickname")
.then().log().all()
.assertThat()
Expand All @@ -442,7 +445,7 @@ void changeNickname() {
restDocs
.cookie(ACCESS_TOKEN)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(MemberFixture.POST_NICKNAME_REQUEST)
.body(memberFixture.createPostNicknameReqDto())
.when().patch("members/nickname")
.then().log().all()
.assertThat()
Expand All @@ -461,7 +464,7 @@ void changeMemberMajor() {
restDocs
.cookie(ACCESS_TOKEN)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(MemberFixture.PATCH_IS_MAJOR_REQUEST)
.body(memberFixture.createPatchMemberMajorReqDto())
.when().patch("members/major")
.then().log().all()
.assertThat()
Expand All @@ -480,7 +483,7 @@ void changeMemberMajorTrack() {
restDocs
.cookie(ACCESS_TOKEN)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(MemberFixture.PATCH_MEMBER_MAJOR_TRACK)
.body(memberFixture.createPatchMemberMajorTrackReqDto())
.when().patch("members/major-track")
.then().log().all()
.assertThat()
Expand Down
Loading

0 comments on commit 72a658f

Please sign in to comment.