Skip to content

Commit

Permalink
test: runner로 runnerPost 조회 api 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
shb03323 committed Sep 29, 2023
1 parent b63e0ba commit f897e9d
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 5 deletions.
10 changes: 5 additions & 5 deletions backend/baton/src/docs/asciidoc/RunnerPostReadApi.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ include::{snippets}/../../build/generated-snippets/runner-post-read-one-api-test

===== *Http Request*

include::{snippets}/../../build/generated-snippets/runner-post-read-all-api-test/read-runner-my-page/http-request.adoc[]
include::{snippets}/../../build/generated-snippets/runner-post-read-with-logined-runner-api-test/read-runner-post-by-logined-runner-and-review-status/http-request.adoc[]

===== *Http Request Headers*

include::{snippets}/../../build/generated-snippets/runner-post-read-all-api-test/read-runner-my-page/request-headers.adoc[]
include::{snippets}/../../build/generated-snippets/runner-post-read-with-logined-runner-api-test/read-runner-post-by-logined-runner-and-review-status/request-headers.adoc[]

===== *Http Request Query Parameters*

include::{snippets}/../../build/generated-snippets/runner-post-read-all-api-test/read-runner-my-page/query-parameters.adoc[]
include::{snippets}/../../build/generated-snippets/runner-post-read-with-logined-runner-api-test/read-runner-post-by-logined-runner-and-review-status/query-parameters.adoc[]

===== *Http Response*

include::{snippets}/../../build/generated-snippets/runner-post-read-all-api-test/read-runner-my-page/http-response.adoc[]
include::{snippets}/../../build/generated-snippets/runner-post-read-with-logined-runner-api-test/read-runner-post-by-logined-runner-and-review-status/http-response.adoc[]

===== *Http Response Fields*

include::{snippets}/../../build/generated-snippets/runner-post-read-all-api-test/read-runner-my-page/response-fields.adoc[]
include::{snippets}/../../build/generated-snippets/runner-post-read-with-logined-runner-api-test/read-runner-post-by-logined-runner-and-review-status/response-fields.adoc[]

==== *리뷰 지원한 서포터 조회 API*

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package touch.baton.document.runnerpost.read;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import touch.baton.config.RestdocsConfig;
import touch.baton.domain.common.response.PageResponse;
import touch.baton.domain.member.command.Member;
import touch.baton.domain.member.command.Runner;
import touch.baton.domain.runnerpost.command.RunnerPost;
import touch.baton.domain.runnerpost.command.vo.Deadline;
import touch.baton.domain.runnerpost.command.vo.ReviewStatus;
import touch.baton.domain.runnerpost.query.controller.RunnerPostQueryController;
import touch.baton.domain.runnerpost.query.controller.response.RunnerPostResponse;
import touch.baton.domain.runnerpost.query.service.RunnerPostQueryService;
import touch.baton.domain.runnerpost.query.service.dto.PageParams;
import touch.baton.domain.tag.command.Tag;
import touch.baton.fixture.domain.MemberFixture;
import touch.baton.fixture.domain.RunnerFixture;
import touch.baton.fixture.domain.RunnerPostFixture;
import touch.baton.fixture.domain.RunnerPostTagFixture;
import touch.baton.fixture.domain.TagFixture;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import static org.springframework.http.HttpHeaders.AUTHORIZATION;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
import static org.springframework.restdocs.headers.HeaderDocumentation.requestHeaders;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.payload.JsonFieldType.*;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.request.RequestDocumentation.queryParameters;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static touch.baton.fixture.vo.DeadlineFixture.deadline;
import static touch.baton.fixture.vo.TagNameFixture.tagName;

