diff --git a/backend/src/main/java/com/votogether/domain/post/entity/comment/Content.java b/backend/src/main/java/com/votogether/domain/post/entity/comment/Content.java index e6e0fd3b8..c994cfe2b 100644 --- a/backend/src/main/java/com/votogether/domain/post/entity/comment/Content.java +++ b/backend/src/main/java/com/votogether/domain/post/entity/comment/Content.java @@ -11,7 +11,7 @@ @NoArgsConstructor(access = AccessLevel.PROTECTED) @Getter @Embeddable -class Content { +public class Content { private static final int MAXIMUM_LENGTH = 500; diff --git a/backend/src/test/java/com/votogether/domain/member/service/MemberServiceTest.java b/backend/src/test/java/com/votogether/domain/member/service/MemberServiceTest.java index 034f64040..51fafe6e0 100644 --- a/backend/src/test/java/com/votogether/domain/member/service/MemberServiceTest.java +++ b/backend/src/test/java/com/votogether/domain/member/service/MemberServiceTest.java @@ -24,7 +24,6 @@ import com.votogether.global.exception.BadRequestException; import com.votogether.test.annotation.ServiceTest; import com.votogether.test.fixtures.MemberFixtures; -import com.votogether.test.persister.MemberTestPersister; import jakarta.persistence.EntityManager; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; @@ -60,9 +59,6 @@ class MemberServiceTest { @Autowired CommentRepository commentRepository; - @Autowired - MemberTestPersister memberTestPersister; - @Autowired EntityManager em; diff --git a/backend/src/test/java/com/votogether/domain/post/repository/CommentRepositoryTest.java b/backend/src/test/java/com/votogether/domain/post/repository/CommentRepositoryTest.java index c50d89c9d..a09f72d60 100644 --- a/backend/src/test/java/com/votogether/domain/post/repository/CommentRepositoryTest.java +++ b/backend/src/test/java/com/votogether/domain/post/repository/CommentRepositoryTest.java @@ -9,6 +9,7 @@ import com.votogether.domain.post.entity.comment.Comment; import com.votogether.test.annotation.RepositoryTest; import com.votogether.test.fixtures.MemberFixtures; +import com.votogether.test.persister.PostTestPersister; import java.time.LocalDateTime; import java.util.List; import org.junit.jupiter.api.DisplayName; @@ -27,18 +28,21 @@ class CommentRepositoryTest { @Autowired PostRepository postRepository; + @Autowired + PostTestPersister postTestPersister; + @Test @DisplayName("게시글의 댓글 목록을 조회한다.") void findAllByPost() { // given Member member = memberRepository.save(MemberFixtures.MALE_20.get()); - Post post = postRepository.save( - Post.builder() - .writer(member) - .postBody(PostBody.builder().title("titleA").content("contentA").build()) - .deadline(LocalDateTime.of(2100, 7, 12, 0, 0)) - .build() - ); + + final Post post = postTestPersister.builder() + .writer(member) + .postBody(PostBody.builder().title("titleA").content("contentA").build()) + .deadline(LocalDateTime.of(2100, 7, 12, 0, 0)) + .save(); + Comment commentA = commentRepository.save( Comment.builder() .member(member) diff --git a/backend/src/test/java/com/votogether/domain/report/controller/ReportControllerTest.java b/backend/src/test/java/com/votogether/domain/report/controller/ReportControllerTest.java index 699f90f96..b39bae846 100644 --- a/backend/src/test/java/com/votogether/domain/report/controller/ReportControllerTest.java +++ b/backend/src/test/java/com/votogether/domain/report/controller/ReportControllerTest.java @@ -79,7 +79,8 @@ void reportPost() throws Exception { .body(request) .when().post("/report") .then().log().all() - .statusCode(HttpStatus.OK.value()); + .assertThat() + .status(HttpStatus.OK); } @Test @@ -110,7 +111,8 @@ void reportComment() throws Exception { .body(request) .when().post("/report") .then().log().all() - .statusCode(HttpStatus.OK.value()); + .assertThat() + .status(HttpStatus.OK); } @Test @@ -141,7 +143,8 @@ void reportNickname() throws Exception { .body(request) .when().post("/report") .then().log().all() - .statusCode(HttpStatus.OK.value()); + .assertThat() + .status(HttpStatus.OK); } } @@ -175,7 +178,8 @@ void report(Long id) throws Exception { .body(request) .when().post("/report") .then().log().all() - .statusCode(HttpStatus.BAD_REQUEST.value()); + .assertThat() + .status(HttpStatus.BAD_REQUEST); } @ParameterizedTest @@ -207,7 +211,8 @@ void reportBadRequest(String reason) throws Exception { .body(request) .when().post("/report") .then().log().all() - .statusCode(HttpStatus.BAD_REQUEST.value()); + .assertThat() + .status(HttpStatus.BAD_REQUEST); } } diff --git a/backend/src/test/java/com/votogether/domain/report/repository/ReportRepositoryTest.java b/backend/src/test/java/com/votogether/domain/report/repository/ReportRepositoryTest.java index 2c1422730..b144eea35 100644 --- a/backend/src/test/java/com/votogether/domain/report/repository/ReportRepositoryTest.java +++ b/backend/src/test/java/com/votogether/domain/report/repository/ReportRepositoryTest.java @@ -12,7 +12,10 @@ import com.votogether.domain.report.entity.vo.ReportType; import com.votogether.test.annotation.RepositoryTest; import com.votogether.test.fixtures.MemberFixtures; +import com.votogether.test.persister.PostTestPersister; +import com.votogether.test.persister.ReportTestPersister; import java.time.LocalDateTime; +import lombok.RequiredArgsConstructor; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -29,34 +32,37 @@ class ReportRepositoryTest { @Autowired PostRepository postRepository; + @Autowired + PostTestPersister postTestPersister; + + @Autowired + ReportTestPersister reportTestPersister; + + @Test @DisplayName("회원, 신고타입, 대상ID를 통해서 신고 횟수를 반환한다.") void countByMemberAndReportTypeAndTargetId() { // given Member member = MemberFixtures.MALE_20.get(); ReportType reportType = ReportType.POST; - PostBody postBody = PostBody.builder() .title("title") .content("content") .build(); - Post post = Post.builder() + memberRepository.save(member); + Post post = postTestPersister.builder() .writer(member) .postBody(postBody) .deadline(LocalDateTime.of(2100, 7, 12, 0, 0)) - .build(); - - memberRepository.save(member); - postRepository.save(post); + .save(); - Report report = Report.builder() + reportTestPersister.builder() .member(member) .reportType(reportType) .targetId(post.getId()) .reason("불건전한 게시글") - .build(); - reportRepository.save(report); + .save(); // when int reportCount = reportRepository.countByReportTypeAndTargetId(reportType, post.getId()); @@ -76,22 +82,19 @@ void findByMemberAndReportTypeAndTargetId() { .content("content") .build(); - Post post = Post.builder() + memberRepository.save(member); + Post post = postTestPersister.builder() .writer(member) .postBody(postBody) .deadline(LocalDateTime.of(2100, 7, 12, 0, 0)) - .build(); - - memberRepository.save(member); - postRepository.save(post); + .save(); - Report report = Report.builder() - .targetId(post.getId()) - .reportType(ReportType.POST) + reportTestPersister.builder() .member(member) + .reportType(ReportType.POST) + .targetId(post.getId()) .reason("불건전한 게시글") - .build(); - reportRepository.save(report); + .save(); // when Report actualReport = reportRepository.findByMemberAndReportTypeAndTargetId( diff --git a/backend/src/test/java/com/votogether/domain/report/service/ReportServiceTest.java b/backend/src/test/java/com/votogether/domain/report/service/ReportServiceTest.java index 74374aed9..50344d3ce 100644 --- a/backend/src/test/java/com/votogether/domain/report/service/ReportServiceTest.java +++ b/backend/src/test/java/com/votogether/domain/report/service/ReportServiceTest.java @@ -11,6 +11,7 @@ import com.votogether.domain.post.entity.Post; import com.votogether.domain.post.entity.PostBody; import com.votogether.domain.post.entity.comment.Comment; +import com.votogether.domain.post.entity.comment.Content; import com.votogether.domain.post.entity.vo.PostClosingType; import com.votogether.domain.post.entity.vo.PostSortType; import com.votogether.domain.post.repository.CommentRepository; @@ -24,6 +25,8 @@ import com.votogether.global.exception.NotFoundException; import com.votogether.test.annotation.ServiceTest; import com.votogether.test.fixtures.MemberFixtures; +import com.votogether.test.persister.CommentTestPersister; +import com.votogether.test.persister.PostTestPersister; import java.time.LocalDateTime; import java.util.List; import org.junit.jupiter.api.DisplayName; @@ -55,6 +58,12 @@ class ReportServiceTest { @Autowired PostCommentService postCommentService; + @Autowired + PostTestPersister postTestPersister; + + @Autowired + CommentTestPersister commentTestPersister; + @Nested @DisplayName("게시글 신고기능은") class ReportPost { @@ -71,13 +80,11 @@ void reportPost() { .content("content") .build(); - Post post = Post.builder() + Post post = postTestPersister.builder() .writer(writer) .postBody(postBody) .deadline(LocalDateTime.of(2100, 7, 12, 0, 0)) - .build(); - - postRepository.save(post); + .save(); ReportRequest request = new ReportRequest(ReportType.POST, post.getId(), "불건전한 게시글"); @@ -110,13 +117,11 @@ void reportOwnPostThrowsException() { .content("content") .build(); - Post post = Post.builder() + Post post = postTestPersister.builder() .writer(writer) .postBody(postBody) .deadline(LocalDateTime.of(2100, 7, 12, 0, 0)) - .build(); - - postRepository.save(post); + .save(); ReportRequest request = new ReportRequest(ReportType.POST, post.getId(), "불건전한 게시글"); @@ -138,14 +143,12 @@ void reportHiddenPost() { .content("content") .build(); - Post post = Post.builder() + Post post = postTestPersister.builder() .writer(writer) .postBody(postBody) .deadline(LocalDateTime.of(2100, 7, 12, 0, 0)) - .build(); - post.blind(); - - postRepository.save(post); + .blind() + .save(); ReportRequest request = new ReportRequest(ReportType.POST, post.getId(), "불건전한 게시글"); @@ -168,13 +171,11 @@ void reportDuplicated() { .content("content") .build(); - Post post = Post.builder() + Post post = postTestPersister.builder() .writer(writer) .postBody(postBody) .deadline(LocalDateTime.of(2100, 7, 12, 0, 0)) - .build(); - - postRepository.save(post); + .save(); ReportRequest request = new ReportRequest(ReportType.POST, post.getId(), "불건전한 게시글"); @@ -203,13 +204,11 @@ void reportAndBlind() { .content("content") .build(); - Post post = Post.builder() + Post post = postTestPersister.builder() .writer(writer) .postBody(postBody) .deadline(LocalDateTime.of(2100, 7, 12, 0, 0)) - .build(); - - postRepository.save(post); + .save(); ReportRequest request = new ReportRequest(ReportType.POST, post.getId(), "불건전한 게시글"); @@ -252,20 +251,17 @@ void reportComment() { .content("content") .build(); - Post post = Post.builder() + Post post = postTestPersister.builder() .writer(writer) .postBody(postBody) .deadline(LocalDateTime.of(2100, 7, 12, 0, 0)) - .build(); + .save(); - Comment comment = Comment.builder() + Comment comment = commentTestPersister.builder() .post(post) .member(writer) - .content("으어어어어") - .build(); - - postRepository.save(post); - commentRepository.save(comment); + .content(new Content("으어어어어")) + .save(); ReportRequest request = new ReportRequest(ReportType.COMMENT, comment.getId(), "불건전한 게시글"); @@ -298,20 +294,17 @@ void reportOwnCommentThrowsException() { .content("content") .build(); - Post post = Post.builder() + Post post = postTestPersister.builder() .writer(writer) .postBody(postBody) .deadline(LocalDateTime.of(2100, 7, 12, 0, 0)) - .build(); + .save(); - Comment comment = Comment.builder() + Comment comment = commentTestPersister.builder() .post(post) .member(writer) - .content("으어어어어") - .build(); - - postRepository.save(post); - commentRepository.save(comment); + .content(new Content("으어어어어")) + .save(); ReportRequest request = new ReportRequest(ReportType.COMMENT, comment.getId(), "불건전한 댓글"); @@ -333,20 +326,17 @@ void reportHiddenComment() { .content("content") .build(); - Post post = Post.builder() + Post post = postTestPersister.builder() .writer(writer) .postBody(postBody) .deadline(LocalDateTime.of(2100, 7, 12, 0, 0)) - .build(); + .save(); - Comment comment = Comment.builder() + Comment comment = commentTestPersister.builder() .post(post) .member(writer) - .content("으어어어어") - .build(); - - postRepository.save(post); - commentRepository.save(comment); + .content(new Content("으어어어어")) + .save(); ReportRequest request = new ReportRequest(ReportType.COMMENT, comment.getId(), "불건전한 댓글"); @@ -369,20 +359,17 @@ void reportDuplicated() { .content("content") .build(); - Post post = Post.builder() + Post post = postTestPersister.builder() .writer(writer) .postBody(postBody) .deadline(LocalDateTime.of(2100, 7, 12, 0, 0)) - .build(); + .save(); - Comment comment = Comment.builder() + Comment comment = commentTestPersister.builder() .post(post) .member(writer) - .content("으어어어어") - .build(); - - postRepository.save(post); - commentRepository.save(comment); + .content(new Content("으어어어어")) + .save(); ReportRequest request = new ReportRequest(ReportType.COMMENT, comment.getId(), "불건전한 댓글"); @@ -411,20 +398,17 @@ void reportAndBlind() { .content("content") .build(); - Post post = Post.builder() + Post post = postTestPersister.builder() .writer(writer) .postBody(postBody) .deadline(LocalDateTime.of(2100, 7, 12, 0, 0)) - .build(); + .save(); - Comment comment = Comment.builder() + Comment comment = commentTestPersister.builder() .post(post) .member(writer) - .content("으어어어어") - .build(); - - postRepository.save(post); - commentRepository.save(comment); + .content(new Content("으어어어어")) + .save(); ReportRequest request = new ReportRequest(ReportType.COMMENT, comment.getId(), "불건전한 댓글"); diff --git a/backend/src/test/java/com/votogether/test/persister/CommentTestPersister.java b/backend/src/test/java/com/votogether/test/persister/CommentTestPersister.java new file mode 100644 index 000000000..e01c3dd12 --- /dev/null +++ b/backend/src/test/java/com/votogether/test/persister/CommentTestPersister.java @@ -0,0 +1,52 @@ +package com.votogether.test.persister; + +import com.votogether.domain.member.entity.Member; +import com.votogether.domain.post.entity.Post; +import com.votogether.domain.post.entity.comment.Comment; +import com.votogether.domain.post.entity.comment.Content; +import com.votogether.domain.post.repository.CommentRepository; +import com.votogether.test.fixtures.MemberFixtures; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +@Persister +public class CommentTestPersister { + + private final CommentRepository commentRepository; + + public CommentBuilder builder() { + return new CommentTestPersister.CommentBuilder(); + } + + public final class CommentBuilder { + + private Post post; + private Member member; + private Content content; + + public CommentBuilder post(Post post) { + this.post = post; + return this; + } + + public CommentBuilder member(Member member) { + this.member = member; + return this; + } + + public CommentBuilder content(Content content) { + this.content = content; + return this; + } + + public Comment save() { + Comment comment = Comment.builder() + .post(post == null ? Post.builder().build() : post) + .member(member == null ? MemberFixtures.MALE_20.get() : member) + .content(content == null ? "content" : content.getValue()) + .build(); + return commentRepository.save(comment); + } + } + +} diff --git a/backend/src/test/java/com/votogether/test/persister/PostTestPersister.java b/backend/src/test/java/com/votogether/test/persister/PostTestPersister.java index 7f2e3cfb0..2826bd6e9 100644 --- a/backend/src/test/java/com/votogether/test/persister/PostTestPersister.java +++ b/backend/src/test/java/com/votogether/test/persister/PostTestPersister.java @@ -4,8 +4,10 @@ import com.votogether.domain.post.entity.Post; import com.votogether.domain.post.entity.PostBody; import com.votogether.domain.post.repository.PostRepository; +import com.votogether.domain.post.service.PostService; import java.time.LocalDateTime; import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; @RequiredArgsConstructor @Persister @@ -23,6 +25,7 @@ public final class PostBuilder { private Member writer; private PostBody postBody; private LocalDateTime deadline; + private boolean isHidden; public PostBuilder writer(Member writer) { this.writer = writer; @@ -39,12 +42,21 @@ public PostBuilder deadline(LocalDateTime deadline) { return this; } + public PostBuilder blind() { + this.isHidden = true; + return this; + } + public Post save() { Post post = Post.builder() .writer(writer == null ? memberTestPersister.builder().save() : writer) .postBody(postBody == null ? generatePostBody() : postBody) .deadline(deadline == null ? LocalDateTime.of(2100, 12, 25, 0, 0) : deadline) .build(); + if (isHidden) { + post.blind(); + } + return postRepository.save(post); } @@ -54,7 +66,6 @@ private PostBody generatePostBody() { .content("content") .build(); } - } } diff --git a/backend/src/test/java/com/votogether/test/persister/ReportTestPersister.java b/backend/src/test/java/com/votogether/test/persister/ReportTestPersister.java new file mode 100644 index 000000000..a2b2c88a5 --- /dev/null +++ b/backend/src/test/java/com/votogether/test/persister/ReportTestPersister.java @@ -0,0 +1,59 @@ +package com.votogether.test.persister; + +import com.votogether.domain.member.entity.Member; +import com.votogether.domain.report.entity.Report; +import com.votogether.domain.report.entity.vo.ReportType; +import com.votogether.domain.report.repository.ReportRepository; +import java.time.LocalDateTime; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +@Persister +public class ReportTestPersister { + + private final ReportRepository reportRepository; + private final MemberTestPersister memberTestPersister; + + public ReportBuilder builder() { + return new ReportTestPersister.ReportBuilder(); + } + + public final class ReportBuilder { + + private Member member; + private ReportType reportType; + private Long targetId; + private String reason; + + public ReportBuilder member(final Member member) { + this.member = member; + return this; + } + + public ReportBuilder reportType(final ReportType reportType) { + this.reportType = reportType; + return this; + } + + public ReportBuilder targetId(final Long targetId) { + this.targetId = targetId; + return this; + } + + public ReportBuilder reason(final String reason) { + this.reason = reason; + return this; + } + + public Report save() { + Report report = Report.builder() + .member(member == null ? memberTestPersister.builder().save() : member) + .reportType(reportType == null ? ReportType.POST : reportType) + .targetId(targetId == null ? 1L : targetId) + .reason(reason == null ? "reason" : reason) + .build(); + + return reportRepository.save(report); + } + } +}