From 8f323372843e17f9041b23e941cb5e4ca96c57d7 Mon Sep 17 00:00:00 2001 From: Ethan Date: Tue, 15 Aug 2023 14:21:21 +0900 Subject: [PATCH] =?UTF-8?q?=EB=9F=AC=EB=84=88=20=EA=B2=8C=EC=8B=9C?= =?UTF-8?q?=EA=B8=80=20=EC=83=9D=EC=84=B1=20API=20=EC=88=98=EC=A0=95=20(#3?= =?UTF-8?q?48)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: RunnerPostService create 로직 변경 * test: 게시글 생성 인수테스트 작성 * test: restdos 테스트 추가 * docs: restdocs 파일 포맷팅 * test: HttpHeaders 를 spring 패키지를 import 를 받도록 변경 * refactor: FutureValidator 에서 null 인 localdatetime을 체크 안하도록 변경 * refactor: 없던 중괄호 추가 --- .../docs/asciidoc/RunnerPostCreateApi.adoc | 27 +++ backend/baton/src/docs/asciidoc/index.adoc | 2 + .../exception/validator/FutureValidator.java | 3 +- .../validator/MaxLengthValidator.java | 4 +- .../runnerpost/service/RunnerPostService.java | 35 ++-- .../baton/assure/common/AssuredSupport.java | 19 +- .../runnerpost/RunnerPostAssuredSupport.java | 26 ++- .../RunnerPostCreateAssuredTest.java | 190 ++++++++++++++++++ .../post/RunnerPostCreateApiTest.java | 79 ++++++++ .../RunnerPostControllerCreateTest.java | 88 -------- .../service/RunnerPostServiceCreateTest.java | 8 +- 11 files changed, 366 insertions(+), 115 deletions(-) create mode 100644 backend/baton/src/docs/asciidoc/RunnerPostCreateApi.adoc create mode 100644 backend/baton/src/test/java/touch/baton/assure/runnerpost/RunnerPostCreateAssuredTest.java create mode 100644 backend/baton/src/test/java/touch/baton/document/runnerpost/post/RunnerPostCreateApiTest.java delete mode 100644 backend/baton/src/test/java/touch/baton/domain/runnerpost/controller/RunnerPostControllerCreateTest.java diff --git a/backend/baton/src/docs/asciidoc/RunnerPostCreateApi.adoc b/backend/baton/src/docs/asciidoc/RunnerPostCreateApi.adoc new file mode 100644 index 000000000..9104580f8 --- /dev/null +++ b/backend/baton/src/docs/asciidoc/RunnerPostCreateApi.adoc @@ -0,0 +1,27 @@ +ifndef::snippets[] +:snippets: ../../../build/generated-snippets +endif::[] +:doctype: book +:icons: font +:source-highlighter: highlight.js +:toc: left +:toclevels: 3 +:sectlinks: +:operation-http-request-title: Example Request +:operation-http-response-title: Example Response + +=== *러너 게시글 생성* + +==== *러너 게시글 생성 API* + +===== *Http Request* + +include::{snippets}/../../build/generated-snippets/runner-post-create-api-test/create-runner-post/http-request.adoc[] + +===== *Http Request Header* + +include::{snippets}/../../build/generated-snippets/runner-post-create-api-test/create-runner-post/request-headers.adoc[] + +===== *Http Response Header* + +include::{snippets}/../../build/generated-snippets/runner-post-create-api-test/create-runner-post/response-headers.adoc[] diff --git a/backend/baton/src/docs/asciidoc/index.adoc b/backend/baton/src/docs/asciidoc/index.adoc index c91427d63..013f81fe8 100644 --- a/backend/baton/src/docs/asciidoc/index.adoc +++ b/backend/baton/src/docs/asciidoc/index.adoc @@ -27,9 +27,11 @@ include::RunnerPostDeleteApi.adoc[] include::RunnerProfileReadApi.adoc[] include::RunnerLoginProfileReadApi.adoc[] include::RunnerProfileUpdateApi.adoc[] +include::RunnerPostCreateApi.adoc[] == *[ 서포터 ]* include::SupporterProfileReadApi.adoc[] + include::SupporterProfileUpdateApi.adoc[] include::RunnerPostUpdateApi.adoc[] diff --git a/backend/baton/src/main/java/touch/baton/domain/runnerpost/exception/validator/FutureValidator.java b/backend/baton/src/main/java/touch/baton/domain/runnerpost/exception/validator/FutureValidator.java index 64239752b..ae9ac82f3 100644 --- a/backend/baton/src/main/java/touch/baton/domain/runnerpost/exception/validator/FutureValidator.java +++ b/backend/baton/src/main/java/touch/baton/domain/runnerpost/exception/validator/FutureValidator.java @@ -6,6 +6,7 @@ import touch.baton.domain.common.exception.ClientRequestException; import java.time.LocalDateTime; +import java.util.Objects; public class FutureValidator implements ConstraintValidator { @@ -18,7 +19,7 @@ public void initialize(final ValidFuture constraintAnnotation) { @Override public boolean isValid(final LocalDateTime value, final ConstraintValidatorContext context) { - if (value.isBefore(LocalDateTime.now())) { + if (Objects.nonNull(value) && value.isBefore(LocalDateTime.now())) { throw new ClientRequestException(errorCode); } return true; diff --git a/backend/baton/src/main/java/touch/baton/domain/runnerpost/exception/validator/MaxLengthValidator.java b/backend/baton/src/main/java/touch/baton/domain/runnerpost/exception/validator/MaxLengthValidator.java index 2acb27d41..c13fdade3 100644 --- a/backend/baton/src/main/java/touch/baton/domain/runnerpost/exception/validator/MaxLengthValidator.java +++ b/backend/baton/src/main/java/touch/baton/domain/runnerpost/exception/validator/MaxLengthValidator.java @@ -5,6 +5,8 @@ import touch.baton.domain.common.exception.ClientErrorCode; import touch.baton.domain.common.exception.ClientRequestException; +import java.util.Objects; + public class MaxLengthValidator implements ConstraintValidator { private ClientErrorCode errorCode; @@ -18,7 +20,7 @@ public void initialize(final ValidMaxLength constraintAnnotation) { @Override public boolean isValid(final String value, final ConstraintValidatorContext context) { - if (value.length() > max) { + if (Objects.nonNull(value) && value.length() > max) { throw new ClientRequestException(errorCode); } return true; diff --git a/backend/baton/src/main/java/touch/baton/domain/runnerpost/service/RunnerPostService.java b/backend/baton/src/main/java/touch/baton/domain/runnerpost/service/RunnerPostService.java index 2de45ab69..5186aa4a5 100644 --- a/backend/baton/src/main/java/touch/baton/domain/runnerpost/service/RunnerPostService.java +++ b/backend/baton/src/main/java/touch/baton/domain/runnerpost/service/RunnerPostService.java @@ -50,30 +50,35 @@ public Long createRunnerPost(final Runner runner, final RunnerPostCreateRequest final RunnerPost runnerPost = toDomain(runner, request); runnerPostRepository.save(runnerPost); - List toSaveTags = new ArrayList<>(); - for (final String tagName : request.tags()) { - final Optional maybeTag = tagRepository.findByTagName(new TagName(tagName)); + final List tags = findTagsAfterSave(request.tags()); - if (maybeTag.isEmpty()) { - final Tag savedTag = tagRepository.save(Tag.newInstance(tagName)); - toSaveTags.add(savedTag); - continue; - } - - final Tag presentTag = maybeTag.get(); - toSaveTags.add(presentTag); - } - - final List postTags = toSaveTags.stream() + final List runnerPostTags = tags.stream() .map(tag -> RunnerPostTag.builder() .tag(tag) .runnerPost(runnerPost).build()) .toList(); - runnerPost.addAllRunnerPostTags(postTags); + runnerPost.addAllRunnerPostTags(runnerPostTags); return runnerPost.getId(); } + private List findTagsAfterSave(final List tagNames) { + final List tags = new ArrayList<>(); + for (String tagName : tagNames) { + tagRepository.findByTagName(new TagName(tagName)) + .ifPresentOrElse(tags::add, addTagAfterSave(tags, tagName)); + } + + return tags; + } + + private Runnable addTagAfterSave(final List tags, final String tagName) { + return () -> { + final Tag savedTag = tagRepository.save(Tag.newInstance(tagName)); + tags.add(savedTag); + }; + } + private RunnerPost toDomain(final Runner runner, final RunnerPostCreateRequest request) { return RunnerPost.newInstance(request.title(), request.contents(), diff --git a/backend/baton/src/test/java/touch/baton/assure/common/AssuredSupport.java b/backend/baton/src/test/java/touch/baton/assure/common/AssuredSupport.java index a9e6e61c8..a34e612c6 100644 --- a/backend/baton/src/test/java/touch/baton/assure/common/AssuredSupport.java +++ b/backend/baton/src/test/java/touch/baton/assure/common/AssuredSupport.java @@ -50,20 +50,22 @@ public static ExtractableResponse post(final String uri, .extract(); } - public static ExtractableResponse get(final String uri, final String accessToken) { + public static ExtractableResponse post(final String uri, final Object params, final String accessToken) { return RestAssured .given().log().ifValidationFails() .auth().preemptive().oauth2(accessToken) + .contentType(APPLICATION_JSON_VALUE) + .body(params) .when().log().ifValidationFails() - .get(uri) + .post(uri) .then().log().ifError() .extract(); } - public static ExtractableResponse get(final String uri, final String pathParamName, final Long id) { + public static ExtractableResponse get(final String uri, final String accessToken) { return RestAssured .given().log().ifValidationFails() - .pathParam(pathParamName, id) + .auth().preemptive().oauth2(accessToken) .when().log().ifValidationFails() .get(uri) .then().log().ifError() @@ -85,6 +87,15 @@ public static ExtractableResponse get(final String uri, .extract(); } + public static ExtractableResponse get(final String uri, final String pathParamName, final Long id) { + return RestAssured + .given().log().ifValidationFails() + .pathParam(pathParamName, id) + .when().log().ifValidationFails() + .get(uri) + .then().log().ifError() + .extract(); + } public static ExtractableResponse get(final String uri, final Map queryParams) { return RestAssured diff --git a/backend/baton/src/test/java/touch/baton/assure/runnerpost/RunnerPostAssuredSupport.java b/backend/baton/src/test/java/touch/baton/assure/runnerpost/RunnerPostAssuredSupport.java index e3cfea641..2c3348cd7 100644 --- a/backend/baton/src/test/java/touch/baton/assure/runnerpost/RunnerPostAssuredSupport.java +++ b/backend/baton/src/test/java/touch/baton/assure/runnerpost/RunnerPostAssuredSupport.java @@ -9,10 +9,12 @@ import org.springframework.http.HttpStatus; import touch.baton.assure.common.AssuredSupport; import touch.baton.assure.common.HttpStatusAndLocationHeader; +import touch.baton.domain.common.response.ErrorResponse; import touch.baton.domain.common.response.PageResponse; import touch.baton.domain.runner.Runner; import touch.baton.domain.runner.controller.response.RunnerResponse; import touch.baton.domain.runnerpost.controller.response.RunnerPostResponse; +import touch.baton.domain.runnerpost.service.dto.RunnerPostCreateRequest; import touch.baton.domain.runnerpost.service.dto.RunnerPostUpdateRequest; import touch.baton.domain.runnerpost.vo.ReviewStatus; @@ -20,9 +22,9 @@ import java.util.List; import java.util.Map; -import static org.apache.http.HttpHeaders.LOCATION; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.SoftAssertions.assertSoftly; +import static org.springframework.http.HttpHeaders.LOCATION; @SuppressWarnings("NonAsciiCharacters") public class RunnerPostAssuredSupport { @@ -81,6 +83,11 @@ public static class RunnerPostClientRequestBuilder { return this; } + public RunnerPostClientRequestBuilder 러너_게시글_등록_요청한다(final RunnerPostCreateRequest 게시글_생성_요청) { + response = AssuredSupport.post("/api/v1/posts/runner", 게시글_생성_요청, accessToken); + return this; + } + public RunnerPostClientRequestBuilder 러너_게시글_식별자값으로_러너_게시글을_조회한다(final Long 러너_게시글_식별자값) { response = AssuredSupport.get("/api/v1/posts/runner/{runnerPostId}", "runnerPostId", 러너_게시글_식별자값, accessToken); return this; @@ -201,5 +208,22 @@ public RunnerPostServerResponseBuilder(final ExtractableResponse respo } ); } + + public void 러너_게시글_등록_성공을_검증한다(final HttpStatusAndLocationHeader 게시글_등록_성공_응답) { + assertSoftly(softly -> { + softly.assertThat(response.statusCode()).isEqualTo(게시글_등록_성공_응답.getHttpStatus().value()); + softly.assertThat(response.header(LOCATION)).contains(게시글_등록_성공_응답.getLocation()); + } + ); + } + + public void 러너_게시글_등록_실패를_검증한다(final ErrorResponse 예상_에러_응답) { + final ErrorResponse 실제_에러_응답 = response.as(ErrorResponse.class); + + assertSoftly(softly -> { + softly.assertThat(실제_에러_응답.errorCode()).isEqualTo(예상_에러_응답.errorCode()); + softly.assertThat(실제_에러_응답.message()).isEqualTo(예상_에러_응답.message()); + }); + } } } diff --git a/backend/baton/src/test/java/touch/baton/assure/runnerpost/RunnerPostCreateAssuredTest.java b/backend/baton/src/test/java/touch/baton/assure/runnerpost/RunnerPostCreateAssuredTest.java new file mode 100644 index 000000000..9e8104782 --- /dev/null +++ b/backend/baton/src/test/java/touch/baton/assure/runnerpost/RunnerPostCreateAssuredTest.java @@ -0,0 +1,190 @@ +package touch.baton.assure.runnerpost; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import touch.baton.assure.common.HttpStatusAndLocationHeader; +import touch.baton.config.AssuredTestConfig; +import touch.baton.domain.common.response.ErrorResponse; +import touch.baton.domain.member.Member; +import touch.baton.domain.runnerpost.service.dto.RunnerPostCreateRequest; +import touch.baton.fixture.domain.MemberFixture; +import touch.baton.fixture.domain.RunnerFixture; + +import java.time.LocalDateTime; +import java.util.List; + +import static org.springframework.http.HttpStatus.CREATED; + +@SuppressWarnings("NonAsciiCharacters") +class RunnerPostCreateAssuredTest extends AssuredTestConfig { + + private static String 토큰; + + @BeforeEach + void setUp() { + final String 소셜_아이디 = "hongSile"; + final Member 사용자 = memberRepository.save(MemberFixture.createWithSocialId(소셜_아이디)); + runnerRepository.save(RunnerFixture.createRunner(사용자)); + 토큰 = login(소셜_아이디); + } + + @Test + void 러너_게시글_등록이_성공한다() { + // given + final RunnerPostCreateRequest 게시글_생성_요청 = new RunnerPostCreateRequest("코드 리뷰 해주세요.", + List.of("Java", "Spring"), + "https://github.com/cookienc", + LocalDateTime.now().plusDays(10), + "싸게 부탁드려요." + ); + + // when, then + RunnerPostAssuredSupport + .클라이언트_요청() + .토큰으로_로그인한다(토큰) + .러너_게시글_등록_요청한다(게시글_생성_요청) + + .서버_응답() + .러너_게시글_등록_성공을_검증한다(new HttpStatusAndLocationHeader(CREATED, "/api/v1/posts/runner")); + } + + @Test + void 게시글_제목이_null이면_러너_게시글_등록_실패한다() { + // given + final RunnerPostCreateRequest 게시글_생성_요청 = new RunnerPostCreateRequest(null, + List.of("Java", "Spring"), + "https://github.com/cookienc", + LocalDateTime.now().plusDays(10), + "싸게 부탁드려요." + ); + + // when, then + RunnerPostAssuredSupport + .클라이언트_요청() + .토큰으로_로그인한다(토큰) + .러너_게시글_등록_요청한다(게시글_생성_요청) + + .서버_응답() + .러너_게시글_등록_실패를_검증한다(new ErrorResponse("RP001", "제목을 입력해주세요.")); + } + + @Test + void 게시글_태그가_null이면_러너_게시글_등록_실패한다() { + // given + final RunnerPostCreateRequest 게시글_생성_요청 = new RunnerPostCreateRequest("코드 리뷰 해주세요.", + null, + "https://github.com/cookienc", + LocalDateTime.now().plusDays(10), + "싸게 부탁드려요." + ); + + // when, then + RunnerPostAssuredSupport + .클라이언트_요청() + .토큰으로_로그인한다(토큰) + .러너_게시글_등록_요청한다(게시글_생성_요청) + + .서버_응답() + .러너_게시글_등록_실패를_검증한다(new ErrorResponse("RP008", "태그 목록을 빈 값이라도 입력해주세요.")); + } + + @Test + void 게시글_PR_URL이_null이면_러너_게시글_등록_실패한다() { + // given + final RunnerPostCreateRequest 게시글_생성_요청 = new RunnerPostCreateRequest("코드 리뷰 해주세요.", + List.of("Java", "Spring"), + null, + LocalDateTime.now().plusDays(10), + "싸게 부탁드려요." + ); + + // when, then + RunnerPostAssuredSupport + .클라이언트_요청() + .토큰으로_로그인한다(토큰) + .러너_게시글_등록_요청한다(게시글_생성_요청) + + .서버_응답() + .러너_게시글_등록_실패를_검증한다(new ErrorResponse("RP002", "PR 주소를 입력해주세요.")); + } + + @Test + void 게시글_마감기한이_null이면_러너_게시글_등록_실패한다() { + // given + final RunnerPostCreateRequest 게시글_생성_요청 = new RunnerPostCreateRequest("코드 리뷰 해주세요.", + List.of("Java", "Spring"), + "https://github.com/cookienc", + null, + "싸게 부탁드려요." + ); + + // when, then + RunnerPostAssuredSupport + .클라이언트_요청() + .토큰으로_로그인한다(토큰) + .러너_게시글_등록_요청한다(게시글_생성_요청) + + .서버_응답() + .러너_게시글_등록_실패를_검증한다(new ErrorResponse("RP003", "마감일을 입력해주세요.")); + } + + @Test + void 게시글_마감기한이_현재보다_과거면_러너_게시글_등록_실패한다() { + // given + final RunnerPostCreateRequest 게시글_생성_요청 = new RunnerPostCreateRequest("코드 리뷰 해주세요.", + List.of("Java", "Spring"), + "https://github.com/cookienc", + LocalDateTime.now().minusDays(1), + "싸게 부탁드려요." + ); + + // when, then + RunnerPostAssuredSupport + .클라이언트_요청() + .토큰으로_로그인한다(토큰) + .러너_게시글_등록_요청한다(게시글_생성_요청) + + .서버_응답() + .러너_게시글_등록_실패를_검증한다(new ErrorResponse("RP006", "마감일은 오늘보다 과거일 수 없습니다.")); + } + + @Test + void 게시글_내용이_null이면_러너_게시글_등록_실패한다() { + // given + final RunnerPostCreateRequest 게시글_생성_요청 = new RunnerPostCreateRequest("코드 리뷰 해주세요.", + List.of("Java", "Spring"), + "https://github.com/cookienc", + LocalDateTime.now().plusDays(10), + null + ); + + // when, then + RunnerPostAssuredSupport + .클라이언트_요청() + .토큰으로_로그인한다(토큰) + .러너_게시글_등록_요청한다(게시글_생성_요청) + + .서버_응답() + .러너_게시글_등록_실패를_검증한다(new ErrorResponse("RP004", "내용을 입력해주세요.")); + } + + @Test + void 게시글_내용이_1000자_보다_길면_러너_게시글_등록_실패한다() { + // given + final RunnerPostCreateRequest 게시글_생성_요청 = new RunnerPostCreateRequest("코드 리뷰 해주세요.", + List.of("Java", "Spring"), + "https://github.com/cookienc", + LocalDateTime.now().plusDays(10), + "12345".repeat(200) + "1" + ); + + // when, then + RunnerPostAssuredSupport + .클라이언트_요청() + .토큰으로_로그인한다(토큰) + .러너_게시글_등록_요청한다(게시글_생성_요청) + + .서버_응답() + .러너_게시글_등록_실패를_검증한다(new ErrorResponse("RP005", "내용은 1000자 까지 입력해주세요.")); + } +} diff --git a/backend/baton/src/test/java/touch/baton/document/runnerpost/post/RunnerPostCreateApiTest.java b/backend/baton/src/test/java/touch/baton/document/runnerpost/post/RunnerPostCreateApiTest.java new file mode 100644 index 000000000..2b482c9ad --- /dev/null +++ b/backend/baton/src/test/java/touch/baton/document/runnerpost/post/RunnerPostCreateApiTest.java @@ -0,0 +1,79 @@ +package touch.baton.document.runnerpost.post; + +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.member.Member; +import touch.baton.domain.runner.Runner; +import touch.baton.domain.runnerpost.controller.RunnerPostController; +import touch.baton.domain.runnerpost.service.RunnerPostService; +import touch.baton.domain.runnerpost.service.dto.RunnerPostCreateRequest; +import touch.baton.fixture.domain.MemberFixture; +import touch.baton.fixture.domain.RunnerFixture; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.Optional; + +import static org.apache.http.HttpHeaders.AUTHORIZATION; +import static org.apache.http.HttpHeaders.CONTENT_TYPE; +import static org.apache.http.HttpHeaders.LOCATION; +import static org.mockito.BDDMockito.any; +import static org.mockito.BDDMockito.when; +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; +import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName; +import static org.springframework.restdocs.headers.HeaderDocumentation.requestHeaders; +import static org.springframework.restdocs.headers.HeaderDocumentation.responseHeaders; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@WebMvcTest(RunnerPostController.class) +public class RunnerPostCreateApiTest extends RestdocsConfig { + + @MockBean + private RunnerPostService runnerPostService; + + @BeforeEach + void setUp() { + restdocsSetUp(new RunnerPostController(runnerPostService)); + } + + @DisplayName("러너 게시글 등록 API") + @Test + void createRunnerPost() throws Exception { + // given + final String socialId = "hongSile"; + final Member member = MemberFixture.createWithSocialId(socialId); + final Runner runner = RunnerFixture.createRunner(member); + final String accessToken = getAccessTokenBySocialId(socialId); + + final RunnerPostCreateRequest request = new RunnerPostCreateRequest("코드 리뷰 해주세요.", + List.of("Java", "Spring"), + "https://github.com/cookienc", + LocalDateTime.now().plusDays(10), + "12345".repeat(200) + ); + + // when + when(runnerPostService.createRunnerPost(any(Runner.class), any(RunnerPostCreateRequest.class))).thenReturn(1L); + when(oauthRunnerRepository.joinByMemberSocialId(any())).thenReturn(Optional.ofNullable(runner)); + + // then + mockMvc.perform(post("/api/v1/posts/runner") + .header(AUTHORIZATION, "Bearer " + accessToken) + .contentType(APPLICATION_JSON) + .content(objectMapper.writeValueAsString(request))) + .andExpect(status().isCreated()) + .andExpect(redirectedUrl("/api/v1/posts/runner/1")) + .andDo(restDocs.document( + requestHeaders(headerWithName(AUTHORIZATION).description("Bearer Token"), + headerWithName(CONTENT_TYPE).description(APPLICATION_JSON_VALUE)), + responseHeaders(headerWithName(LOCATION).description("Redirect URI")) + )); + } +} diff --git a/backend/baton/src/test/java/touch/baton/domain/runnerpost/controller/RunnerPostControllerCreateTest.java b/backend/baton/src/test/java/touch/baton/domain/runnerpost/controller/RunnerPostControllerCreateTest.java deleted file mode 100644 index 00880e868..000000000 --- a/backend/baton/src/test/java/touch/baton/domain/runnerpost/controller/RunnerPostControllerCreateTest.java +++ /dev/null @@ -1,88 +0,0 @@ -package touch.baton.domain.runnerpost.controller; - -import io.restassured.RestAssured; -import io.restassured.response.ExtractableResponse; -import io.restassured.response.Response; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -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.web.server.LocalServerPort; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import touch.baton.domain.member.Member; -import touch.baton.domain.member.repository.MemberRepository; -import touch.baton.domain.member.vo.Company; -import touch.baton.domain.member.vo.GithubUrl; -import touch.baton.domain.member.vo.ImageUrl; -import touch.baton.domain.member.vo.MemberName; -import touch.baton.domain.member.vo.OauthId; -import touch.baton.domain.member.vo.SocialId; -import touch.baton.domain.runner.Runner; -import touch.baton.domain.runner.repository.RunnerRepository; -import touch.baton.domain.runnerpost.service.dto.RunnerPostCreateRequest; - -import java.time.LocalDateTime; -import java.util.List; - -import static io.restassured.RestAssured.given; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertAll; -import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; - -@SpringBootTest(webEnvironment = RANDOM_PORT) -class RunnerPostControllerCreateTest { - - @Autowired - private RunnerRepository runnerRepository; - @Autowired - private MemberRepository memberRepository; - - @BeforeEach - void setUp(@LocalServerPort int port) { - RestAssured.port = port; - - final Member member = Member.builder() - .memberName(new MemberName("헤에디주")) - .socialId(new SocialId("testSocialId")) - .oauthId(new OauthId("dsigjh98gh230gn2oinv913bcuo23nqovbvu93b12voi3bc31j")) - .githubUrl(new GithubUrl("github.com/hyena0608")) - .company(new Company("우아한형제들")) - .imageUrl(new ImageUrl("홍혁준")) - .build(); - memberRepository.save(member); - final Runner runner = Runner.builder() - .member(member) - .build(); - runnerRepository.save(runner); - } - - @Disabled - @Test - void 러너_게시글_등록에_성공한다() { - // given - final RunnerPostCreateRequest request = new RunnerPostCreateRequest("코드 리뷰 해주세요.", - List.of("Java", "Spring"), - "https://github.com/cookienc", - LocalDateTime.of(2099, 12, 12, 0, 0), - "싸게 부탁드려요." - ); - - // when - final ExtractableResponse response = given().log().all() - .contentType(MediaType.APPLICATION_JSON_VALUE) - .body(request) - .when() - .post("/api/v1/posts/runner") - .then() - .log().all().extract(); - - // then - assertAll( - () -> assertThat(response.statusCode()).isEqualTo(HttpStatus.CREATED.value()), - () -> assertThat(response.header(HttpHeaders.LOCATION)).contains("/api/v1/posts/runner") - ); - } -} diff --git a/backend/baton/src/test/java/touch/baton/domain/runnerpost/service/RunnerPostServiceCreateTest.java b/backend/baton/src/test/java/touch/baton/domain/runnerpost/service/RunnerPostServiceCreateTest.java index 7387da482..fe9b00599 100644 --- a/backend/baton/src/test/java/touch/baton/domain/runnerpost/service/RunnerPostServiceCreateTest.java +++ b/backend/baton/src/test/java/touch/baton/domain/runnerpost/service/RunnerPostServiceCreateTest.java @@ -40,7 +40,7 @@ class RunnerPostServiceCreateTest extends ServiceTestConfig { private static final String TAG = "Java"; private static final String OTHER_TAG = "Spring"; private static final String PULL_REQUEST_URL = "https://github.com/cookienc"; - private static final LocalDateTime DEADLINE = of(2099, 12, 12, 0, 0); + private static final LocalDateTime DEADLINE = LocalDateTime.now().plusDays(10); private static final String CONTENTS = "싸게 부탁드려요."; private RunnerPostService runnerPostService; @@ -65,10 +65,8 @@ void success() { PULL_REQUEST_URL, DEADLINE, CONTENTS); - final Member ethan = MemberFixture.createEthan(); - memberRepository.save(ethan); - final Runner runner = RunnerFixture.createRunner(ethan); - runnerRepository.save(runner); + final Member ethanMember = memberRepository.save(MemberFixture.createEthan()); + final Runner runner = runnerRepository.save(RunnerFixture.createRunner(ethanMember)); // when final Long savedId = runnerPostService.createRunnerPost(runner, request);