Skip to content

Commit

Permalink
test : 유저를 저장하는 기능 Service Test 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
kpeel5839 committed Aug 3, 2023
1 parent 342f35e commit 864b851
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public MemberCommandService(
}

public Long save(MemberCreateRequest request) {
validateUniqueName(request.name());
validateUniqueEmail(request.email());

Member member = Member.of(
request.name(),
request.email(),
Expand All @@ -43,6 +46,18 @@ public Long save(MemberCreateRequest request) {
.getId();
}

private void validateUniqueName(String name) {
if (memberRepository.existsByMemberInfoName(name)) {
throw new IllegalArgumentException("이미 존재하는 이름입니다.");
}
}

private void validateUniqueEmail(String email) {
if (memberRepository.existsByMemberInfoEmail(email)) {
throw new IllegalArgumentException("이미 존재하는 이메일입니다.");
}
}

public Long saveMemberTopicPermission(
AuthMember authMember,
MemberTopicPermissionCreateRequest request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public interface MemberRepository extends JpaRepository<Member, Long> {

Optional<Member> findByMemberInfoEmail(String email);

boolean existsByMemberInfoName(String name);

boolean existsByMemberInfoEmail(String email);

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import com.mapbefine.mapbefine.member.domain.Member;
import com.mapbefine.mapbefine.member.domain.MemberRepository;
import com.mapbefine.mapbefine.member.domain.Role;
import com.mapbefine.mapbefine.member.dto.request.MemberCreateRequest;
import com.mapbefine.mapbefine.member.dto.request.MemberTopicPermissionCreateRequest;
import com.mapbefine.mapbefine.member.dto.response.MemberDetailResponse;
import com.mapbefine.mapbefine.topic.TopicFixture;
import com.mapbefine.mapbefine.topic.domain.Topic;
import com.mapbefine.mapbefine.topic.domain.TopicRepository;
import java.util.NoSuchElementException;
import java.util.Optional;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -214,4 +216,75 @@ void deleteMemberTopicPermissionByCreator_whenNoneExistsPermission_thenFail() {
.isInstanceOf(NoSuchElementException.class);
}

@Test
@DisplayName("유저를 저장한다.")
void save() {
// given
Member member = MemberFixture.create(
"member",
"member@naver.com",
Role.USER
);
MemberCreateRequest memberCreateRequest = new MemberCreateRequest(
member.getMemberInfo().getName(),
member.getMemberInfo().getEmail(),
member.getMemberInfo().getImageUrl(),
member.getMemberInfo().getRole()
);

// when
Long savedId = memberCommandService.save(memberCreateRequest);
Member savedResult = memberRepository.findById(savedId)
.orElseThrow(NoSuchElementException::new);

// then
assertThat(savedResult).usingRecursiveComparison()
.ignoringFields("id")
.isEqualTo(member);
}

@Test
@DisplayName("이미 존재하는 이름으로 유저를 저장할 때 예외가 발생한다.")
void save_whenDuplicateName_thenFail() {
// given
Member member = MemberFixture.create(
"member",
"member@naver.com",
Role.USER
);
memberRepository.save(member);
MemberCreateRequest memberCreateRequest = new MemberCreateRequest(
member.getMemberInfo().getName(),
"memberr@naver.com",
member.getMemberInfo().getImageUrl(),
member.getMemberInfo().getRole()
);

// when
assertThatThrownBy(() -> memberCommandService.save(memberCreateRequest))
.isInstanceOf(IllegalArgumentException.class);
}

@Test
@DisplayName("이미 존재하는 이메일로 유저를 저장할 때 예외가 발생한다.")
void save_whenDuplicateEmail_thenFail() {
// given
Member member = MemberFixture.create(
"member",
"member@naver.com",
Role.USER
);
memberRepository.save(member);
MemberCreateRequest memberCreateRequest = new MemberCreateRequest(
"memberr",
member.getMemberInfo().getEmail(),
member.getMemberInfo().getImageUrl(),
member.getMemberInfo().getRole()
);

// when
assertThatThrownBy(() -> memberCommandService.save(memberCreateRequest))
.isInstanceOf(IllegalArgumentException.class);
}

}

0 comments on commit 864b851

Please sign in to comment.