Skip to content

Commit

Permalink
Refactor for 2주차
Browse files Browse the repository at this point in the history
  • Loading branch information
unanchoi committed Feb 27, 2024
1 parent 7f62b3e commit 3e2f017
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 34 deletions.
3 changes: 3 additions & 0 deletions spring-code-for-deploy/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ public enum Part {
public static List<Part> developParts() {
return List.of(SERVER, WEB, ANDROID, IOS);
}

public static List<Part> clientDevelopParts() {
return List.of(WEB, ANDROID, IOS);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public interface PostJpaRepository extends JpaRepository<Post, Long> {

List<Post> findAllByMemberId(Long memberId);
List<Post> findAllByMember(Member member);
List<Post> findAllByMemberNameIn(List<String> memberName);

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
@Component
@RequiredArgsConstructor
public class MemberUpdater {
private final MemberJpaRepository memberJpaRepository;

public void updateSopt(Member member, SOPT sopt) {
member.updateSOPT(sopt);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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("한글만 가능합니다.")
// ;
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class PartTest {

@Test
@DisplayName("개발 파트.")
@DisplayName("개발 파트만을 조회할 수 있다.")
void developParts() {
Assertions.assertThat(Part.developParts())
.isEqualTo(
Expand All @@ -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
));
}
}
Original file line number Diff line number Diff line change
@@ -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");
}

}
Original file line number Diff line number Diff line change
@@ -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<Post> 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();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
// 생성

// 조회

}
}

0 comments on commit 3e2f017

Please sign in to comment.