From da057ff04fa7c3757c4171707be7d02efa9d33b5 Mon Sep 17 00:00:00 2001 From: whitemark Date: Wed, 20 Dec 2023 15:41:59 +0900 Subject: [PATCH] =?UTF-8?q?[refactor/#185]=20=EB=82=B4=20=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=20Service=20Test=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../oauth2/member/dto/ProfileIntroDto.java | 2 + .../oauth2/member/dto/ProfileNameDto.java | 2 + .../oauth2/member/service/MemberService.java | 4 +- .../member/service/MemberServiceImpl.java | 33 +-- .../member/domain/entity/MemberTest.java | 2 +- .../member/service/MemberServiceTest.java | 218 ++++++++++++++---- 6 files changed, 182 insertions(+), 79 deletions(-) diff --git a/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/member/dto/ProfileIntroDto.java b/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/member/dto/ProfileIntroDto.java index 881bc917..e141144c 100644 --- a/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/member/dto/ProfileIntroDto.java +++ b/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/member/dto/ProfileIntroDto.java @@ -1,10 +1,12 @@ package com.inhabas.api.auth.domain.oauth2.member.dto; +import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; @Getter @Setter +@AllArgsConstructor public class ProfileIntroDto { private String introduce; diff --git a/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/member/dto/ProfileNameDto.java b/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/member/dto/ProfileNameDto.java index 7098e761..88800572 100644 --- a/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/member/dto/ProfileNameDto.java +++ b/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/member/dto/ProfileNameDto.java @@ -1,5 +1,6 @@ package com.inhabas.api.auth.domain.oauth2.member.dto; +import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; @@ -7,6 +8,7 @@ @Getter @Setter +@AllArgsConstructor public class ProfileNameDto { @NotBlank diff --git a/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/member/service/MemberService.java b/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/member/service/MemberService.java index 035728a8..bb914f1b 100644 --- a/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/member/service/MemberService.java +++ b/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/member/service/MemberService.java @@ -7,13 +7,10 @@ import org.springframework.data.domain.Pageable; import java.util.List; -import java.util.Optional; public interface MemberService { // 가입 관련 - void save(Member member); - Optional updateMember(Member member); void changeRole(Member member, Role role); void finishSignUp(Member member); @@ -36,4 +33,5 @@ public interface MemberService { void updateMyProfileDetail(Long memberId, ProfileDetailDto profileDetailDto); void updateMyProfileIntro(Long memberId, ProfileIntroDto profileIntroDto); void requestMyProfileName(Long memberId, ProfileNameDto profileNameDto); + } diff --git a/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/member/service/MemberServiceImpl.java b/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/member/service/MemberServiceImpl.java index baf8704b..0bfa4b76 100644 --- a/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/member/service/MemberServiceImpl.java +++ b/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/member/service/MemberServiceImpl.java @@ -2,7 +2,6 @@ import com.inhabas.api.auth.domain.oauth2.member.domain.entity.Member; import com.inhabas.api.auth.domain.oauth2.member.domain.entity.UpdateNameRequest; -import com.inhabas.api.auth.domain.oauth2.member.domain.exception.DuplicatedMemberFieldException; import com.inhabas.api.auth.domain.oauth2.member.domain.exception.MemberNotFoundException; import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.Role; import com.inhabas.api.auth.domain.oauth2.member.dto.*; @@ -21,7 +20,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.Optional; import java.util.stream.Collectors; import static com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.Role.*; @@ -41,28 +39,6 @@ public class MemberServiceImpl implements MemberService { private final MemberDuplicationChecker duplicationChecker; - @Override - @Transactional - public void save(Member member) { - - if (duplicationChecker.isDuplicatedMember(member)) { - throw new DuplicatedMemberFieldException("provider 와 uid"); - } - - memberRepository.save(member); - } - - @Override - @Transactional - public Optional updateMember(Member member) { - return DoesExistMember(member) ? - Optional.of(memberRepository.save(member)) : Optional.empty(); - } - - private boolean DoesExistMember(Member member) { - return memberRepository.findById(member.getId()).isPresent(); - } - @Transactional public void changeRole(Member member, Role role) { member.setRole(role); @@ -231,9 +207,12 @@ public void updateMyProfileDetail(Long memberId, ProfileDetailDto profileDetailD Member member = memberRepository.findById(memberId).orElseThrow(MemberNotFoundException::new); - member.getSchoolInformation().setGrade(profileDetailDto.getGrade()); - member.getSchoolInformation().setMajor(profileDetailDto.getMajor()); - member.setPhone(profileDetailDto.getPhoneNumber()); + if(profileDetailDto.getMajor() != null) + member.getSchoolInformation().setMajor(profileDetailDto.getMajor()); + if(profileDetailDto.getPhoneNumber() != null) + member.setPhone(profileDetailDto.getPhoneNumber()); + if(profileDetailDto.getGrade() != null) + member.getSchoolInformation().setGrade(profileDetailDto.getGrade()); } diff --git a/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/member/domain/entity/MemberTest.java b/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/member/domain/entity/MemberTest.java index a4dd1120..89fd0c64 100644 --- a/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/member/domain/entity/MemberTest.java +++ b/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/member/domain/entity/MemberTest.java @@ -31,7 +31,7 @@ private static Member afterSignUpMember(Role role) { // 회원정보 저장 member.setStudentId("12171707"); member.setPhone("010-1111-1111"); - member.setName("유동현"); + member.setName("조승현"); member.setSchoolInformation(new SchoolInformation("컴퓨터공학과", 1, 1, UNDERGRADUATE)); return member; diff --git a/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/member/service/MemberServiceTest.java b/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/member/service/MemberServiceTest.java index 79e747d8..31d4f699 100644 --- a/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/member/service/MemberServiceTest.java +++ b/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/member/service/MemberServiceTest.java @@ -2,22 +2,28 @@ import com.inhabas.api.auth.domain.oauth2.member.domain.entity.Member; -import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.IbasInformation; -import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.SchoolInformation; -import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.StudentId; -import com.inhabas.api.auth.domain.oauth2.member.dto.ContactDto; +import com.inhabas.api.auth.domain.oauth2.member.dto.*; import com.inhabas.api.auth.domain.oauth2.member.repository.MemberRepository; +import com.inhabas.api.auth.domain.oauth2.member.repository.UpdateNameRequestRepository; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import static com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.Role.*; +import java.util.List; +import java.util.Optional; + +import static com.inhabas.api.auth.domain.oauth2.member.domain.entity.MemberTest.*; +import static com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.Role.EXECUTIVES; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; +import static org.mockito.Mockito.times; @ExtendWith(MockitoExtension.class) public class MemberServiceTest { @@ -26,64 +32,180 @@ public class MemberServiceTest { MemberServiceImpl memberService; @Mock MemberRepository memberRepository; + @Mock + UpdateNameRequestRepository updateNameRequestRepository; + + + @DisplayName("미승인 회원을 [역할과 {학번 or 이름}] 으로 조회한다.") + @Test + void getNotApprovedMembersBySearchAndRoleTest() { + //given + Member member = notapprovedMember(); + given(memberRepository.findAllByRoleAndStudentIdLike(any(), any())).willReturn(List.of(member)); + NotApprovedMemberManagementDto notApprovedMemberManagementDto = + new NotApprovedMemberManagementDto( + member.getName(), + member.getId(), + member.getStudentId(), + member.getPhone(), + member.getEmail(), + member.getSchoolInformation().getGrade(), + member.getSchoolInformation().getMajor()); + + //when + List notApprovedMemberManagementDtos = + memberService.getNotApprovedMembersBySearchAndRole("12171707"); + + //then + assertThat(notApprovedMemberManagementDtos.get(0).getMemberId()).isEqualTo(notApprovedMemberManagementDto.getMemberId()); + + } + + @DisplayName("비활동이상 회원을 [역할과 {학번 or 이름}] 으로 조회한다.") + @Test + void getApprovedMembersBySearchAndRole() { + //given + Member member = deactivatedMember(); + given(memberRepository.findAllByRolesInAndStudentIdLike(any(), any())).willReturn(List.of(member)); + ApprovedMemberManagementDto approvedMemberManagementDto = + new ApprovedMemberManagementDto( + member.getName(), + member.getId(), + member.getStudentId(), + member.getPhone(), + member.getRole(), + member.getSchoolInformation().getGeneration(), + member.getSchoolInformation().getMajor()); + + //when + List approvedMemberManagementDtos = + memberService.getApprovedMembersBySearchAndRole("12171707"); + //then + assertThat(approvedMemberManagementDtos.get(0).getMemberId()).isEqualTo(approvedMemberManagementDto.getMemberId()); + + } + + @DisplayName("미승인 회원들을 합격하거나 불합격한다.") + @ParameterizedTest + @ValueSource(strings = {"pass", "fail"}) + void updateUnapprovedMembersTest(String state) { + //given + Member member = notapprovedMember(); + given(memberRepository.findAllById(any())).willReturn(List.of(member)); + + //when + memberService.updateUnapprovedMembers(any(), state); + + //then + if (state.equals("pass")) + then(memberRepository).should(times(1)).saveAll(any()); + else if (state.equals("fail")) + then(memberRepository).should(times(1)).deleteAll(any()); - @DisplayName("회원의 권한을 변경한다.") + } + + @DisplayName("비활동 이상 회원들의 역할을 가능한 만큼만 수정한다.") @Test - public void changeRoleTest() { + void updateApprovedMembersTest() { //given - StudentId studentId = new StudentId("12171652"); - Member targetMember = Member.builder() - .studentId(studentId) - .picture("") - .name("유동현") - .email("my@gmail.com") - .phone("010-0000-0000") - .schoolInformation(SchoolInformation.ofUnderGraduate("정보통신공학과", 1)) - .ibasInformation(new IbasInformation(ANONYMOUS)) - .build(); - - assert targetMember != null; - Member result = Member.builder() - .studentId(studentId) - .picture(targetMember.getPicture()) - .name(targetMember.getName()) - .email("my@gmail.com") - .phone(targetMember.getPhone()) - .schoolInformation(targetMember.getSchoolInformation()) - .ibasInformation(new IbasInformation(NOT_APPROVED)) - .build(); - given(memberRepository.save(any(Member.class))) - .willReturn(result); // NOT care about this return-value of save() in Service logic + Member member = deactivatedMember(); + given(memberRepository.findAllById(any())).willReturn(List.of(member)); //when - memberService.changeRole(targetMember, NOT_APPROVED); + memberService.updateApprovedMembers(List.of(1L), EXECUTIVES); //then - assertThat(targetMember.getIbasInformation().getRole()) - .isEqualTo(NOT_APPROVED); + then(memberRepository).should(times(1)).saveAll(any()); + } - @DisplayName("회장 연락처 불러오기") + @DisplayName("회장 연락처 조회한다.") @Test - public void getChiefContact() { - Member chief = Member.builder() - .studentId(new StudentId("12171652")) - .picture("") - .name("유동현") - .email("my@gmail.com") - .phone("010-0000-0000") - .schoolInformation(SchoolInformation.ofUnderGraduate("정보통신공학과", 1)) - .ibasInformation(new IbasInformation(CHIEF)) - .build(); - given(memberRepository.findByIbasInformation_Role(any())).willReturn(chief); + public void getChiefContactTest() { + //given + Member member = chiefMember(); + given(memberRepository.findByIbasInformation_Role(any())).willReturn(member); //when ContactDto chiefContact = memberService.getChiefContact(); //then - assertThat(chiefContact.getEmail()).isEqualTo(chief.getEmail()); - assertThat(chiefContact.getPhoneNumber()).isEqualTo(chief.getPhone()); - assertThat(chiefContact.getName()).isEqualTo(chief.getName()); + assertThat(chiefContact.getEmail()).isEqualTo(member.getEmail()); + assertThat(chiefContact.getPhoneNumber()).isEqualTo(member.getPhone()); + assertThat(chiefContact.getName()).isEqualTo(member.getName()); } + + @DisplayName("내 정보를 조회한다.") + @Test + void getMyProfileTest() { + //given + Member member = basicMember(); + given(memberRepository.findById(any())).willReturn(Optional.of(member)); + + //when + MyProfileDto myProfileDto = memberService.getMyProfile(any()); + + //then + assertThat(myProfileDto.getName()).isEqualTo(member.getName()); + assertThat(myProfileDto.getStudentId()).isEqualTo(member.getStudentId()); + assertThat(myProfileDto.getMajor()).isEqualTo(member.getSchoolInformation().getMajor()); + assertThat(myProfileDto.getGrade()).isEqualTo(member.getSchoolInformation().getGrade()); + assertThat(myProfileDto.getEmail()).isEqualTo(member.getEmail()); + assertThat(myProfileDto.getPhoneNumber()).isEqualTo(member.getPhone()); + assertThat(myProfileDto.getRole()).isEqualTo(member.getRole()); + assertThat(myProfileDto.getType()).isEqualTo(member.getSchoolInformation().getMemberType()); + assertThat(myProfileDto.getIntroduce()).isEqualTo(member.getIbasInformation().getIntroduce()); + + } + + @DisplayName("내 정보 [학년, 전공, 전화번호] 를 수정한다.") + @Test + void updateMyProfileDetailTest() { + //given + Member member = basicMember(); + given(memberRepository.findById(any())).willReturn(Optional.of(member)); + ProfileDetailDto profileDetailDto = new ProfileDetailDto("경영학과", null, null); + + //when + memberService.updateMyProfileDetail(any(), profileDetailDto); + + //then + assertThat(member.getSchoolInformation().getMajor()).isEqualTo(profileDetailDto.getMajor()); + + } + + @DisplayName("내 정보 [자기소개] 를 수정한다.") + @Test + void updateMyProfileIntroTest() { + //given + Member member = basicMember(); + given(memberRepository.findById(any())).willReturn(Optional.of(member)); + ProfileIntroDto profileIntroDto = new ProfileIntroDto("HELLO", true); + + //when + memberService.updateMyProfileIntro(any(), profileIntroDto); + + //then + assertThat(member.getIbasInformation().getIntroduce()).isEqualTo(profileIntroDto.getIntroduce()); + assertThat(member.getIbasInformation().getIsHOF()).isEqualTo(profileIntroDto.getIsHOF()); + + } + + @DisplayName("내 정보 [이름] 을 수정 요청을 한다.") + @Test + void requestMyProfileNameTest() { + //given + Member member = basicMember(); + given(memberRepository.findById(any())).willReturn(Optional.of(member)); + ProfileNameDto profileNameDto = new ProfileNameDto("유동현"); + + //when + memberService.requestMyProfileName(any(), profileNameDto); + + //then + then(updateNameRequestRepository).should(times(1)).save(any()); + + } + }