From cd3dd21a953dbc347a64d47f176a6a291688e910 Mon Sep 17 00:00:00 2001 From: zillionme Date: Mon, 6 Nov 2023 19:41:47 +0900 Subject: [PATCH 1/3] =?UTF-8?q?test:=20=EB=B9=88=20=EC=AA=BD=EC=A7=80=20?= =?UTF-8?q?=EB=93=B1=EB=A1=9D=EC=8B=9C=20=EC=98=88=EC=99=B8=20=EB=B0=9C?= =?UTF-8?q?=EC=83=9D=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../presentation/LetterControllerTest.java | 73 +++++++++++++++---- 1 file changed, 60 insertions(+), 13 deletions(-) diff --git a/backend/src/test/java/com/now/naaga/letter/presentation/LetterControllerTest.java b/backend/src/test/java/com/now/naaga/letter/presentation/LetterControllerTest.java index 10326043a..a012237b8 100644 --- a/backend/src/test/java/com/now/naaga/letter/presentation/LetterControllerTest.java +++ b/backend/src/test/java/com/now/naaga/letter/presentation/LetterControllerTest.java @@ -1,13 +1,5 @@ package com.now.naaga.letter.presentation; -import static com.now.naaga.common.fixture.PositionFixture.잠실_루터회관_정문_좌표; -import static com.now.naaga.common.fixture.PositionFixture.잠실역_교보문고_110미터_앞_좌표; -import static com.now.naaga.common.fixture.PositionFixture.잠실역_교보문고_좌표; -import static com.now.naaga.game.domain.GameStatus.DONE; -import static com.now.naaga.game.exception.GameExceptionType.NOT_EXIST_IN_PROGRESS; -import static com.now.naaga.player.exception.PlayerExceptionType.PLAYER_NOT_FOUND; -import static org.assertj.core.api.SoftAssertions.assertSoftly; - import com.now.naaga.auth.domain.AuthToken; import com.now.naaga.auth.infrastructure.AuthType; import com.now.naaga.auth.infrastructure.jwt.AuthTokenGenerator; @@ -33,19 +25,28 @@ import io.restassured.RestAssured; import io.restassured.common.mapper.TypeRef; import io.restassured.http.ContentType; -import io.restassured.parsing.Parser; import io.restassured.response.ExtractableResponse; import io.restassured.response.Response; -import java.time.LocalDateTime; -import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; +import java.time.LocalDateTime; +import java.util.List; + +import static com.now.naaga.common.exception.CommonExceptionType.INVALID_REQUEST_BODY; +import static com.now.naaga.common.fixture.PositionFixture.*; +import static com.now.naaga.game.domain.GameStatus.DONE; +import static com.now.naaga.game.exception.GameExceptionType.NOT_EXIST_IN_PROGRESS; +import static com.now.naaga.player.exception.PlayerExceptionType.PLAYER_NOT_FOUND; +import static org.assertj.core.api.SoftAssertions.assertSoftly; + @SuppressWarnings("NonAsciiCharacters") @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class LetterControllerTest extends CommonControllerTest { @@ -275,7 +276,6 @@ protected void setUp() { 잠실_루터회관_정문_좌표.getLongitude().doubleValue()); //when - RestAssured.defaultParser = Parser.JSON; final ExtractableResponse extract = RestAssured .given().log().all() .header("Authorization", "Bearer " + accessToken) @@ -323,7 +323,6 @@ protected void setUp() { 잠실_루터회관_정문_좌표.getLongitude().doubleValue()); //when - RestAssured.defaultParser = Parser.JSON; final ExtractableResponse extract = RestAssured .given().log().all() .header("Authorization", "Bearer " + accessToken) @@ -350,4 +349,52 @@ protected void setUp() { } ); } + + + @ParameterizedTest + @ValueSource(strings = {"", " "}) + void 쪽지_등록_요청시_쪽지의_내용이_blank이면_예외가_발생한다(String message) { + //given + final Player player = playerBuilder.init() + .build(); + final Game game = gameBuilder.init() + .player(player) + .build(); + + final AuthToken generate = authTokenGenerator.generate(player.getMember(), 1L, AuthType.KAKAO); + final String accessToken = generate.getAccessToken(); + + final LetterRequest letterRequest = new LetterRequest(message, + 잠실_루터회관_정문_좌표.getLatitude().doubleValue(), + 잠실_루터회관_정문_좌표.getLongitude().doubleValue()); + + //when + final ExtractableResponse extract = RestAssured + .given().log().all() + .header("Authorization", "Bearer " + accessToken) + .contentType(ContentType.JSON) + .body(letterRequest) + .when() + .post("/letters") + .then().log().all() + .extract(); + + //then + final int statusCode = extract.statusCode(); + final ExceptionResponse actual = extract.as(ExceptionResponse.class); + final ExceptionResponse expected = new ExceptionResponse( + INVALID_REQUEST_BODY.errorCode(), + INVALID_REQUEST_BODY.errorMessage() + ); + + assertSoftly(softAssertions -> { + softAssertions.assertThat(statusCode).isEqualTo(HttpStatus.BAD_REQUEST.value()); + softAssertions.assertThat(actual) + .usingRecursiveComparison() + .isEqualTo(expected); + } + ); + + + } } From 647c83c4c1bd9b0c6f9a753c8c93f6a4415dcb34 Mon Sep 17 00:00:00 2001 From: zillionme Date: Mon, 6 Nov 2023 19:42:01 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=EB=B9=88=20=EC=AA=BD=EC=A7=80=20?= =?UTF-8?q?=EB=93=B1=EB=A1=9D=EC=8B=9C=20=EC=98=88=EC=99=B8=20=EB=B0=9C?= =?UTF-8?q?=EC=83=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/build.gradle | 1 + .../naaga/common/exception/ControllerExceptionHandler.java | 3 ++- .../com/now/naaga/letter/presentation/LetterController.java | 4 +++- .../com/now/naaga/letter/presentation/dto/LetterRequest.java | 4 +++- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/backend/build.gradle b/backend/build.gradle index 0b98200bc..0addcc1de 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -18,6 +18,7 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'io.jsonwebtoken:jjwt-api:0.11.5' implementation 'io.jsonwebtoken:jjwt-impl:0.11.5' diff --git a/backend/src/main/java/com/now/naaga/common/exception/ControllerExceptionHandler.java b/backend/src/main/java/com/now/naaga/common/exception/ControllerExceptionHandler.java index fd1f00863..9ea752599 100644 --- a/backend/src/main/java/com/now/naaga/common/exception/ControllerExceptionHandler.java +++ b/backend/src/main/java/com/now/naaga/common/exception/ControllerExceptionHandler.java @@ -28,7 +28,8 @@ public ResponseEntity handleBaseException(final BaseException @ExceptionHandler({ HttpMessageNotReadableException.class, MethodArgumentNotValidException.class, - HttpMediaTypeNotSupportedException.class}) + HttpMediaTypeNotSupportedException.class, + MethodArgumentNotValidException.class}) public ResponseEntity handleTypeMismatchException(final Exception e) { final CommonExceptionType commonExceptionType = CommonExceptionType.INVALID_REQUEST_BODY; final ExceptionResponse exceptionResponse = new ExceptionResponse(commonExceptionType.errorCode(), commonExceptionType.errorMessage()); diff --git a/backend/src/main/java/com/now/naaga/letter/presentation/LetterController.java b/backend/src/main/java/com/now/naaga/letter/presentation/LetterController.java index af9b288f5..e94b8a69a 100644 --- a/backend/src/main/java/com/now/naaga/letter/presentation/LetterController.java +++ b/backend/src/main/java/com/now/naaga/letter/presentation/LetterController.java @@ -12,6 +12,8 @@ import com.now.naaga.player.presentation.dto.PlayerRequest; import java.net.URI; import java.util.List; + +import jakarta.validation.Valid; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -34,7 +36,7 @@ public LetterController(final LetterService letterService) { @PostMapping public ResponseEntity createLetter(@Auth final PlayerRequest playerRequest, - @RequestBody final LetterRequest letterRequest) { + @Valid @RequestBody final LetterRequest letterRequest) { final CreateLetterCommand createLetterCommand = CreateLetterCommand.of(playerRequest, letterRequest); final Letter letter = letterService.writeLetter(createLetterCommand); return ResponseEntity diff --git a/backend/src/main/java/com/now/naaga/letter/presentation/dto/LetterRequest.java b/backend/src/main/java/com/now/naaga/letter/presentation/dto/LetterRequest.java index 03af3dc15..c640a5a19 100644 --- a/backend/src/main/java/com/now/naaga/letter/presentation/dto/LetterRequest.java +++ b/backend/src/main/java/com/now/naaga/letter/presentation/dto/LetterRequest.java @@ -1,6 +1,8 @@ package com.now.naaga.letter.presentation.dto; -public record LetterRequest(String message, +import jakarta.validation.constraints.NotBlank; + +public record LetterRequest(@NotBlank String message, Double latitude, Double longitude) { } From 03443e89fc6831f25ce6a72c142033e6a24d09c1 Mon Sep 17 00:00:00 2001 From: dooboocookie Date: Wed, 8 Nov 2023 10:43:32 +0900 Subject: [PATCH 3/3] =?UTF-8?q?chore:=20=EC=BB=A8=ED=94=8C=EB=A6=AD?= =?UTF-8?q?=ED=8A=B8=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../letter/presentation/LetterControllerTest.java | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/backend/src/test/java/com/now/naaga/letter/presentation/LetterControllerTest.java b/backend/src/test/java/com/now/naaga/letter/presentation/LetterControllerTest.java index c0b0d76b0..c3e17cc0a 100644 --- a/backend/src/test/java/com/now/naaga/letter/presentation/LetterControllerTest.java +++ b/backend/src/test/java/com/now/naaga/letter/presentation/LetterControllerTest.java @@ -40,15 +40,6 @@ import java.util.List; import static com.now.naaga.common.exception.CommonExceptionType.INVALID_REQUEST_BODY; -import static com.now.naaga.common.fixture.PositionFixture.*; -import static com.now.naaga.game.domain.GameStatus.DONE; -import static com.now.naaga.game.exception.GameExceptionType.NOT_EXIST_IN_PROGRESS; -import static com.now.naaga.player.exception.PlayerExceptionType.PLAYER_NOT_FOUND; -import static org.assertj.core.api.SoftAssertions.assertSoftly; - -import java.time.LocalDateTime; -import java.util.List; - import static com.now.naaga.common.exception.CommonExceptionType.INVALID_REQUEST_PARAMETERS; import static com.now.naaga.common.fixture.PositionFixture.*; import static com.now.naaga.game.domain.GameStatus.DONE; @@ -601,7 +592,5 @@ protected void setUp() { .isEqualTo(expected); } ); - - } }