Skip to content

Commit

Permalink
refactor: 메서드 분리 및 builder 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeHanEum committed Jan 15, 2025
1 parent 3091f26 commit 3b0ab20
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ public void init() {
aboutCommandService = new AboutCommandService(fakeAboutRepository);

fakeAboutRepository.save(
About.builder()
.mainCategory(DEPT_INTRO)
.subCategory(HISTORY)
.detailCategory("detailCategory")
.content("about content")
.build()
About.create(DEPT_INTRO, HISTORY, "detailCategory", "about content")
);
}

Expand Down Expand Up @@ -62,8 +57,8 @@ public void createAbout_Failed() {

// when
// then
assertThatThrownBy(() -> aboutCommandService.createAbout(mainCategory, subCategory, detail, content))
.isInstanceOf(CategoryNotMatchException.class)
.hasMessage("메인 카테고리와 보조 카테고리가 일치하지 않습니다.");
assertThatThrownBy(
() -> aboutCommandService.createAbout(mainCategory, subCategory, detail, content)).isInstanceOf(
CategoryNotMatchException.class).hasMessage("메인 카테고리와 보조 카테고리가 일치하지 않습니다.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ public void init() {
aboutQueryService = new AboutQueryService(fakeAboutRepository);

fakeAboutRepository.save(
About.builder()
.mainCategory(DEPT_INTRO)
.subCategory(HISTORY)
.detailCategory("detail category")
.content("about content")
.build()
About.create(DEPT_INTRO, HISTORY, "detail category", "about content")
);
}

Expand Down Expand Up @@ -62,9 +57,8 @@ public void getAbout_NotFound_ThrowsException() {
String detail = "nonexistentDetail";

// when & then
assertThatThrownBy(() -> aboutQueryService.getAbout(main, sub, detail))
.isInstanceOf(AboutNotFoundException.class)
.hasMessageContaining("해당 소개글을 찾을 수 없습니다.");
assertThatThrownBy(() -> aboutQueryService.getAbout(main, sub, detail)).isInstanceOf(
AboutNotFoundException.class).hasMessageContaining("해당 소개글을 찾을 수 없습니다.");
}

@Test
Expand Down Expand Up @@ -94,8 +88,7 @@ public void getAbout_CategoryNotMatch_ThrowsException() {
String detail = "someDetail";

// when & then
assertThatThrownBy(() -> aboutQueryService.getAbout(main, sub, detail))
.isInstanceOf(CategoryNotMatchException.class)
.hasMessageContaining("메인 카테고리와 보조 카테고리가 일치하지 않습니다.");
assertThatThrownBy(() -> aboutQueryService.getAbout(main, sub, detail)).isInstanceOf(
CategoryNotMatchException.class).hasMessageContaining("메인 카테고리와 보조 카테고리가 일치하지 않습니다.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public void createAbout_Success() {
About about = About.create(mainCategory, subCategory, detailCategory, content);

// then
assertEquals(about.getMainCategory(), mainCategory);
assertEquals(about.getSubCategory(), subCategory);
assertEquals(about.getDetailCategory(), detailCategory);
assertEquals(about.getContent(), content);
assertEquals(mainCategory, about.getMainCategory());
assertEquals(subCategory, about.getSubCategory());
assertEquals(detailCategory, about.getDetailCategory());
assertEquals(content, about.getContent());
}

@Test
Expand All @@ -45,6 +45,6 @@ public void updateContent_Success() {
about.updateContent(updateContent);

// then
assertEquals(about.getContent(), updateContent);
assertEquals(updateContent, about.getContent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ public class ClubCommandServiceTest {

@BeforeEach
public void init() {
clubCommandService = new ClubCommandService(
new FakeClubRepository()
);
FakeClubRepository fakeClubRepository = new FakeClubRepository();
clubCommandService = new ClubCommandService(fakeClubRepository);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,11 @@ public void init() {
clubQueryService = new ClubQueryService(fakeClubRepository);

fakeClubRepository.save(
Club.builder()
.name("Club a")
.description("a 동아리입니다.")
.site("http://club-a.kyonggi.ac.kr")
.build()
Club.create("Club a", "a 동아리입니다.", "http://club-a.kyonggi.ac.kr")
);

fakeClubRepository.save(
Club.builder()
.name("Club b")
.description("b 동아리입니다.")
.site("http://club-b.kyonggi.ac.kr")
.build()
Club.create("Club b", "b 동아리입니다.", "http://club-b.kyonggi.ac.kr")
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,44 @@
public class CommentCommandServiceTest {
private CommentCommandService commentCommandService;

private static final Long TARGET_COMMENT_ID = 1L;
private static final Long TEST_POST_ID = 1L;
private static final String TEST_USER_ID = "202411345";

@BeforeEach
public void init() {
FakePostRepository fakePostRepository = new FakePostRepository();
FakeUserRepository fakeUserRepository = new FakeUserRepository();
UserQueryService userQueryService = new UserQueryService(fakeUserRepository);

initializeCommentCommandService(fakePostRepository, userQueryService);
saveTestUserAndPost(fakeUserRepository, fakePostRepository);
setTestSecurityContext(userQueryService);
}

private static void saveTestUserAndPost(FakeUserRepository fakeUserRepository,
FakePostRepository fakePostRepository) {
fakeUserRepository.save(
User.create(TEST_USER_ID, "password1234", "홍길동", "[email protected]",
"010-1234-5678", CSE)
);
fakePostRepository.save(Post.create(
"SW 부트캠프 4기 교육생 모집", "SW전문인재양성사업단에서는 SW부트캠프 4기 교육생을 모집합니다.", NEWS,
User.builder().build()
));
}

private void initializeCommentCommandService(FakePostRepository fakePostRepository,
UserQueryService userQueryService) {
commentCommandService = new CommentCommandService(
new PostQueryService(fakePostRepository),
userQueryService,
new FakeCommentRepository()
);
}

fakeUserRepository.save(User.builder()
.id("202411345")
.password("password1234")
.name("홍길동")
.email("[email protected]")
.phone("010-1234-5678")
.major(CSE)
.build());

User author = User.builder()
.build();

fakePostRepository.save(Post.create(
"테스트용 제목1", "테스트용 내용1", NEWS, author
));

UserDetails user = userQueryService.getUserById("202411345");
private static void setTestSecurityContext(UserQueryService userQueryService) {
UserDetails user = userQueryService.getUserById(TEST_USER_ID);
SecurityContext context = SecurityContextHolder.getContext();
context.setAuthentication(
new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getAuthorities())
Expand All @@ -65,45 +74,41 @@ public void init() {
@DisplayName("createComment는 댓글을 생성할 수 있다.")
public void createComment_Success() {
// given
String content = "content";
Long postId = 1L;
String testCommentContent = "SW 부트캠프 모집이 정말 기대됩니다.";

// when
Long result = commentCommandService.createComment(content, postId);
Long createdCommentId = commentCommandService.createComment(testCommentContent, TEST_POST_ID);

// then
assertEquals(1L, result);
assertEquals(TARGET_COMMENT_ID, createdCommentId);
}

@Test
@DisplayName("updateComment는 댓글을 수정할 수 있다.")
public void updateComment_Success() {
// given
User user = User.builder().build();
Post post = Post.builder().build();
Comment comment = Comment.create("test", user, post);

String newContent = "content";
Comment targetComment = Comment.create("SW 부트캠프 모집이 정말 기대됩니다.", User.builder().build(),
Post.builder().build());
String updatedCommentContent = "SW 부트캠프 모집이 정말 기대됩니다. 4기 교육생 확정이 언제일까요?";

// when
commentCommandService.updateComment(comment, newContent);
commentCommandService.updateComment(targetComment, updatedCommentContent);

// then
assertEquals("content", comment.getContent());
assertEquals(updatedCommentContent, targetComment.getContent());
}

@Test
@DisplayName("deleteComment는 댓글을 삭제할 수 있다.")
public void deleteComment_Success() {
// given
User user = User.builder().build();
Post post = Post.builder().build();
Comment comment = Comment.create("test", user, post);
Comment targetComment = Comment.create("SW 부트캠프 모집이 정말 기대됩니다.", User.builder().build(),
Post.builder().build());

// when
commentCommandService.deleteComment(comment);
commentCommandService.deleteComment(targetComment);

// then
assertNotNull(comment.getDeletedAt());
assertNotNull(targetComment.getDeletedAt());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package comment.application;

import static kgu.developers.domain.post.domain.Category.NEWS;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;

Expand All @@ -12,78 +14,86 @@
import kgu.developers.domain.comment.application.query.CommentQueryService;
import kgu.developers.domain.comment.domain.Comment;
import kgu.developers.domain.comment.exception.CommentNotFoundException;
import kgu.developers.domain.post.domain.Category;
import kgu.developers.domain.post.domain.Post;
import kgu.developers.domain.user.domain.User;
import mock.repository.FakeCommentRepository;
import mock.repository.FakePostRepository;

public class CommentQueryServiceTest {
private CommentQueryService commentQueryService;

private static final int TARGET_COMMENT_COUNT = 1;
private static final String TARGET_COMMENT_CONTENT = "SW 부트캠프 모집이 정말 기대됩니다.";
private static final Long SAVED_COMMENT_ID = 2L;
private static final Long NOT_EXIST_COMMENT_ID = 3L;
private static final Long TEST_POST_ID = 1L;

@BeforeEach
public void init() {
FakeCommentRepository fakeCommentRepository = new FakeCommentRepository();
commentQueryService = new CommentQueryService(fakeCommentRepository);

User user = User.builder()
.build();

Post post = Post.builder()
.id(1L)
.build();
deletedComment(fakeCommentRepository);
saveTestComment(fakeCommentRepository);
}

Comment delete = fakeCommentRepository.save(Comment.builder()
.author(user)
.content("deleted")
.post(post)
.build()
private static void deletedComment(FakeCommentRepository fakeCommentRepository) {
Comment commentToDelete = fakeCommentRepository.save(
Comment.create("삭제된 댓글 입니다", User.builder().build(), Post.builder().build())
);
commentToDelete.delete();
}

fakeCommentRepository.save(Comment.builder()
.author(user)
.content("get")
.post(post)
.build()
private static void saveTestComment(FakeCommentRepository fakeCommentRepository) {
Post commentedPost = saveTestPost();
fakeCommentRepository.save(
Comment.create(TARGET_COMMENT_CONTENT, User.builder().build(), commentedPost)
);
delete.delete();
}

private static Post saveTestPost() {
FakePostRepository fakePostRepository = new FakePostRepository();
Post commentedPost = Post.create("SW 부트캠프 4기 교육생 모집",
"SW전문인재양성사업단에서는 SW부트캠프 4기 교육생을 모집합니다.", NEWS, User.builder().build());
fakePostRepository.save(commentedPost);
return commentedPost;
}

@Test
@DisplayName("getComments는 등록된 순서 오름차순으로 정렬하고, 삭제된 댓글을 제외하여 조회한다.")
public void getComments_Success() {
// given
Long postId = 1L;

// when
List<Comment> comments = commentQueryService.getComments(postId);
List<Comment> comments = commentQueryService.getComments(TEST_POST_ID);

// then
assertEquals(1, comments.size());
assertEquals("get", comments.get(0).getContent());
assertEquals(TARGET_COMMENT_COUNT, comments.size());
assertTrue(comments.stream().findFirst().isPresent(), "댓글이 존재하지 않습니다.");
assertEquals(TARGET_COMMENT_CONTENT, comments.stream().findFirst().get().getContent());
}

@Test
@DisplayName("getById는 해당 댓글을 가져올 수 있다.")
public void getById_Success() {
// given
Long commentId = 2L;
Long savedCommentId = SAVED_COMMENT_ID;

// when
Comment result = commentQueryService.getById(commentId);
Comment result = commentQueryService.getById(savedCommentId);

// then
assertEquals(commentId, result.getId());
assertEquals("get", result.getContent());
assertEquals(savedCommentId, result.getId());
assertEquals(TARGET_COMMENT_CONTENT, result.getContent());
}

@Test
@DisplayName("getById는 존재하지 않는 댓글을 조회할 경우 CommentNotFoundException을 발생시킨다.")
public void getById_NotFound_ThrowsException() {
// given
Long commentId = 0L;

// when
// then
assertThatThrownBy(() -> commentQueryService.getById(commentId))
assertThatThrownBy(() -> commentQueryService.getById(NOT_EXIST_COMMENT_ID))
.isInstanceOf(CommentNotFoundException.class)
.hasMessage("해당 댓글을 찾을 수 없습니다.");
}
Expand Down

0 comments on commit 3b0ab20

Please sign in to comment.