Skip to content

Commit

Permalink
Merge pull request #23 from ssafy-19-final-pjt/test/comment-testcode
Browse files Browse the repository at this point in the history
Test : comment testcode 추가
  • Loading branch information
chwangmin authored May 23, 2024
2 parents ef06035 + cc9c9df commit 5bb4a73
Show file tree
Hide file tree
Showing 8 changed files with 508 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.ssafy.home.domain.comment.dto.request;

import lombok.Builder;
import lombok.Getter;

@Getter
public class CommentRequestUpdateDto {
private String content;

@Builder
private CommentRequestUpdateDto(String content) {
this.content = content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ private Comment(String content, Board board, Member member) {
public void updateContent(String content) {
this.content = content;
}

@Builder
private Comment(String content, Member member, Board board) {
this.content = content;
this.member = member;
this.board = board;
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
import com.ssafy.home.global.auth.dto.MemberDto;
import com.ssafy.home.global.error.ErrorCode;
import com.ssafy.home.global.error.exception.BadRequestException;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class CommentService {
private final CommentReadService commentReadService;
private final CommentWriteService commentWriteService;
Expand All @@ -30,7 +31,7 @@ public class CommentService {

public List<CommentResponseDto> getCommentAll(Long boardId) {
Board board = boardReadService.getBoard(boardId);
return commentResponseMapper.toListCommentResponse(board.getCommentList());
return commentResponseMapper.toListCommentResponse(commentReadService.getCommentFromBoardId(boardId));
}

@Transactional
Expand All @@ -39,6 +40,7 @@ public void createComment(MemberDto memberDto, Long boardId, CommentRequestDto c
Board board = boardReadService.getBoard(boardId);

commentWriteService.create(member, board, commentRequestDto);
board.increaseHit();
}

@Transactional
Expand All @@ -48,6 +50,7 @@ public void createCommentWithPessimisticLock(MemberDto memberDto, Long boardId,
Board board = boardReadService.getboardWithPessimisticLock(boardId);

commentWriteService.create(member, board, commentRequestDto);
board.increaseHit();
}

@Transactional
Expand All @@ -58,6 +61,7 @@ public void createCommentWithOptimisticLock(MemberDto memberDto, Long boardId,
Board board = boardReadService.getBoardWithOptimisticLock(boardId);

commentWriteService.create(member, board, commentRequestDto);
board.increaseHit();
}

@Transactional
Expand All @@ -68,6 +72,7 @@ public void createCommentWithDistributedLock(MemberDto memberDto, Long boardId,
Board board = boardReadService.getBoard(boardId);

commentWriteService.create(member, board, commentRequestDto);
board.increaseHit();
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ public class CommentWriteService {
public void create(Member member, Board board, CommentRequestDto commentRequestDto) {
Comment comment = commentRequestDto.toEntity(member, board);
commentRepository.save(comment);
board.increaseHit();
}

public void delete(Comment comment) {
commentRepository.delete(comment);
}

public void deleteByBoardId(Long boardId){
public void deleteByBoardId(Long boardId) {
commentRepository.deleteAllByBoardId(boardId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.ssafy.home.domain.comment.service;

import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

import com.ssafy.home.config.TestConfig;
import com.ssafy.home.domain.board.entity.Board;
import com.ssafy.home.domain.board.repository.BoardRepository;
import com.ssafy.home.domain.comment.entity.Comment;
import com.ssafy.home.domain.comment.repository.CommentRepository;
import com.ssafy.home.domain.member.repository.MemberRepository;
import com.ssafy.home.entity.member.Member;
import com.ssafy.home.global.error.ErrorCode;
import com.ssafy.home.global.error.exception.BadRequestException;
import groovy.util.logging.Slf4j;
import java.util.List;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Transactional
class CommentReadServiceTest extends TestConfig {
private final CommentReadService commentReadService;
private final MemberRepository memberRepository;
private final BoardRepository boardRepository;
private final CommentRepository commentRepository;

private Member member;
private Board board;
private Comment comment;

@Autowired
public CommentReadServiceTest(CommentReadService commentReadService, MemberRepository memberRepository,
BoardRepository boardRepository, CommentRepository commentRepository) {
this.commentReadService = commentReadService;
this.memberRepository = memberRepository;
this.boardRepository = boardRepository;
this.commentRepository = commentRepository;
}

@BeforeEach
void before() {
member = memberRepository.findById(11L).orElseThrow(IllegalStateException::new);
board = boardRepository.save(Board.builder()
.content("test")
.member(member)
.title("testTitled")
.build());
comment = commentRepository.save(Comment.builder()
.content("test")
.member(member)
.board(board)
.build());
commentRepository.save(Comment.builder()
.content("test2")
.member(member)
.board(board)
.build());
}

@Nested
@DisplayName("댓글 읽기 기능 테스트")
class CommentRead {
@Test
void 성공_게시판_ID를_입력_시_게시판에_작성된_댓글을_가져온다() {
//given
//when
List<Comment> result = commentReadService.getCommentFromBoardId(board.getId());

//then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(result).isNotNull()
.hasSize(2)
.allMatch(comment -> comment.getMember().getId().equals(member.getId()))
.allMatch(comment -> comment.getContent().startsWith("test"));
}
);
}

@Test
void 성공_댓글_ID를_입력_시_작성된_댓글을_가져온다() {
//given
//when
Comment result = commentReadService.getComment(comment.getId());

//then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(result).isNotNull();
softAssertions.assertThat(result.getBoard().getId()).isEqualTo(board.getId());
softAssertions.assertThat(result.getMember().getId()).isEqualTo(member.getId());
softAssertions.assertThat(result.getContent()).isEqualTo(comment.getContent());
});
}

@Test
void 실패_댓글_ID가_존재하지_않는_경우_예외가_발생한다() {
//given
Long noneExistCommentID = 0L;

//when then
assertThatThrownBy(() -> commentReadService.getComment(noneExistCommentID))
.isInstanceOf(BadRequestException.class)
.extracting("errorCode")
.isEqualTo(ErrorCode.COMMENT_NOT_FOUND);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.ssafy.home.domain.comment.service;

import com.ssafy.home.config.TestConfig;
import com.ssafy.home.domain.board.entity.Board;
import com.ssafy.home.domain.comment.dto.response.CommentResponseDto;
import com.ssafy.home.domain.comment.entity.Comment;
import com.ssafy.home.entity.member.Member;
import groovy.util.logging.Slf4j;
import java.util.ArrayList;
import java.util.List;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Transactional
@ExtendWith(MockitoExtension.class)
class CommentResponseMapperTest extends TestConfig {
private static final Logger log = LoggerFactory.getLogger(CommentResponseMapperTest.class);
@InjectMocks
private CommentResponseMapper commentResponseMapper;

@Mock
private Member member;

@Mock
private Board board;

private List<Comment> comments;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
comments = new ArrayList<>();

for (int i = 0; i < 5; i++) {
comments.add(Comment.builder().content("Content " + i).member(member).board(board).build());
}
}

@Test
void 성공_댓글_리스트를_받을_시_응답_형식에_맞게_매핑된다() {
// given
// when
List<CommentResponseDto> result = commentResponseMapper.toListCommentResponse(comments);

// then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(result).isNotNull()
.hasSize(5)
.allMatch(dto -> dto.getContent().startsWith("Content"));
});
}
}
Loading

0 comments on commit 5bb4a73

Please sign in to comment.