Skip to content

Commit

Permalink
Merge pull request #72 from iTpple-S-POT/test/#71-comment
Browse files Browse the repository at this point in the history
[test] 댓글 관련 테스트코드 작성
  • Loading branch information
sujeong11 authored Jan 23, 2024
2 parents b83758e + cf0346a commit 12f1ded
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.com.itpple.spot.server.domain.comment.repository;

import lombok.RequiredArgsConstructor;
import org.com.itpple.spot.server.domain.comment.entity.Comment;
import org.com.itpple.spot.server.domain.pot.category.entity.Category;
import org.com.itpple.spot.server.domain.pot.entity.Pot;
import org.com.itpple.spot.server.domain.user.entity.User;
import org.com.itpple.spot.server.util.CommentTestUtil;
import org.com.itpple.spot.server.util.PotTestUtil;
import org.com.itpple.spot.server.util.UserTestUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestConstructor;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.*;
import static org.springframework.test.context.TestConstructor.*;

@RequiredArgsConstructor
@TestConstructor(autowireMode = AutowireMode.ALL)
@AutoConfigureTestDatabase(replace = Replace.NONE)
@ActiveProfiles("test")
@DataJpaTest
class CommentRepositoryTest {

private final CommentRepository sut;
private final TestEntityManager testEntityManager;

private final User user = UserTestUtil.create();
private final Category category = PotTestUtil.createCategory();
private final Pot pot = PotTestUtil.create(user, category);
private final Comment parentComment = CommentTestUtil.create(pot, user, null, "content1");
private final Comment comment = CommentTestUtil.create(pot, user, parentComment, "content1_1");
private final Comment parentComment2 = CommentTestUtil.create(pot, user, null, "content2");

@BeforeEach
public void setUp() {
testEntityManager.persist(user);
testEntityManager.persist(category);
testEntityManager.persist(pot);
testEntityManager.persist(parentComment);
testEntityManager.persist(comment);
testEntityManager.persist(parentComment2);
}

@Test
void POT의_모든_댓글_조회_시_부모_댓글이_NULL인_데이터를_먼저_조회한다() {
// given
Comment comment1 = CommentTestUtil.create(pot, user, parentComment, "content1_2");
Comment comment2 = CommentTestUtil.create(pot, user, parentComment2, "content2_1");
sut.save(comment1);
sut.save(comment2);
testEntityManager.flush();

// when
List<Comment> commentList = sut.findAllComment(pot.getId());

// then
assertThat(commentList.get(0).getParentComment()).isNull();
assertThat(commentList.get(1).getParentComment()).isNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package org.com.itpple.spot.server.domain.comment.service;

import org.com.itpple.spot.server.domain.comment.dto.CommentDto;
import org.com.itpple.spot.server.domain.comment.dto.request.CreateCommentRequest;
import org.com.itpple.spot.server.domain.comment.dto.request.UpdateCommentRequest;
import org.com.itpple.spot.server.domain.comment.dto.response.CreateCommentResponse;
import org.com.itpple.spot.server.domain.comment.entity.Comment;
import org.com.itpple.spot.server.domain.comment.exception.CommentPotNotMatchException;
import org.com.itpple.spot.server.domain.comment.exception.CommentWriterNotMatchException;
import org.com.itpple.spot.server.domain.comment.repository.CommentRepository;
import org.com.itpple.spot.server.domain.comment.service.impl.CommentServiceImpl;
import org.com.itpple.spot.server.domain.pot.entity.Pot;
import org.com.itpple.spot.server.domain.pot.repository.PotRepository;
import org.com.itpple.spot.server.domain.user.entity.User;
import org.com.itpple.spot.server.domain.user.repository.UserRepository;
import org.com.itpple.spot.server.util.CommentTestUtil;
import org.com.itpple.spot.server.util.PotTestUtil;
import org.com.itpple.spot.server.util.UserTestUtil;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.*;

@ExtendWith(MockitoExtension.class)
class CommentServiceTest {

@InjectMocks
private CommentServiceImpl sut;

@Mock
private CommentRepository commentRepository;
@Mock
private UserRepository userRepository;
@Mock
private PotRepository potRepository;

@Test
void 작성하고자_하는_댓글에_대한_데이터를_받아_추가한다() {
// given
User user = UserTestUtil.create();
Pot pot = PotTestUtil.create();
Comment expectSaveComment = CommentTestUtil.create(pot, user, null, "content");
given(userRepository.findById(anyLong())).willReturn(Optional.of(user));
given(potRepository.findById(anyLong())).willReturn(Optional.of(pot));
given(commentRepository.save(any(Comment.class))).willReturn(expectSaveComment);

// when
CreateCommentResponse actualSavedComment = sut.addComment(
anyLong(), createCommentRequest(1L, null, expectSaveComment.getContent())
);

// then
assertThat(actualSavedComment.writerProfileImageUrl()).isEqualTo(user.getProfileImageUrl());
assertThat(actualSavedComment.writerName()).isEqualTo(user.getName());
assertThat(actualSavedComment.content()).isEqualTo(expectSaveComment.getContent());
}

@Test
void 추가할_댓글의_potId가_부모_댓글의_potId와_다르다면_에러가_발생한다() {
// given
Long wrongPotId = 50L;
User user = UserTestUtil.create();
Pot pot = spy(PotTestUtil.create());
Comment parentComment = CommentTestUtil.create(pot, user, null, "content1");
doReturn(1L).when(pot).getId();
given(userRepository.findById(anyLong())).willReturn(Optional.of(user));
given(potRepository.findById(anyLong())).willReturn(Optional.of(pot));
given(commentRepository.findById(anyLong())).willReturn(Optional.of(parentComment));

// when
Throwable throwable = catchThrowable(
() -> sut.addComment(anyLong(), createCommentRequest(wrongPotId, 1L, "content")));

// then
assertThat(throwable).isInstanceOf(CommentPotNotMatchException.class);
}

@Test
void POT에_존재하는_모든_댓글을_조회해_자식댓글이_존재한다면_부모댓글에_하위계층에_넣어_반환한다() {
// given
User user = UserTestUtil.create();
Pot pot = PotTestUtil.create();
Comment parentComment = CommentTestUtil.create(pot, user, null, "content");
List<Comment> commentList = List.of(
parentComment,
CommentTestUtil.create(pot, user, parentComment, "content2"),
CommentTestUtil.create(pot, user, parentComment, "content3")
);
given(userRepository.existsById(anyLong())).willReturn(true);
given(potRepository.existsById(anyLong())).willReturn(true);
given(commentRepository.findAllComment(anyLong())).willReturn(commentList);

// when
List<CommentDto> getCommentList = sut.getCommentList(1L, 1L);

// then
assertThat(getCommentList.size()).isEqualTo(1);
}

@Test
void 댓글을_수정할_때_요청을_보낸_사용자가_등록한_댓글이_아니라면_에러가_발생한다() {
// given
Long wrongUserId = 50L;
User user = spy(UserTestUtil.create());
Pot pot = PotTestUtil.create();
Comment comment = CommentTestUtil.create(pot, user, null, "content");
doReturn(1L).when(user).getId();
given(userRepository.existsById(anyLong())).willReturn(true);
given(commentRepository.findById(anyLong())).willReturn(Optional.of(comment));

// when
Throwable throwable = catchThrowable(
() -> sut.updateComment(wrongUserId, anyLong(), updateCommentRequest("update content")));

// then
assertThat(throwable).isInstanceOf(CommentWriterNotMatchException.class);
}

@Test
void 삭제할_댓글의_PK를_받아_댓글을_삭제한다() {
// given
Long userId = 1L;
User user = spy(UserTestUtil.create());
Pot pot = PotTestUtil.create();
Comment comment = CommentTestUtil.create(pot, user, null, "content");
doReturn(userId).when(user).getId();
given(userRepository.existsById(anyLong())).willReturn(true);
given(commentRepository.findById(anyLong())).willReturn(Optional.of(comment));
willDoNothing().given(commentRepository).deleteById(anyLong());

// when
sut.deleteComment(userId, anyLong());

// then
then(commentRepository).should().deleteById(anyLong());
}

@Test
void 댓글을_삭제할_때_요청을_보낸_사용자가_등록한_댓글이_아니라면_에러가_발생한다() {
// given
Long wrongUserId = 50L;
User user = spy(UserTestUtil.create());
Pot pot = PotTestUtil.create();
Comment comment = CommentTestUtil.create(pot, user, null, "content");
doReturn(1L).when(user).getId();
given(userRepository.existsById(anyLong())).willReturn(true);
given(commentRepository.findById(anyLong())).willReturn(Optional.of(comment));

// when
Throwable throwable = catchThrowable(() -> sut.deleteComment(wrongUserId, 1L));

// then
assertThat(throwable).isInstanceOf(CommentWriterNotMatchException.class);
}

private CreateCommentRequest createCommentRequest(Long potId, Long parentCommentId, String content) {
return new CreateCommentRequest(potId, parentCommentId, content);
}

private UpdateCommentRequest updateCommentRequest(String content) {
return new UpdateCommentRequest(content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
@DataJpaTest
class ReactionRepositoryTest {

private final ReactionRepository reactionRepository;
private final ReactionRepository sut;
private final TestEntityManager testEntityManager;

private final User user = UserTestUtil.create();
Expand All @@ -56,11 +56,11 @@ public void setUp() {
ReactionTestUtil.create(pot, user, REACTION_TYPE, false),
ReactionTestUtil.create(pot, user, REACTION_TYPE, false)
);
reactionRepository.saveAll(reactionList);
sut.saveAll(reactionList);
testEntityManager.flush();

// when
List<Reaction> actualReactionList = reactionRepository.findAll();
List<Reaction> actualReactionList = sut.findAll();

// then
assertThat(actualReactionList.size()).isEqualTo(3);
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/com/itpple/spot/server/util/CommentTestUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.com.itpple.spot.server.util;

import org.com.itpple.spot.server.domain.comment.entity.Comment;
import org.com.itpple.spot.server.domain.pot.entity.Pot;
import org.com.itpple.spot.server.domain.user.entity.User;

public class CommentTestUtil {

public static Comment create(Pot pot, User writer, Comment parentComment, String content) {
return Comment.builder()
.pot(pot)
.writer(writer)
.parentComment(parentComment)
.content(content)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
public class UserTestUtil {

public static User create() {
return User.builder().build();
return User.builder()
.profileImageUrl("profileImageUrl")
.name("name")
.build();
}
}

0 comments on commit 12f1ded

Please sign in to comment.