Skip to content

Commit

Permalink
스크랩 관련 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
yumzen committed May 22, 2024
1 parent 45d2782 commit f855618
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -97,4 +63,6 @@ void scrapPost() throws Exception {
verify(postService).scrapPost(any(Member.class), any(Long.class));
}
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -180,17 +181,19 @@ void getMyScrapProjectTest() {
List<Post> testPosts = PostUtilTest.builderMultiplePosts();
List<PostScrap> testPostScraps= PostScrapUtilTest.builderMultiplePostScraps(testPosts, member);
Pageable pageable = PageRequest.of(0, 6);
Page<Post> testPage = new PageImpl<>(testPostScraps, pageable, testPostScraps.size()); // Page 생성
Page<PostScrap> 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<GetContestRes> res = postService.getContests("createdAt", pageable);
Page<GetProjectRes> 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");
Expand All @@ -200,7 +203,28 @@ void getMyScrapProjectTest() {
@Test
@DisplayName("내가 스크랩한 공모전 목록 조회 및 페이지네이션 기능 테스트")
void getMyScrapContestTest() {
// given
Member member = MemberUtilTest.buildMemberAndId(1L);
List<Post> testPosts = PostUtilTest.builderMultiplePosts();
List<PostScrap> testPostScraps= PostScrapUtilTest.builderMultiplePostScraps(testPosts, member);
Pageable pageable = PageRequest.of(0, 6);
Page<PostScrap> testPage = new PageImpl<>(testPostScraps, pageable, testPostScraps.size()); // Page 생성


given(postScrapRepository.findAllByMemberAndPostPostTypeFalseAndPostDeletedAtIsNullAndScrapStatusTrueOrderByPostScrapIdDesc(
any(Member.class), any(Pageable.class)
)).willReturn(testPage);

// when
Page<GetContestRes> 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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit f855618

Please sign in to comment.