@WebMvcTest(RunnerPostQueryController.class)
public class RunnerPostReadWithLoginedRunnerApiTest extends RestdocsConfig {

@MockBean
private RunnerPostQueryService runnerPostQueryService;

@BeforeEach
void setUp() {
final RunnerPostQueryController runnerPostQueryController = new RunnerPostQueryController(runnerPostQueryService);
restdocsSetUp(runnerPostQueryController);
}

@DisplayName("로그인한 러너가 작성한 러너 게시글 페이징 조회 API")
@Test
void readRunnerPostByLoginedRunnerAndReviewStatus() throws Exception {
// given
final String socialId = "ditooSocialId";
final Member loginedMember = MemberFixture.createWithSocialId(socialId);
final Runner loginedRunner = RunnerFixture.createRunner(loginedMember);
final String token = getAccessTokenBySocialId(socialId);

final Tag javaTag = TagFixture.create(tagName("자바"));
final Deadline deadline = deadline(LocalDateTime.now().plusHours(100));
final RunnerPost runnerPost = RunnerPostFixture.create(loginedRunner, deadline, List.of(javaTag));

final Runner spyLoginedRunner = spy(loginedRunner);
given(oauthRunnerCommandRepository.joinByMemberSocialId(any())).willReturn(Optional.ofNullable(spyLoginedRunner));
assert spyLoginedRunner != null;
given(spyLoginedRunner.getId()).willReturn(1L);
final RunnerPost spyRunnerPost = spy(runnerPost);
given(spyRunnerPost.getId()).willReturn(1L);

// when
final PageResponse<RunnerPostResponse.SimpleByRunner> runnerPostResponses = PageResponse.of(
List.of(spyRunnerPost),
eachRunnerPost -> RunnerPostResponse.SimpleByRunner.of(
eachRunnerPost,
0L,
List.of(RunnerPostTagFixture.create(eachRunnerPost, javaTag))),
10
);
when(runnerPostQueryService.pageRunnerPostByRunnerIdAndReviewStatus(any(PageParams.class), eq(1L), eq(ReviewStatus.NOT_STARTED)))
.thenReturn(runnerPostResponses);

// then
mockMvc.perform(get("/api/v1/posts/runner/me/runner")
.header(AUTHORIZATION, "Bearer " + token)
.queryParam("cursor", String.valueOf(1000L))
.queryParam("limit", String.valueOf(10))
.queryParam("reviewStatus", ReviewStatus.NOT_STARTED.name()))
.andExpect(status().isOk())
.andExpect(content().contentType(APPLICATION_JSON))
.andDo(restDocs.document(
requestHeaders(
headerWithName(AUTHORIZATION).description("Bearer JWT")
),
queryParameters(
parameterWithName("cursor").description("(Optional) 이전 페이지 마지막 게시글 식별자값(id)"),
parameterWithName("limit").description("페이지 사이즈"),
parameterWithName("reviewStatus").description("(Optional) 리뷰 상태")
),
responseFields(
fieldWithPath("data.[].runnerPostId").type(NUMBER).description("러너 게시글 식별자값(id)"),
fieldWithPath("data.[].title").type(STRING).description("러너 게시글의 제목"),
fieldWithPath("data.[].deadline").type(STRING).description("러너 게시글의 마감 기한"),
fieldWithPath("data.[].watchedCount").type(NUMBER).description("러너 게시글의 조회수"),
fieldWithPath("data.[].applicantCount").type(NUMBER).description("러너 게시글에 신청한 서포터 수"),
fieldWithPath("data.[].reviewStatus").type(STRING).description("러너 게시글의 리뷰 상태"),
fieldWithPath("data.[].isReviewed").type(BOOLEAN).description("러너 게시글의 리뷰 완료 여부"),
fieldWithPath("data.[].supporterId").type(VARIES)
.optional()
.description("서포터 id (서포터가 존재할 때 NUMBER, 아닌 경우에 NULL)"),
fieldWithPath("data.[].tags.[]").type(ARRAY).description("러너 게시글의 태그 목록"),
fieldWithPath("pageInfo.isLast").type(BOOLEAN).description("마지막 페이지 여부")
))
);
}
}

0 comments on commit f897e9d

Please sign in to comment.