Reflect grouping in directory structure
This commit is contained in:
7
framework/validation/README.adoc
Normal file
7
framework/validation/README.adoc
Normal file
@@ -0,0 +1,7 @@
|
||||
Tests if validation is working.
|
||||
|
||||
Tests:
|
||||
|
||||
* Method validation
|
||||
* Configuration properties validation
|
||||
* WebMVC Controller response body validation
|
||||
21
framework/validation/build.gradle
Normal file
21
framework/validation/build.gradle
Normal file
@@ -0,0 +1,21 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'org.springframework.boot'
|
||||
id 'org.springframework.aot.smoke-test'
|
||||
id 'org.graalvm.buildtools.native'
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))
|
||||
implementation("org.springframework.boot:spring-boot-starter-web")
|
||||
implementation("org.springframework.boot:spring-boot-starter-validation")
|
||||
|
||||
testImplementation("org.springframework.boot:spring-boot-starter-test")
|
||||
|
||||
appTestImplementation(project(":aot-smoke-test-support"))
|
||||
appTestImplementation("org.awaitility:awaitility:4.2.0")
|
||||
}
|
||||
|
||||
aotSmokeTest {
|
||||
webApplication = true
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.validation;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.awaitility.Awaitility;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.aot.smoketest.support.assertj.AssertableOutput;
|
||||
import org.springframework.aot.smoketest.support.junit.ApplicationTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@ApplicationTest
|
||||
class ValidationApplicationAotTests {
|
||||
|
||||
@Test
|
||||
void methodValidationWorks(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
assertThat(output).hasSingleLineContaining("this.someService.hello('world'): hello world")
|
||||
.hasSingleLineContaining("this.someService.hello(''): Got expected exception").hasNoLinesContaining(
|
||||
"this.someService.hello(''): Invocation worked, this should not have happened!");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void configurationPropertiesValidationWorks() {
|
||||
// No way to test that without letting the application startup fail
|
||||
}
|
||||
|
||||
@Test
|
||||
void controllerValidationWorks(WebTestClient client) {
|
||||
client.post().uri("/hello").contentType(MediaType.APPLICATION_JSON).bodyValue("{\"name\": \"world\"}")
|
||||
.exchange().expectStatus().isOk().expectBody().consumeWith(
|
||||
(result) -> assertThat(new String(result.getResponseBodyContent())).isEqualTo("Hello world"));
|
||||
client.post().uri("/hello").contentType(MediaType.APPLICATION_JSON).bodyValue("{\"name\": \"\"}").exchange()
|
||||
.expectStatus().isBadRequest();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.validation;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ValidationApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ValidationApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.example.validation.controller;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
class TestRestController {
|
||||
|
||||
@PostMapping(value = "hello", consumes = MediaType.APPLICATION_JSON_VALUE)
|
||||
public String hello(@Validated @RequestBody Dto dto) {
|
||||
return "Hello " + dto.getName();
|
||||
}
|
||||
|
||||
static class Dto {
|
||||
|
||||
@NotBlank
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.example.validation.method;
|
||||
|
||||
import jakarta.validation.ConstraintViolationException;
|
||||
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
class MethodCLR implements CommandLineRunner {
|
||||
|
||||
private final SomeService someService;
|
||||
|
||||
MethodCLR(SomeService someService) {
|
||||
this.someService = someService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
System.out.printf("this.someService.hello('world'): %s%n", this.someService.hello("world"));
|
||||
try {
|
||||
this.someService.hello("");
|
||||
System.out.println("this.someService.hello(''): Invocation worked, this should not have happened!");
|
||||
}
|
||||
catch (ConstraintViolationException e) {
|
||||
System.out.println("this.someService.hello(''): Got expected exception");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.example.validation.method;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
@Validated
|
||||
@Service
|
||||
class SomeService {
|
||||
|
||||
public String hello(@NotBlank String name) {
|
||||
return "hello " + name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.example.validation.properties;
|
||||
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@EnableConfigurationProperties(TestConfigurationProperties.class)
|
||||
class PropertiesCLR implements CommandLineRunner {
|
||||
|
||||
private final TestConfigurationProperties testConfigurationProperties;
|
||||
|
||||
PropertiesCLR(TestConfigurationProperties testConfigurationProperties) {
|
||||
this.testConfigurationProperties = testConfigurationProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
System.out.printf("testConfigurationProperties.getField(): %s%n", testConfigurationProperties.getField());
|
||||
System.out.printf("testConfigurationProperties.getNested().getField(): %s%n",
|
||||
testConfigurationProperties.getNested().getField());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.example.validation.properties;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
@ConfigurationProperties(prefix = "test")
|
||||
@Validated
|
||||
class TestConfigurationProperties {
|
||||
|
||||
@NotBlank
|
||||
private String field;
|
||||
|
||||
@Valid
|
||||
private Nested nested = new Nested();
|
||||
|
||||
public String getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
public void setField(String field) {
|
||||
this.field = field;
|
||||
}
|
||||
|
||||
public Nested getNested() {
|
||||
return nested;
|
||||
}
|
||||
|
||||
static class Nested {
|
||||
|
||||
@NotBlank
|
||||
private String field;
|
||||
|
||||
public String getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
public void setField(String field) {
|
||||
this.field = field;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
test.field=not-blank
|
||||
test.nested.field=not-blank
|
||||
Reference in New Issue
Block a user