Skip to content

Commit

Permalink
✨ validate fields in CreateCommentRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
hyxrxn committed Aug 4, 2024
1 parent 5925ec5 commit c56251f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -30,7 +31,7 @@ public List<CommentOfRecipeResponse> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

0 comments on commit c56251f

Please sign in to comment.