From f003f01077618b21182dfc9f78dd28ae5627bde4 Mon Sep 17 00:00:00 2001 From: mperor Date: Thu, 19 Jun 2025 23:04:23 +0200 Subject: [PATCH] Add Java Bean Validation with test case --- spring-web-lab/pom.xml | 4 ++++ .../spring/controller/HelloController.java | 23 ++++++++++++++---- .../src/main/resources/application.yml | 6 ++++- .../controller/HelloControllerTest.java | 24 +++++++++++++++++++ 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/spring-web-lab/pom.xml b/spring-web-lab/pom.xml index 503eccc..c53f9f5 100644 --- a/spring-web-lab/pom.xml +++ b/spring-web-lab/pom.xml @@ -25,6 +25,10 @@ springdoc-openapi-starter-webmvc-ui 2.8.8 + + org.springframework.boot + spring-boot-starter-validation + \ No newline at end of file diff --git a/spring-web-lab/src/main/java/pl/mperor/lab/spring/controller/HelloController.java b/spring-web-lab/src/main/java/pl/mperor/lab/spring/controller/HelloController.java index 596ddc4..7c38bd0 100644 --- a/spring-web-lab/src/main/java/pl/mperor/lab/spring/controller/HelloController.java +++ b/spring-web-lab/src/main/java/pl/mperor/lab/spring/controller/HelloController.java @@ -1,8 +1,8 @@ package pl.mperor.lab.spring.controller; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import jakarta.validation.Valid; +import jakarta.validation.constraints.*; +import org.springframework.web.bind.annotation.*; @RequestMapping("/hello") @RestController @@ -13,4 +13,19 @@ String hello() { return "πŸ—ΊοΈ Hello World!"; } -} + @PostMapping("/{name}") + String welcomeName(@PathVariable @NotBlank String name) { + return "🀚 Welcome %s!".formatted(name); + } + + @PostMapping("/user") + String welcomeUser(@RequestBody @Valid HelloUser user) { + return "🀚 Welcome πŸ‘€ %s (%s)!".formatted(user.name, user.email); + } + + record HelloUser(@NotNull @Size(min = 3) String name, + @NotNull @Email String email, + @Min(0) Integer age, + @Pattern(regexp = "^\\d{3}[\\s-]?\\d{3}[\\s-]?\\d{3}$") String phoneNumber) { + } +} \ No newline at end of file diff --git a/spring-web-lab/src/main/resources/application.yml b/spring-web-lab/src/main/resources/application.yml index 046a23c..65c27a1 100644 --- a/spring-web-lab/src/main/resources/application.yml +++ b/spring-web-lab/src/main/resources/application.yml @@ -16,4 +16,8 @@ springdoc: swagger-ui: use-root-path: true - display-request-duration: true \ No newline at end of file + display-request-duration: true + +server: + error: + include-binding-errors: always \ No newline at end of file diff --git a/spring-web-lab/src/test/java/pl/mperor/lab/spring/controller/HelloControllerTest.java b/spring-web-lab/src/test/java/pl/mperor/lab/spring/controller/HelloControllerTest.java index dfcbc31..af46a07 100644 --- a/spring-web-lab/src/test/java/pl/mperor/lab/spring/controller/HelloControllerTest.java +++ b/spring-web-lab/src/test/java/pl/mperor/lab/spring/controller/HelloControllerTest.java @@ -3,6 +3,7 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; import org.springframework.test.web.servlet.assertj.MockMvcTester; @WebMvcTest @@ -19,4 +20,27 @@ void shouldReturnHelloWorld() { .assertThat().hasStatusOk() .bodyText().isEqualTo("πŸ—ΊοΈ Hello World!"); } + + @Test + void shouldReturnWelcomeWhenNameNotBlank() { + tester.post() + .uri("/hello/{name}", "NotBlank") + .exchange() + .assertThat().hasStatusOk() + .bodyText().isEqualTo("🀚 Welcome NotBlank!"); + } + + @Test + void shouldReturn400WhenNotValidContent() { + tester.post() + .uri("/hello/user") + .contentType(MediaType.APPLICATION_JSON) + .content(""" + { + "name": "josh", + "email": "not a valid email" + }""") + .exchange() + .assertThat().hasStatus4xxClientError(); + } } \ No newline at end of file