diff --git a/src/main/java/com/gongjakso/server/domain/apply/controller/ApplyController.java b/src/main/java/com/gongjakso/server/domain/apply/controller/ApplyController.java index 5b1ba9b4..3127eb39 100644 --- a/src/main/java/com/gongjakso/server/domain/apply/controller/ApplyController.java +++ b/src/main/java/com/gongjakso/server/domain/apply/controller/ApplyController.java @@ -15,8 +15,6 @@ import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; -import java.util.List; - @RequiredArgsConstructor @RestController @RequestMapping("/api/v1/apply") diff --git a/src/main/java/com/gongjakso/server/domain/apply/service/ApplyService.java b/src/main/java/com/gongjakso/server/domain/apply/service/ApplyService.java index b134fa70..0394cae1 100644 --- a/src/main/java/com/gongjakso/server/domain/apply/service/ApplyService.java +++ b/src/main/java/com/gongjakso/server/domain/apply/service/ApplyService.java @@ -18,7 +18,6 @@ import com.gongjakso.server.domain.post.repository.StackNameRepository; import com.gongjakso.server.global.exception.ApplicationException; import com.gongjakso.server.global.exception.ErrorCode; -// import com.gongjakso.server.global.util.email.EmailClient; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.*; import org.springframework.stereotype.Service; 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 411e1e1f..c2092807 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 @@ -145,4 +145,31 @@ public void updatePostView(Long postView) { public void updateStatus(PostStatus status) { this.status = status; } + + @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/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 eb064670..76b2e428 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 @@ -18,7 +18,10 @@ import com.gongjakso.server.global.security.PrincipalDetails; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.data.domain.*; +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 org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -377,17 +380,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() && - post.getDeletedAt() == null; - }) + List myScrapProjects = scrapPageList.stream() .map(scrap -> { Post post = scrap.getPost(); post.getCategories().size(); @@ -397,24 +392,16 @@ public Page getMyScrapProject(Member member, Pageable page){ .collect(Collectors.toList()); // 리스트로 수집 // 필터링된 리스트를 페이지로 반환 - return new PageImpl<>(filteredProjects, pageable, scrapPageList.getTotalElements()); + return new PageImpl<>( myScrapProjects, pageable, myScrapProjects.size()); } @Transactional 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() && - post.getDeletedAt() == null; - }) + List myScrapContests = scrapPageList.stream() .map(scrap -> { Post post = scrap.getPost(); post.getCategories().size(); @@ -424,9 +411,10 @@ public Page getMyScrapContest(Member member, Pageable page){ .collect(Collectors.toList()); // 리스트로 수집 // 필터링된 리스트를 페이지로 반환 - return new PageImpl<>(filteredContests, pageable, scrapPageList.getTotalElements()); + return new PageImpl<>(myScrapContests, pageable, myScrapContests.size()); } + @Transactional public PostSimpleRes completePost(Member member, Long postId) { // Validation: Post 논리적 삭제 및 사용자의 권한 여부 확인 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, "파트별 인원수가 전체 인원수와 일치하지 않습니다."); 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/controller/PaginationControllerTest.java b/src/test/java/com/gongjakso/server/domain/post/controller/PaginationControllerTest.java new file mode 100644 index 00000000..19406783 --- /dev/null +++ b/src/test/java/com/gongjakso/server/domain/post/controller/PaginationControllerTest.java @@ -0,0 +1,68 @@ +package com.gongjakso.server.domain.post.controller; + +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; + +@AutoConfigureMockMvc +@ExtendWith(MockitoExtension.class) +public class PaginationControllerTest { +/* + 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; + + 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, member); + 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/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 new file mode 100644 index 00000000..704fb786 --- /dev/null +++ b/src/test/java/com/gongjakso/server/domain/post/service/PaginationServiceTest.java @@ -0,0 +1,237 @@ +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.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; +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; +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 java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; + +@DisplayName("페이지네이션 및 스크랩 공고 Service 테스트") +@ExtendWith(MockitoExtension.class) +public class PaginationServiceTest { + @InjectMocks + PostService postService; + + @Mock + PostRepository postRepository; + + @Mock + PostScrapRepository postScrapRepository; + + @Nested + @DisplayName("공모전 공고 페이지네이션") + class ContestTests{ + + @Nested + @DisplayName("공모전 공고 전체 조회") + class ContestTestsAll{ + + @Test + @DisplayName("공모전 공고 전체 조회 최신순") + void getContestsByCreatedAt() { + // given + List testPosts = PostUtilTest.builderMultiplePosts(false); + 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(7); + assertThat(res.getTotalPages()).isEqualTo(2); + assertThat(res.getContent().get(0).title()).isEqualTo("Title1"); + assertThat(res.getContent().get(1).title()).isEqualTo("제목5"); + assertThat(res.getContent().get(2).title()).isEqualTo("Title3"); + } + + @Test + @DisplayName("공모전 공고 전체 조회 인기순") + void getContestsByScrapCount() { + // given + List testPosts = PostUtilTest.builderMultiplePosts(false); + 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(7); + assertThat(res.getTotalPages()).isEqualTo(2); + assertThat(res.getContent().get(0).title()).isEqualTo("Title1"); + assertThat(res.getContent().get(1).title()).isEqualTo("제목5"); + 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(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 생성 + + + given(postScrapRepository.findAllByMemberAndPostPostTypeTrueAndPostDeletedAtIsNullAndScrapStatusTrueOrderByPostScrapIdDesc( + any(Member.class), any(Pageable.class) + )).willReturn(testPage); + + // when + Page res = postService.getMyScrapProject(member, pageable); + + // then + assertThat(res).isNotNull(); + 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("제목5"); + assertThat(res.getContent().get(2).title()).isEqualTo("Title3"); + } + + @Test + @DisplayName("내가 스크랩한 공모전 목록 조회 및 페이지네이션 기능 테스트") + void getMyScrapContestTest() { + // given + Member member = MemberUtilTest.buildMemberAndId(1L); + 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 생성 + + + given(postScrapRepository.findAllByMemberAndPostPostTypeFalseAndPostDeletedAtIsNullAndScrapStatusTrueOrderByPostScrapIdDesc( + any(Member.class), any(Pageable.class) + )).willReturn(testPage); + + // when + Page res = postService.getMyScrapContest(member, pageable); + + // then + assertThat(res).isNotNull(); + 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("제목5"); + assertThat(res.getContent().get(2).title()).isEqualTo("Title3"); + } + } +} 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 d7591597..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 @@ -1,32 +1,26 @@ package com.gongjakso.server.domain.post.service; import com.gongjakso.server.domain.post.entity.Post; -import com.gongjakso.server.domain.post.repository.PostRepository; -import com.gongjakso.server.domain.post.util.PostUtilTest; import org.junit.jupiter.api.BeforeEach; 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.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; -@ExtendWith(MockitoExtension.class) +@SpringBootTest class PostServiceTest { - @InjectMocks - private PostService postService; - - @Mock - private PostRepository postRepository; + @MockBean + PostService postService; + Post post = null; @BeforeEach void beforeEach() { - Post post = PostUtilTest.buildPost(); + } @Test void create() { - + } @Test 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..87912db1 --- /dev/null +++ b/src/test/java/com/gongjakso/server/domain/post/util/PostScrapUtilTest.java @@ -0,0 +1,43 @@ +package com.gongjakso.server.domain.post.util; + +import com.gongjakso.server.domain.member.entity.Member; +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 java.util.List; +import java.util.stream.Collectors; + +public class PostScrapUtilTest { + public static PostScrap builderPostScrap(Post post, Member member) { + return new PostScrap( + post, + member, + true + ); + } + + 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( + PostScrapId, + post, + member, + true + ); + } + + public static PostScrapRes ScrapPostRes(Long postId, Long memberId, boolean scrapStatus){ + return PostScrapRes.builder() + .postId(postId) + .memberId(memberId) + .scrapStatus(scrapStatus) + .build(); + } +} 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 fb6c5e88..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 @@ -1,11 +1,80 @@ 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; +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(Long id, Member member){ + return new Post( + id, + "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(Long id, String title, String contents, String link, Boolean postType) { + Member member = MemberUtilTest.buildMember(); + + return new Post( + id, + title, + member, + contents, + link, + LocalDateTime.now(), + LocalDateTime.now().plusDays(10), + LocalDateTime.now().plusDays(16), + 8L, + MeetingMethod.ONLINE, + "서울시", + "마포구", + true, + ".com", + postType, + new ArrayList<>(), + new ArrayList<>() + ); + } + + public static List builderMultiplePosts(Boolean postType) { + return Arrays.asList( + 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) + ); + } - public static Post buildPost() { - return Post.builder() - .build(); + public static PostScrap builderPostScrap(Post post, Member member, Boolean postStatus){ + return new PostScrap( + post, + member, + postStatus + ); } }