From 95bd73f3a80bf651423d13f794f3ddc38a8f9b21 Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Sun, 25 Aug 2024 06:47:53 +0900 Subject: [PATCH 01/26] =?UTF-8?q?fix:=20Custom=20=EC=97=90=EB=9F=AC=20?= =?UTF-8?q?=EC=9E=A1=EC=A7=80=20=EB=AA=BB=ED=95=98=EA=B3=A0,=20Internal=20?= =?UTF-8?q?Server=20Error=EB=A1=9C=20=EC=B2=98=EB=A6=AC=ED=95=98=EB=8A=94?= =?UTF-8?q?=20=EB=AC=B8=EC=A0=9C=20=ED=95=B4=EA=B2=B0=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/GlobalExceptionHandler.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/team05/integrated_feed_backend/exception/GlobalExceptionHandler.java b/src/main/java/team05/integrated_feed_backend/exception/GlobalExceptionHandler.java index 3bd9293..58c74d4 100644 --- a/src/main/java/team05/integrated_feed_backend/exception/GlobalExceptionHandler.java +++ b/src/main/java/team05/integrated_feed_backend/exception/GlobalExceptionHandler.java @@ -11,6 +11,7 @@ import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; import com.fasterxml.jackson.databind.exc.InvalidFormatException; @@ -27,6 +28,7 @@ @Slf4j @RequiredArgsConstructor +@RestControllerAdvice public class GlobalExceptionHandler { /** From ca83d21c26ba9a16564a29c06ae36c47c7e11bc0 Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Sun, 25 Aug 2024 08:45:58 +0900 Subject: [PATCH 02/26] =?UTF-8?q?feat:=20=EC=A2=8B=EC=95=84=EC=9A=94,=20?= =?UTF-8?q?=EA=B3=B5=EC=9C=A0=20=EC=88=98=20=EC=98=AC=EB=A6=AC=EB=8A=94=20?= =?UTF-8?q?=EB=B9=84=EC=A6=88=EB=8B=88=EC=8A=A4=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/post/entity/Post.java | 69 ++++++++++++------- 1 file changed, 44 insertions(+), 25 deletions(-) diff --git a/src/main/java/team05/integrated_feed_backend/module/post/entity/Post.java b/src/main/java/team05/integrated_feed_backend/module/post/entity/Post.java index 84aa3e3..72f2c53 100644 --- a/src/main/java/team05/integrated_feed_backend/module/post/entity/Post.java +++ b/src/main/java/team05/integrated_feed_backend/module/post/entity/Post.java @@ -1,15 +1,25 @@ package team05.integrated_feed_backend.module.post.entity; -import jakarta.persistence.*; -import lombok.*; -import team05.integrated_feed_backend.common.enums.SocialMediaType; -import team05.integrated_feed_backend.common.BaseEntity; - import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Set; +import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import team05.integrated_feed_backend.common.BaseEntity; +import team05.integrated_feed_backend.common.enums.SocialMediaType; @Entity @Getter @@ -18,30 +28,39 @@ @Builder public class Post extends BaseEntity { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long postId; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long postId; + + @Column(nullable = false) + private String title; - @Column(nullable = false) - private String title; + @Column(nullable = false, columnDefinition = "TEXT") + private String content; - @Column(nullable = false, columnDefinition = "TEXT") - private String content; + @Enumerated(EnumType.STRING) + @Column(nullable = false) + private SocialMediaType type; - @Enumerated(EnumType.STRING) - @Column(nullable = false) - private SocialMediaType type; + @Column(nullable = false) + private Long viewCount; - @Column(nullable = false) - private Long viewCount; + @Column(nullable = false) + private Long likeCount; - @Column(nullable = false) - private Long likeCount; + @Column(nullable = false) + private Long shareCount; - @Column(nullable = false) - private Long shareCount; + @OneToMany(mappedBy = "post", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) + private List postHashtags = new ArrayList<>(); - @OneToMany(mappedBy = "post", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) - private List postHashtags = new ArrayList<>(); + // 좋아요 수 증가시키는 메서드 + public void increaseLikeCount() { + this.likeCount += 1; + } + // 공유 수 증가시키는 메서드 + public void increaseShareCount() { + this.shareCount += 1; + } } From 38a488fabd800e099e459be48c82e4d9ebc516aa Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Sun, 25 Aug 2024 08:46:42 +0900 Subject: [PATCH 03/26] =?UTF-8?q?feat:=20=EC=A2=8B=EC=95=84=EC=9A=94,=20?= =?UTF-8?q?=EA=B3=B5=EC=9C=A0=20=EC=88=98=20=EC=98=AC=EB=A6=B4=20=EB=95=8C?= =?UTF-8?q?=20=EC=99=B8=EB=B6=80=20api=20=ED=98=B8=EC=B6=9C=EC=9D=84=20?= =?UTF-8?q?=EC=9C=84=ED=95=9C=20=EC=9D=B4=EB=B2=A4=ED=8A=B8=20=EC=A0=95?= =?UTF-8?q?=EC=9D=98=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/post/event/LikeCountIncreaseEvent.java | 10 ++++++++++ .../module/post/event/ShareCountIncreaseEvent.java | 10 ++++++++++ 2 files changed, 20 insertions(+) create mode 100644 src/main/java/team05/integrated_feed_backend/module/post/event/LikeCountIncreaseEvent.java create mode 100644 src/main/java/team05/integrated_feed_backend/module/post/event/ShareCountIncreaseEvent.java diff --git a/src/main/java/team05/integrated_feed_backend/module/post/event/LikeCountIncreaseEvent.java b/src/main/java/team05/integrated_feed_backend/module/post/event/LikeCountIncreaseEvent.java new file mode 100644 index 0000000..1400aa2 --- /dev/null +++ b/src/main/java/team05/integrated_feed_backend/module/post/event/LikeCountIncreaseEvent.java @@ -0,0 +1,10 @@ +package team05.integrated_feed_backend.module.post.event; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class LikeCountIncreaseEvent { + private final Long postId; +} diff --git a/src/main/java/team05/integrated_feed_backend/module/post/event/ShareCountIncreaseEvent.java b/src/main/java/team05/integrated_feed_backend/module/post/event/ShareCountIncreaseEvent.java new file mode 100644 index 0000000..128a3a4 --- /dev/null +++ b/src/main/java/team05/integrated_feed_backend/module/post/event/ShareCountIncreaseEvent.java @@ -0,0 +1,10 @@ +package team05.integrated_feed_backend.module.post.event; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class ShareCountIncreaseEvent { + private final Long postId; +} \ No newline at end of file From 1518188299427c8339ca82c8e32af2d348beb581 Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Sun, 25 Aug 2024 08:48:20 +0900 Subject: [PATCH 04/26] =?UTF-8?q?feat:=20=EC=9D=B4=EB=B2=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=88=98=EC=8B=A0=ED=95=98=EC=97=AC=20=EC=99=B8=EB=B6=80=20api?= =?UTF-8?q?=20=ED=98=B8=EC=B6=9C=ED=95=98=EA=B8=B0=20=EC=9C=84=ED=95=9C=20?= =?UTF-8?q?=EC=9D=B4=EB=B2=A4=ED=8A=B8=20=EB=A6=AC=EC=8A=A4=EB=84=88=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../event/listener/PostEventListener.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/main/java/team05/integrated_feed_backend/module/post/event/listener/PostEventListener.java diff --git a/src/main/java/team05/integrated_feed_backend/module/post/event/listener/PostEventListener.java b/src/main/java/team05/integrated_feed_backend/module/post/event/listener/PostEventListener.java new file mode 100644 index 0000000..51eea8a --- /dev/null +++ b/src/main/java/team05/integrated_feed_backend/module/post/event/listener/PostEventListener.java @@ -0,0 +1,47 @@ +package team05.integrated_feed_backend.module.post.event.listener; + +import org.springframework.context.event.EventListener; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import team05.integrated_feed_backend.infra.sns.adapter.FacebookAdapter; +import team05.integrated_feed_backend.infra.sns.adapter.InstagramAdapter; +import team05.integrated_feed_backend.infra.sns.adapter.TwitterAdapter; +import team05.integrated_feed_backend.module.post.event.LikeCountIncreaseEvent; +import team05.integrated_feed_backend.module.post.event.ShareCountIncreaseEvent; + +@Component +@RequiredArgsConstructor +@Slf4j +public class PostEventListener { + + private final FacebookAdapter facebookAdapter; + private final TwitterAdapter twitterAdapter; + private final InstagramAdapter instagramAdapter; + + @Async + @EventListener + public void handleLikeCountIncreaseEvent(LikeCountIncreaseEvent event) { + Long postId = event.getPostId(); + log.info("Asynchronously handling like count increase event for post ID: {}", postId); + + // 외부 API 호출 + facebookAdapter.increaseLikeCount(postId); + twitterAdapter.increaseLikeCount(postId); + instagramAdapter.increaseLikeCount(postId); + } + + @Async + @EventListener + public void handleShareCountIncreaseEvent(ShareCountIncreaseEvent event) { + Long postId = event.getPostId(); + log.info("Asynchronously handling share count increase event for post ID: {}", postId); + + // 외부 API 호출 + facebookAdapter.increaseShareCount(postId); + twitterAdapter.increaseShareCount(postId); + instagramAdapter.increaseShareCount(postId); + } +} \ No newline at end of file From 1fc62d1db2570915cfbe2a1128c9e23d2bd98347 Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Sun, 25 Aug 2024 08:48:52 +0900 Subject: [PATCH 05/26] =?UTF-8?q?feat:=20PostRepository=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/post/repository/PostRepository.java | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/main/java/team05/integrated_feed_backend/module/post/repository/PostRepository.java diff --git a/src/main/java/team05/integrated_feed_backend/module/post/repository/PostRepository.java b/src/main/java/team05/integrated_feed_backend/module/post/repository/PostRepository.java new file mode 100644 index 0000000..58bf3db --- /dev/null +++ b/src/main/java/team05/integrated_feed_backend/module/post/repository/PostRepository.java @@ -0,0 +1,11 @@ +package team05.integrated_feed_backend.module.post.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import team05.integrated_feed_backend.module.post.entity.Post; + +@Repository +public interface PostRepository extends JpaRepository { + +} From 0de28c2e9e2d0e0f1a29480cc2c67841da2a804a Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Sun, 25 Aug 2024 08:49:59 +0900 Subject: [PATCH 06/26] =?UTF-8?q?feat:=20=EC=A2=8B=EC=95=84=EC=9A=94,=20?= =?UTF-8?q?=EA=B3=B5=EC=9C=A0=20=EC=88=98=20=EC=98=AC=EB=A6=AC=EB=8A=94=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/post/service/PostService.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/main/java/team05/integrated_feed_backend/module/post/service/PostService.java diff --git a/src/main/java/team05/integrated_feed_backend/module/post/service/PostService.java b/src/main/java/team05/integrated_feed_backend/module/post/service/PostService.java new file mode 100644 index 0000000..35605bf --- /dev/null +++ b/src/main/java/team05/integrated_feed_backend/module/post/service/PostService.java @@ -0,0 +1,51 @@ +package team05.integrated_feed_backend.module.post.service; + +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import team05.integrated_feed_backend.exception.code.StatusCode; +import team05.integrated_feed_backend.exception.custom.DataNotFoundException; +import team05.integrated_feed_backend.module.post.entity.Post; +import team05.integrated_feed_backend.module.post.event.LikeCountIncreaseEvent; +import team05.integrated_feed_backend.module.post.event.ShareCountIncreaseEvent; +import team05.integrated_feed_backend.module.post.repository.PostRepository; + +@Service +@Transactional(readOnly = true) +@RequiredArgsConstructor +@Slf4j +public class PostService { + + private final PostRepository postRepository; + private final ApplicationEventPublisher eventPublisher; + + @Transactional + public void increaseLikeCount(Long postId) { + // 게시물 존재 여부 확인 + Post post = postRepository.findById(postId) + .orElseThrow(() -> new DataNotFoundException(StatusCode.POST_NOT_EXIST)); + + // 내부 DB count 올리기 + post.increaseLikeCount(); + + // 이벤트 발행 + eventPublisher.publishEvent(new LikeCountIncreaseEvent(postId)); + + } + + @Transactional + public void increaseShareCount(Long postId) { + // 게시물 존재 여부 확인 + Post post = postRepository.findById(postId) + .orElseThrow(() -> new DataNotFoundException(StatusCode.POST_NOT_EXIST)); + + // 내부 db count 올리기 + post.increaseShareCount(); + + // 이벤트 발행 + eventPublisher.publishEvent(new ShareCountIncreaseEvent(postId)); + } +} From 49e73333aa3ac0457dcaa4fd4a26d08afcf5231c Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Sun, 25 Aug 2024 08:50:54 +0900 Subject: [PATCH 07/26] =?UTF-8?q?feat:=20=EC=A2=8B=EC=95=84=EC=9A=94,=20?= =?UTF-8?q?=EA=B3=B5=EC=9C=A0=20=EC=88=98=20=EC=98=AC=EB=A6=AC=EB=8A=94=20?= =?UTF-8?q?api=20=EC=B6=94=EA=B0=80=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/controller/PostController.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/main/java/team05/integrated_feed_backend/module/post/controller/PostController.java b/src/main/java/team05/integrated_feed_backend/module/post/controller/PostController.java index 58e2ef4..160881d 100644 --- a/src/main/java/team05/integrated_feed_backend/module/post/controller/PostController.java +++ b/src/main/java/team05/integrated_feed_backend/module/post/controller/PostController.java @@ -3,7 +3,10 @@ import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; import lombok.RequiredArgsConstructor; @@ -11,12 +14,15 @@ import team05.integrated_feed_backend.exception.code.StatusCode; import team05.integrated_feed_backend.module.post.dto.request.PostSearchReq; import team05.integrated_feed_backend.module.post.dto.response.PostSearchRes; +import team05.integrated_feed_backend.module.post.service.PostService; @RestController @RequiredArgsConstructor @RequestMapping("/api/posts") public class PostController implements PostControllerDocs { + private final PostService postService; + @Override @GetMapping public BaseApiResponse getPosts( @@ -28,4 +34,20 @@ public BaseApiResponse getPosts( return new BaseApiResponse<>(HttpStatus.OK, StatusCode.OK.getMessage(), res); } + // 좋아요 수 증가시키는 api + @ResponseStatus(HttpStatus.OK) + @PatchMapping("/{postId}/like") + public BaseApiResponse increaseLikeCount(@PathVariable(name = "postId") Long postId) { + postService.increaseLikeCount(postId); + return BaseApiResponse.of(HttpStatus.OK); + } + + // 공유 수 증가시키는 api + @ResponseStatus(HttpStatus.OK) + @PatchMapping("/{postId}/share") + public BaseApiResponse increaseShareCount(@PathVariable(name = "postId") Long postId) { + postService.increaseShareCount(postId); + return BaseApiResponse.of(HttpStatus.OK); + } + } From e16e2f312bd8b5a35a49c36f627927cd1b5424ac Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Sun, 25 Aug 2024 08:51:14 +0900 Subject: [PATCH 08/26] =?UTF-8?q?docs:=20=EC=A2=8B=EC=95=84=EC=9A=94,=20?= =?UTF-8?q?=EA=B3=B5=EC=9C=A0=20=EC=88=98=20=EC=98=AC=EB=A6=AC=EB=8A=94=20?= =?UTF-8?q?api=20docs=20=EC=B6=94=EA=B0=80=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/post/controller/PostControllerDocs.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/team05/integrated_feed_backend/module/post/controller/PostControllerDocs.java b/src/main/java/team05/integrated_feed_backend/module/post/controller/PostControllerDocs.java index b2caeda..5948c2a 100644 --- a/src/main/java/team05/integrated_feed_backend/module/post/controller/PostControllerDocs.java +++ b/src/main/java/team05/integrated_feed_backend/module/post/controller/PostControllerDocs.java @@ -2,6 +2,7 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; import team05.integrated_feed_backend.common.BaseApiResponse; import team05.integrated_feed_backend.module.post.dto.request.PostSearchReq; @@ -16,4 +17,17 @@ BaseApiResponse getPosts( PostSearchReq postSearchReq ); + @Operation(summary = "게시물 좋아요 수 올리기") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "게시물 좋아요 수가 증가되었습니다.", useReturnTypeSchema = true), + @ApiResponse(responseCode = "404", description = "존재하지 않는 게시물입니다.", useReturnTypeSchema = true), + }) + BaseApiResponse increaseLikeCount(Long postId); + + @Operation(summary = "게시물 공유 수 올리기") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "게시물 공유 수가 증가되었습니다.", useReturnTypeSchema = true), + @ApiResponse(responseCode = "404", description = "존재하지 않는 게시물입니다.", useReturnTypeSchema = true), + }) + BaseApiResponse increaseShareCount(Long postId); } \ No newline at end of file From c3a1b727df3993da2e30e93bb5b184c24f5904b7 Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Sun, 25 Aug 2024 08:51:43 +0900 Subject: [PATCH 09/26] =?UTF-8?q?feat:=20=EC=A1=B4=EC=9E=AC=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EA=B2=8C=EC=8B=9C=EB=AC=BC?= =?UTF-8?q?=EC=9D=B4=EB=9D=BC=EB=8A=94=20=EC=9D=91=EB=8B=B5=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=B6=94=EA=B0=80=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../integrated_feed_backend/exception/code/StatusCode.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/team05/integrated_feed_backend/exception/code/StatusCode.java b/src/main/java/team05/integrated_feed_backend/exception/code/StatusCode.java index 7b408a0..04e5b25 100644 --- a/src/main/java/team05/integrated_feed_backend/exception/code/StatusCode.java +++ b/src/main/java/team05/integrated_feed_backend/exception/code/StatusCode.java @@ -17,6 +17,7 @@ public enum StatusCode { * 400 번대 CODE **/ METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, "요청 경로가 지원되지 않습니다."), + POST_NOT_EXIST(HttpStatus.NOT_FOUND, "존재하지 않는 게시물입니다."), /** * 500 번대 CODE From f9ee7706e39de105ab339ecf5315172dd2c78be4 Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Sun, 25 Aug 2024 08:52:00 +0900 Subject: [PATCH 10/26] =?UTF-8?q?feat:=20=EB=B9=84=EB=8F=99=EA=B8=B0=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=EB=A5=BC=20=EC=9C=84=ED=95=9C=20=EC=96=B4?= =?UTF-8?q?=EB=85=B8=ED=85=8C=EC=9D=B4=EC=85=98=20=EC=B6=94=EA=B0=80=20(#2?= =?UTF-8?q?2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IntegratedFeedBackendApplication.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/team05/integrated_feed_backend/IntegratedFeedBackendApplication.java b/src/main/java/team05/integrated_feed_backend/IntegratedFeedBackendApplication.java index cdbeb07..715e0f6 100644 --- a/src/main/java/team05/integrated_feed_backend/IntegratedFeedBackendApplication.java +++ b/src/main/java/team05/integrated_feed_backend/IntegratedFeedBackendApplication.java @@ -3,8 +3,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication +@EnableAsync @EnableFeignClients(basePackages = "team05.integrated_feed_backend.infra.sns.api") public class IntegratedFeedBackendApplication { From 826d48f2c406e9d9a7aaf08c2e2cafa0914f5b18 Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Sun, 25 Aug 2024 08:52:32 +0900 Subject: [PATCH 11/26] =?UTF-8?q?feat:=20post=20mock=20=EA=B0=9D=EC=B2=B4?= =?UTF-8?q?=20=EC=83=9D=EC=84=B1=20=EB=A7=A4=EC=84=9C=EB=93=9C=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MockEntityFactory.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/test/java/team05/integrated_feed_backend/MockEntityFactory.java diff --git a/src/test/java/team05/integrated_feed_backend/MockEntityFactory.java b/src/test/java/team05/integrated_feed_backend/MockEntityFactory.java new file mode 100644 index 0000000..4c29674 --- /dev/null +++ b/src/test/java/team05/integrated_feed_backend/MockEntityFactory.java @@ -0,0 +1,23 @@ +package team05.integrated_feed_backend; + +import java.util.ArrayList; + +import team05.integrated_feed_backend.common.enums.SocialMediaType; +import team05.integrated_feed_backend.module.post.entity.Post; + +public class MockEntityFactory { + + // Post 생성 + public static Post createMockPost() { + return Post.builder() + .title("Sample Post Title") + .content("This is a sample post content.") + .type(SocialMediaType.FACEBOOK) + .viewCount(100L) + .likeCount(10L) + .shareCount(5L) + .postHashtags(new ArrayList<>()) + .build(); + } + +} From d3ecedbc19b8a927631ca3d4119215091104a57f Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Sun, 25 Aug 2024 08:53:00 +0900 Subject: [PATCH 12/26] =?UTF-8?q?test:=20postService=20=EC=A2=8B=EC=95=84?= =?UTF-8?q?=EC=9A=94,=20=EA=B3=B5=EC=9C=A0=20=EC=88=98=20=EC=98=AC?= =?UTF-8?q?=EB=A6=AC=EB=8A=94=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=B6=94=EA=B0=80=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/PostServiceTest.java | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/test/java/team05/integrated_feed_backend/post/PostServiceTest.java diff --git a/src/test/java/team05/integrated_feed_backend/post/PostServiceTest.java b/src/test/java/team05/integrated_feed_backend/post/PostServiceTest.java new file mode 100644 index 0000000..f427eac --- /dev/null +++ b/src/test/java/team05/integrated_feed_backend/post/PostServiceTest.java @@ -0,0 +1,102 @@ +package team05.integrated_feed_backend.post; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.BDDMockito.*; + +import java.util.Optional; + +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.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.context.ApplicationEventPublisher; + +import team05.integrated_feed_backend.MockEntityFactory; +import team05.integrated_feed_backend.exception.custom.DataNotFoundException; +import team05.integrated_feed_backend.module.post.entity.Post; +import team05.integrated_feed_backend.module.post.event.LikeCountIncreaseEvent; +import team05.integrated_feed_backend.module.post.event.ShareCountIncreaseEvent; +import team05.integrated_feed_backend.module.post.repository.PostRepository; +import team05.integrated_feed_backend.module.post.service.PostService; + +@ExtendWith(MockitoExtension.class) +public class PostServiceTest { + + @Mock + private PostRepository postRepository; + + @Mock + private ApplicationEventPublisher eventPublisher; + + @InjectMocks + private PostService postService; + + private Post mockPost; + + @BeforeEach + void setUp() { + mockPost = MockEntityFactory.createMockPost(); + } + + @Nested + @DisplayName("게시물 좋아요 수 올리기") + class increaseLikeCount { + + @Test + @DisplayName("[성공] 게시물 좋아요 수를 증가시켰다.") + void shouldIncreaseLikeCountSuccessfully() { + // Given + when(postRepository.findById(mockPost.getPostId())).thenReturn(Optional.of(mockPost)); + + // when + postService.increaseLikeCount(mockPost.getPostId()); + + // then + verify(postRepository, times(1)).findById(mockPost.getPostId()); + verify(eventPublisher, times(1)).publishEvent(any(LikeCountIncreaseEvent.class)); + assert (mockPost.getLikeCount() == 11L); + + } + + @Test + @DisplayName("[실패] 게시물이 없어 좋아요 수를 증가에 실패했다.") + void shouldThrowExceptionWhenPostNotExists() { + // Given + when(postRepository.findById(mockPost.getPostId())).thenReturn(Optional.empty()); + + // when & then + assertThrows(DataNotFoundException.class, () -> { + postService.increaseLikeCount(mockPost.getPostId()); + }); + + verify(postRepository, times(1)).findById(mockPost.getPostId()); + verify(eventPublisher, times(0)).publishEvent(any(LikeCountIncreaseEvent.class)); + } + } + + @Nested + @DisplayName("게시물 공유 수 올리기") + class increaseShareCount { + + @Test + @DisplayName("[성공] 게시물 공유 수를 증가시켰다.") + void shouldIncreaseShareCountSuccessfully() { + // Given + when(postRepository.findById(mockPost.getPostId())).thenReturn(Optional.of(mockPost)); + + // when + postService.increaseShareCount(mockPost.getPostId()); + + // then + verify(postRepository, times(1)).findById(mockPost.getPostId()); + verify(eventPublisher, times(1)).publishEvent(any(ShareCountIncreaseEvent.class)); + assert (mockPost.getShareCount() == 6L); + + } + } +} From 71a1a66686b90cd6b7434e782feaa19acd16ae4c Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Sun, 25 Aug 2024 08:53:12 +0900 Subject: [PATCH 13/26] =?UTF-8?q?remove:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20gitkeep=20=ED=8C=8C=EC=9D=BC=20=EC=A0=9C=EA=B1=B0?= =?UTF-8?q?=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../integrated_feed_backend/module/post/repository/.gitkeep | 0 .../team05/integrated_feed_backend/module/post/service/.gitkeep | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/main/java/team05/integrated_feed_backend/module/post/repository/.gitkeep delete mode 100644 src/main/java/team05/integrated_feed_backend/module/post/service/.gitkeep diff --git a/src/main/java/team05/integrated_feed_backend/module/post/repository/.gitkeep b/src/main/java/team05/integrated_feed_backend/module/post/repository/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/team05/integrated_feed_backend/module/post/service/.gitkeep b/src/main/java/team05/integrated_feed_backend/module/post/service/.gitkeep deleted file mode 100644 index e69de29..0000000 From e11d3d07a3b29cdb6b2a704e3d0a9880428569fc Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Sun, 25 Aug 2024 09:54:44 +0900 Subject: [PATCH 14/26] =?UTF-8?q?fix:=20=EB=B9=84=EB=8F=99=EA=B8=B0=20?= =?UTF-8?q?=ED=98=B8=EC=B6=9C=20=EA=B3=BC=EC=A0=95=EC=97=90=EC=84=9C?= =?UTF-8?q?=EC=9D=98=20=EB=AC=B8=EC=9E=90=EC=85=8B=20=EC=B2=98=EB=A6=AC=20?= =?UTF-8?q?=EC=97=90=EB=9F=AC=20=EC=B2=98=EB=A6=AC=EB=A5=BC=20=EC=9C=84?= =?UTF-8?q?=ED=95=B4=20publisher=20=EC=B6=94=EA=B0=80=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../event/publisher/PostEventPublisher.java | 30 +++++++++++++++++++ .../module/post/service/PostService.java | 14 ++++----- 2 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 src/main/java/team05/integrated_feed_backend/module/post/event/publisher/PostEventPublisher.java diff --git a/src/main/java/team05/integrated_feed_backend/module/post/event/publisher/PostEventPublisher.java b/src/main/java/team05/integrated_feed_backend/module/post/event/publisher/PostEventPublisher.java new file mode 100644 index 0000000..c2f9151 --- /dev/null +++ b/src/main/java/team05/integrated_feed_backend/module/post/event/publisher/PostEventPublisher.java @@ -0,0 +1,30 @@ +package team05.integrated_feed_backend.module.post.event.publisher; + +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import team05.integrated_feed_backend.module.post.event.LikeCountIncreaseEvent; +import team05.integrated_feed_backend.module.post.event.ShareCountIncreaseEvent; + +@Component +@RequiredArgsConstructor +@Slf4j +public class PostEventPublisher { + + private final ApplicationEventPublisher eventPublisher; + + @Async + public void publishLikeCountIncreaseEvent(Long postId) { + eventPublisher.publishEvent(new LikeCountIncreaseEvent(postId)); + log.info("LikeCountIncreaseEvent published asynchronously for post ID: {}", postId); + } + + @Async + public void publishShareCountIncreaseEvent(Long postId) { + eventPublisher.publishEvent(new ShareCountIncreaseEvent(postId)); + log.info("ShareCountIncreaseEvent published asynchronously for post ID: {}", postId); + } +} diff --git a/src/main/java/team05/integrated_feed_backend/module/post/service/PostService.java b/src/main/java/team05/integrated_feed_backend/module/post/service/PostService.java index 35605bf..4c226df 100644 --- a/src/main/java/team05/integrated_feed_backend/module/post/service/PostService.java +++ b/src/main/java/team05/integrated_feed_backend/module/post/service/PostService.java @@ -1,6 +1,5 @@ package team05.integrated_feed_backend.module.post.service; -import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -9,8 +8,7 @@ import team05.integrated_feed_backend.exception.code.StatusCode; import team05.integrated_feed_backend.exception.custom.DataNotFoundException; import team05.integrated_feed_backend.module.post.entity.Post; -import team05.integrated_feed_backend.module.post.event.LikeCountIncreaseEvent; -import team05.integrated_feed_backend.module.post.event.ShareCountIncreaseEvent; +import team05.integrated_feed_backend.module.post.event.publisher.PostEventPublisher; import team05.integrated_feed_backend.module.post.repository.PostRepository; @Service @@ -20,7 +18,7 @@ public class PostService { private final PostRepository postRepository; - private final ApplicationEventPublisher eventPublisher; + private final PostEventPublisher postEventPublisher; @Transactional public void increaseLikeCount(Long postId) { @@ -31,8 +29,8 @@ public void increaseLikeCount(Long postId) { // 내부 DB count 올리기 post.increaseLikeCount(); - // 이벤트 발행 - eventPublisher.publishEvent(new LikeCountIncreaseEvent(postId)); + // 이벤트 비동기 발행 + postEventPublisher.publishLikeCountIncreaseEvent(postId); } @@ -45,7 +43,7 @@ public void increaseShareCount(Long postId) { // 내부 db count 올리기 post.increaseShareCount(); - // 이벤트 발행 - eventPublisher.publishEvent(new ShareCountIncreaseEvent(postId)); + // 이벤트 비동기 발행 + postEventPublisher.publishShareCountIncreaseEvent(postId); } } From b45ec66501861213455605617caa208253b1378b Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Sun, 25 Aug 2024 09:55:35 +0900 Subject: [PATCH 15/26] =?UTF-8?q?fix:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20@Async=20=EC=96=B4=EB=85=B8=ED=85=8C=EC=9D=B4?= =?UTF-8?q?=EC=85=98=20=EC=A0=9C=EA=B1=B0=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IntegratedFeedBackendApplication.java | 2 -- .../module/post/event/listener/PostEventListener.java | 3 --- 2 files changed, 5 deletions(-) diff --git a/src/main/java/team05/integrated_feed_backend/IntegratedFeedBackendApplication.java b/src/main/java/team05/integrated_feed_backend/IntegratedFeedBackendApplication.java index 715e0f6..cdbeb07 100644 --- a/src/main/java/team05/integrated_feed_backend/IntegratedFeedBackendApplication.java +++ b/src/main/java/team05/integrated_feed_backend/IntegratedFeedBackendApplication.java @@ -3,10 +3,8 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; -import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication -@EnableAsync @EnableFeignClients(basePackages = "team05.integrated_feed_backend.infra.sns.api") public class IntegratedFeedBackendApplication { diff --git a/src/main/java/team05/integrated_feed_backend/module/post/event/listener/PostEventListener.java b/src/main/java/team05/integrated_feed_backend/module/post/event/listener/PostEventListener.java index 51eea8a..e6a67f3 100644 --- a/src/main/java/team05/integrated_feed_backend/module/post/event/listener/PostEventListener.java +++ b/src/main/java/team05/integrated_feed_backend/module/post/event/listener/PostEventListener.java @@ -1,7 +1,6 @@ package team05.integrated_feed_backend.module.post.event.listener; import org.springframework.context.event.EventListener; -import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import lombok.RequiredArgsConstructor; @@ -21,7 +20,6 @@ public class PostEventListener { private final TwitterAdapter twitterAdapter; private final InstagramAdapter instagramAdapter; - @Async @EventListener public void handleLikeCountIncreaseEvent(LikeCountIncreaseEvent event) { Long postId = event.getPostId(); @@ -33,7 +31,6 @@ public void handleLikeCountIncreaseEvent(LikeCountIncreaseEvent event) { instagramAdapter.increaseLikeCount(postId); } - @Async @EventListener public void handleShareCountIncreaseEvent(ShareCountIncreaseEvent event) { Long postId = event.getPostId(); From 55982bed01b8444400394218d003ad750f068538 Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Sun, 25 Aug 2024 09:56:32 +0900 Subject: [PATCH 16/26] =?UTF-8?q?test:=20publisher=EB=A1=9C=20service=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/PostServiceTest.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/test/java/team05/integrated_feed_backend/post/PostServiceTest.java b/src/test/java/team05/integrated_feed_backend/post/PostServiceTest.java index f427eac..5dd74b0 100644 --- a/src/test/java/team05/integrated_feed_backend/post/PostServiceTest.java +++ b/src/test/java/team05/integrated_feed_backend/post/PostServiceTest.java @@ -1,7 +1,6 @@ package team05.integrated_feed_backend.post; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.ArgumentMatchers.*; import static org.mockito.BDDMockito.*; import java.util.Optional; @@ -14,13 +13,11 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.context.ApplicationEventPublisher; import team05.integrated_feed_backend.MockEntityFactory; import team05.integrated_feed_backend.exception.custom.DataNotFoundException; import team05.integrated_feed_backend.module.post.entity.Post; -import team05.integrated_feed_backend.module.post.event.LikeCountIncreaseEvent; -import team05.integrated_feed_backend.module.post.event.ShareCountIncreaseEvent; +import team05.integrated_feed_backend.module.post.event.publisher.PostEventPublisher; import team05.integrated_feed_backend.module.post.repository.PostRepository; import team05.integrated_feed_backend.module.post.service.PostService; @@ -31,7 +28,7 @@ public class PostServiceTest { private PostRepository postRepository; @Mock - private ApplicationEventPublisher eventPublisher; + private PostEventPublisher postEventPublisher; @InjectMocks private PostService postService; @@ -58,7 +55,7 @@ void shouldIncreaseLikeCountSuccessfully() { // then verify(postRepository, times(1)).findById(mockPost.getPostId()); - verify(eventPublisher, times(1)).publishEvent(any(LikeCountIncreaseEvent.class)); + verify(postEventPublisher, times(1)).publishLikeCountIncreaseEvent(mockPost.getPostId()); assert (mockPost.getLikeCount() == 11L); } @@ -75,7 +72,7 @@ void shouldThrowExceptionWhenPostNotExists() { }); verify(postRepository, times(1)).findById(mockPost.getPostId()); - verify(eventPublisher, times(0)).publishEvent(any(LikeCountIncreaseEvent.class)); + verify(postEventPublisher, times(0)).publishLikeCountIncreaseEvent(mockPost.getPostId()); } } @@ -94,7 +91,7 @@ void shouldIncreaseShareCountSuccessfully() { // then verify(postRepository, times(1)).findById(mockPost.getPostId()); - verify(eventPublisher, times(1)).publishEvent(any(ShareCountIncreaseEvent.class)); + verify(postEventPublisher, times(1)).publishShareCountIncreaseEvent(mockPost.getPostId()); assert (mockPost.getShareCount() == 6L); } From 3f09722875207286c2570598b4ce125963055204 Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Sun, 25 Aug 2024 09:56:58 +0900 Subject: [PATCH 17/26] =?UTF-8?q?test:=20event=20listener,=20publisher=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/PostEventListenerTest.java | 76 +++++++++++++++++++ .../post/PostEventPublisherTest.java | 67 ++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 src/test/java/team05/integrated_feed_backend/post/PostEventListenerTest.java create mode 100644 src/test/java/team05/integrated_feed_backend/post/PostEventPublisherTest.java diff --git a/src/test/java/team05/integrated_feed_backend/post/PostEventListenerTest.java b/src/test/java/team05/integrated_feed_backend/post/PostEventListenerTest.java new file mode 100644 index 0000000..4e27a03 --- /dev/null +++ b/src/test/java/team05/integrated_feed_backend/post/PostEventListenerTest.java @@ -0,0 +1,76 @@ +package team05.integrated_feed_backend.post; + +import static org.mockito.Mockito.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +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 team05.integrated_feed_backend.infra.sns.adapter.FacebookAdapter; +import team05.integrated_feed_backend.infra.sns.adapter.InstagramAdapter; +import team05.integrated_feed_backend.infra.sns.adapter.TwitterAdapter; +import team05.integrated_feed_backend.module.post.event.LikeCountIncreaseEvent; +import team05.integrated_feed_backend.module.post.event.ShareCountIncreaseEvent; +import team05.integrated_feed_backend.module.post.event.listener.PostEventListener; + +@ExtendWith(MockitoExtension.class) +public class PostEventListenerTest { + + @Mock + private FacebookAdapter facebookAdapter; + + @Mock + private TwitterAdapter twitterAdapter; + + @Mock + private InstagramAdapter instagramAdapter; + + @InjectMocks + private PostEventListener postEventListener; + + @Nested + @DisplayName("게시물 좋아요 수 증가 이벤트 처리") + class HandleLikeCountIncreaseEvent { + + @Test + @DisplayName("[성공] LikeCountIncreaseEvent가 수신되어 외부 API가 호출된다.") + void shouldHandleLikeCountIncreaseEvent() { + // Given + Long postId = 1L; + LikeCountIncreaseEvent event = new LikeCountIncreaseEvent(postId); + + // when + postEventListener.handleLikeCountIncreaseEvent(event); + + // then + verify(facebookAdapter, times(1)).increaseLikeCount(postId); + verify(twitterAdapter, times(1)).increaseLikeCount(postId); + verify(instagramAdapter, times(1)).increaseLikeCount(postId); + } + } + + @Nested + @DisplayName("게시물 공유 수 증가 이벤트 처리") + class HandleShareCountIncreaseEvent { + + @Test + @DisplayName("[성공] ShareCountIncreaseEvent가 수신되어 외부 API가 호출된다.") + void shouldHandleShareCountIncreaseEvent() { + // Given + Long postId = 1L; + ShareCountIncreaseEvent event = new ShareCountIncreaseEvent(postId); + + // when + postEventListener.handleShareCountIncreaseEvent(event); + + // then + verify(facebookAdapter, times(1)).increaseShareCount(postId); + verify(twitterAdapter, times(1)).increaseShareCount(postId); + verify(instagramAdapter, times(1)).increaseShareCount(postId); + } + } +} \ No newline at end of file diff --git a/src/test/java/team05/integrated_feed_backend/post/PostEventPublisherTest.java b/src/test/java/team05/integrated_feed_backend/post/PostEventPublisherTest.java new file mode 100644 index 0000000..1776870 --- /dev/null +++ b/src/test/java/team05/integrated_feed_backend/post/PostEventPublisherTest.java @@ -0,0 +1,67 @@ +package team05.integrated_feed_backend.post; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.context.ApplicationEventPublisher; + +import team05.integrated_feed_backend.module.post.event.LikeCountIncreaseEvent; +import team05.integrated_feed_backend.module.post.event.ShareCountIncreaseEvent; +import team05.integrated_feed_backend.module.post.event.publisher.PostEventPublisher; + +@ExtendWith(MockitoExtension.class) +public class PostEventPublisherTest { + + @Mock + private ApplicationEventPublisher eventPublisher; + + @InjectMocks + private PostEventPublisher postEventPublisher; + + @Test + @DisplayName("[성공] LikeCountIncreaseEvent가 비동기적으로 발행된다.") + void shouldPublishLikeCountIncreaseEvent() { + // Given + Long postId = 1L; + + // when + postEventPublisher.publishLikeCountIncreaseEvent(postId); + + // then + // LikeCountIncreaseEvent 생성 및 호출 확인 + ArgumentCaptor captor = ArgumentCaptor.forClass(LikeCountIncreaseEvent.class); + verify(eventPublisher, times(1)).publishEvent(captor.capture()); + + // 캡쳐된 이벤트의 존재 및 해당 postId를 갖고 있는지 확인 + LikeCountIncreaseEvent capturedEvent = captor.getValue(); + assertNotNull(capturedEvent); + assertEquals(postId, capturedEvent.getPostId(), "Post ID should match"); + } + + @Test + @DisplayName("[성공] ShareCountIncreaseEvent가 비동기적으로 발행된다.") + void shouldPublishShareCountIncreaseEvent() { + // Given + Long postId = 1L; + + // when + postEventPublisher.publishShareCountIncreaseEvent(postId); + + // then + // LikeCountIncreaseEvent 생성 및 호출 확인 + ArgumentCaptor captor = ArgumentCaptor.forClass(ShareCountIncreaseEvent.class); + verify(eventPublisher, times(1)).publishEvent(captor.capture()); + + // 캡쳐된 이벤트의 존재 및 해당 postId를 갖고 있는지 확인 + ShareCountIncreaseEvent capturedEvent = captor.getValue(); + assertNotNull(capturedEvent); + assertEquals(postId, capturedEvent.getPostId(), "Post ID should match"); + } +} From fc734ac4cc1c4d4f52064852ca794e2431848966 Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Sun, 25 Aug 2024 10:00:28 +0900 Subject: [PATCH 18/26] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EC=96=B4=EB=85=B8=ED=85=8C?= =?UTF-8?q?=EC=9D=B4=EC=85=98=20=EC=A0=9C=EA=B1=B0=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/post/service/PostService.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/team05/integrated_feed_backend/module/post/service/PostService.java b/src/main/java/team05/integrated_feed_backend/module/post/service/PostService.java index 4c226df..a8e64da 100644 --- a/src/main/java/team05/integrated_feed_backend/module/post/service/PostService.java +++ b/src/main/java/team05/integrated_feed_backend/module/post/service/PostService.java @@ -4,7 +4,6 @@ import org.springframework.transaction.annotation.Transactional; import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; import team05.integrated_feed_backend.exception.code.StatusCode; import team05.integrated_feed_backend.exception.custom.DataNotFoundException; import team05.integrated_feed_backend.module.post.entity.Post; @@ -14,7 +13,6 @@ @Service @Transactional(readOnly = true) @RequiredArgsConstructor -@Slf4j public class PostService { private final PostRepository postRepository; From 81f7236488d8fef958a14fb6e63f6708563b9e40 Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Sun, 25 Aug 2024 10:06:01 +0900 Subject: [PATCH 19/26] =?UTF-8?q?refactor:=20HttpStatus=EA=B0=80=20?= =?UTF-8?q?=EC=95=84=EB=8B=8C=20StatusCode=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=97=AC=20=EC=9D=91=EB=8B=B5=20=EB=A9=94=EC=84=B8=EC=A7=80=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/post/controller/PostController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/team05/integrated_feed_backend/module/post/controller/PostController.java b/src/main/java/team05/integrated_feed_backend/module/post/controller/PostController.java index 160881d..bbca375 100644 --- a/src/main/java/team05/integrated_feed_backend/module/post/controller/PostController.java +++ b/src/main/java/team05/integrated_feed_backend/module/post/controller/PostController.java @@ -39,7 +39,7 @@ public BaseApiResponse getPosts( @PatchMapping("/{postId}/like") public BaseApiResponse increaseLikeCount(@PathVariable(name = "postId") Long postId) { postService.increaseLikeCount(postId); - return BaseApiResponse.of(HttpStatus.OK); + return BaseApiResponse.of(StatusCode.OK); } // 공유 수 증가시키는 api @@ -47,7 +47,7 @@ public BaseApiResponse increaseLikeCount(@PathVariable(name = "postId") Lo @PatchMapping("/{postId}/share") public BaseApiResponse increaseShareCount(@PathVariable(name = "postId") Long postId) { postService.increaseShareCount(postId); - return BaseApiResponse.of(HttpStatus.OK); + return BaseApiResponse.of(StatusCode.OK); } } From a5d85a38ac52abda1caf4c090df64b8284a0fba1 Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Mon, 26 Aug 2024 12:54:18 +0900 Subject: [PATCH 20/26] =?UTF-8?q?feat:=20Async=20=EB=B9=84=EB=8F=99?= =?UTF-8?q?=EA=B8=B0=20=EC=84=A4=EC=A0=95=20=EC=B6=94=EA=B0=80=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IntegratedFeedBackendApplication.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/team05/integrated_feed_backend/IntegratedFeedBackendApplication.java b/src/main/java/team05/integrated_feed_backend/IntegratedFeedBackendApplication.java index cdbeb07..715e0f6 100644 --- a/src/main/java/team05/integrated_feed_backend/IntegratedFeedBackendApplication.java +++ b/src/main/java/team05/integrated_feed_backend/IntegratedFeedBackendApplication.java @@ -3,8 +3,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication +@EnableAsync @EnableFeignClients(basePackages = "team05.integrated_feed_backend.infra.sns.api") public class IntegratedFeedBackendApplication { From aa8da8dd68da64a6b23c01966348a79e800ac69b Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Mon, 26 Aug 2024 12:55:06 +0900 Subject: [PATCH 21/26] =?UTF-8?q?fix:=20=EA=B2=8C=EC=8B=9C=EB=AC=BC=20?= =?UTF-8?q?=EC=A2=85=EB=A5=98=20SocialType=20=EB=B0=9B=EB=8F=84=EB=A1=9D?= =?UTF-8?q?=20event=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20=EC=9D=B4=EB=B2=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=9D=B4=EB=A6=84=20=EA=B3=BC=EA=B1=B0=ED=98=95?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EC=88=98=EC=A0=95(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...keCountIncreaseEvent.java => LikeCountIncreasedEvent.java} | 4 +++- ...eCountIncreaseEvent.java => ShareCountIncreasedEvent.java} | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) rename src/main/java/team05/integrated_feed_backend/module/post/event/{LikeCountIncreaseEvent.java => LikeCountIncreasedEvent.java} (54%) rename src/main/java/team05/integrated_feed_backend/module/post/event/{ShareCountIncreaseEvent.java => ShareCountIncreasedEvent.java} (54%) diff --git a/src/main/java/team05/integrated_feed_backend/module/post/event/LikeCountIncreaseEvent.java b/src/main/java/team05/integrated_feed_backend/module/post/event/LikeCountIncreasedEvent.java similarity index 54% rename from src/main/java/team05/integrated_feed_backend/module/post/event/LikeCountIncreaseEvent.java rename to src/main/java/team05/integrated_feed_backend/module/post/event/LikeCountIncreasedEvent.java index 1400aa2..d27a93b 100644 --- a/src/main/java/team05/integrated_feed_backend/module/post/event/LikeCountIncreaseEvent.java +++ b/src/main/java/team05/integrated_feed_backend/module/post/event/LikeCountIncreasedEvent.java @@ -2,9 +2,11 @@ import lombok.AllArgsConstructor; import lombok.Getter; +import team05.integrated_feed_backend.common.enums.SocialMediaType; @Getter @AllArgsConstructor -public class LikeCountIncreaseEvent { +public class LikeCountIncreasedEvent { private final Long postId; + private final SocialMediaType type; } diff --git a/src/main/java/team05/integrated_feed_backend/module/post/event/ShareCountIncreaseEvent.java b/src/main/java/team05/integrated_feed_backend/module/post/event/ShareCountIncreasedEvent.java similarity index 54% rename from src/main/java/team05/integrated_feed_backend/module/post/event/ShareCountIncreaseEvent.java rename to src/main/java/team05/integrated_feed_backend/module/post/event/ShareCountIncreasedEvent.java index 128a3a4..c7f2d59 100644 --- a/src/main/java/team05/integrated_feed_backend/module/post/event/ShareCountIncreaseEvent.java +++ b/src/main/java/team05/integrated_feed_backend/module/post/event/ShareCountIncreasedEvent.java @@ -2,9 +2,11 @@ import lombok.AllArgsConstructor; import lombok.Getter; +import team05.integrated_feed_backend.common.enums.SocialMediaType; @Getter @AllArgsConstructor -public class ShareCountIncreaseEvent { +public class ShareCountIncreasedEvent { private final Long postId; + private final SocialMediaType type; } \ No newline at end of file From ff94cb2b6c5a4ffef81efb9d27adfb7d28217406 Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Mon, 26 Aug 2024 12:56:12 +0900 Subject: [PATCH 22/26] =?UTF-8?q?fix:=20=EA=B2=8C=EC=8B=9C=EB=AC=BC=20Soci?= =?UTF-8?q?alType=20=EC=97=90=20=EB=A7=9E=EA=B2=8C=20=EC=99=B8=EB=B6=80=20?= =?UTF-8?q?api=20=ED=98=B8=EC=B6=9C=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../event/listener/PostEventListener.java | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/main/java/team05/integrated_feed_backend/module/post/event/listener/PostEventListener.java b/src/main/java/team05/integrated_feed_backend/module/post/event/listener/PostEventListener.java index e6a67f3..00f8ac9 100644 --- a/src/main/java/team05/integrated_feed_backend/module/post/event/listener/PostEventListener.java +++ b/src/main/java/team05/integrated_feed_backend/module/post/event/listener/PostEventListener.java @@ -1,15 +1,17 @@ package team05.integrated_feed_backend.module.post.event.listener; import org.springframework.context.event.EventListener; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import team05.integrated_feed_backend.common.enums.SocialMediaType; import team05.integrated_feed_backend.infra.sns.adapter.FacebookAdapter; import team05.integrated_feed_backend.infra.sns.adapter.InstagramAdapter; import team05.integrated_feed_backend.infra.sns.adapter.TwitterAdapter; -import team05.integrated_feed_backend.module.post.event.LikeCountIncreaseEvent; -import team05.integrated_feed_backend.module.post.event.ShareCountIncreaseEvent; +import team05.integrated_feed_backend.module.post.event.LikeCountIncreasedEvent; +import team05.integrated_feed_backend.module.post.event.ShareCountIncreasedEvent; @Component @RequiredArgsConstructor @@ -20,25 +22,35 @@ public class PostEventListener { private final TwitterAdapter twitterAdapter; private final InstagramAdapter instagramAdapter; + @Async @EventListener - public void handleLikeCountIncreaseEvent(LikeCountIncreaseEvent event) { + public void handleLikeCountIncreasedEvent(LikeCountIncreasedEvent event) { Long postId = event.getPostId(); + SocialMediaType type = event.getType(); log.info("Asynchronously handling like count increase event for post ID: {}", postId); // 외부 API 호출 - facebookAdapter.increaseLikeCount(postId); - twitterAdapter.increaseLikeCount(postId); - instagramAdapter.increaseLikeCount(postId); + switch (type) { + case FACEBOOK -> facebookAdapter.increaseLikeCount(postId); + case INSTAGRAM -> instagramAdapter.increaseLikeCount(postId); + case TWITTER -> twitterAdapter.increaseLikeCount(postId); + default -> log.error(postId + " 게시물의 " + type + ": facebook, instagram, twitter 중 하나로 설정되어 있지 않습니다."); + } } + @Async @EventListener - public void handleShareCountIncreaseEvent(ShareCountIncreaseEvent event) { + public void handleShareCountIncreasedEvent(ShareCountIncreasedEvent event) { Long postId = event.getPostId(); + SocialMediaType type = event.getType(); log.info("Asynchronously handling share count increase event for post ID: {}", postId); // 외부 API 호출 - facebookAdapter.increaseShareCount(postId); - twitterAdapter.increaseShareCount(postId); - instagramAdapter.increaseShareCount(postId); + switch (type) { + case FACEBOOK -> facebookAdapter.increaseShareCount(postId); + case INSTAGRAM -> instagramAdapter.increaseShareCount(postId); + case TWITTER -> twitterAdapter.increaseShareCount(postId); + default -> log.error(postId + " 게시물의 " + type + ": facebook, instagram, twitter 중 하나로 설정되어 있지 않습니다."); + } } } \ No newline at end of file From 95650275cc7b90f83866c4cb205abc560d8d4ad3 Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Mon, 26 Aug 2024 12:57:17 +0900 Subject: [PATCH 23/26] =?UTF-8?q?refactor:=20=EC=9D=B4=EB=B2=A4=ED=8A=B8?= =?UTF-8?q?=20=EA=B4=80=EB=A0=A8=20=EC=9D=B4=EB=A6=84=20=EA=B3=BC=EA=B1=B0?= =?UTF-8?q?=ED=98=95=EC=9C=BC=EB=A1=9C=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20?= =?UTF-8?q?socialType=20=EC=9D=B8=EC=9E=90=EB=A1=9C=20=EC=A0=84=EB=8B=AC?= =?UTF-8?q?=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../event/publisher/PostEventPublisher.java | 17 +++++++++-------- .../module/post/service/PostService.java | 4 ++-- .../post/PostServiceTest.java | 9 ++++++--- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/main/java/team05/integrated_feed_backend/module/post/event/publisher/PostEventPublisher.java b/src/main/java/team05/integrated_feed_backend/module/post/event/publisher/PostEventPublisher.java index c2f9151..99b4b66 100644 --- a/src/main/java/team05/integrated_feed_backend/module/post/event/publisher/PostEventPublisher.java +++ b/src/main/java/team05/integrated_feed_backend/module/post/event/publisher/PostEventPublisher.java @@ -6,8 +6,9 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import team05.integrated_feed_backend.module.post.event.LikeCountIncreaseEvent; -import team05.integrated_feed_backend.module.post.event.ShareCountIncreaseEvent; +import team05.integrated_feed_backend.common.enums.SocialMediaType; +import team05.integrated_feed_backend.module.post.event.LikeCountIncreasedEvent; +import team05.integrated_feed_backend.module.post.event.ShareCountIncreasedEvent; @Component @RequiredArgsConstructor @@ -17,14 +18,14 @@ public class PostEventPublisher { private final ApplicationEventPublisher eventPublisher; @Async - public void publishLikeCountIncreaseEvent(Long postId) { - eventPublisher.publishEvent(new LikeCountIncreaseEvent(postId)); - log.info("LikeCountIncreaseEvent published asynchronously for post ID: {}", postId); + public void publishLikeCountIncreasedEvent(Long postId, SocialMediaType type) { + eventPublisher.publishEvent(new LikeCountIncreasedEvent(postId, type)); + log.info("LikeCountIncreasedEvent published asynchronously for post ID: {}", postId); } @Async - public void publishShareCountIncreaseEvent(Long postId) { - eventPublisher.publishEvent(new ShareCountIncreaseEvent(postId)); - log.info("ShareCountIncreaseEvent published asynchronously for post ID: {}", postId); + public void publishShareCountIncreasedEvent(Long postId, SocialMediaType type) { + eventPublisher.publishEvent(new ShareCountIncreasedEvent(postId, type)); + log.info("ShareCountIncreasedEvent published asynchronously for post ID: {}", postId); } } diff --git a/src/main/java/team05/integrated_feed_backend/module/post/service/PostService.java b/src/main/java/team05/integrated_feed_backend/module/post/service/PostService.java index a8e64da..8cfe851 100644 --- a/src/main/java/team05/integrated_feed_backend/module/post/service/PostService.java +++ b/src/main/java/team05/integrated_feed_backend/module/post/service/PostService.java @@ -28,7 +28,7 @@ public void increaseLikeCount(Long postId) { post.increaseLikeCount(); // 이벤트 비동기 발행 - postEventPublisher.publishLikeCountIncreaseEvent(postId); + postEventPublisher.publishLikeCountIncreasedEvent(postId, post.getType()); } @@ -42,6 +42,6 @@ public void increaseShareCount(Long postId) { post.increaseShareCount(); // 이벤트 비동기 발행 - postEventPublisher.publishShareCountIncreaseEvent(postId); + postEventPublisher.publishShareCountIncreasedEvent(postId, post.getType()); } } diff --git a/src/test/java/team05/integrated_feed_backend/post/PostServiceTest.java b/src/test/java/team05/integrated_feed_backend/post/PostServiceTest.java index 5dd74b0..dca261e 100644 --- a/src/test/java/team05/integrated_feed_backend/post/PostServiceTest.java +++ b/src/test/java/team05/integrated_feed_backend/post/PostServiceTest.java @@ -55,7 +55,8 @@ void shouldIncreaseLikeCountSuccessfully() { // then verify(postRepository, times(1)).findById(mockPost.getPostId()); - verify(postEventPublisher, times(1)).publishLikeCountIncreaseEvent(mockPost.getPostId()); + verify(postEventPublisher, times(1)).publishLikeCountIncreasedEvent(mockPost.getPostId(), + mockPost.getType()); assert (mockPost.getLikeCount() == 11L); } @@ -72,7 +73,8 @@ void shouldThrowExceptionWhenPostNotExists() { }); verify(postRepository, times(1)).findById(mockPost.getPostId()); - verify(postEventPublisher, times(0)).publishLikeCountIncreaseEvent(mockPost.getPostId()); + verify(postEventPublisher, times(0)).publishLikeCountIncreasedEvent(mockPost.getPostId(), + mockPost.getType()); } } @@ -91,7 +93,8 @@ void shouldIncreaseShareCountSuccessfully() { // then verify(postRepository, times(1)).findById(mockPost.getPostId()); - verify(postEventPublisher, times(1)).publishShareCountIncreaseEvent(mockPost.getPostId()); + verify(postEventPublisher, times(1)).publishShareCountIncreasedEvent(mockPost.getPostId(), + mockPost.getType()); assert (mockPost.getShareCount() == 6L); } From f9b40857e0e72f23eab29b379fd24f79da5ab744 Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Mon, 26 Aug 2024 12:58:45 +0900 Subject: [PATCH 24/26] =?UTF-8?q?fix:=20socialType=20=EC=9D=B8=EC=9E=90?= =?UTF-8?q?=EB=A1=9C=20=EB=B0=9B=EB=8A=94=20=EA=B2=83,=20event=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EB=B3=80=EA=B2=BD=ED=95=9C=20=EA=B2=83=20=EB=A7=9E?= =?UTF-8?q?=EC=B6=B0=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/PostEventListenerTest.java | 35 ++++---- .../post/PostEventPublisherTest.java | 79 +++++++++++-------- 2 files changed, 66 insertions(+), 48 deletions(-) diff --git a/src/test/java/team05/integrated_feed_backend/post/PostEventListenerTest.java b/src/test/java/team05/integrated_feed_backend/post/PostEventListenerTest.java index 4e27a03..8452de5 100644 --- a/src/test/java/team05/integrated_feed_backend/post/PostEventListenerTest.java +++ b/src/test/java/team05/integrated_feed_backend/post/PostEventListenerTest.java @@ -10,11 +10,12 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import team05.integrated_feed_backend.common.enums.SocialMediaType; import team05.integrated_feed_backend.infra.sns.adapter.FacebookAdapter; import team05.integrated_feed_backend.infra.sns.adapter.InstagramAdapter; import team05.integrated_feed_backend.infra.sns.adapter.TwitterAdapter; -import team05.integrated_feed_backend.module.post.event.LikeCountIncreaseEvent; -import team05.integrated_feed_backend.module.post.event.ShareCountIncreaseEvent; +import team05.integrated_feed_backend.module.post.event.LikeCountIncreasedEvent; +import team05.integrated_feed_backend.module.post.event.ShareCountIncreasedEvent; import team05.integrated_feed_backend.module.post.event.listener.PostEventListener; @ExtendWith(MockitoExtension.class) @@ -34,43 +35,45 @@ public class PostEventListenerTest { @Nested @DisplayName("게시물 좋아요 수 증가 이벤트 처리") - class HandleLikeCountIncreaseEvent { + class HandleLikeCountIncreasedEvent { @Test - @DisplayName("[성공] LikeCountIncreaseEvent가 수신되어 외부 API가 호출된다.") - void shouldHandleLikeCountIncreaseEvent() { + @DisplayName("[성공] LikeCountIncreasedEvent가 수신되어 Instagram API가 호출된다.") + void shouldHandleLikeCountIncreasedEvent() { // Given Long postId = 1L; - LikeCountIncreaseEvent event = new LikeCountIncreaseEvent(postId); + SocialMediaType type = SocialMediaType.INSTAGRAM; + LikeCountIncreasedEvent event = new LikeCountIncreasedEvent(1L, type); // when - postEventListener.handleLikeCountIncreaseEvent(event); + postEventListener.handleLikeCountIncreasedEvent(event); // then - verify(facebookAdapter, times(1)).increaseLikeCount(postId); - verify(twitterAdapter, times(1)).increaseLikeCount(postId); + verify(facebookAdapter, times(0)).increaseLikeCount(postId); + verify(twitterAdapter, times(0)).increaseLikeCount(postId); verify(instagramAdapter, times(1)).increaseLikeCount(postId); } } @Nested @DisplayName("게시물 공유 수 증가 이벤트 처리") - class HandleShareCountIncreaseEvent { + class HandleShareCountIncreasedEvent { @Test - @DisplayName("[성공] ShareCountIncreaseEvent가 수신되어 외부 API가 호출된다.") - void shouldHandleShareCountIncreaseEvent() { + @DisplayName("[성공] ShareCountIncreasedEvent가 수신되어 Facebook API가 호출된다.") + void shouldHandleShareCountIncreasedEvent() { // Given Long postId = 1L; - ShareCountIncreaseEvent event = new ShareCountIncreaseEvent(postId); + SocialMediaType type = SocialMediaType.FACEBOOK; + ShareCountIncreasedEvent event = new ShareCountIncreasedEvent(1L, type); // when - postEventListener.handleShareCountIncreaseEvent(event); + postEventListener.handleShareCountIncreasedEvent(event); // then verify(facebookAdapter, times(1)).increaseShareCount(postId); - verify(twitterAdapter, times(1)).increaseShareCount(postId); - verify(instagramAdapter, times(1)).increaseShareCount(postId); + verify(twitterAdapter, times(0)).increaseShareCount(postId); + verify(instagramAdapter, times(0)).increaseShareCount(postId); } } } \ No newline at end of file diff --git a/src/test/java/team05/integrated_feed_backend/post/PostEventPublisherTest.java b/src/test/java/team05/integrated_feed_backend/post/PostEventPublisherTest.java index 1776870..d5a5d0c 100644 --- a/src/test/java/team05/integrated_feed_backend/post/PostEventPublisherTest.java +++ b/src/test/java/team05/integrated_feed_backend/post/PostEventPublisherTest.java @@ -4,6 +4,7 @@ import static org.mockito.Mockito.*; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; @@ -12,8 +13,9 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.context.ApplicationEventPublisher; -import team05.integrated_feed_backend.module.post.event.LikeCountIncreaseEvent; -import team05.integrated_feed_backend.module.post.event.ShareCountIncreaseEvent; +import team05.integrated_feed_backend.common.enums.SocialMediaType; +import team05.integrated_feed_backend.module.post.event.LikeCountIncreasedEvent; +import team05.integrated_feed_backend.module.post.event.ShareCountIncreasedEvent; import team05.integrated_feed_backend.module.post.event.publisher.PostEventPublisher; @ExtendWith(MockitoExtension.class) @@ -25,43 +27,56 @@ public class PostEventPublisherTest { @InjectMocks private PostEventPublisher postEventPublisher; - @Test - @DisplayName("[성공] LikeCountIncreaseEvent가 비동기적으로 발행된다.") - void shouldPublishLikeCountIncreaseEvent() { - // Given - Long postId = 1L; + @Nested + @DisplayName("LikeCountIncreasedEvent 비동기 발행") + class LikeCountIncreasedEventPublisher { + @Test + @DisplayName("[성공] LikeCountIncreasedEvent가 비동기적으로 발행된다.") + void shouldPublishLikeCountIncreasedEvent() { + // Given + Long postId = 1L; + SocialMediaType type = SocialMediaType.FACEBOOK; - // when - postEventPublisher.publishLikeCountIncreaseEvent(postId); + // when + postEventPublisher.publishLikeCountIncreasedEvent(postId, type); - // then - // LikeCountIncreaseEvent 생성 및 호출 확인 - ArgumentCaptor captor = ArgumentCaptor.forClass(LikeCountIncreaseEvent.class); - verify(eventPublisher, times(1)).publishEvent(captor.capture()); + // then + // LikeCountIncreasedEvent 생성 및 호출 확인 + ArgumentCaptor captor = ArgumentCaptor.forClass(LikeCountIncreasedEvent.class); + verify(eventPublisher, times(1)).publishEvent(captor.capture()); - // 캡쳐된 이벤트의 존재 및 해당 postId를 갖고 있는지 확인 - LikeCountIncreaseEvent capturedEvent = captor.getValue(); - assertNotNull(capturedEvent); - assertEquals(postId, capturedEvent.getPostId(), "Post ID should match"); + // 캡쳐된 이벤트의 존재 및 해당 postId, SocialType 을 갖고 있는지 확인 + LikeCountIncreasedEvent capturedEvent = captor.getValue(); + assertNotNull(capturedEvent); + assertEquals(postId, capturedEvent.getPostId(), "Post ID should match"); + assertEquals(type, capturedEvent.getType(), "Post Type should match"); + } } - @Test - @DisplayName("[성공] ShareCountIncreaseEvent가 비동기적으로 발행된다.") - void shouldPublishShareCountIncreaseEvent() { - // Given - Long postId = 1L; + @Nested + @DisplayName("ShareCountIncreasedEvent 비동기 발행") + class ShareCountIncreasedEventPublisher { + @Test + @DisplayName("[성공] ShareCountIncreasedEvent가 비동기적으로 발행된다.") + void shouldPublishShareCountIncreasedEvent() { + // Given + Long postId = 1L; + SocialMediaType type = SocialMediaType.FACEBOOK; - // when - postEventPublisher.publishShareCountIncreaseEvent(postId); + // when + postEventPublisher.publishShareCountIncreasedEvent(postId, type); - // then - // LikeCountIncreaseEvent 생성 및 호출 확인 - ArgumentCaptor captor = ArgumentCaptor.forClass(ShareCountIncreaseEvent.class); - verify(eventPublisher, times(1)).publishEvent(captor.capture()); + // then + // ShareCountIncreasedEvent 생성 및 호출 확인 + ArgumentCaptor captor = ArgumentCaptor.forClass(ShareCountIncreasedEvent.class); + verify(eventPublisher, times(1)).publishEvent(captor.capture()); - // 캡쳐된 이벤트의 존재 및 해당 postId를 갖고 있는지 확인 - ShareCountIncreaseEvent capturedEvent = captor.getValue(); - assertNotNull(capturedEvent); - assertEquals(postId, capturedEvent.getPostId(), "Post ID should match"); + // 캡쳐된 이벤트의 존재 및 해당 postId, SocialType 을 갖고 있는지 확인 + ShareCountIncreasedEvent capturedEvent = captor.getValue(); + assertNotNull(capturedEvent); + assertEquals(postId, capturedEvent.getPostId(), "Post ID should match"); + assertEquals(type, capturedEvent.getType(), "Post Type should match"); + + } } } From 907fb779b4fa78ab3b4395f4677402bba382ab2e Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Mon, 26 Aug 2024 13:30:58 +0900 Subject: [PATCH 25/26] =?UTF-8?q?fix:=20=EB=B3=91=ED=95=A9=20=EA=B3=BC?= =?UTF-8?q?=EC=A0=95=20=EC=A4=91=20=EC=82=AD=EC=A0=9C=ED=95=9C=20=EB=B9=84?= =?UTF-8?q?=EC=A6=88=EB=8B=88=EC=8A=A4=20=EB=A1=9C=EC=A7=81=20=EB=8B=A4?= =?UTF-8?q?=EC=8B=9C=20=EC=B6=94=EA=B0=80=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../integrated_feed_backend/module/post/entity/Post.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/team05/integrated_feed_backend/module/post/entity/Post.java b/src/main/java/team05/integrated_feed_backend/module/post/entity/Post.java index 99e62e5..82c2436 100644 --- a/src/main/java/team05/integrated_feed_backend/module/post/entity/Post.java +++ b/src/main/java/team05/integrated_feed_backend/module/post/entity/Post.java @@ -55,4 +55,13 @@ public class Post extends BaseEntity { @Builder.Default private List postHashtags = new ArrayList<>(); + // 좋아요 수 증가시키는 메서드 + public void increaseLikeCount() { + this.likeCount += 1; + } + + // 공유 수 증가시키는 메서드 + public void increaseShareCount() { + this.shareCount += 1; + } } From 2a7b15c26e2c5bf3bac826515007ddeb40c7bac7 Mon Sep 17 00:00:00 2001 From: yerim1ee <97941141+yerim123456@users.noreply.github.com> Date: Mon, 26 Aug 2024 13:59:42 +0900 Subject: [PATCH 26/26] =?UTF-8?q?refactor:=20StatusCode=20=EC=9C=84?= =?UTF-8?q?=EC=B9=98=20=EB=B3=80=EA=B2=BD=EC=97=90=20=EB=94=B0=EB=9D=BC=20?= =?UTF-8?q?import=20=EB=8B=A4=EC=8B=9C=20=EC=A7=84=ED=96=89=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/post/controller/PostController.java | 2 +- .../module/post/service/PostService.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/team05/integrated_feed_backend/module/post/controller/PostController.java b/src/main/java/team05/integrated_feed_backend/module/post/controller/PostController.java index bbca375..4f27b99 100644 --- a/src/main/java/team05/integrated_feed_backend/module/post/controller/PostController.java +++ b/src/main/java/team05/integrated_feed_backend/module/post/controller/PostController.java @@ -11,7 +11,7 @@ import lombok.RequiredArgsConstructor; import team05.integrated_feed_backend.common.BaseApiResponse; -import team05.integrated_feed_backend.exception.code.StatusCode; +import team05.integrated_feed_backend.common.code.StatusCode; import team05.integrated_feed_backend.module.post.dto.request.PostSearchReq; import team05.integrated_feed_backend.module.post.dto.response.PostSearchRes; import team05.integrated_feed_backend.module.post.service.PostService; diff --git a/src/main/java/team05/integrated_feed_backend/module/post/service/PostService.java b/src/main/java/team05/integrated_feed_backend/module/post/service/PostService.java index 8cfe851..05fa5b7 100644 --- a/src/main/java/team05/integrated_feed_backend/module/post/service/PostService.java +++ b/src/main/java/team05/integrated_feed_backend/module/post/service/PostService.java @@ -4,7 +4,7 @@ import org.springframework.transaction.annotation.Transactional; import lombok.RequiredArgsConstructor; -import team05.integrated_feed_backend.exception.code.StatusCode; +import team05.integrated_feed_backend.common.code.StatusCode; import team05.integrated_feed_backend.exception.custom.DataNotFoundException; import team05.integrated_feed_backend.module.post.entity.Post; import team05.integrated_feed_backend.module.post.event.publisher.PostEventPublisher;