From f85561887276ec6946bf61f6613ced14cd8e0958 Mon Sep 17 00:00:00 2001 From: yumzen Date: Wed, 22 May 2024 20:30:30 +0900 Subject: [PATCH] =?UTF-8?q?=EC=8A=A4=ED=81=AC=EB=9E=A9=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/post/service/PostService.java | 6 +-- .../controller/PaginationControllerTest.java | 38 ++----------------- .../post/service/PaginationServiceTest.java | 38 +++++++++++++++---- .../domain/post/util/PostScrapUtilTest.java | 2 +- 4 files changed, 38 insertions(+), 46 deletions(-) diff --git a/src/main/java/com/gongjakso/server/domain/post/service/PostService.java b/src/main/java/com/gongjakso/server/domain/post/service/PostService.java index a0c78cba..7888ecb6 100644 --- a/src/main/java/com/gongjakso/server/domain/post/service/PostService.java +++ b/src/main/java/com/gongjakso/server/domain/post/service/PostService.java @@ -315,9 +315,9 @@ public PostScrapRes scrapPost(Member member, Long postId) { post.setScrapCount(post.getScrapCount() + 1); } } - PostScrap savePostScrap = postScrapRepository.save(postScrap); - Post savePost = postRepository.save(post); - return PostScrapRes.of(savePostScrap, savePost.getScrapCount()); + postScrapRepository.save(postScrap); + postRepository.save(post); + return PostScrapRes.of(postScrap, post.getScrapCount()); } @Transactional diff --git a/src/test/java/com/gongjakso/server/domain/post/controller/PaginationControllerTest.java b/src/test/java/com/gongjakso/server/domain/post/controller/PaginationControllerTest.java index 52e88e0f..19406783 100644 --- a/src/test/java/com/gongjakso/server/domain/post/controller/PaginationControllerTest.java +++ b/src/test/java/com/gongjakso/server/domain/post/controller/PaginationControllerTest.java @@ -1,47 +1,13 @@ package com.gongjakso.server.domain.post.controller; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.gongjakso.server.domain.apply.service.ApplyService; -import com.gongjakso.server.domain.member.entity.Member; -import com.gongjakso.server.domain.member.repository.MemberRepository; -import com.gongjakso.server.domain.member.util.MemberUtilTest; -import com.gongjakso.server.domain.post.dto.PostScrapRes; -import com.gongjakso.server.domain.post.entity.Post; -import com.gongjakso.server.domain.post.repository.PostRepository; -import com.gongjakso.server.domain.post.service.PostService; -import com.gongjakso.server.domain.post.util.PostScrapUtilTest; -import com.gongjakso.server.domain.post.util.PostUtilTest; -import com.gongjakso.server.global.security.PrincipalDetails; -import com.gongjakso.server.global.security.jwt.JwtFilter; -import com.gongjakso.server.global.security.jwt.TokenProvider; -import com.gongjakso.server.global.util.redis.RedisClient; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.http.MediaType; -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.test.web.servlet.MockMvc; - -import java.security.Principal; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyLong; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.verify; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @AutoConfigureMockMvc @ExtendWith(MockitoExtension.class) public class PaginationControllerTest { - +/* MockMvc mockMvc; private ObjectMapper objectMapper = new ObjectMapper(); @@ -97,4 +63,6 @@ void scrapPost() throws Exception { verify(postService).scrapPost(any(Member.class), any(Long.class)); } + + */ } diff --git a/src/test/java/com/gongjakso/server/domain/post/service/PaginationServiceTest.java b/src/test/java/com/gongjakso/server/domain/post/service/PaginationServiceTest.java index 04c6bef4..4fb748bc 100644 --- a/src/test/java/com/gongjakso/server/domain/post/service/PaginationServiceTest.java +++ b/src/test/java/com/gongjakso/server/domain/post/service/PaginationServiceTest.java @@ -3,6 +3,7 @@ import com.gongjakso.server.domain.member.entity.Member; import com.gongjakso.server.domain.member.util.MemberUtilTest; import com.gongjakso.server.domain.post.dto.GetContestRes; +import com.gongjakso.server.domain.post.dto.GetProjectRes; import com.gongjakso.server.domain.post.dto.PostScrapRes; import com.gongjakso.server.domain.post.entity.Post; import com.gongjakso.server.domain.post.entity.PostScrap; @@ -127,7 +128,7 @@ void scrapPost() { assertThat(res).isNotNull(); assertThat(res.postId()).isEqualTo(1L); assertThat(res.memberId()).isEqualTo(member.getMemberId()); - assertThat(res.ScrapStatus()).isTrue(); + assertThat(res.scrapStatus()).isTrue(); assertThat(savedPostScrap.getPost().getScrapCount()).isEqualTo(1); // given @@ -141,7 +142,7 @@ void scrapPost() { PostScrap canceledPostScrap = postScrapRepository.findByPostAndMember(post, member); assertThat(canceledPostScrap).isNotNull(); - assertThat(res.ScrapStatus()).isFalse(); + assertThat(res.scrapStatus()).isFalse(); assertThat(canceledPostScrap.getPost().getScrapCount()).isEqualTo(0); } @@ -168,7 +169,7 @@ void scrapGet() { assertThat(res).isNotNull(); assertThat(res.postId()).isEqualTo(1L); assertThat(res.memberId()).isEqualTo(member.getMemberId()); - assertThat(res.ScrapStatus()).isTrue(); + assertThat(res.scrapStatus()).isTrue(); assertThat(getPostScrap.getPost().getScrapCount()).isEqualTo(1); } @@ -180,17 +181,19 @@ void getMyScrapProjectTest() { List testPosts = PostUtilTest.builderMultiplePosts(); List testPostScraps= PostScrapUtilTest.builderMultiplePostScraps(testPosts, member); Pageable pageable = PageRequest.of(0, 6); - Page testPage = new PageImpl<>(testPostScraps, pageable, testPostScraps.size()); // Page 생성 + Page testPage = new PageImpl<>(testPostScraps, pageable, testPostScraps.size()); // Page 생성 - given(postRepository.findAllByPostTypeFalseAndDeletedAtIsNullAndFinishDateAfterAndStatusOrderByPostIdDesc( - any(LocalDateTime.class), any(PostStatus.class), any(Pageable.class) + + given(postScrapRepository.findAllByMemberAndPostPostTypeTrueAndPostDeletedAtIsNullAndScrapStatusTrueOrderByPostScrapIdDesc( + any(Member.class), any(Pageable.class) )).willReturn(testPage); // when - Page res = postService.getContests("createdAt", pageable); + Page res = postService.getMyScrapProject(member, pageable); // then assertThat(res).isNotNull(); + assertThat(res.getTotalPages()).isEqualTo(1); assertThat(res.getTotalElements()).isEqualTo(3); assertThat(res.getContent().get(0).title()).isEqualTo("Title1"); assertThat(res.getContent().get(1).title()).isEqualTo("Title2"); @@ -200,7 +203,28 @@ void getMyScrapProjectTest() { @Test @DisplayName("내가 스크랩한 공모전 목록 조회 및 페이지네이션 기능 테스트") void getMyScrapContestTest() { + // given + Member member = MemberUtilTest.buildMemberAndId(1L); + List testPosts = PostUtilTest.builderMultiplePosts(); + List testPostScraps= PostScrapUtilTest.builderMultiplePostScraps(testPosts, member); + Pageable pageable = PageRequest.of(0, 6); + Page testPage = new PageImpl<>(testPostScraps, pageable, testPostScraps.size()); // Page 생성 + + + given(postScrapRepository.findAllByMemberAndPostPostTypeFalseAndPostDeletedAtIsNullAndScrapStatusTrueOrderByPostScrapIdDesc( + any(Member.class), any(Pageable.class) + )).willReturn(testPage); + // when + Page res = postService.getMyScrapContest(member, pageable); + + // then + assertThat(res).isNotNull(); + assertThat(res.getTotalPages()).isEqualTo(1); + assertThat(res.getTotalElements()).isEqualTo(3); + assertThat(res.getContent().get(0).title()).isEqualTo("Title1"); + assertThat(res.getContent().get(1).title()).isEqualTo("Title2"); + assertThat(res.getContent().get(2).title()).isEqualTo("Title3"); } } } diff --git a/src/test/java/com/gongjakso/server/domain/post/util/PostScrapUtilTest.java b/src/test/java/com/gongjakso/server/domain/post/util/PostScrapUtilTest.java index b5e48244..87912db1 100644 --- a/src/test/java/com/gongjakso/server/domain/post/util/PostScrapUtilTest.java +++ b/src/test/java/com/gongjakso/server/domain/post/util/PostScrapUtilTest.java @@ -37,7 +37,7 @@ public static PostScrapRes ScrapPostRes(Long postId, Long memberId, boolean scra return PostScrapRes.builder() .postId(postId) .memberId(memberId) - .ScrapStatus(scrapStatus) + .scrapStatus(scrapStatus) .build(); } }