|
| 1 | +package com.gdsc.blog.comment.service; |
| 2 | + |
| 3 | +import com.gdsc.blog.article.dto.ArticleCreateDto; |
| 4 | +import com.gdsc.blog.article.dto.ArticleDto; |
| 5 | +import com.gdsc.blog.article.service.ArticleService; |
| 6 | +import com.gdsc.blog.comment.dto.CommentUpdateDto; |
| 7 | +import com.gdsc.blog.comment.entity.Comment; |
| 8 | +import com.gdsc.blog.user.entity.User; |
| 9 | +import com.gdsc.blog.user.entity.UserRole; |
| 10 | +import com.gdsc.blog.user.service.UserService; |
| 11 | +import jakarta.transaction.Transactional; |
| 12 | +import lombok.extern.slf4j.Slf4j; |
| 13 | +import org.junit.jupiter.api.BeforeAll; |
| 14 | +import org.junit.jupiter.api.DisplayName; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.junit.jupiter.api.TestInstance; |
| 17 | +import org.springframework.beans.factory.annotation.Autowired; |
| 18 | +import org.springframework.boot.test.context.SpringBootTest; |
| 19 | +import org.springframework.context.annotation.Profile; |
| 20 | + |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | +import java.util.NoSuchElementException; |
| 24 | + |
| 25 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 27 | + |
| 28 | +@Slf4j |
| 29 | +@SpringBootTest |
| 30 | +@Profile("test") |
| 31 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 32 | +class CommentServiceTest { |
| 33 | + |
| 34 | + @Autowired |
| 35 | + private UserService userService; |
| 36 | + |
| 37 | + @Autowired |
| 38 | + private ArticleService articleService; |
| 39 | + |
| 40 | + @Autowired |
| 41 | + private CommentService commentService; |
| 42 | + |
| 43 | + User user; |
| 44 | + User admin; |
| 45 | + ArticleDto articleDto; |
| 46 | + |
| 47 | + @BeforeAll |
| 48 | + void setUp() { |
| 49 | + // 회원 가입 |
| 50 | + user = userService.signup(User.builder() |
| 51 | + .username("user1") |
| 52 | + |
| 53 | + .password("password") |
| 54 | + .build()); |
| 55 | + admin = userService.signup(User.builder() |
| 56 | + .username("admin1") |
| 57 | + |
| 58 | + .password("password") |
| 59 | + .roles(Collections.singletonList(UserRole.ROLE_ADMIN)) |
| 60 | + .build()); |
| 61 | + userService.becomeAdmin(admin.getUsername()); |
| 62 | + |
| 63 | + // 게시글 생성 |
| 64 | + articleDto = articleService.createArticle(ArticleCreateDto.builder() |
| 65 | + .title("title") |
| 66 | + .content("content") |
| 67 | + .build(), user.getUsername()); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + @Transactional |
| 73 | + @DisplayName("댓글 생성") |
| 74 | + void create() { |
| 75 | + Comment comment = Comment.builder() |
| 76 | + .content("content") |
| 77 | + .build(); |
| 78 | + Comment saved = commentService.create(comment, articleDto.getIdx(), user); |
| 79 | + assertEquals(comment.getContent(), saved.getContent()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + @Transactional |
| 84 | + @DisplayName("게시글 별 댓글 조회") |
| 85 | + void getAllCommandByArticle() { |
| 86 | + // given |
| 87 | + Comment comment1 = Comment.builder() |
| 88 | + .content("content1") |
| 89 | + .build(); |
| 90 | + Comment comment2 = Comment.builder() |
| 91 | + .content("content2") |
| 92 | + .build(); |
| 93 | + Comment comment3 = Comment.builder() |
| 94 | + .content("content3") |
| 95 | + .build(); |
| 96 | + commentService.create(comment1, articleDto.getIdx(), user); |
| 97 | + commentService.create(comment2, articleDto.getIdx(), user); |
| 98 | + commentService.create(comment3, articleDto.getIdx(), user); |
| 99 | + |
| 100 | + // when |
| 101 | + List<Comment> comments = commentService.getAllCommandByArticle(articleDto.getIdx()); |
| 102 | + |
| 103 | + // then |
| 104 | + assertEquals(3, comments.size()); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + @Transactional |
| 109 | + @DisplayName("특정 댓글 조회") |
| 110 | + void getCommentById() { |
| 111 | + // given |
| 112 | + Comment comment = Comment.builder() |
| 113 | + .content("content") |
| 114 | + .build(); |
| 115 | + Comment saved = commentService.create(comment, articleDto.getIdx(), user); |
| 116 | + |
| 117 | + // when |
| 118 | + Comment found = commentService.getCommentById(saved.getIdx()); |
| 119 | + |
| 120 | + // then |
| 121 | + assertEquals(saved.getIdx(), found.getIdx()); |
| 122 | + assertEquals(saved.getContent(), found.getContent()); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + @Transactional |
| 127 | + @DisplayName("댓글 수정") |
| 128 | + void updateComment() { |
| 129 | + // given |
| 130 | + Comment comment = Comment.builder() |
| 131 | + .content("content") |
| 132 | + .build(); |
| 133 | + Comment saved = commentService.create(comment, articleDto.getIdx(), user); |
| 134 | + |
| 135 | + CommentUpdateDto commentUpdateDto = CommentUpdateDto.builder() |
| 136 | + .content("updated") |
| 137 | + .build(); |
| 138 | + |
| 139 | + // when |
| 140 | + Comment updated = commentService.updateComment(saved.getIdx(), commentUpdateDto); |
| 141 | + |
| 142 | + // then |
| 143 | + assertEquals(saved.getIdx(), updated.getIdx()); |
| 144 | + assertEquals("updated", updated.getContent()); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + @DisplayName("댓글 삭제") |
| 149 | + @Transactional |
| 150 | + void delete() { |
| 151 | + // given |
| 152 | + Comment comment = Comment.builder() |
| 153 | + .content("content") |
| 154 | + .build(); |
| 155 | + Comment saved = commentService.create(comment, articleDto.getIdx(), user); |
| 156 | + |
| 157 | + // when |
| 158 | + commentService.delete(saved); |
| 159 | + |
| 160 | + // then |
| 161 | + assertThrows(NoSuchElementException.class, () -> commentService.getCommentById(saved.getIdx())); |
| 162 | + } |
| 163 | +} |
0 commit comments