diff --git a/backend/src/main/java/net/pengcook/comment/controller/CommentController.java b/backend/src/main/java/net/pengcook/comment/controller/CommentController.java index df44f4fa..79998007 100644 --- a/backend/src/main/java/net/pengcook/comment/controller/CommentController.java +++ b/backend/src/main/java/net/pengcook/comment/controller/CommentController.java @@ -1,5 +1,6 @@ package net.pengcook.comment.controller; +import jakarta.validation.Valid; import java.util.List; import lombok.RequiredArgsConstructor; import net.pengcook.authentication.domain.UserInfo; @@ -30,7 +31,7 @@ public List readComments(@PathVariable long recipeId, @ @PostMapping @ResponseStatus(HttpStatus.CREATED) - public void createComment(@RequestBody CreateCommentRequest request, @LoginUser UserInfo userInfo) { + public void createComment(@RequestBody @Valid CreateCommentRequest request, @LoginUser UserInfo userInfo) { commentService.createComment(request, userInfo); } } diff --git a/backend/src/main/java/net/pengcook/comment/dto/CreateCommentRequest.java b/backend/src/main/java/net/pengcook/comment/dto/CreateCommentRequest.java index 8ad6d48d..3fe44eed 100644 --- a/backend/src/main/java/net/pengcook/comment/dto/CreateCommentRequest.java +++ b/backend/src/main/java/net/pengcook/comment/dto/CreateCommentRequest.java @@ -1,4 +1,7 @@ package net.pengcook.comment.dto; -public record CreateCommentRequest(long recipeId, String message) { +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotBlank; + +public record CreateCommentRequest(@Min(1) long recipeId, @NotBlank String message) { } diff --git a/backend/src/test/java/net/pengcook/comment/controller/CommentControllerTest.java b/backend/src/test/java/net/pengcook/comment/controller/CommentControllerTest.java index 540c4dcd..e1a38edc 100644 --- a/backend/src/test/java/net/pengcook/comment/controller/CommentControllerTest.java +++ b/backend/src/test/java/net/pengcook/comment/controller/CommentControllerTest.java @@ -16,6 +16,9 @@ import net.pengcook.comment.dto.CreateCommentRequest; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullAndEmptySource; +import org.junit.jupiter.params.provider.ValueSource; import org.springframework.test.context.jdbc.Sql; @WithLoginUserTest @@ -69,4 +72,48 @@ void createComment() { .then().log().all() .statusCode(201); } + + @Test + @DisplayName("댓글 등록 시 레시피 아이디에 0을 입력하면 예외가 발생한다.") + void createCommentWithInvalidRecipeId() { + CreateCommentRequest request = new CreateCommentRequest(0L, "thank you!"); + + RestAssured.given(spec).log().all() + .filter(document(DEFAULT_RESTDOCS_PATH, + "레시피에 댓글을 등록할 때 유효하지 않은 레시피 아이디를 입력하면 예외가 발생합니다.", + "댓글 조회 API", + requestFields( + fieldWithPath("recipeId").description("레시피 아이디"), + fieldWithPath("message").description("댓글 내용") + ) + )) + .contentType(ContentType.JSON) + .body(request) + .when().post("api/comments") + .then().log().all() + .statusCode(400); + } + + @ParameterizedTest + @NullAndEmptySource + @ValueSource(strings = {" "}) + @DisplayName("댓글 등록 시 빈 댓글 내용을 입력하면 예외가 발생한다.") + void createCommentWithBlankMessage(String message) { + CreateCommentRequest request = new CreateCommentRequest(1L, message); + + RestAssured.given(spec).log().all() + .filter(document(DEFAULT_RESTDOCS_PATH, + "레시피에 댓글을 등록할 때 유효하지 않은 댓글 내용을 입력하면 예외가 발생합니다.", + "댓글 조회 API", + requestFields( + fieldWithPath("recipeId").description("레시피 아이디"), + fieldWithPath("message").description("댓글 내용") + ) + )) + .contentType(ContentType.JSON) + .body(request) + .when().post("api/comments") + .then().log().all() + .statusCode(400); + } }