Skip to content

Commit

Permalink
[refactor/#185] 내 정보 Service Test code
Browse files Browse the repository at this point in the history
  • Loading branch information
whitem4rk committed Dec 20, 2023
1 parent 3ca17ec commit da057ff
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.inhabas.api.auth.domain.oauth2.member.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import javax.validation.constraints.NotBlank;

@Getter
@Setter
@AllArgsConstructor
public class ProfileNameDto {

@NotBlank
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Member> updateMember(Member member);
void changeRole(Member member, Role role);
void finishSignUp(Member member);

Expand All @@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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.*;
Expand All @@ -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<Member> 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);
Expand Down Expand Up @@ -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());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<NotApprovedMemberManagementDto> 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<ApprovedMemberManagementDto> 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("[email protected]")
.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("[email protected]")
.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("[email protected]")
.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());

}

}

0 comments on commit da057ff

Please sign in to comment.