From 2e5c027b00bd8e762a60f617e62d90a4a9412c52 Mon Sep 17 00:00:00 2001 From: yumzen Date: Sun, 5 May 2024 23:09:49 +0900 Subject: [PATCH 01/11] =?UTF-8?q?[#130]=20fix:=20=EC=B5=9C=EC=8B=A0?= =?UTF-8?q?=EC=88=9C=20=EC=A1=B0=ED=9A=8C=20=EB=AC=B8=EC=A0=9C=20=ED=95=B4?= =?UTF-8?q?=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/domain/post/service/PostService.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 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 d7d7fba5..9b9d9b08 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 @@ -1,6 +1,5 @@ package com.gongjakso.server.domain.post.service; -import com.gongjakso.server.domain.apply.enumerate.ApplyType; import com.gongjakso.server.domain.apply.repository.ApplyRepository; import com.gongjakso.server.domain.member.entity.Member; import com.gongjakso.server.domain.post.dto.*; @@ -141,7 +140,7 @@ public PostDeleteRes delete(Member member, Long id) { public Page getContests(String sort, Pageable page) throws ApplicationException { Pageable pageable = PageRequest.of(page.getPageNumber(), page.getPageSize()); Page posts; - if(sort.equals("createdAt,desc")){ //최신순 + if(sort.equals("createdAt")){ //최신순 posts = postRepository.findAllByPostTypeFalseAndDeletedAtIsNullAndFinishDateAfterAndStatusOrderByCreatedAtDesc(LocalDateTime.now(), RECRUITING, pageable); } else{ //스크랩순 posts = postRepository.findAllByPostTypeFalseAndDeletedAtIsNullAndFinishDateAfterAndStatusOrderByScrapCountDescCreatedAtDesc(LocalDateTime.now(), RECRUITING, pageable); @@ -160,7 +159,7 @@ public Page getContestsBySearchWord(String sort, String searchWor Pageable pageable = PageRequest.of(page.getPageNumber(), page.getPageSize()); searchWord = searchWord.replaceAll(" ", ""); // 검색어에서 공백 제거 Page posts; - if (sort.equals("createdAt,desc")) { + if (sort.equals("createdAt")) { posts = postRepository.findAllByTitleContainsAndPostTypeFalseAndDeletedAtIsNullAndFinishDateAfterAndStatusOrderByCreatedAtDesc(searchWord.toLowerCase(), LocalDateTime.now(), RECRUITING, pageable); } else{ posts = postRepository.findAllByTitleContainsAndPostTypeFalseAndDeletedAtIsNullAndFinishDateAfterAndStatusOrderByScrapCountDescCreatedAtDesc(searchWord.toLowerCase(), LocalDateTime.now(), RECRUITING, pageable); @@ -187,7 +186,7 @@ public Page getContestsByMeetingAreaAndCategoryAndSearchWord( throw new ApplicationException(INVALID_VALUE_EXCEPTION); } Page posts; - if (sort.equals("createdAt,desc")) { + if (sort.equals("createdAt")) { posts = postRepository.findAllPostsJoinedWithCategoriesByTitleContainsAndPostTypeFalseAndDeletedAtIsNullAndFinishDateAfterAndStatusAndMeetingCityContainsAndMeetingTownContainsAndCategoriesCategoryTypeContainsOrderByCreatedAtDesc(searchWord.toLowerCase(), LocalDateTime.now(), RECRUITING, meetingCity, meetingTown, category.toString(), pageable); }else{ posts = postRepository.findAllPostsJoinedWithCategoriesByTitleContainsAndPostTypeFalseAndDeletedAtIsNullAndFinishDateAfterAndStatusAndMeetingCityContainsAndMeetingTownContainsAndCategoriesCategoryTypeContainsOrderByScrapCountDescCreatedAtDesc(searchWord.toLowerCase(), LocalDateTime.now(), RECRUITING, meetingCity, meetingTown, category.toString(), pageable); @@ -197,7 +196,7 @@ public Page getContestsByMeetingAreaAndCategoryAndSearchWord( return posts.map(post -> GetContestRes.of(post)); } else{ Page posts; - if (sort.equals("createdAt,desc")) { + if (sort.equals("createdAt")) { posts = postRepository.findAllByTitleContainsAndPostTypeFalseAndDeletedAtIsNullAndFinishDateAfterAndStatusAndMeetingCityContainsAndMeetingTownContainsOrderByCreatedAtDesc(searchWord.toLowerCase(), LocalDateTime.now(), RECRUITING, meetingCity, meetingTown, pageable); }else{ posts = postRepository.findAllByTitleContainsAndPostTypeFalseAndDeletedAtIsNullAndFinishDateAfterAndStatusAndMeetingCityContainsAndMeetingTownContainsOrderByScrapCountDescCreatedAtDesc(searchWord.toLowerCase(), LocalDateTime.now(), RECRUITING, meetingCity, meetingTown, pageable); @@ -215,7 +214,7 @@ public Page getContestsByMeetingAreaAndCategoryAndSearchWord( public Page getProjects(String sort, Pageable page) throws ApplicationException { Pageable pageable = PageRequest.of(page.getPageNumber(), page.getPageSize()); Page posts; - if(sort.equals("createdAt,desc")){ //최신순 + if(sort.equals("createdAt")){ //최신순 posts = postRepository.findAllByPostTypeTrueAndDeletedAtIsNullAndFinishDateAfterAndStatusOrderByCreatedAtDesc(LocalDateTime.now(), RECRUITING, pageable); } else{ //스크랩순 posts = postRepository.findAllByPostTypeTrueAndDeletedAtIsNullAndFinishDateAfterAndStatusOrderByScrapCountDescCreatedAtDesc(LocalDateTime.now(), RECRUITING, pageable); @@ -233,7 +232,7 @@ public Page getProjectsBySearchWord(String sort, String searchWor Pageable pageable = PageRequest.of(page.getPageNumber(), page.getPageSize()); searchWord = searchWord.replaceAll(" ", ""); // 검색어에서 공백 제거 Page posts; - if (sort.equals("createdAt,desc")) { + if (sort.equals("createdAt")) { posts = postRepository.findAllByTitleContainsAndPostTypeTrueAndDeletedAtIsNullAndFinishDateAfterAndStatusOrderByCreatedAtDesc(searchWord.toLowerCase(), LocalDateTime.now(), RECRUITING, pageable); } else{ posts = postRepository.findAllByTitleContainsAndPostTypeTrueAndDeletedAtIsNullAndFinishDateAfterAndStatusOrderByScrapCountDescCreatedAtDesc(searchWord.toLowerCase(), LocalDateTime.now(), RECRUITING, pageable); @@ -262,7 +261,7 @@ public Page getProjectsByMeetingAreaAndStackNameAndSearchWord( throw new ApplicationException(INVALID_VALUE_EXCEPTION); } Page posts; - if (sort.equals("createdAt,desc")) { + if (sort.equals("createdAt")) { posts = postRepository.findAllPostsJoinedWithStackNamesByTitleContainsAndPostTypeTrueAndDeletedAtIsNullAndFinishDateAfterAndStatusAndMeetingCityContainsAndMeetingTownContainsAndStackNamesStackNameTypeContainsOrderByCreatedAtDesc(searchWord.toLowerCase(), LocalDateTime.now(), RECRUITING, meetingCity,meetingTown, stackName.toString(), pageable); }else{ posts = postRepository.findAllPostsJoinedWithStackNamesByTitleContainsAndPostTypeTrueAndDeletedAtIsNullAndFinishDateAfterAndStatusAndMeetingCityContainsAndMeetingTownContainsAndStackNamesStackNameTypeContainsOrderByScrapCountDescCreatedAtDesc(searchWord.toLowerCase(), LocalDateTime.now(), RECRUITING, meetingCity,meetingTown, stackName.toString(), pageable); @@ -272,7 +271,7 @@ public Page getProjectsByMeetingAreaAndStackNameAndSearchWord( return posts.map(post -> GetProjectRes.of(post)); } else{ Page posts; - if (sort.equals("createdAt,desc")) { + if (sort.equals("createdAt")) { posts = postRepository.findAllByTitleContainsAndPostTypeTrueAndDeletedAtIsNullAndFinishDateAfterAndStatusAndMeetingCityContainsAndMeetingTownContainsOrderByCreatedAtDesc(searchWord.toLowerCase(), LocalDateTime.now(), RECRUITING, meetingCity,meetingTown, pageable); }else{ posts = postRepository.findAllByTitleContainsAndPostTypeTrueAndDeletedAtIsNullAndFinishDateAfterAndStatusAndMeetingCityContainsAndMeetingTownContainsOrderByScrapCountDescCreatedAtDesc(searchWord.toLowerCase(), LocalDateTime.now(), RECRUITING, meetingCity,meetingTown, pageable); From adc07233c5c89e454578ab0396abc3ee85e8ce4e Mon Sep 17 00:00:00 2001 From: yumzen Date: Thu, 9 May 2024 15:35:02 +0900 Subject: [PATCH 02/11] =?UTF-8?q?test:=20PostUtilTest=20builderPost=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/domain/post/util/PostUtilTest.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/test/java/com/gongjakso/server/domain/post/util/PostUtilTest.java diff --git a/src/test/java/com/gongjakso/server/domain/post/util/PostUtilTest.java b/src/test/java/com/gongjakso/server/domain/post/util/PostUtilTest.java new file mode 100644 index 00000000..873d3e68 --- /dev/null +++ b/src/test/java/com/gongjakso/server/domain/post/util/PostUtilTest.java @@ -0,0 +1,67 @@ +package com.gongjakso.server.domain.post.util; + +import com.gongjakso.server.domain.member.entity.Member; +import com.gongjakso.server.domain.member.util.MemberUtilTest; +import com.gongjakso.server.domain.post.entity.Post; +import com.gongjakso.server.domain.post.enumerate.MeetingMethod; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class PostUtilTest { + public static Post builderPost(){ + Member member = MemberUtilTest.buildMember(); + + return Post.builder() + .title("Title1") + .member(member) + .contents("Content1") + .contestLink("Link1") + .startDate(LocalDateTime.now()) + .endDate(LocalDateTime.now().plusDays(10)) + .finishDate(LocalDateTime.now().plusDays(16)) + .maxPerson(8L) + .meetingMethod(MeetingMethod.ONLINE) + .meetingCity("서울시") + .meetingTown("마포구") + .questionMethod(true) + .questionLink(".com") + .postType(false) + .categories(new ArrayList<>()) + .stackNames(new ArrayList<>()) + .build(); + } + + public static Post builderPosts(String title, String contents, String link) { + Member member = MemberUtilTest.buildMember(); + + return Post.builder() + .title(title) + .member(member) + .contents(contents) + .contestLink(link) + .startDate(LocalDateTime.now()) + .endDate(LocalDateTime.now().plusDays(10)) + .finishDate(LocalDateTime.now().plusDays(16)) + .maxPerson(8L) + .meetingMethod(MeetingMethod.ONLINE) + .meetingCity("서울시") + .meetingTown("마포구") + .questionMethod(true) + .questionLink(".com") + .postType(false) + .categories(new ArrayList<>()) + .stackNames(new ArrayList<>()) + .build(); + } + + public static List builderMultiplePosts() { + return Arrays.asList( + builderPosts("Title1", "Content1", "Link1"), + builderPosts("Title2", "Content2", "Link2"), + builderPosts("Title3", "Content3", "Link3") + ); + } +} From 26e412b831ef40471e08de387a88407c1505d878 Mon Sep 17 00:00:00 2001 From: yumzen Date: Thu, 9 May 2024 15:35:21 +0900 Subject: [PATCH 03/11] =?UTF-8?q?[#135]=20test:=20PostServiceTest=20?= =?UTF-8?q?=EC=B4=88=EA=B8=B0=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gongjakso/server/domain/post/service/PostServiceTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/gongjakso/server/domain/post/service/PostServiceTest.java b/src/test/java/com/gongjakso/server/domain/post/service/PostServiceTest.java index 8a84f198..456ef164 100644 --- a/src/test/java/com/gongjakso/server/domain/post/service/PostServiceTest.java +++ b/src/test/java/com/gongjakso/server/domain/post/service/PostServiceTest.java @@ -18,7 +18,7 @@ void beforeEach() { @Test void create() { - + } @Test From cbc99ec7e832a326373d35ade3b78fda27750a0e Mon Sep 17 00:00:00 2001 From: yumzen Date: Sat, 11 May 2024 01:04:23 +0900 Subject: [PATCH 04/11] =?UTF-8?q?[#139]=20test:=20postService=20scrapPost?= =?UTF-8?q?=EB=A9=94=EC=86=8C=EB=93=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/domain/member/entity/Member.java | 14 ++ .../server/domain/post/entity/Post.java | 28 ++++ .../server/domain/post/entity/PostScrap.java | 8 ++ .../domain/member/util/MemberUtilTest.java | 16 +++ .../post/service/PaginationServiceTest.java | 134 ++++++++++++++++++ .../domain/post/service/PostServiceTest.java | 2 + .../domain/post/util/PostScrapUtilTest.java | 20 +++ .../server/domain/post/util/PostUtilTest.java | 94 ++++++------ 8 files changed, 274 insertions(+), 42 deletions(-) create mode 100644 src/test/java/com/gongjakso/server/domain/post/service/PaginationServiceTest.java create mode 100644 src/test/java/com/gongjakso/server/domain/post/util/PostScrapUtilTest.java diff --git a/src/main/java/com/gongjakso/server/domain/member/entity/Member.java b/src/main/java/com/gongjakso/server/domain/member/entity/Member.java index e773c0c5..295503a8 100644 --- a/src/main/java/com/gongjakso/server/domain/member/entity/Member.java +++ b/src/main/java/com/gongjakso/server/domain/member/entity/Member.java @@ -77,4 +77,18 @@ public Member(String email, String password, String name, String phone, String p this.major = major; this.job = job; } + + public Member(Long id, String email, String password, String name, String phone, String profileUrl, String memberType, String loginType, String status, String major, String job){ + this.memberId = id; + this.email = email; + this.password = password; + this.name = name; + this.phone = phone; + this.profileUrl = profileUrl; + this.memberType = MemberType.valueOf(memberType); + this.loginType = LoginType.valueOf(loginType); + this.status = status; + this.major = major; + this.job = job; + } } diff --git a/src/main/java/com/gongjakso/server/domain/post/entity/Post.java b/src/main/java/com/gongjakso/server/domain/post/entity/Post.java index 9a60516d..be1012f8 100644 --- a/src/main/java/com/gongjakso/server/domain/post/entity/Post.java +++ b/src/main/java/com/gongjakso/server/domain/post/entity/Post.java @@ -120,6 +120,7 @@ public Post(String title, Member member, String contents, String contestLink, Lo this.postView = 0L; } + @Builder public void modify(PostModifyReq req) { this.title = req.title(); this.contents = req.contents(); @@ -141,4 +142,31 @@ public void modify(PostModifyReq req) { public void updatePostView(Long postView) { this.postView = postView + 1; } + + @Builder + public Post(Long id, String title, Member member, String contents, String contestLink, LocalDateTime startDate, + LocalDateTime endDate, LocalDateTime finishDate, Long maxPerson, MeetingMethod meetingMethod, + String meetingCity, String meetingTown, boolean questionMethod, String questionLink, boolean postType, + List stackNames, List categories) { + this.postId = id; + this.title = title; + this.member = member; + this.contents = contents; + this.contestLink = contestLink; + this.status = PostStatus.RECRUITING; + this.startDate = startDate; + this.finishDate = finishDate; + this.endDate = endDate; + this.maxPerson = maxPerson; + this.meetingMethod = meetingMethod; + this.meetingCity = meetingCity; + this.meetingTown = meetingTown; + this.questionMethod = questionMethod; + this.questionLink = questionLink; + this.postType = postType; + this.daysRemaining = finishDate.isBefore(LocalDateTime.now()) ? -1 : ChronoUnit.DAYS.between(LocalDateTime.now(), finishDate); + this.stackNames = stackNames; + this.categories = categories; + this.postView = 0L; + } } \ No newline at end of file diff --git a/src/main/java/com/gongjakso/server/domain/post/entity/PostScrap.java b/src/main/java/com/gongjakso/server/domain/post/entity/PostScrap.java index 0797d3e7..c9c4ec55 100644 --- a/src/main/java/com/gongjakso/server/domain/post/entity/PostScrap.java +++ b/src/main/java/com/gongjakso/server/domain/post/entity/PostScrap.java @@ -31,6 +31,14 @@ public PostScrap(Post post, Member member, Boolean scrapStatus){ this.scrapStatus = scrapStatus; } + @Builder + public PostScrap(long postScrapId, Post post, Member member, Boolean scrapStatus){ + this.postScrapId = postScrapId; + this.post = post; + this.member = member; + this.scrapStatus = scrapStatus; + } + public Boolean getScrapStatus(){ return scrapStatus; } diff --git a/src/test/java/com/gongjakso/server/domain/member/util/MemberUtilTest.java b/src/test/java/com/gongjakso/server/domain/member/util/MemberUtilTest.java index cde50ff4..c499fc57 100644 --- a/src/test/java/com/gongjakso/server/domain/member/util/MemberUtilTest.java +++ b/src/test/java/com/gongjakso/server/domain/member/util/MemberUtilTest.java @@ -20,6 +20,22 @@ public static Member buildMember() { .build(); } + public static Member buildMemberAndId(Long id) { + return new Member( + id, + "example@gmail.com", + "password123", + "공작소", + "010-1234-5678", + "http://example.com", + MemberType.GENERAL.toString(), + LoginType.KAKAO.toString(), + "상태", + "전공", + "직업" + ); + } + public static MemberReq buildMemberReq() { return MemberReq.builder() .name("변경 공작소") 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 new file mode 100644 index 00000000..7d32c94c --- /dev/null +++ b/src/test/java/com/gongjakso/server/domain/post/service/PaginationServiceTest.java @@ -0,0 +1,134 @@ +package com.gongjakso.server.domain.post.service; + +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.PostScrapRes; +import com.gongjakso.server.domain.post.entity.Post; +import com.gongjakso.server.domain.post.entity.PostScrap; +import com.gongjakso.server.domain.post.enumerate.PostStatus; +import com.gongjakso.server.domain.post.repository.PostRepository; +import com.gongjakso.server.domain.post.repository.PostScrapRepository; +import com.gongjakso.server.domain.post.util.PostUtilTest; +import org.junit.jupiter.api.DisplayName; +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.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; + +@ExtendWith(MockitoExtension.class) +public class PaginationServiceTest { + @InjectMocks + PostService postService; + + @Mock + PostRepository postRepository; + + @Mock + PostScrapRepository postScrapRepository; + + private PostScrap postScrap; + @Test + @DisplayName("공모전 공고 전체 조회 최신순") + void getContestsByCreatedAt() { + // given + List testPosts = PostUtilTest.builderMultiplePosts(); + Pageable pageable = PageRequest.of(0, 6); + Page testPage = new PageImpl<>(testPosts, pageable, testPosts.size()); // Page 생성 + + given(postRepository.findAllByPostTypeFalseAndDeletedAtIsNullAndFinishDateAfterAndStatusOrderByPostIdDesc( + any(LocalDateTime.class), any(PostStatus.class), any(Pageable.class) + )).willReturn(testPage); + + // when + Page res= postService.getContests("createdAt", pageable); + + // then + assertThat(res).isNotNull(); + 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"); + } + + @Test + @DisplayName("공모전 공고 전체 조회 인기순") + void getContestsByScrapCount() { + // given + List testPosts = PostUtilTest.builderMultiplePosts(); + Pageable pageable = PageRequest.of(0, 6); + Page testPage = new PageImpl<>(testPosts, pageable, testPosts.size()); // Page 생성 + + given(postRepository.findAllByPostTypeFalseAndDeletedAtIsNullAndFinishDateAfterAndStatusOrderByScrapCountDescPostIdDesc( + any(LocalDateTime.class), any(PostStatus.class), any(Pageable.class) + )).willReturn(testPage); + + // when + Page res= postService.getContests("scrapCount", pageable); + + // then + assertThat(res).isNotNull(); + 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"); + } + + @Test + @DisplayName("공고 스크랩 기능") + void scrapPost() { + // Given + Post post = PostUtilTest.builderPost(1L); + Member member = MemberUtilTest.buildMemberAndId(1L); + PostScrap postScrap = PostUtilTest.builderPostScrap(post, member, true); + + given(postRepository.findByPostIdAndDeletedAtIsNull(any(Long.class))) + .willReturn(Optional.of(post)); + given(postScrapRepository.findByPostAndMember(any(Post.class), any(Member.class))) + .willReturn(null) + .willReturn(postScrap); + + // When + PostScrapRes res = postService.scrapPost(member, post.getPostId()); + + // Then + PostScrap savedPostScrap = postScrapRepository.findByPostAndMember(post, member); + assertThat(savedPostScrap).isNotNull(); + System.out.println("=====================" + savedPostScrap); + + assertThat(res).isNotNull(); + assertThat(res.postId()).isEqualTo(1L); + assertThat(res.memberId()).isEqualTo(member.getMemberId()); + assertThat(res.ScrapStatus()).isTrue(); + assertThat(savedPostScrap.getPost().getScrapCount()).isEqualTo(1); + + // given + given(postScrapRepository.findByPostAndMember(any(Post.class), any(Member.class))) + .willReturn(savedPostScrap); + + // When + res = postService.scrapPost(member, post.getPostId()); + + // Then + PostScrap canceledPostScrap = postScrapRepository.findByPostAndMember(post, member); + assertThat(canceledPostScrap).isNotNull(); + System.out.println("=====================" + canceledPostScrap); + + assertThat(res.ScrapStatus()).isFalse(); + assertThat(canceledPostScrap.getPost().getScrapCount()).isEqualTo(0); + } + +} diff --git a/src/test/java/com/gongjakso/server/domain/post/service/PostServiceTest.java b/src/test/java/com/gongjakso/server/domain/post/service/PostServiceTest.java index 456ef164..845e1706 100644 --- a/src/test/java/com/gongjakso/server/domain/post/service/PostServiceTest.java +++ b/src/test/java/com/gongjakso/server/domain/post/service/PostServiceTest.java @@ -4,7 +4,9 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +@SpringBootTest class PostServiceTest { @Autowired 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 new file mode 100644 index 00000000..bacd401d --- /dev/null +++ b/src/test/java/com/gongjakso/server/domain/post/util/PostScrapUtilTest.java @@ -0,0 +1,20 @@ +package com.gongjakso.server.domain.post.util; + +import com.gongjakso.server.domain.member.entity.Member; +import com.gongjakso.server.domain.member.util.MemberUtilTest; +import com.gongjakso.server.domain.post.entity.Post; +import com.gongjakso.server.domain.post.entity.PostScrap; + +public class PostScrapUtilTest { + + public static PostScrap builderPostScrap() { + Member member = MemberUtilTest.buildMemberAndId(1L); + Post post = PostUtilTest.builderPost(1L); + return new PostScrap( + 1L, + post, + member, + true + ); + } +} diff --git a/src/test/java/com/gongjakso/server/domain/post/util/PostUtilTest.java b/src/test/java/com/gongjakso/server/domain/post/util/PostUtilTest.java index 873d3e68..51624d2a 100644 --- a/src/test/java/com/gongjakso/server/domain/post/util/PostUtilTest.java +++ b/src/test/java/com/gongjakso/server/domain/post/util/PostUtilTest.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.entity.Post; +import com.gongjakso.server.domain.post.entity.PostScrap; import com.gongjakso.server.domain.post.enumerate.MeetingMethod; import java.time.LocalDateTime; @@ -11,57 +12,66 @@ import java.util.List; public class PostUtilTest { - public static Post builderPost(){ + public static Post builderPost(Long id){ Member member = MemberUtilTest.buildMember(); - - return Post.builder() - .title("Title1") - .member(member) - .contents("Content1") - .contestLink("Link1") - .startDate(LocalDateTime.now()) - .endDate(LocalDateTime.now().plusDays(10)) - .finishDate(LocalDateTime.now().plusDays(16)) - .maxPerson(8L) - .meetingMethod(MeetingMethod.ONLINE) - .meetingCity("서울시") - .meetingTown("마포구") - .questionMethod(true) - .questionLink(".com") - .postType(false) - .categories(new ArrayList<>()) - .stackNames(new ArrayList<>()) - .build(); + return new Post( + 1L, + "Title1", + member, + "Content1", + "Link1", + LocalDateTime.now(), + LocalDateTime.now().plusDays(10), + LocalDateTime.now().plusDays(16), + 8L, + MeetingMethod.ONLINE, + "서울시", + "마포구", + true, + ".com", + false, + new ArrayList<>(), + new ArrayList<>() + ); } - public static Post builderPosts(String title, String contents, String link) { + public static Post builderPosts(Long id, String title, String contents, String link) { Member member = MemberUtilTest.buildMember(); - return Post.builder() - .title(title) - .member(member) - .contents(contents) - .contestLink(link) - .startDate(LocalDateTime.now()) - .endDate(LocalDateTime.now().plusDays(10)) - .finishDate(LocalDateTime.now().plusDays(16)) - .maxPerson(8L) - .meetingMethod(MeetingMethod.ONLINE) - .meetingCity("서울시") - .meetingTown("마포구") - .questionMethod(true) - .questionLink(".com") - .postType(false) - .categories(new ArrayList<>()) - .stackNames(new ArrayList<>()) - .build(); + return new Post( + id, + title, + member, + contents, + link, + LocalDateTime.now(), + LocalDateTime.now().plusDays(10), + LocalDateTime.now().plusDays(16), + 8L, + MeetingMethod.ONLINE, + "서울시", + "마포구", + true, + ".com", + false, + new ArrayList<>(), + new ArrayList<>() + ); } public static List builderMultiplePosts() { return Arrays.asList( - builderPosts("Title1", "Content1", "Link1"), - builderPosts("Title2", "Content2", "Link2"), - builderPosts("Title3", "Content3", "Link3") + builderPosts(1L, "Title1", "Content1", "Link1"), + builderPosts(2L, "Title2", "Content2", "Link2"), + builderPosts(3L, "Title3", "Content3", "Link3") + ); + } + + public static PostScrap builderPostScrap(Post post, Member member, Boolean postStatus){ + return new PostScrap( + post, + member, + postStatus ); } } From 924d08198e45f9f67f7f76b271c9b1aac6faa204 Mon Sep 17 00:00:00 2001 From: yumzen Date: Sat, 18 May 2024 01:12:25 +0900 Subject: [PATCH 05/11] =?UTF-8?q?[#139]=20test:=20=EC=8A=A4=ED=81=AC?= =?UTF-8?q?=EB=9E=A9=20=EA=B8=B0=EB=8A=A5=20Controller=20Test=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/PaginationControllerTest.java | 102 ++++++++++++++++++ .../post/service/PaginationServiceTest.java | 28 +++++ .../domain/post/util/PostScrapUtilTest.java | 9 ++ 3 files changed, 139 insertions(+) create mode 100644 src/test/java/com/gongjakso/server/domain/post/controller/PaginationControllerTest.java 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 new file mode 100644 index 00000000..db7a1d48 --- /dev/null +++ b/src/test/java/com/gongjakso/server/domain/post/controller/PaginationControllerTest.java @@ -0,0 +1,102 @@ +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.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Import; +import org.springframework.data.jpa.mapping.JpaMetamodelMappingContext; +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; + +@WebMvcTest(controllers = PostController.class) +@AutoConfigureMockMvc +@Import(JpaMetamodelMappingContext.class) +@ExtendWith(MockitoExtension.class) +public class PaginationControllerTest { + + @Autowired + private MockMvc mockMvc; + @Autowired + private ObjectMapper objectMapper; //response를 json으로 바꿔주기위해 필요한 의존성 + @MockBean + private PostService postService; + @MockBean + private PostRepository postRepository; + @MockBean + private MemberRepository memberRepository; + @MockBean + private ApplyService applyService; + @MockBean + private TokenProvider tokenProvider; + @MockBean + private RedisClient redisClient; + @MockBean + private JwtFilter jwtFilter; + + private Member member; + private PrincipalDetails principalDetails; + + @BeforeEach + void setUp() { + member = MemberUtilTest.buildMemberAndId(1L); + principalDetails = new PrincipalDetails(member); + UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken( + principalDetails, null, principalDetails.getAuthorities()); + SecurityContextHolder.getContext().setAuthentication(authentication); + } + + @Test + @DisplayName("공고 스크랩 저장 및 취소 기능") + void scrapPost() throws Exception { + //given + Post post = PostUtilTest.builderPost(1L); + PostScrapRes postScrapRes = PostScrapUtilTest.ScrapPostRes(post.getPostId(), member.getMemberId(), true); + given(postService.scrapPost(any(Member.class), anyLong())).willReturn(postScrapRes); + String resJson = objectMapper.writeValueAsString(postScrapRes); + + mockMvc.perform(post("/api/v1/post/{id}", post.getPostId()) + .contentType(MediaType.APPLICATION_JSON) + .principal((Principal) principalDetails)) + .andExpect(status().isOk()) + .andExpect(content().json(resJson)) + .andDo(print()); + + 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 7d32c94c..9e782b98 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 @@ -131,4 +131,32 @@ void scrapPost() { assertThat(canceledPostScrap.getPost().getScrapCount()).isEqualTo(0); } + /* + @Test + @DisplayName("공고 스크랩 조회 기능") + void scrapGet() { + // Given + Post post = PostUtilTest.builderPost(1L); + Member member = MemberUtilTest.buildMemberAndId(1L); + PostScrap postScrap = PostUtilTest.builderPostScrap(post, member, true); + + given(postRepository.findByPostIdAndDeletedAtIsNull(any(Long.class))) + .willReturn(Optional.of(post)); + given(postScrapRepository.findByPostAndMember(any(Post.class), any(Member.class))) + .willReturn(null) + .willReturn(postScrap); + + // When + postService.scrapPost(member, post.getPostId()); + + // Then + PostScrap getPostScrap = postScrapRepository.findByPostAndMember(post, member); + postScrapRepository.save(getPostScrap); + assertThat(getPostScrap).isNotNull(); + System.out.println("!!!!!!!!!!!!!!"+getPostScrap); + + assertThat(getPostScrap.getPostScrapId()).isEqualTo(1L); + assertThat(getPostScrap.getScrapStatus()).isEqualTo(true); + } + */ } 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 bacd401d..a0c28e57 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 @@ -2,6 +2,7 @@ import com.gongjakso.server.domain.member.entity.Member; 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.entity.PostScrap; @@ -17,4 +18,12 @@ public static PostScrap builderPostScrap() { true ); } + + public static PostScrapRes ScrapPostRes(Long postId, Long memberId, boolean scrapStatus){ + return PostScrapRes.builder() + .postId(postId) + .memberId(memberId) + .ScrapStatus(scrapStatus) + .build(); + } } From 412026d67331f7acca462e8ac27a277f5a4b81ee Mon Sep 17 00:00:00 2001 From: yumzen Date: Sat, 18 May 2024 18:07:17 +0900 Subject: [PATCH 06/11] =?UTF-8?q?fix:=20=EC=8A=A4=ED=81=AC=EB=9E=A9=20?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=A7=80=EB=84=A4=EC=9D=B4=EC=85=98=20?= =?UTF-8?q?=EC=A0=84=EC=B2=B4=20=ED=8E=98=EC=9D=B4=EC=A7=80=EC=88=98=20?= =?UTF-8?q?=EB=B0=98=ED=99=98=20=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/domain/post/service/PostService.java | 15 ++++++++++----- .../server/global/exception/ErrorCode.java | 2 +- 2 files changed, 11 insertions(+), 6 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 205c7969..f8daf9dd 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 @@ -395,8 +395,11 @@ public Page getMyScrapProject(Member member, Pageable page){ }) .collect(Collectors.toList()); // 리스트로 수집 - // 필터링된 리스트를 페이지로 반환 - return new PageImpl<>(filteredProjects, pageable, scrapPageList.getTotalElements()); + if (filteredProjects.isEmpty()) { + return Page.empty(pageable); + } else { + return new PageImpl<>(filteredProjects, pageable, filteredProjects.size()); + } } @Transactional @@ -404,7 +407,6 @@ public Page getMyScrapContest(Member member, Pageable page){ Pageable pageable = PageRequest.of(page.getPageNumber(), page.getPageSize()); Page scrapPageList = postScrapRepository.findAllByMemberAndScrapStatusTrueOrderByPostScrapIdDesc(member, pageable); - List filteredContests = scrapPageList.stream() .filter(scrap -> { Post post = scrap.getPost(); @@ -422,7 +424,10 @@ public Page getMyScrapContest(Member member, Pageable page){ }) .collect(Collectors.toList()); // 리스트로 수집 - // 필터링된 리스트를 페이지로 반환 - return new PageImpl<>(filteredContests, pageable, scrapPageList.getTotalElements()); + if(filteredContests.isEmpty()){ + return Page.empty(pageable); + }else{ + return new PageImpl<>(filteredContests, pageable, filteredContests.size()); + } } } \ No newline at end of file diff --git a/src/main/java/com/gongjakso/server/global/exception/ErrorCode.java b/src/main/java/com/gongjakso/server/global/exception/ErrorCode.java index fa4b0b80..9148edb2 100644 --- a/src/main/java/com/gongjakso/server/global/exception/ErrorCode.java +++ b/src/main/java/com/gongjakso/server/global/exception/ErrorCode.java @@ -44,7 +44,7 @@ public enum ErrorCode { //5000: Post Error NOT_POST_EXCEPTION(HttpStatus.BAD_REQUEST,5000,"공고를 더 이상 생성할 수 없습니다"), POST_VALUE_EXCEPTION(HttpStatus.BAD_REQUEST,5001,"올바르지 않은 요청 값입니다."), - NOT_FOUNT_SCRAP_EXCEPTION(HttpStatus.NOT_FOUND,5002,"스크랩 정보가 존재하지 않습니다."), + NOT_FOUND_SCRAP_EXCEPTION(HttpStatus.NOT_FOUND,5002,"스크랩 정보가 존재하지 않습니다."), ALREADY_FINISH_EXCEPTION(HttpStatus.BAD_REQUEST, 5003, "이미 모집 기간이 마감된 공고입니다."), ILLEGAL_POST_EXCEPTION(HttpStatus.BAD_REQUEST, 5004, "파트별 인원수가 전체 인원수와 일치하지 않습니다."); From 09be46ad36d7e784f12d50ed9bf6e70e27d80223 Mon Sep 17 00:00:00 2001 From: yumzen Date: Sun, 19 May 2024 20:13:45 +0900 Subject: [PATCH 07/11] =?UTF-8?q?feat:=20PostScrapDomainTest=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/PaginationControllerTest.java | 2 +- .../domain/post/domain/ScrapDomainTest.java | 55 +++++++++++++++++++ .../post/service/PaginationServiceTest.java | 3 +- .../domain/post/util/PostScrapUtilTest.java | 15 +++-- .../server/domain/post/util/PostUtilTest.java | 3 +- 5 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 src/test/java/com/gongjakso/server/domain/post/domain/ScrapDomainTest.java 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 db7a1d48..1bc96eab 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 @@ -83,7 +83,7 @@ void setUp() { @DisplayName("공고 스크랩 저장 및 취소 기능") void scrapPost() throws Exception { //given - Post post = PostUtilTest.builderPost(1L); + Post post = PostUtilTest.builderPost(1L, member); PostScrapRes postScrapRes = PostScrapUtilTest.ScrapPostRes(post.getPostId(), member.getMemberId(), true); given(postService.scrapPost(any(Member.class), anyLong())).willReturn(postScrapRes); String resJson = objectMapper.writeValueAsString(postScrapRes); diff --git a/src/test/java/com/gongjakso/server/domain/post/domain/ScrapDomainTest.java b/src/test/java/com/gongjakso/server/domain/post/domain/ScrapDomainTest.java new file mode 100644 index 00000000..6814e51a --- /dev/null +++ b/src/test/java/com/gongjakso/server/domain/post/domain/ScrapDomainTest.java @@ -0,0 +1,55 @@ +package com.gongjakso.server.domain.post.domain; + + +import com.gongjakso.server.domain.member.entity.Member; +import com.gongjakso.server.domain.member.util.MemberUtilTest; +import com.gongjakso.server.domain.post.entity.Post; +import com.gongjakso.server.domain.post.entity.PostScrap; +import com.gongjakso.server.domain.post.util.PostScrapUtilTest; +import com.gongjakso.server.domain.post.util.PostUtilTest; +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 static org.assertj.core.api.Assertions.assertThat; + +@DisplayName("PostScrap 도메인 테스트") +public class ScrapDomainTest{ + + private Post post; + private Member member; + + @BeforeEach + public void setUp() { + member = MemberUtilTest.buildMemberAndId(1L); + post = PostUtilTest.builderPost(1L, member); + } + + @Nested + @DisplayName("Builder테스트") + class BuilderTests { + + @Test + @DisplayName("Id없이 빌드 되어야 함") + public void testPostScrapBuilder() { + PostScrap postScrap = PostScrapUtilTest.builderPostScrap(post, post.getMember()); + System.out.println(postScrap.getMember()); + System.out.println(member); + assertThat(postScrap.getPost()).isEqualTo(post); + assertThat(postScrap.getMember()).isEqualTo(member); + assertThat(postScrap.getScrapStatus()).isTrue(); + } + + @Test + @DisplayName("Id와 함께 빌드 되어야 함") + public void testPostScrapBuilderWithId() { + Long postScrapId = 1L; + PostScrap postScrap = PostScrapUtilTest.builderPostScrapWithPostAndMember(postScrapId, post, post.getMember()); + assertThat(postScrap.getPostScrapId()).isEqualTo(postScrapId); + assertThat(postScrap.getPost()).isEqualTo(post); + assertThat(postScrap.getMember()).isEqualTo(member); + assertThat(postScrap.getScrapStatus()).isTrue(); + } + } +} \ No newline at end of file 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 9e782b98..418e0458 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 @@ -91,8 +91,9 @@ void getContestsByScrapCount() { @DisplayName("공고 스크랩 기능") void scrapPost() { // Given - Post post = PostUtilTest.builderPost(1L); + Member member = MemberUtilTest.buildMemberAndId(1L); + Post post = PostUtilTest.builderPost(1L, member); PostScrap postScrap = PostUtilTest.builderPostScrap(post, member, true); given(postRepository.findByPostIdAndDeletedAtIsNull(any(Long.class))) 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 a0c28e57..a07d3eb4 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 @@ -1,18 +1,23 @@ package com.gongjakso.server.domain.post.util; import com.gongjakso.server.domain.member.entity.Member; -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.entity.PostScrap; public class PostScrapUtilTest { + public static PostScrap builderPostScrap(Post post, Member member) { + return new PostScrap( + post, + member, + true + ); + } + - public static PostScrap builderPostScrap() { - Member member = MemberUtilTest.buildMemberAndId(1L); - Post post = PostUtilTest.builderPost(1L); + public static PostScrap builderPostScrapWithPostAndMember(Long PostScrapId, Post post, Member member) { return new PostScrap( - 1L, + PostScrapId, post, member, true diff --git a/src/test/java/com/gongjakso/server/domain/post/util/PostUtilTest.java b/src/test/java/com/gongjakso/server/domain/post/util/PostUtilTest.java index 51624d2a..88d90cda 100644 --- a/src/test/java/com/gongjakso/server/domain/post/util/PostUtilTest.java +++ b/src/test/java/com/gongjakso/server/domain/post/util/PostUtilTest.java @@ -12,8 +12,7 @@ import java.util.List; public class PostUtilTest { - public static Post builderPost(Long id){ - Member member = MemberUtilTest.buildMember(); + public static Post builderPost(Long id, Member member){ return new Post( 1L, "Title1", From 1365b3e3ed84e623fa18f4b22d5f3efbfe9dccc6 Mon Sep 17 00:00:00 2001 From: yumzen Date: Mon, 20 May 2024 00:59:23 +0900 Subject: [PATCH 08/11] =?UTF-8?q?fix:=20=EC=8A=A4=ED=81=AC=EB=9E=A9=20?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=A7=80=EB=84=A4=EC=9D=B4=EC=85=98=20JPA=20?= =?UTF-8?q?=EB=A9=94=EC=86=8C=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/repository/PostScrapRepository.java | 5 ++++- .../domain/post/service/PostService.java | 20 ++----------------- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/gongjakso/server/domain/post/repository/PostScrapRepository.java b/src/main/java/com/gongjakso/server/domain/post/repository/PostScrapRepository.java index 0cddf14b..aae0932f 100644 --- a/src/main/java/com/gongjakso/server/domain/post/repository/PostScrapRepository.java +++ b/src/main/java/com/gongjakso/server/domain/post/repository/PostScrapRepository.java @@ -13,5 +13,8 @@ public interface PostScrapRepository extends JpaRepository { PostScrap findByPostAndMember(Post post, Member member); List findByMemberAndScrapStatus(Member member,boolean scrapStatus); - Page findAllByMemberAndScrapStatusTrueOrderByPostScrapIdDesc(Member member, Pageable pageable); + Page findAllByMemberAndPostPostTypeTrueAndPostDeletedAtIsNullAndScrapStatusTrueOrderByPostScrapIdDesc(Member member, Pageable pageable); + + Page findAllByMemberAndPostPostTypeFalseAndPostDeletedAtIsNullAndScrapStatusTrueOrderByPostScrapIdDesc(Member member, Pageable pageable); + } 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 f8daf9dd..6db16505 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 @@ -376,17 +376,9 @@ public GetPostRelation checkPostRelation(Member member, Long postId) { public Page getMyScrapProject(Member member, Pageable page){ Pageable pageable = PageRequest.of(page.getPageNumber(), page.getPageSize()); - Page scrapPageList = postScrapRepository.findAllByMemberAndScrapStatusTrueOrderByPostScrapIdDesc(member, pageable); + Page scrapPageList = postScrapRepository.findAllByMemberAndPostPostTypeTrueAndPostDeletedAtIsNullAndScrapStatusTrueOrderByPostScrapIdDesc(member, pageable); List filteredProjects = scrapPageList.stream() - .filter(scrap -> { - Post post = scrap.getPost(); - - //유효한 post만 남기기 - return post != null && - post.isPostType() == true && - post.getDeletedAt() == null; - }) .map(scrap -> { Post post = scrap.getPost(); post.getCategories().size(); @@ -406,16 +398,8 @@ public Page getMyScrapProject(Member member, Pageable page){ public Page getMyScrapContest(Member member, Pageable page){ Pageable pageable = PageRequest.of(page.getPageNumber(), page.getPageSize()); - Page scrapPageList = postScrapRepository.findAllByMemberAndScrapStatusTrueOrderByPostScrapIdDesc(member, pageable); + Page scrapPageList = postScrapRepository.findAllByMemberAndPostPostTypeFalseAndPostDeletedAtIsNullAndScrapStatusTrueOrderByPostScrapIdDesc(member, pageable); List filteredContests = scrapPageList.stream() - .filter(scrap -> { - Post post = scrap.getPost(); - - //유효한 post만 남기기 - return post != null && - post.isPostType() == false && - post.getDeletedAt() == null; - }) .map(scrap -> { Post post = scrap.getPost(); post.getCategories().size(); From d0227dfb4c7786102794463cb1913426c0af53fc Mon Sep 17 00:00:00 2001 From: yumzen Date: Mon, 20 May 2024 00:08:27 +0900 Subject: [PATCH 09/11] =?UTF-8?q?=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/PaginationControllerTest.java | 22 +- .../post/service/PaginationServiceTest.java | 277 ++++++++++-------- .../domain/post/service/PostServiceTest.java | 4 +- .../domain/post/util/PostScrapUtilTest.java | 9 + 4 files changed, 181 insertions(+), 131 deletions(-) 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 1bc96eab..52e88e0f 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 @@ -20,12 +20,8 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.context.annotation.Import; -import org.springframework.data.jpa.mapping.JpaMetamodelMappingContext; import org.springframework.http.MediaType; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.context.SecurityContextHolder; @@ -42,28 +38,32 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -@WebMvcTest(controllers = PostController.class) @AutoConfigureMockMvc -@Import(JpaMetamodelMappingContext.class) @ExtendWith(MockitoExtension.class) public class PaginationControllerTest { - @Autowired - private MockMvc mockMvc; - @Autowired - private ObjectMapper objectMapper; //response를 json으로 바꿔주기위해 필요한 의존성 + MockMvc mockMvc; + + private ObjectMapper objectMapper = new ObjectMapper(); + @MockBean private PostService postService; + @MockBean private PostRepository postRepository; + @MockBean private MemberRepository memberRepository; + @MockBean private ApplyService applyService; + @MockBean private TokenProvider tokenProvider; + @MockBean private RedisClient redisClient; + @MockBean private JwtFilter jwtFilter; @@ -96,7 +96,5 @@ void scrapPost() throws Exception { .andDo(print()); 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 418e0458..04c6bef4 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 @@ -9,8 +9,10 @@ import com.gongjakso.server.domain.post.enumerate.PostStatus; import com.gongjakso.server.domain.post.repository.PostRepository; import com.gongjakso.server.domain.post.repository.PostScrapRepository; +import com.gongjakso.server.domain.post.util.PostScrapUtilTest; import com.gongjakso.server.domain.post.util.PostUtilTest; 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; @@ -29,6 +31,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; +@DisplayName("페이지네이션 및 스크랩 공고 Service 테스트") @ExtendWith(MockitoExtension.class) public class PaginationServiceTest { @InjectMocks @@ -40,124 +43,164 @@ public class PaginationServiceTest { @Mock PostScrapRepository postScrapRepository; - private PostScrap postScrap; - @Test - @DisplayName("공모전 공고 전체 조회 최신순") - void getContestsByCreatedAt() { - // given - List testPosts = PostUtilTest.builderMultiplePosts(); - Pageable pageable = PageRequest.of(0, 6); - Page testPage = new PageImpl<>(testPosts, pageable, testPosts.size()); // Page 생성 - - given(postRepository.findAllByPostTypeFalseAndDeletedAtIsNullAndFinishDateAfterAndStatusOrderByPostIdDesc( - any(LocalDateTime.class), any(PostStatus.class), any(Pageable.class) - )).willReturn(testPage); - - // when - Page res= postService.getContests("createdAt", pageable); - - // then - assertThat(res).isNotNull(); - 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"); + @Nested + @DisplayName("공모전 공고 페이지네이션") + class ContestTests{ + + @Nested + @DisplayName("공모전 공고 전체 조회") + class ContestTestsAll{ + + @Test + @DisplayName("공모전 공고 전체 조회 최신순") + void getContestsByCreatedAt() { + // given + List testPosts = PostUtilTest.builderMultiplePosts(); + Pageable pageable = PageRequest.of(0, 6); + Page testPage = new PageImpl<>(testPosts, pageable, testPosts.size()); // Page 생성 + + given(postRepository.findAllByPostTypeFalseAndDeletedAtIsNullAndFinishDateAfterAndStatusOrderByPostIdDesc( + any(LocalDateTime.class), any(PostStatus.class), any(Pageable.class) + )).willReturn(testPage); + + // when + Page res = postService.getContests("createdAt", pageable); + + // then + assertThat(res).isNotNull(); + 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"); + } + + @Test + @DisplayName("공모전 공고 전체 조회 인기순") + void getContestsByScrapCount() { + // given + List testPosts = PostUtilTest.builderMultiplePosts(); + Pageable pageable = PageRequest.of(0, 6); + Page testPage = new PageImpl<>(testPosts, pageable, testPosts.size()); // Page 생성 + + given(postRepository.findAllByPostTypeFalseAndDeletedAtIsNullAndFinishDateAfterAndStatusOrderByScrapCountDescPostIdDesc( + any(LocalDateTime.class), any(PostStatus.class), any(Pageable.class) + )).willReturn(testPage); + + // when + Page res = postService.getContests("scrapCount", pageable); + + // then + assertThat(res).isNotNull(); + 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"); + } + } } - @Test - @DisplayName("공모전 공고 전체 조회 인기순") - void getContestsByScrapCount() { - // given - List testPosts = PostUtilTest.builderMultiplePosts(); - Pageable pageable = PageRequest.of(0, 6); - Page testPage = new PageImpl<>(testPosts, pageable, testPosts.size()); // Page 생성 - - given(postRepository.findAllByPostTypeFalseAndDeletedAtIsNullAndFinishDateAfterAndStatusOrderByScrapCountDescPostIdDesc( - any(LocalDateTime.class), any(PostStatus.class), any(Pageable.class) - )).willReturn(testPage); - - // when - Page res= postService.getContests("scrapCount", pageable); - - // then - assertThat(res).isNotNull(); - 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"); + @Nested + @DisplayName("공고 스크랩 테스트") + class scrapTests { + + @Test + @DisplayName("공고 스크랩 저장 및 취소 기능") + void scrapPost() { + // Given + Member member = MemberUtilTest.buildMemberAndId(1L); + Post post = PostUtilTest.builderPost(1L, member); + PostScrap postScrap = PostUtilTest.builderPostScrap(post, member, true); + + given(postRepository.findByPostIdAndDeletedAtIsNull(any(Long.class))) + .willReturn(Optional.of(post)); + given(postScrapRepository.findByPostAndMember(any(Post.class), any(Member.class))) + .willReturn(null) + .willReturn(postScrap); + + // When + PostScrapRes res = postService.scrapPost(member, post.getPostId()); + + // Then + PostScrap savedPostScrap = postScrapRepository.findByPostAndMember(post, member); + assertThat(savedPostScrap).isNotNull(); + + assertThat(res).isNotNull(); + assertThat(res.postId()).isEqualTo(1L); + assertThat(res.memberId()).isEqualTo(member.getMemberId()); + assertThat(res.ScrapStatus()).isTrue(); + assertThat(savedPostScrap.getPost().getScrapCount()).isEqualTo(1); + + // given + given(postScrapRepository.findByPostAndMember(any(Post.class), any(Member.class))) + .willReturn(savedPostScrap); + + // When + res = postService.scrapPost(member, post.getPostId()); + + // Then + PostScrap canceledPostScrap = postScrapRepository.findByPostAndMember(post, member); + assertThat(canceledPostScrap).isNotNull(); + + assertThat(res.ScrapStatus()).isFalse(); + assertThat(canceledPostScrap.getPost().getScrapCount()).isEqualTo(0); + } + + @Test + @DisplayName("공고 스크랩 조회 기능") + void scrapGet() { + // Given + Member member = MemberUtilTest.buildMemberAndId(1L); + Post post = PostUtilTest.builderPost(1L, member); + PostScrap postScrap = PostUtilTest.builderPostScrap(post, member, true); + + given(postRepository.findByPostIdAndDeletedAtIsNull(any(Long.class))) + .willReturn(Optional.of(post)); + given(postScrapRepository.findByPostAndMember(any(Post.class), any(Member.class))) + .willReturn(null) + .willReturn(postScrap); + + // When + PostScrapRes res = postService.scrapPost(member, post.getPostId()); + PostScrap getPostScrap = postScrapRepository.findByPostAndMember(post, member); + + // Then + assertThat(getPostScrap).isNotNull(); + assertThat(res).isNotNull(); + assertThat(res.postId()).isEqualTo(1L); + assertThat(res.memberId()).isEqualTo(member.getMemberId()); + assertThat(res.ScrapStatus()).isTrue(); + assertThat(getPostScrap.getPost().getScrapCount()).isEqualTo(1); + } + + @Test + @DisplayName("내가 스크랩한 프로젝트 목록 조회 및 페이지네이션 기능 테스트") + void getMyScrapProjectTest() { + // 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(postRepository.findAllByPostTypeFalseAndDeletedAtIsNullAndFinishDateAfterAndStatusOrderByPostIdDesc( + any(LocalDateTime.class), any(PostStatus.class), any(Pageable.class) + )).willReturn(testPage); + + // when + Page res = postService.getContests("createdAt", pageable); + + // then + assertThat(res).isNotNull(); + 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"); + } + + @Test + @DisplayName("내가 스크랩한 공모전 목록 조회 및 페이지네이션 기능 테스트") + void getMyScrapContestTest() { + + } } - - @Test - @DisplayName("공고 스크랩 기능") - void scrapPost() { - // Given - - Member member = MemberUtilTest.buildMemberAndId(1L); - Post post = PostUtilTest.builderPost(1L, member); - PostScrap postScrap = PostUtilTest.builderPostScrap(post, member, true); - - given(postRepository.findByPostIdAndDeletedAtIsNull(any(Long.class))) - .willReturn(Optional.of(post)); - given(postScrapRepository.findByPostAndMember(any(Post.class), any(Member.class))) - .willReturn(null) - .willReturn(postScrap); - - // When - PostScrapRes res = postService.scrapPost(member, post.getPostId()); - - // Then - PostScrap savedPostScrap = postScrapRepository.findByPostAndMember(post, member); - assertThat(savedPostScrap).isNotNull(); - System.out.println("=====================" + savedPostScrap); - - assertThat(res).isNotNull(); - assertThat(res.postId()).isEqualTo(1L); - assertThat(res.memberId()).isEqualTo(member.getMemberId()); - assertThat(res.ScrapStatus()).isTrue(); - assertThat(savedPostScrap.getPost().getScrapCount()).isEqualTo(1); - - // given - given(postScrapRepository.findByPostAndMember(any(Post.class), any(Member.class))) - .willReturn(savedPostScrap); - - // When - res = postService.scrapPost(member, post.getPostId()); - - // Then - PostScrap canceledPostScrap = postScrapRepository.findByPostAndMember(post, member); - assertThat(canceledPostScrap).isNotNull(); - System.out.println("=====================" + canceledPostScrap); - - assertThat(res.ScrapStatus()).isFalse(); - assertThat(canceledPostScrap.getPost().getScrapCount()).isEqualTo(0); - } - - /* - @Test - @DisplayName("공고 스크랩 조회 기능") - void scrapGet() { - // Given - Post post = PostUtilTest.builderPost(1L); - Member member = MemberUtilTest.buildMemberAndId(1L); - PostScrap postScrap = PostUtilTest.builderPostScrap(post, member, true); - - given(postRepository.findByPostIdAndDeletedAtIsNull(any(Long.class))) - .willReturn(Optional.of(post)); - given(postScrapRepository.findByPostAndMember(any(Post.class), any(Member.class))) - .willReturn(null) - .willReturn(postScrap); - - // When - postService.scrapPost(member, post.getPostId()); - - // Then - PostScrap getPostScrap = postScrapRepository.findByPostAndMember(post, member); - postScrapRepository.save(getPostScrap); - assertThat(getPostScrap).isNotNull(); - System.out.println("!!!!!!!!!!!!!!"+getPostScrap); - - assertThat(getPostScrap.getPostScrapId()).isEqualTo(1L); - assertThat(getPostScrap.getScrapStatus()).isEqualTo(true); - } - */ } diff --git a/src/test/java/com/gongjakso/server/domain/post/service/PostServiceTest.java b/src/test/java/com/gongjakso/server/domain/post/service/PostServiceTest.java index 845e1706..328fdb30 100644 --- a/src/test/java/com/gongjakso/server/domain/post/service/PostServiceTest.java +++ b/src/test/java/com/gongjakso/server/domain/post/service/PostServiceTest.java @@ -3,13 +3,13 @@ import com.gongjakso.server.domain.post.entity.Post; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; @SpringBootTest class PostServiceTest { - @Autowired + @MockBean PostService postService; Post post = null; 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 a07d3eb4..b5e48244 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 @@ -5,6 +5,9 @@ import com.gongjakso.server.domain.post.entity.Post; import com.gongjakso.server.domain.post.entity.PostScrap; +import java.util.List; +import java.util.stream.Collectors; + public class PostScrapUtilTest { public static PostScrap builderPostScrap(Post post, Member member) { return new PostScrap( @@ -14,6 +17,12 @@ public static PostScrap builderPostScrap(Post post, Member member) { ); } + public static List builderMultiplePostScraps(List posts, Member member){ + return posts.stream() + .map(post -> new PostScrap(post, member, true)) + .collect(Collectors.toList()); + } + public static PostScrap builderPostScrapWithPostAndMember(Long PostScrapId, Post post, Member member) { return new PostScrap( From f85561887276ec6946bf61f6613ced14cd8e0958 Mon Sep 17 00:00:00 2001 From: yumzen Date: Wed, 22 May 2024 20:30:30 +0900 Subject: [PATCH 10/11] =?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(); } } From 6af4ca9b05082bf8fe86146a6f62fd6795faeaa2 Mon Sep 17 00:00:00 2001 From: yumzen Date: Thu, 23 May 2024 16:12:38 +0900 Subject: [PATCH 11/11] =?UTF-8?q?fix:=20=ED=8E=98=EC=9D=B4=EC=A7=80?= =?UTF-8?q?=EB=84=A4=EC=9D=B4=EC=85=98=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/service/PaginationServiceTest.java | 39 +++++++++++-------- .../server/domain/post/util/PostUtilTest.java | 16 +++++--- 2 files changed, 33 insertions(+), 22 deletions(-) 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 4fb748bc..704fb786 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 @@ -27,6 +27,7 @@ import java.time.LocalDateTime; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; @@ -56,7 +57,7 @@ class ContestTestsAll{ @DisplayName("공모전 공고 전체 조회 최신순") void getContestsByCreatedAt() { // given - List testPosts = PostUtilTest.builderMultiplePosts(); + List testPosts = PostUtilTest.builderMultiplePosts(false); Pageable pageable = PageRequest.of(0, 6); Page testPage = new PageImpl<>(testPosts, pageable, testPosts.size()); // Page 생성 @@ -69,9 +70,10 @@ void getContestsByCreatedAt() { // then assertThat(res).isNotNull(); - assertThat(res.getTotalElements()).isEqualTo(3); + assertThat(res.getTotalElements()).isEqualTo(7); + assertThat(res.getTotalPages()).isEqualTo(2); assertThat(res.getContent().get(0).title()).isEqualTo("Title1"); - assertThat(res.getContent().get(1).title()).isEqualTo("Title2"); + assertThat(res.getContent().get(1).title()).isEqualTo("제목5"); assertThat(res.getContent().get(2).title()).isEqualTo("Title3"); } @@ -79,7 +81,7 @@ void getContestsByCreatedAt() { @DisplayName("공모전 공고 전체 조회 인기순") void getContestsByScrapCount() { // given - List testPosts = PostUtilTest.builderMultiplePosts(); + List testPosts = PostUtilTest.builderMultiplePosts(false); Pageable pageable = PageRequest.of(0, 6); Page testPage = new PageImpl<>(testPosts, pageable, testPosts.size()); // Page 생성 @@ -92,9 +94,10 @@ void getContestsByScrapCount() { // then assertThat(res).isNotNull(); - assertThat(res.getTotalElements()).isEqualTo(3); + assertThat(res.getTotalElements()).isEqualTo(7); + assertThat(res.getTotalPages()).isEqualTo(2); assertThat(res.getContent().get(0).title()).isEqualTo("Title1"); - assertThat(res.getContent().get(1).title()).isEqualTo("Title2"); + assertThat(res.getContent().get(1).title()).isEqualTo("제목5"); assertThat(res.getContent().get(2).title()).isEqualTo("Title3"); } } @@ -178,8 +181,10 @@ void scrapGet() { void getMyScrapProjectTest() { // given Member member = MemberUtilTest.buildMemberAndId(1L); - List testPosts = PostUtilTest.builderMultiplePosts(); - List testPostScraps= PostScrapUtilTest.builderMultiplePostScraps(testPosts, member); + List testPosts = PostUtilTest.builderMultiplePosts(false); + List testPostScraps= PostScrapUtilTest.builderMultiplePostScraps(testPosts, member).stream() + .filter(PostScrap::getScrapStatus) + .collect(Collectors.toList()); Pageable pageable = PageRequest.of(0, 6); Page testPage = new PageImpl<>(testPostScraps, pageable, testPostScraps.size()); // Page 생성 @@ -193,10 +198,10 @@ void getMyScrapProjectTest() { // then assertThat(res).isNotNull(); - assertThat(res.getTotalPages()).isEqualTo(1); - assertThat(res.getTotalElements()).isEqualTo(3); + assertThat(res.getTotalElements()).isEqualTo(7); + assertThat(res.getTotalPages()).isEqualTo(2); assertThat(res.getContent().get(0).title()).isEqualTo("Title1"); - assertThat(res.getContent().get(1).title()).isEqualTo("Title2"); + assertThat(res.getContent().get(1).title()).isEqualTo("제목5"); assertThat(res.getContent().get(2).title()).isEqualTo("Title3"); } @@ -205,8 +210,10 @@ void getMyScrapProjectTest() { void getMyScrapContestTest() { // given Member member = MemberUtilTest.buildMemberAndId(1L); - List testPosts = PostUtilTest.builderMultiplePosts(); - List testPostScraps= PostScrapUtilTest.builderMultiplePostScraps(testPosts, member); + List testPosts = PostUtilTest.builderMultiplePosts(false); + List testPostScraps= PostScrapUtilTest.builderMultiplePostScraps(testPosts, member).stream() + .filter(PostScrap::getScrapStatus) + .collect(Collectors.toList()); Pageable pageable = PageRequest.of(0, 6); Page testPage = new PageImpl<>(testPostScraps, pageable, testPostScraps.size()); // Page 생성 @@ -220,10 +227,10 @@ void getMyScrapContestTest() { // then assertThat(res).isNotNull(); - assertThat(res.getTotalPages()).isEqualTo(1); - assertThat(res.getTotalElements()).isEqualTo(3); + assertThat(res.getTotalElements()).isEqualTo(7); + assertThat(res.getTotalPages()).isEqualTo(2); assertThat(res.getContent().get(0).title()).isEqualTo("Title1"); - assertThat(res.getContent().get(1).title()).isEqualTo("Title2"); + assertThat(res.getContent().get(1).title()).isEqualTo("제목5"); assertThat(res.getContent().get(2).title()).isEqualTo("Title3"); } } diff --git a/src/test/java/com/gongjakso/server/domain/post/util/PostUtilTest.java b/src/test/java/com/gongjakso/server/domain/post/util/PostUtilTest.java index 21455479..52b19863 100644 --- a/src/test/java/com/gongjakso/server/domain/post/util/PostUtilTest.java +++ b/src/test/java/com/gongjakso/server/domain/post/util/PostUtilTest.java @@ -34,7 +34,7 @@ public static Post builderPost(Long id, Member member){ ); } - public static Post builderPosts(Long id, String title, String contents, String link) { + public static Post builderPosts(Long id, String title, String contents, String link, Boolean postType) { Member member = MemberUtilTest.buildMember(); return new Post( @@ -52,17 +52,21 @@ public static Post builderPosts(Long id, String title, String contents, String l "마포구", true, ".com", - false, + postType, new ArrayList<>(), new ArrayList<>() ); } - public static List builderMultiplePosts() { + public static List builderMultiplePosts(Boolean postType) { return Arrays.asList( - builderPosts(1L, "Title1", "Content1", "Link1"), - builderPosts(2L, "Title2", "Content2", "Link2"), - builderPosts(3L, "Title3", "Content3", "Link3") + builderPosts(1L, "Title1", "Content1", "Link1", postType), + builderPosts(5L, "제목5", "Content5", "Link5", postType), + builderPosts(3L, "Title3", "Content3", "Link3", postType), + builderPosts(4L, "제목4", "Content4", "Link4", postType), + builderPosts(2L, "Title2", "Content2", "Link2", postType), + builderPosts(6L, "제목6", "Content6", "Link6", postType), + builderPosts(7L, "제목7", "Content7", "Link7", postType) ); }