From 3e2f0175b048c68f48ec5dad97896f8fa6637e5e Mon Sep 17 00:00:00 2001 From: unanchoi Date: Wed, 28 Feb 2024 02:35:52 +0900 Subject: [PATCH] =?UTF-8?q?Refactor=20for=202=EC=A3=BC=EC=B0=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring-code-for-deploy/build.gradle | 3 + .../com/example/seminar/domain/Member.java | 13 ++++ .../java/com/example/seminar/domain/Part.java | 4 ++ .../java/com/example/seminar/domain/Post.java | 8 +++ .../seminar/repository/PostJpaRepository.java | 1 + .../seminar/service/member/MemberUpdater.java | 1 - .../example/seminar/domain/MemberTest.java | 67 +++++++++++------- .../com/example/seminar/domain/PartTest.java | 14 +++- .../repository/MemberJpaRepositoryTest.java | 59 ++++++++++++++++ .../repository/PostJpaRepositoryTest.java | 69 +++++++++++++++++++ .../service/member/MemberSaverTest.java | 13 ++-- 11 files changed, 218 insertions(+), 34 deletions(-) diff --git a/spring-code-for-deploy/build.gradle b/spring-code-for-deploy/build.gradle index c0b6906..2f9202f 100644 --- a/spring-code-for-deploy/build.gradle +++ b/spring-code-for-deploy/build.gradle @@ -53,6 +53,9 @@ dependencies { annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' + // validation + implementation 'org.springframework.boot:spring-boot-starter-validation' + //h2 runtimeOnly 'com.h2database:h2' diff --git a/spring-code-for-deploy/src/main/java/com/example/seminar/domain/Member.java b/spring-code-for-deploy/src/main/java/com/example/seminar/domain/Member.java index d75ba18..90ea064 100644 --- a/spring-code-for-deploy/src/main/java/com/example/seminar/domain/Member.java +++ b/spring-code-for-deploy/src/main/java/com/example/seminar/domain/Member.java @@ -7,6 +7,7 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import org.springframework.util.Assert; import java.util.ArrayList; import java.util.List; @@ -42,6 +43,7 @@ private Member(String name, SOPT sopt) { validateAge(age); validateName(name); + validateNickname(nickname); this.name = name; this.nickname = nickname; this.age = age; @@ -54,12 +56,23 @@ private void validateAge(final int age) { } } + // SOPT는 한국인만 가입 가능함. private void validateName(final String name) { +// if (!name.matches("ㅎ가-힣")) { +// throw new MemberException("한글만 가능합니다."); +// } if (name.length() > MAX_LENGTH) { throw new MemberException("유저의 이름은 12자를 넘을 수 없습니다."); } } + private void validateNickname(final String nickname) { + + if (nickname.length() > 8) { + throw new MemberException("유저의 닉네임은 8자를 넘길 수 없습니다."); + } + } + public void updateSOPT(SOPT sopt) { this.sopt = sopt; } diff --git a/spring-code-for-deploy/src/main/java/com/example/seminar/domain/Part.java b/spring-code-for-deploy/src/main/java/com/example/seminar/domain/Part.java index b8f2da1..e9a77fc 100644 --- a/spring-code-for-deploy/src/main/java/com/example/seminar/domain/Part.java +++ b/spring-code-for-deploy/src/main/java/com/example/seminar/domain/Part.java @@ -22,4 +22,8 @@ public enum Part { public static List developParts() { return List.of(SERVER, WEB, ANDROID, IOS); } + + public static List clientDevelopParts() { + return List.of(WEB, ANDROID, IOS); + } } diff --git a/spring-code-for-deploy/src/main/java/com/example/seminar/domain/Post.java b/spring-code-for-deploy/src/main/java/com/example/seminar/domain/Post.java index 6351600..fe17c54 100644 --- a/spring-code-for-deploy/src/main/java/com/example/seminar/domain/Post.java +++ b/spring-code-for-deploy/src/main/java/com/example/seminar/domain/Post.java @@ -1,6 +1,7 @@ package com.example.seminar.domain; +import com.example.seminar.common.exception.PostException; import jakarta.persistence.*; import lombok.Builder; import lombok.Getter; @@ -30,11 +31,18 @@ public class Post extends BaseTimeEntity { @Builder public Post(String title, String content, Member member) { + validateTitle(title); this.title = title; this.content = content; this.member = member; } + private void validateTitle(String title) { + if (title.length() > 50) { + throw new PostException("제목은 50자 이하여야 합니다."); + } + } + public void updateContent(final String content) { this.content = content; } diff --git a/spring-code-for-deploy/src/main/java/com/example/seminar/repository/PostJpaRepository.java b/spring-code-for-deploy/src/main/java/com/example/seminar/repository/PostJpaRepository.java index 81d8702..8bf5dec 100644 --- a/spring-code-for-deploy/src/main/java/com/example/seminar/repository/PostJpaRepository.java +++ b/spring-code-for-deploy/src/main/java/com/example/seminar/repository/PostJpaRepository.java @@ -10,5 +10,6 @@ public interface PostJpaRepository extends JpaRepository { List findAllByMemberId(Long memberId); List findAllByMember(Member member); + List findAllByMemberNameIn(List memberName); } diff --git a/spring-code-for-deploy/src/main/java/com/example/seminar/service/member/MemberUpdater.java b/spring-code-for-deploy/src/main/java/com/example/seminar/service/member/MemberUpdater.java index 85497e5..22316fc 100644 --- a/spring-code-for-deploy/src/main/java/com/example/seminar/service/member/MemberUpdater.java +++ b/spring-code-for-deploy/src/main/java/com/example/seminar/service/member/MemberUpdater.java @@ -9,7 +9,6 @@ @Component @RequiredArgsConstructor public class MemberUpdater { - private final MemberJpaRepository memberJpaRepository; public void updateSopt(Member member, SOPT sopt) { member.updateSOPT(sopt); diff --git a/spring-code-for-deploy/src/test/java/com/example/seminar/domain/MemberTest.java b/spring-code-for-deploy/src/test/java/com/example/seminar/domain/MemberTest.java index 615a84a..7e4b82f 100644 --- a/spring-code-for-deploy/src/test/java/com/example/seminar/domain/MemberTest.java +++ b/spring-code-for-deploy/src/test/java/com/example/seminar/domain/MemberTest.java @@ -1,6 +1,7 @@ package com.example.seminar.domain; +import com.example.seminar.common.exception.MemberException; import com.example.seminar.repository.MemberJpaRepository; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; @@ -13,32 +14,46 @@ @ActiveProfiles("test") public class MemberTest { - @Autowired - MemberJpaRepository memberJpaRepository; - @Test - @DisplayName("사용자 정보를 입력하면 회원을 등록할 수 있다.") - void memberSaveTest() { - - // given - SOPT sopt = SOPT.builder() - .part(Part.SERVER) - .build(); - - Member member = Member.builder() - .age(99) - .name("오해영") - .sopt(sopt) - .nickname("5hae0") - .build(); - - // when - Member savedMember = memberJpaRepository.save(member); - - // then - Assertions.assertThat(savedMember) - .extracting("age", "name", "sopt", "nickname") - .containsExactly(99, "오해영", sopt, "5hae0"); - + @DisplayName("100살이 넘은 사용자는 회원가입을 할 수 없다.") + void memberRegister() { + Assertions.assertThatThrownBy( + () -> { + SOPT sopt = SOPT.builder() + .part(Part.SERVER) + .build(); + + Member member = Member.builder() + .age(101) + .name("오해영") + .sopt(sopt) + .nickname("5hae0") + .build(); + + } + ).isInstanceOf(MemberException.class) + .hasMessage("회원의 나이는 0세 이상 100세 이하입니다."); } + +// @Test +// @DisplayName("한국인 사용자만 가입할 수 있다.") +// void memberWithNotKorean() { +// Assertions.assertThatThrownBy( +// () -> { +// SOPT sopt = SOPT.builder() +// .part(Part.SERVER) +// .build(); +// +// Member member = Member.builder() +// .age(88) +// .name("mike") +// .sopt(sopt) +// .nickname("5hae0") +// .build(); +// +// } +// ).isInstanceOf(MemberException.class) +// .hasMessage("한글만 가능합니다.") +// ; +// } } diff --git a/spring-code-for-deploy/src/test/java/com/example/seminar/domain/PartTest.java b/spring-code-for-deploy/src/test/java/com/example/seminar/domain/PartTest.java index 1f1f87f..9b88a39 100644 --- a/spring-code-for-deploy/src/test/java/com/example/seminar/domain/PartTest.java +++ b/spring-code-for-deploy/src/test/java/com/example/seminar/domain/PartTest.java @@ -10,7 +10,7 @@ public class PartTest { @Test - @DisplayName("개발 파트.") + @DisplayName("개발 파트만을 조회할 수 있다.") void developParts() { Assertions.assertThat(Part.developParts()) .isEqualTo( @@ -21,4 +21,16 @@ void developParts() { Part.IOS )); } + + @Test + @DisplayName("클라이언트 파트만을 조회할 수 있다.") + void clientDevelopParts() { + Assertions.assertThat(Part.clientDevelopParts()) + .isEqualTo( + List.of( + Part.WEB, + Part.ANDROID, + Part.IOS + )); + } } diff --git a/spring-code-for-deploy/src/test/java/com/example/seminar/repository/MemberJpaRepositoryTest.java b/spring-code-for-deploy/src/test/java/com/example/seminar/repository/MemberJpaRepositoryTest.java index ff5232b..5d47afa 100644 --- a/spring-code-for-deploy/src/test/java/com/example/seminar/repository/MemberJpaRepositoryTest.java +++ b/spring-code-for-deploy/src/test/java/com/example/seminar/repository/MemberJpaRepositoryTest.java @@ -1,10 +1,69 @@ package com.example.seminar.repository; +import com.example.seminar.domain.Member; +import com.example.seminar.domain.Part; +import com.example.seminar.domain.SOPT; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.test.context.ActiveProfiles; +import org.springframework.transaction.annotation.Transactional; @DataJpaTest @ActiveProfiles("test") public class MemberJpaRepositoryTest { + @Autowired + MemberJpaRepository memberJpaRepository; + + @Test + @DisplayName("사용자 정보를 입력하면 회원을 등록할 수 있다.") + void save() { + // given + SOPT sopt = SOPT.builder() + .part(Part.SERVER) + .build(); + + Member member = Member.builder() + .age(99) + .name("오해영") + .sopt(sopt) + .nickname("5hae0") + .build(); + + // when + Member savedMember = memberJpaRepository.save(member); + + // then + Assertions.assertThat(savedMember) + .extracting("age", "name", "sopt", "nickname") + .containsExactly(99, "오해영", sopt, "5hae0"); + + } + + @Test + @DisplayName("사용자 id를 입력하면 회원을 조회할 수 있다. 존재하지 않는 사용자는 조회할 수 없다.") + void findByIdOrThrow() { + SOPT sopt = SOPT.builder() + .part(Part.SERVER) + .build(); + + Member member = Member.builder() + .age(99) + .name("오해영") + .sopt(sopt) + .nickname("5hae0") + .build(); + + Member savedMember = memberJpaRepository.save(member); + Member findMember = memberJpaRepository.findByIdOrThrow(savedMember.getId()); + + Assertions.assertThat(findMember) + .extracting("id", "age", "name", "sopt", "nickname") + .containsExactly(savedMember.getId(), 99, "오해영", sopt, "5hae0"); + } + } diff --git a/spring-code-for-deploy/src/test/java/com/example/seminar/repository/PostJpaRepositoryTest.java b/spring-code-for-deploy/src/test/java/com/example/seminar/repository/PostJpaRepositoryTest.java index 8f3224c..a84301a 100644 --- a/spring-code-for-deploy/src/test/java/com/example/seminar/repository/PostJpaRepositoryTest.java +++ b/spring-code-for-deploy/src/test/java/com/example/seminar/repository/PostJpaRepositoryTest.java @@ -1,4 +1,73 @@ package com.example.seminar.repository; +import com.example.seminar.domain.Member; +import com.example.seminar.domain.Part; +import com.example.seminar.domain.Post; +import com.example.seminar.domain.SOPT; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.ActiveProfiles; + +import java.util.List; + +@DataJpaTest +@ActiveProfiles("test") public class PostJpaRepositoryTest { + + @Autowired + PostJpaRepository postJpaRepository; + + @Autowired + MemberJpaRepository memberJpaRepository; + + @Test + @DisplayName("사용자 이름으로 작성한 게시글을 모두 조회할 수 있다.") + void findAllByMemberNameIn() { + // given + Member member1 = createMember("오해영"); + Member member2 = createMember("또오해영"); + memberJpaRepository.save(member1); + memberJpaRepository.save(member2); + Post post1 = createPost("제목1", "내용1", member1); + Post post2 = createPost("제목2", "내용2", member1); + Post post3 = createPost("제목3", "내용3", member2); + postJpaRepository.saveAll(List.of(post1, post2, post3)); + + // when + List findPosts = postJpaRepository.findAllByMemberNameIn(List.of("오해영", "또오해영")); + + // then + Assertions.assertThat(findPosts) + .extracting("title", "content") + .containsExactlyInAnyOrder( + Assertions.tuple("제목1", "내용1"), + Assertions.tuple("제목2", "내용2"), + Assertions.tuple("제목3", "내용3") + ); + } + + private Post createPost(String title, String content, Member member) { + return Post.builder() + .title(title) + .content(content) + .member(member) + .build(); + } + + private Member createMember(String name) { + SOPT sopt = SOPT.builder() + .part(Part.SERVER) + .build(); + return Member.builder() + .age(99) + .name(name) + .sopt(sopt) + .nickname("5hae0") + .build(); + } + + } diff --git a/spring-code-for-deploy/src/test/java/com/example/seminar/service/member/MemberSaverTest.java b/spring-code-for-deploy/src/test/java/com/example/seminar/service/member/MemberSaverTest.java index 42c7372..c9b801b 100644 --- a/spring-code-for-deploy/src/test/java/com/example/seminar/service/member/MemberSaverTest.java +++ b/spring-code-for-deploy/src/test/java/com/example/seminar/service/member/MemberSaverTest.java @@ -6,6 +6,7 @@ import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; +import org.springframework.util.StopWatch; @SpringBootTest @ActiveProfiles("test") @@ -17,17 +18,17 @@ public class MemberSaverTest { @Test @DisplayName("SOPT 회원을 등록할 수 있다.") void register() { - // given - - // when - - // then - + StopWatch stopWatch = new StopWatch(); } @Test @DisplayName("새롭게 등록한 SOPT 회원은 현재 기수이다.") void checkIsCurrentGeneration() { + // set up + // 생성 + + // 조회 + } }