νμΈν΄λ³΄λκΉ, μ λ²μ λκΈμ΄λ λλκΈ μλΉμ€λ λλΆλΆ ꡬνμ ν΄λμμλ€μ...γ γ ;;
μ΄λ²μλ μ λ²μ ꡬννλ μ½λλ₯Ό μ‘°κΈλ§ μμ νκ³ , κ·Έμ λ°λΌ ν μ€νΈμ½λλ₯Ό μμ νλλ‘ νκ² μ΅λλ€
- μν리ν°λ₯Ό μ΄μ©ν JSON λ°μ΄ν°λ‘ λ‘κ·ΈμΈ (μλ£)
- JWTλ₯Ό μ΄μ©ν μΈμ¦ (μλ£)
- λλ©μΈ, ν μ΄λΈ μ€κ³, μν°ν° μμ± (μλ£)
- λκΈ μμ λ‘μ§ κ΅¬ν (μλ£)
- νμκ°μ + μ 보μμ λ± νμ μλΉμ€ ꡬν (μλ£)
- κ²μν μλΉμ€ ꡬν (μ§ν μ€)
- λκΈ μλΉμ€ ꡬν (1λκΈ -> *(무ν) λλκΈ κ΅¬μ‘°) (μ§ν μ€)
- μμΈ μ²λ¦¬ (μλ£)
- μμΈ λ©μΈμ§ κ΅μ ν
- μΉ΄ν κ³ λ¦¬λ³ κ²μν λΆλ₯
- κ²μκΈ νμ΄μ§
- λμ μΈ κ²μ 쑰건μ μ¬μ©ν κ²μ
- μ¬μ©μ κ° μͺ½μ§ κΈ°λ₯
- 무ν μͺ½μ§ μ€ν¬λ‘€
- κ²μλ¬Ό & λκΈμ λν μλ
- μͺ½μ§μ λν μλ
- μ μν μ¬μ©μ κ° μ€μκ° μ±ν
- νμκ°μ μ κ²μ¦(μ: XXλνκ΅ XXκ³Όκ° μλλ©΄ κ°μ ν μ μκ²)
- Swaggerλ₯Ό μ¬μ©ν API λ¬Έμ λ§λ€κΈ°
- μ κ³ & λΈλ리μ€νΈ κΈ°λ₯
- AOPλ₯Ό ν΅ν λ‘κ·Έ
- μ΄λλ―Ό νμ΄μ§
- μΊμ
- λ°°ν¬ (+ 무μ€λ¨ λ°°ν¬)
- λ°°ν¬ μλν
- ν¬νΈμ μ΄λν° μ€κ³λ₯Ό λ°λ₯΄λ ν¨ν€μ§ ꡬ쑰 μ€κ³νκΈ°
- ...
μ΄μ κΈ°μ‘΄ μ½λλ₯Ό μμ ν΄λ³΄λλ‘ νκ² μ΅λλ€.
CommentService μμ
public interface CommentService {
void save(Long postId , CommentSaveDto commentSaveDto);
void saveReComment(Long postId, Long parentId ,CommentSaveDto commentSaveDto);
void update(Long id, CommentUpdateDto commentUpdateDto);
void remove(Long id) throws CommentException;
}
saveλ μμ λμκ³ , saveReCommentμ updateλ μΆκ°λμμ΅λλ€.
κ·Έλ¦¬κ³ findByIdμ findAllμ μ κ±°νμλλ°, μκ°ν΄λ³΄λ κ΅³μ΄ λκΈλ€λ§ λ°λ‘ μ‘°νν μΌμ΄ μμ κ±° κ°μμ μΌλ¨ μμ νμ΅λλ€.
νμ νμνλ€λ©΄ μΆκ°νκ² μ΅λλ€.
μ΄μ ꡬννκΈ° μ μ DTOλ₯Ό μμ±ν΄λ³΄λλ‘ νκ² μ΅λλ€.
CommentSaveDto μμ±
public record CommentSaveDto (String content){
public Comment toEntity() {
return Comment.builder().content(content).build();
}
}
CommentUpdateDto
μμΉλ CommentSaveDtoμ κ°μ΅λλ€.
public record CommentUpdateDto (Optional<String> content){ }
CommentServiceImpl ꡬν
package boardexample.myboard.domain.commnet.service;
import boardexample.myboard.domain.commnet.Comment;
import boardexample.myboard.domain.commnet.dto.CommentSaveDto;
import boardexample.myboard.domain.commnet.dto.CommentUpdateDto;
import boardexample.myboard.domain.commnet.exception.CommentException;
import boardexample.myboard.domain.commnet.exception.CommentExceptionType;
import boardexample.myboard.domain.commnet.repository.CommentRepository;
import boardexample.myboard.domain.member.exception.MemberException;
import boardexample.myboard.domain.member.exception.MemberExceptionType;
import boardexample.myboard.domain.member.repository.MemberRepository;
import boardexample.myboard.domain.post.Post;
import boardexample.myboard.domain.post.exception.PostException;
import boardexample.myboard.domain.post.exception.PostExceptionType;
import boardexample.myboard.domain.post.repository.PostRepository;
import boardexample.myboard.domain.post.service.PostService;
import boardexample.myboard.global.util.security.SecurityUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@RequiredArgsConstructor
@Transactional
public class CommentServiceImpl implements CommentService{
private final CommentRepository commentRepository;
private final MemberRepository memberRepository;
private final PostRepository postRepository;
@Override
public void save(Long postId, CommentSaveDto commentSaveDto) {
Comment comment = commentSaveDto.toEntity();
comment.confirmWriter(memberRepository.findByUsername(SecurityUtil.getLoginUsername()).orElseThrow(() -> new MemberException(MemberExceptionType.NOT_FOUND_MEMBER)));
comment.confirmPost(postRepository.findById(postId).orElseThrow(() -> new PostException(PostExceptionType.POST_NOT_POUND)));
commentRepository.save(comment);
}
@Override
public void saveReComment(Long postId, Long parentId, CommentSaveDto commentSaveDto) {
Comment comment = commentSaveDto.toEntity();
comment.confirmWriter(memberRepository.findByUsername(SecurityUtil.getLoginUsername()).orElseThrow(() -> new MemberException(MemberExceptionType.NOT_FOUND_MEMBER)));
comment.confirmPost(postRepository.findById(postId).orElseThrow(() -> new PostException(PostExceptionType.POST_NOT_POUND)));
comment.confirmParent(commentRepository.findById(parentId).orElseThrow(() -> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)));
commentRepository.save(comment);
}
@Override
public void update(Long id, CommentUpdateDto commentUpdateDto) {
Comment comment = commentRepository.findById(id).orElseThrow(() -> new CommentException(CommentExceptionType.NOT_POUND_COMMENT));
if(!comment.getWriter().getUsername().equals(SecurityUtil.getLoginUsername())){
throw new CommentException(CommentExceptionType.NOT_AUTHORITY_UPDATE_COMMENT);
}
commentUpdateDto.content().ifPresent(comment::updateContent);
}
@Override
public void remove(Long id) throws CommentException {
Comment comment = commentRepository.findById(id).orElseThrow(() -> new CommentException(CommentExceptionType.NOT_POUND_COMMENT));
if(!comment.getWriter().getUsername().equals(SecurityUtil.getLoginUsername())){
throw new CommentException(CommentExceptionType.NOT_AUTHORITY_DELETE_COMMENT);
}
comment.remove();
List<Comment> removableCommentList = comment.findRemovableList();
commentRepository.deleteAll(removableCommentList);
}
}
μ΄μ ν μ€νΈμ½λλ₯Ό μμ νλλ‘ νκ² μ΅λλ€.
CommentServiceTest μμ
κΈ°μ‘΄ ν μ€νΈμ½λλ λ€μκ³Ό κ°μ΅λλ€.
package boardexample.myboard.domain.commnet.service;
import boardexample.myboard.domain.commnet.Comment;
import boardexample.myboard.domain.commnet.repository.CommentRepository;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import java.util.stream.LongStream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@SpringBootTest
@Transactional
class CommentServiceTest {
@Autowired
CommentService commentService;
@Autowired CommentRepository commentRepository;
@Autowired EntityManager em;
private void clear(){
em.flush();
em.clear();
}
private Long saveComment(){
Comment comment = Comment.builder().content("λκΈ").build();
Long id = commentRepository.save(comment).getId();
clear();
return id;
}
private Long saveReComment(Long parentId){
Comment parent = commentRepository.findById(parentId).orElse(null);
Comment comment = Comment.builder().content("λκΈ").parent(parent).build();
Long id = commentRepository.save(comment).getId();
clear();
return id;
}
// λκΈμ μμ νλ κ²½μ°
// λλκΈμ΄ λ¨μμλ κ²½μ°
// DBμ νλ©΄μμλ μ§μμ§μ§ μκ³ , "μμ λ λκΈμ
λλ€"λΌκ³ νμ
@Test
public void λκΈμμ _λλκΈμ΄_λ¨μμλ_κ²½μ°() throws Exception {
//given
Long commentId = saveComment();
saveReComment(commentId);
saveReComment(commentId);
saveReComment(commentId);
saveReComment(commentId);
Assertions.assertThat(commentService.findById(commentId).getChildList().size()).isEqualTo(4);
//when
commentService.remove(commentId);
clear();
//then
Comment findComment = commentService.findById(commentId);
assertThat(findComment).isNotNull();
assertThat(findComment.isRemoved()).isTrue();
assertThat(findComment.getChildList().size()).isEqualTo(4);
}
// λκΈμ μμ νλ κ²½μ°
//λλκΈμ΄ μμ μ‘΄μ¬νμ§ μλ κ²½μ° : 곧λ°λ‘ DBμμ μμ
@Test
public void λκΈμμ _λλκΈμ΄_μλ_κ²½μ°() throws Exception {
//given
Long commentId = saveComment();
//when
commentService.remove(commentId);
clear();
//then
Assertions.assertThat(commentService.findAll().size()).isSameAs(0);
assertThat( assertThrows(Exception.class, () -> commentService.findById(commentId)).getMessage()).isEqualTo("λκΈμ΄ μμ΅λλ€.");
}
// λκΈμ μμ νλ κ²½μ°
// λλκΈμ΄ μ‘΄μ¬νλ λͺ¨λ μμ λ κ²½μ°
//λκΈκ³Ό, λ¬λ €μλ λλκΈ λͺ¨λ DBμμ μΌκ΄ μμ , νλ©΄μμλ νμλμ§ μμ
@Test
public void λκΈμμ _λλκΈμ΄_μ‘΄μ¬νλ_λͺ¨λ_μμ λ_λλκΈμΈ_κ²½μ°() throws Exception {
//given
Long commentId = saveComment();
Long reCommend1Id = saveReComment(commentId);
Long reCommend2Id = saveReComment(commentId);
Long reCommend3Id = saveReComment(commentId);
Long reCommend4Id = saveReComment(commentId);
Assertions.assertThat(commentService.findById(commentId).getChildList().size()).isEqualTo(4);
clear();
commentService.remove(reCommend1Id);
clear();
commentService.remove(reCommend2Id);
clear();
commentService.remove(reCommend3Id);
clear();
commentService.remove(reCommend4Id);
clear();
Assertions.assertThat(commentService.findById(reCommend1Id).isRemoved()).isTrue();
Assertions.assertThat(commentService.findById(reCommend2Id).isRemoved()).isTrue();
Assertions.assertThat(commentService.findById(reCommend3Id).isRemoved()).isTrue();
Assertions.assertThat(commentService.findById(reCommend4Id).isRemoved()).isTrue();
clear();
//when
commentService.remove(commentId);
clear();
//then
LongStream.rangeClosed(commentId, reCommend4Id).forEach(id ->
assertThat(assertThrows(Exception.class, () -> commentService.findById(id)).getMessage()).isEqualTo("λκΈμ΄ μμ΅λλ€.")
);
}
// λλκΈμ μμ νλ κ²½μ°
// λΆλͺ¨ λκΈμ΄ μμ λμ§ μμ κ²½μ°
// λ΄μ©λ§ μμ , DBμμλ μμ X
@Test
public void λλκΈμμ _λΆλͺ¨λκΈμ΄_λ¨μμλ_κ²½μ°() throws Exception {
//given
Long commentId = saveComment();
Long reCommend1Id = saveReComment(commentId);
//when
commentService.remove(reCommend1Id);
clear();
//then
Assertions.assertThat(commentService.findById(commentId)).isNotNull();
Assertions.assertThat(commentService.findById(reCommend1Id)).isNotNull();
Assertions.assertThat(commentService.findById(commentId).isRemoved()).isFalse();
Assertions.assertThat(commentService.findById(reCommend1Id).isRemoved()).isTrue();
}
// λλκΈμ μμ νλ κ²½μ°
// λΆλͺ¨ λκΈμ΄ μμ λμ΄μκ³ , λλκΈλ€λ λͺ¨λ μμ λ κ²½μ°
// λΆλͺ¨λ₯Ό ν¬ν¨ν λͺ¨λ λλκΈμ DBμμ μΌκ΄ μμ , νλ©΄μμμλ μ§μ
@Test
public void λλκΈμμ _λΆλͺ¨λκΈμ΄_μμ λ_κ²½μ°_λͺ¨λ _λλκΈμ΄_μμ λ_κ²½μ°() throws Exception {
//given
Long commentId = saveComment();
Long reCommend1Id = saveReComment(commentId);
Long reCommend2Id = saveReComment(commentId);
Long reCommend3Id = saveReComment(commentId);
commentService.remove(reCommend2Id);
clear();
commentService.remove(commentId);
clear();
commentService.remove(reCommend3Id);
clear();
Assertions.assertThat(commentService.findById(commentId)).isNotNull();
Assertions.assertThat(commentService.findById(commentId).getChildList().size()).isEqualTo(3);
//when
commentService.remove(reCommend1Id);
//then
LongStream.rangeClosed(commentId, reCommend3Id).forEach(id ->
assertThat(assertThrows(Exception.class, () -> commentService.findById(id)).getMessage()).isEqualTo("λκΈμ΄ μμ΅λλ€.")
);
}
// λλκΈμ μμ νλ κ²½μ°
// λΆλͺ¨ λκΈμ΄ μμ λμ΄μκ³ , λ€λ₯Έ λλκΈμ΄ μμ§ μμ λμ§ μκ³ λ¨μμλ κ²½μ°
//ν΄λΉ λλκΈλ§ μμ , κ·Έλ¬λ DBμμ μμ λμ§λ μκ³ , νλ©΄μμλ "μμ λ λκΈμ
λλ€"λΌκ³ νμ
@Test
public void λλκΈμμ _λΆλͺ¨λκΈμ΄_μμ λ_κ²½μ°_λ€λ₯Έ_λλκΈμ΄_λ¨μμλ_κ²½μ°() throws Exception {
//given
Long commentId = saveComment();
Long reCommend1Id = saveReComment(commentId);
Long reCommend2Id = saveReComment(commentId);
Long reCommend3Id = saveReComment(commentId);
commentService.remove(reCommend3Id);
commentService.remove(commentId);
clear();
Assertions.assertThat(commentService.findById(commentId)).isNotNull();
Assertions.assertThat(commentService.findById(commentId).getChildList().size()).isEqualTo(3);
//when
commentService.remove(reCommend2Id);
Assertions.assertThat(commentService.findById(commentId)).isNotNull();
//then
Assertions.assertThat(commentService.findById(reCommend2Id)).isNotNull();
Assertions.assertThat(commentService.findById(reCommend2Id).isRemoved()).isTrue();
Assertions.assertThat(commentService.findById(reCommend1Id).getId()).isNotNull();
Assertions.assertThat(commentService.findById(reCommend3Id).getId()).isNotNull();
Assertions.assertThat(commentService.findById(commentId).getId()).isNotNull();
}
}
λ³κ²½μ λλΆλΆμ λ€μκ³Ό κ°μ΅λλ€.
commentService.findById(commentId)
=>
commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT))
μμ μ΄ μλ£λ μ 체 μ½λμ λλ€.
package boardexample.myboard.domain.commnet.service;
import boardexample.myboard.domain.commnet.Comment;
import boardexample.myboard.domain.commnet.exception.CommentException;
import boardexample.myboard.domain.commnet.exception.CommentExceptionType;
import boardexample.myboard.domain.commnet.repository.CommentRepository;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import java.util.stream.LongStream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@SpringBootTest
@Transactional
class CommentServiceTest {
@Autowired
CommentService commentService;
@Autowired CommentRepository commentRepository;
@Autowired EntityManager em;
private void clear(){
em.flush();
em.clear();
}
private Long saveComment(){
Comment comment = Comment.builder().content("λκΈ").build();
Long id = commentRepository.save(comment).getId();
clear();
return id;
}
private Long saveReComment(Long parentId){
Comment parent = commentRepository.findById(parentId).orElse(null);
Comment comment = Comment.builder().content("λκΈ").parent(parent).build();
Long id = commentRepository.save(comment).getId();
clear();
return id;
}
// λκΈμ μμ νλ κ²½μ°
// λλκΈμ΄ λ¨μμλ κ²½μ°
// DBμ νλ©΄μμλ μ§μμ§μ§ μκ³ , "μμ λ λκΈμ
λλ€"λΌκ³ νμ
@Test
public void λκΈμμ _λλκΈμ΄_λ¨μμλ_κ²½μ°() throws Exception {
//given
Long commentId = saveComment();
saveReComment(commentId);
saveReComment(commentId);
saveReComment(commentId);
saveReComment(commentId);
Assertions.assertThat(commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).getChildList().size()).isEqualTo(4);
//when
commentService.remove(commentId);
clear();
//then
Comment findComment = commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT));
assertThat(findComment).isNotNull();
assertThat(findComment.isRemoved()).isTrue();
assertThat(findComment.getChildList().size()).isEqualTo(4);
}
// λκΈμ μμ νλ κ²½μ°
//λλκΈμ΄ μμ μ‘΄μ¬νμ§ μλ κ²½μ° : 곧λ°λ‘ DBμμ μμ
@Test
public void λκΈμμ _λλκΈμ΄_μλ_κ²½μ°() throws Exception {
//given
Long commentId = saveComment();
//when
commentService.remove(commentId);
clear();
//then
Assertions.assertThat(commentRepository.findAll().size()).isSameAs(0);
assertThat(assertThrows(CommentException.class, () ->commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT))).getExceptionType()).isEqualTo(CommentExceptionType.NOT_POUND_COMMENT);
}
// λκΈμ μμ νλ κ²½μ°
// λλκΈμ΄ μ‘΄μ¬νλ λͺ¨λ μμ λ κ²½μ°
//λκΈκ³Ό, λ¬λ €μλ λλκΈ λͺ¨λ DBμμ μΌκ΄ μμ , νλ©΄μμλ νμλμ§ μμ
@Test
public void λκΈμμ _λλκΈμ΄_μ‘΄μ¬νλ_λͺ¨λ_μμ λ_λλκΈμΈ_κ²½μ°() throws Exception {
//given
Long commentId = saveComment();
Long reCommend1Id = saveReComment(commentId);
Long reCommend2Id = saveReComment(commentId);
Long reCommend3Id = saveReComment(commentId);
Long reCommend4Id = saveReComment(commentId);
Assertions.assertThat(commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).getChildList().size()).isEqualTo(4);
clear();
commentService.remove(reCommend1Id);
clear();
commentService.remove(reCommend2Id);
clear();
commentService.remove(reCommend3Id);
clear();
commentService.remove(reCommend4Id);
clear();
Assertions.assertThat(commentRepository.findById(reCommend1Id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).isRemoved()).isTrue();
Assertions.assertThat(commentRepository.findById(reCommend2Id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).isRemoved()).isTrue();
Assertions.assertThat(commentRepository.findById(reCommend3Id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).isRemoved()).isTrue();
Assertions.assertThat(commentRepository.findById(reCommend4Id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).isRemoved()).isTrue();
clear();
//when
commentService.remove(commentId);
clear();
//then
LongStream.rangeClosed(commentId, reCommend4Id).forEach(id ->
assertThat(assertThrows(CommentException.class, () -> commentRepository.findById(id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT))).getExceptionType()).isEqualTo(CommentExceptionType.NOT_POUND_COMMENT)
);
}
// λλκΈμ μμ νλ κ²½μ°
// λΆλͺ¨ λκΈμ΄ μμ λμ§ μμ κ²½μ°
// λ΄μ©λ§ μμ , DBμμλ μμ X
@Test
public void λλκΈμμ _λΆλͺ¨λκΈμ΄_λ¨μμλ_κ²½μ°() throws Exception {
//given
Long commentId = saveComment();
Long reCommend1Id = saveReComment(commentId);
//when
commentService.remove(reCommend1Id);
clear();
//then
Assertions.assertThat(commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT))).isNotNull();
Assertions.assertThat(commentRepository.findById(reCommend1Id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT))).isNotNull();
Assertions.assertThat(commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).isRemoved()).isFalse();
Assertions.assertThat(commentRepository.findById(reCommend1Id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).isRemoved()).isTrue();
}
// λλκΈμ μμ νλ κ²½μ°
// λΆλͺ¨ λκΈμ΄ μμ λμ΄μκ³ , λλκΈλ€λ λͺ¨λ μμ λ κ²½μ°
// λΆλͺ¨λ₯Ό ν¬ν¨ν λͺ¨λ λλκΈμ DBμμ μΌκ΄ μμ , νλ©΄μμμλ μ§μ
@Test
public void λλκΈμμ _λΆλͺ¨λκΈμ΄_μμ λ_κ²½μ°_λͺ¨λ _λλκΈμ΄_μμ λ_κ²½μ°() throws Exception {
//given
Long commentId = saveComment();
Long reCommend1Id = saveReComment(commentId);
Long reCommend2Id = saveReComment(commentId);
Long reCommend3Id = saveReComment(commentId);
commentService.remove(reCommend2Id);
clear();
commentService.remove(commentId);
clear();
commentService.remove(reCommend3Id);
clear();
Assertions.assertThat(commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT))).isNotNull();
Assertions.assertThat(commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).getChildList().size()).isEqualTo(3);
//when
commentService.remove(reCommend1Id);
//then
LongStream.rangeClosed(commentId, reCommend3Id).forEach(id ->
assertThat(assertThrows(CommentException.class, () -> commentRepository.findById(id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT))).getExceptionType()).isEqualTo(CommentExceptionType.NOT_POUND_COMMENT)
);
}
// λλκΈμ μμ νλ κ²½μ°
// λΆλͺ¨ λκΈμ΄ μμ λμ΄μκ³ , λ€λ₯Έ λλκΈμ΄ μμ§ μμ λμ§ μκ³ λ¨μμλ κ²½μ°
//ν΄λΉ λλκΈλ§ μμ , κ·Έλ¬λ DBμμ μμ λμ§λ μκ³ , νλ©΄μμλ "μμ λ λκΈμ
λλ€"λΌκ³ νμ
@Test
public void λλκΈμμ _λΆλͺ¨λκΈμ΄_μμ λ_κ²½μ°_λ€λ₯Έ_λλκΈμ΄_λ¨μμλ_κ²½μ°() throws Exception {
//given
Long commentId = saveComment();
Long reCommend1Id = saveReComment(commentId);
Long reCommend2Id = saveReComment(commentId);
Long reCommend3Id = saveReComment(commentId);
commentService.remove(reCommend3Id);
commentService.remove(commentId);
clear();
Assertions.assertThat(commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT))).isNotNull();
Assertions.assertThat(commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).getChildList().size()).isEqualTo(3);
//when
commentService.remove(reCommend2Id);
Assertions.assertThat(commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT))).isNotNull();
//then
Assertions.assertThat(commentRepository.findById(reCommend2Id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT))).isNotNull();
Assertions.assertThat(commentRepository.findById(reCommend2Id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).isRemoved()).isTrue();
Assertions.assertThat(commentRepository.findById(reCommend1Id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).getId()).isNotNull();
Assertions.assertThat(commentRepository.findById(reCommend3Id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).getId()).isNotNull();
Assertions.assertThat(commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).getId()).isNotNull();
}
}
μ΄μ μ¬κΈ°μ μΆκ°λ‘ κ°λ°ν λκΈκ³Ό λλκΈ μ μ₯, μ λ°μ΄νΈ κΈ°λ₯μ ν μ€νΈνλ μ½λλ₯Ό μμ±νκ² μ΅λλ€.
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@SpringBootTest
@Transactional
class CommentServiceTest {
@Autowired
CommentService commentService;
@Autowired CommentRepository commentRepository;
@Autowired
PostRepository postRepository;
@Autowired
MemberService memberService;
@Autowired EntityManager em;
private void clear(){
em.flush();
em.clear();
}
@BeforeEach
private void signUpAndSetAuthentication() throws Exception {
memberService.signUp(new MemberSignUpDto("USERNAME","PASSWORD","name","nickName",22));
SecurityContext emptyContext = SecurityContextHolder.createEmptyContext();
emptyContext.setAuthentication(
new UsernamePasswordAuthenticationToken(
User.builder()
.username("USERNAME")
.password("PASSWORD")
.roles(Role.USER.toString())
.build(),
null)
);
SecurityContextHolder.setContext(emptyContext);
clear();
}
private void anotherSignUpAndSetAuthentication() throws Exception {
memberService.signUp(new MemberSignUpDto("USERNAME1","PASSWORD123","name","nickName",22));
SecurityContext emptyContext = SecurityContextHolder.createEmptyContext();
emptyContext.setAuthentication(
new UsernamePasswordAuthenticationToken(
User.builder()
.username("USERNAME1")
.password("PASSWORD123")
.roles(Role.USER.toString())
.build(),
null)
);
SecurityContextHolder.setContext(emptyContext);
clear();
}
private Long savePost(){
String title = "μ λͺ©";
String content = "λ΄μ©";
PostSaveDto postSaveDto = new PostSaveDto(title, content, Optional.empty());
//when
Post save = postRepository.save(postSaveDto.toEntity());
clear();
return save.getId();
}
private Long saveComment(){
CommentSaveDto commentSaveDto = new CommentSaveDto("λκΈ");
commentService.save(savePost(),commentSaveDto);
clear();
List<Comment> resultList = em.createQuery("select c from Comment c order by c.createdDate desc ", Comment.class).getResultList();
return resultList.get(0).getId();
}
private Long saveReComment(Long parentId){
CommentSaveDto commentSaveDto = new CommentSaveDto("λλκΈ");
commentService.saveReComment(savePost(),parentId,commentSaveDto);
clear();
List<Comment> resultList = em.createQuery("select c from Comment c order by c.createdDate desc ", Comment.class).getResultList();
return resultList.get(0).getId();
}
@Test
public void λκΈμ μ₯_μ±κ³΅() throws Exception {
//given
Long postId = savePost();
CommentSaveDto commentSaveDto = new CommentSaveDto("λκΈ");
//when
commentService.save(postId,commentSaveDto);
clear();
//then
List<Comment> resultList = em.createQuery("select c from Comment c order by c.createdDate desc ", Comment.class).getResultList();
assertThat(resultList.size()).isEqualTo(1);
}
@Test
public void λλκΈμ μ₯_μ±κ³΅() throws Exception {
//given
Long postId = savePost();
Long parentId = saveComment();
CommentSaveDto commentSaveDto = new CommentSaveDto("λλκΈ");
//when
commentService.saveReComment(postId,parentId,commentSaveDto);
clear();
//then
List<Comment> resultList = em.createQuery("select c from Comment c order by c.createdDate desc ", Comment.class).getResultList();
assertThat(resultList.size()).isEqualTo(2);
}
@Test
public void λκΈμ μ₯_μ€ν¨_κ²μλ¬Όμ΄_μμ() throws Exception {
//given
Long postId = savePost();
CommentSaveDto commentSaveDto = new CommentSaveDto("λκΈ");
//when, then
assertThat(assertThrows(PostException.class, () -> commentService.save(postId+1,commentSaveDto)).getExceptionType()).isEqualTo(PostExceptionType.POST_NOT_POUND);
}
@Test
public void λλκΈμ μ₯_μ€ν¨_κ²μλ¬Όμ΄_μμ() throws Exception {
//given
Long postId = savePost();
Long parentId = saveComment();
CommentSaveDto commentSaveDto = new CommentSaveDto("λκΈ");
//when, then
assertThat(assertThrows(PostException.class, () -> commentService.saveReComment(postId+123, parentId,commentSaveDto)).getExceptionType()).isEqualTo(PostExceptionType.POST_NOT_POUND);
}
@Test
public void λλκΈμ μ₯_μ€ν¨_λκΈμ΄_μμ() throws Exception {
//given
Long postId = savePost();
Long parentId = saveComment();
CommentSaveDto commentSaveDto = new CommentSaveDto("λκΈ");
//when, then
assertThat(assertThrows(CommentException.class, () -> commentService.saveReComment(postId, parentId+1,commentSaveDto)).getExceptionType()).isEqualTo(CommentExceptionType.NOT_POUND_COMMENT);
}
@Test
public void μ
λ°μ΄νΈ_μ±κ³΅() throws Exception {
//given
Long postId = savePost();
Long parentId = saveComment();
Long reCommentId = saveReComment(parentId);
clear();
//when
commentService.update(reCommentId, new CommentUpdateDto(Optional.ofNullable("μ
λ°μ΄νΈ")));
clear();
//then
Comment comment = commentRepository.findById(reCommentId).orElse(null);
assertThat(comment.getContent()).isEqualTo("μ
λ°μ΄νΈ");
}
@Test
public void μ
λ°μ΄νΈ_μ€ν¨_κΆνμ΄μμ() throws Exception {
//given
Long postId = savePost();
Long parentId = saveComment();
Long reCommentId = saveReComment(parentId);
clear();
anotherSignUpAndSetAuthentication();
//when, then
BaseExceptionType type = assertThrows(CommentException.class, () -> commentService.update(reCommentId, new CommentUpdateDto(Optional.ofNullable("μ
λ°μ΄νΈ")))).getExceptionType();
assertThat(type).isEqualTo(CommentExceptionType.NOT_AUTHORITY_UPDATE_COMMENT);
}
@Test
public void λκΈμμ _μ€ν¨_κΆνμ΄_μμ() throws Exception {
//given
Long postId = savePost();
Long parentId = saveComment();
Long reCommentId = saveReComment(parentId);
clear();
anotherSignUpAndSetAuthentication();
//when, then
BaseExceptionType type = assertThrows(CommentException.class, () -> commentService.remove(reCommentId)).getExceptionType();
assertThat(type).isEqualTo(CommentExceptionType.NOT_AUTHORITY_DELETE_COMMENT);
}
// λκΈμ μμ νλ κ²½μ°
// λλκΈμ΄ λ¨μμλ κ²½μ°
// DBμ νλ©΄μμλ μ§μμ§μ§ μκ³ , "μμ λ λκΈμ
λλ€"λΌκ³ νμ
@Test
public void λκΈμμ _λλκΈμ΄_λ¨μμλ_κ²½μ°() throws Exception {
//given
Long commentId = saveComment();
saveReComment(commentId);
saveReComment(commentId);
saveReComment(commentId);
saveReComment(commentId);
Assertions.assertThat(commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).getChildList().size()).isEqualTo(4);
//when
commentService.remove(commentId);
clear();
//then
Comment findComment = commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT));
assertThat(findComment).isNotNull();
assertThat(findComment.isRemoved()).isTrue();
assertThat(findComment.getChildList().size()).isEqualTo(4);
}
// λκΈμ μμ νλ κ²½μ°
//λλκΈμ΄ μμ μ‘΄μ¬νμ§ μλ κ²½μ° : 곧λ°λ‘ DBμμ μμ
@Test
public void λκΈμμ _λλκΈμ΄_μλ_κ²½μ°() throws Exception {
//given
Long commentId = saveComment();
//when
commentService.remove(commentId);
clear();
//then
Assertions.assertThat(commentRepository.findAll().size()).isSameAs(0);
assertThat(assertThrows(CommentException.class, () ->commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT))).getExceptionType()).isEqualTo(CommentExceptionType.NOT_POUND_COMMENT);
}
// λκΈμ μμ νλ κ²½μ°
// λλκΈμ΄ μ‘΄μ¬νλ λͺ¨λ μμ λ κ²½μ°
//λκΈκ³Ό, λ¬λ €μλ λλκΈ λͺ¨λ DBμμ μΌκ΄ μμ , νλ©΄μμλ νμλμ§ μμ
@Test
public void λκΈμμ _λλκΈμ΄_μ‘΄μ¬νλ_λͺ¨λ_μμ λ_λλκΈμΈ_κ²½μ°() throws Exception {
//given
Long commentId = saveComment();
Long reCommend1Id = saveReComment(commentId);
Long reCommend2Id = saveReComment(commentId);
Long reCommend3Id = saveReComment(commentId);
Long reCommend4Id = saveReComment(commentId);
Assertions.assertThat(commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).getChildList().size()).isEqualTo(4);
clear();
commentService.remove(reCommend1Id);
clear();
commentService.remove(reCommend2Id);
clear();
commentService.remove(reCommend3Id);
clear();
commentService.remove(reCommend4Id);
clear();
Assertions.assertThat(commentRepository.findById(reCommend1Id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).isRemoved()).isTrue();
Assertions.assertThat(commentRepository.findById(reCommend2Id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).isRemoved()).isTrue();
Assertions.assertThat(commentRepository.findById(reCommend3Id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).isRemoved()).isTrue();
Assertions.assertThat(commentRepository.findById(reCommend4Id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).isRemoved()).isTrue();
clear();
//when
commentService.remove(commentId);
clear();
//then
LongStream.rangeClosed(commentId, reCommend4Id).forEach(id ->
assertThat(assertThrows(CommentException.class, () -> commentRepository.findById(id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT))).getExceptionType()).isEqualTo(CommentExceptionType.NOT_POUND_COMMENT)
);
}
// λλκΈμ μμ νλ κ²½μ°
// λΆλͺ¨ λκΈμ΄ μμ λμ§ μμ κ²½μ°
// λ΄μ©λ§ μμ , DBμμλ μμ X
@Test
public void λλκΈμμ _λΆλͺ¨λκΈμ΄_λ¨μμλ_κ²½μ°() throws Exception {
//given
Long commentId = saveComment();
Long reCommend1Id = saveReComment(commentId);
//when
commentService.remove(reCommend1Id);
clear();
//then
Assertions.assertThat(commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT))).isNotNull();
Assertions.assertThat(commentRepository.findById(reCommend1Id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT))).isNotNull();
Assertions.assertThat(commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).isRemoved()).isFalse();
Assertions.assertThat(commentRepository.findById(reCommend1Id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).isRemoved()).isTrue();
}
// λλκΈμ μμ νλ κ²½μ°
// λΆλͺ¨ λκΈμ΄ μμ λμ΄μκ³ , λλκΈλ€λ λͺ¨λ μμ λ κ²½μ°
// λΆλͺ¨λ₯Ό ν¬ν¨ν λͺ¨λ λλκΈμ DBμμ μΌκ΄ μμ , νλ©΄μμμλ μ§μ
@Test
public void λλκΈμμ _λΆλͺ¨λκΈμ΄_μμ λ_κ²½μ°_λͺ¨λ _λλκΈμ΄_μμ λ_κ²½μ°() throws Exception {
//given
Long commentId = saveComment();
Long reCommend1Id = saveReComment(commentId);
Long reCommend2Id = saveReComment(commentId);
Long reCommend3Id = saveReComment(commentId);
commentService.remove(reCommend2Id);
clear();
commentService.remove(commentId);
clear();
commentService.remove(reCommend3Id);
clear();
Assertions.assertThat(commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT))).isNotNull();
Assertions.assertThat(commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).getChildList().size()).isEqualTo(3);
//when
commentService.remove(reCommend1Id);
//then
LongStream.rangeClosed(commentId, reCommend3Id).forEach(id ->
assertThat(assertThrows(CommentException.class, () -> commentRepository.findById(id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT))).getExceptionType()).isEqualTo(CommentExceptionType.NOT_POUND_COMMENT)
);
}
// λλκΈμ μμ νλ κ²½μ°
// λΆλͺ¨ λκΈμ΄ μμ λμ΄μκ³ , λ€λ₯Έ λλκΈμ΄ μμ§ μμ λμ§ μκ³ λ¨μμλ κ²½μ°
//ν΄λΉ λλκΈλ§ μμ , κ·Έλ¬λ DBμμ μμ λμ§λ μκ³ , νλ©΄μμλ "μμ λ λκΈμ
λλ€"λΌκ³ νμ
@Test
public void λλκΈμμ _λΆλͺ¨λκΈμ΄_μμ λ_κ²½μ°_λ€λ₯Έ_λλκΈμ΄_λ¨μμλ_κ²½μ°() throws Exception {
//given
Long commentId = saveComment();
Long reCommend1Id = saveReComment(commentId);
Long reCommend2Id = saveReComment(commentId);
Long reCommend3Id = saveReComment(commentId);
commentService.remove(reCommend3Id);
commentService.remove(commentId);
clear();
Assertions.assertThat(commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT))).isNotNull();
Assertions.assertThat(commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).getChildList().size()).isEqualTo(3);
//when
commentService.remove(reCommend2Id);
Assertions.assertThat(commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT))).isNotNull();
//then
Assertions.assertThat(commentRepository.findById(reCommend2Id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT))).isNotNull();
Assertions.assertThat(commentRepository.findById(reCommend2Id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).isRemoved()).isTrue();
Assertions.assertThat(commentRepository.findById(reCommend1Id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).getId()).isNotNull();
Assertions.assertThat(commentRepository.findById(reCommend3Id).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).getId()).isNotNull();
Assertions.assertThat(commentRepository.findById(commentId).orElseThrow(()-> new CommentException(CommentExceptionType.NOT_POUND_COMMENT)).getId()).isNotNull();
}
}
μ ν μ€νΈμ½λκ° λ무 λμ‘νλ€μ...
μ΄ν νλ² μ 체 ν μ€νΈμ½λλ₯Ό 리νν λ§ νλλ‘ νκ² μ΅λλ€.
μ΄λ κ² ν΄μ λκΈκ³Ό λλκΈμ λν μλΉμ€ κ°λ°μ μλ£νμμ΅λλ€.
μ΄μ λ€μ ν¬μ€ν μμ κ²μκΈμ λν μ 보 μ‘°ν, κ²μκΈ λ¦¬μ€νΈ μ‘°νλ±μ κΈ°λ₯μ μΆκ°νμ¬, κΈ°λ³Έμ μΈ κ²μν APIλ₯Ό μμ±μν€λλ‘ νκ² μ΅λλ€. κ°μ¬ν©λλ€.
μ 체 μ½λλ κΉνλΈμμ νμΈνμ€ μ μμ΅λλ€.
(μΆκ°λ‘ μ κ° μ μ μ΄ μμ΄μ κ³ μΉκ³ λ°μμ νμ§ λͺ»ν λΆλΆμ΄ μμ μλ μκΈ°μ, μ½λκ° μ λλ‘ μλνμ§ μλλ€λ©΄ μΈμ λ μ§ λκΈμ΄λ λ©μΌ 보λ΄μ£Όμλ©΄ νμΈνλλ‘ νκ² μ΅λλ€.)
https://github.com/ShinDongHun1/SpringBoot-Board-API