This is an automated email from the ASF dual-hosted git repository. jamesnetherton pushed a commit to branch 2.7.x in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
commit f73cfe7325e826e024d19d9da28dd4e82327be60 Author: JiriOndrusek <[email protected]> AuthorDate: Wed Feb 23 12:01:27 2022 +0100 xImprove camel-quarkus-bean-validator test coverage #3567 --- .../pages/reference/extensions/bean-validator.adoc | 30 ++++++++++++ .../runtime/src/main/doc/limitations.adoc | 2 + .../bean-validator/runtime/src/main/doc/usage.adoc | 20 ++++++++ .../bean/validator/it/BeanValidatorResource.java | 21 ++++++--- .../bean/validator/it/BeanValidatorRoute.java | 4 ++ .../validator/it/ValidatorFactoryCustomizer.java | 55 ++++++++++++++++++++++ .../component/bean/validator/it/model/Car.java | 3 +- .../OptionalChecks.java} | 15 ++---- .../bean/validator/it/BeanValidatorTest.java | 10 +++- 9 files changed, 139 insertions(+), 21 deletions(-) diff --git a/docs/modules/ROOT/pages/reference/extensions/bean-validator.adoc b/docs/modules/ROOT/pages/reference/extensions/bean-validator.adoc index c983099..89a9c98 100644 --- a/docs/modules/ROOT/pages/reference/extensions/bean-validator.adoc +++ b/docs/modules/ROOT/pages/reference/extensions/bean-validator.adoc @@ -38,3 +38,33 @@ Or add the coordinates to your existing project: ---- Check the xref:user-guide/index.adoc[User guide] for more information about writing Camel Quarkus applications. + +== Usage + +=== Configuring the ValidatorFactory + +Implementation of this extension leverages the https://quarkus.io/guides/validation[Quarkus Hibernate Validator extension]. + +Therefore it is not possible to configure the `ValidatorFactory` by Camel's properties ('constraintValidatorFactory`, `messageInterpolator`, `traversableResolver`, `validationProviderResolver` and `validatorFactory`). + +You can configure the `ValidatorFactory` by the creation of beans which will be injected into the default `ValidatorFactory` (created by Quarkus). +See the https://quarkus.io/guides/validation#hibernate-validator-extension-and-cdi[Quarkus CDI documentation] for more information. + +=== Custom validation groups in native mode + +When using custom validation groups in native mode, all the interfaces need to be registered for reflection (see the https://quarkus.io/guides/writing-native-applications-tips#register-reflection[documentation]). + +Example: +[source,java] +---- +@RegisterForReflection +public interface OptionalChecks { +} +---- + + +== Camel Quarkus limitations + +It is not possible to describe your constraints as XML (by providing the file META-INF/validation.xml), only Java annotations are supported. +This is caused by the limitation of the Quarkus Hibernate Validator extension (see the https://github.com/quarkusio/quarkus/issues/24027[issue]). + diff --git a/extensions/bean-validator/runtime/src/main/doc/limitations.adoc b/extensions/bean-validator/runtime/src/main/doc/limitations.adoc new file mode 100644 index 0000000..8d0ed15 --- /dev/null +++ b/extensions/bean-validator/runtime/src/main/doc/limitations.adoc @@ -0,0 +1,2 @@ +It is not possible to describe your constraints as XML (by providing the file META-INF/validation.xml), only Java annotations are supported. +This is caused by the limitation of the Quarkus Hibernate Validator extension (see the https://github.com/quarkusio/quarkus/issues/24027[issue]). \ No newline at end of file diff --git a/extensions/bean-validator/runtime/src/main/doc/usage.adoc b/extensions/bean-validator/runtime/src/main/doc/usage.adoc new file mode 100644 index 0000000..1c85a6f --- /dev/null +++ b/extensions/bean-validator/runtime/src/main/doc/usage.adoc @@ -0,0 +1,20 @@ +=== Configuring the ValidatorFactory + +Implementation of this extension leverages the https://quarkus.io/guides/validation[Quarkus Hibernate Validator extension]. + +Therefore it is not possible to configure the `ValidatorFactory` by Camel's properties ('constraintValidatorFactory`, `messageInterpolator`, `traversableResolver`, `validationProviderResolver` and `validatorFactory`). + +You can configure the `ValidatorFactory` by the creation of beans which will be injected into the default `ValidatorFactory` (created by Quarkus). +See the https://quarkus.io/guides/validation#hibernate-validator-extension-and-cdi[Quarkus CDI documentation] for more information. + +=== Custom validation groups in native mode + +When using custom validation groups in native mode, all the interfaces need to be registered for reflection (see the https://quarkus.io/guides/writing-native-applications-tips#register-reflection[documentation]). + +Example: +[source,java] +---- +@RegisterForReflection +public interface OptionalChecks { +} +---- diff --git a/integration-tests/bean-validator/src/main/java/org/apache/camel/quarkus/component/bean/validator/it/BeanValidatorResource.java b/integration-tests/bean-validator/src/main/java/org/apache/camel/quarkus/component/bean/validator/it/BeanValidatorResource.java index 5bf17e0..e648a14 100644 --- a/integration-tests/bean-validator/src/main/java/org/apache/camel/quarkus/component/bean/validator/it/BeanValidatorResource.java +++ b/integration-tests/bean-validator/src/main/java/org/apache/camel/quarkus/component/bean/validator/it/BeanValidatorResource.java @@ -39,18 +39,27 @@ public class BeanValidatorResource { @Inject ProducerTemplate producerTemplate; - @Path("/get/{manufactor}/{plate}") + @Inject + ValidatorFactoryCustomizer.MyMessageInterpolator messageInterpolator; + + @Path("/get/{optional}/{manufactor}/{plate}") @GET @Produces(MediaType.TEXT_PLAIN) - public Response get(@PathParam("manufactor") String manufactor, @PathParam("plate") String plate) throws Exception { - LOG.info("bean-validator: " + manufactor + "/" + plate); - Car car = new Car(manufactor, plate); - Exchange out = producerTemplate.request("direct:start", e -> e.getMessage().setBody(car)); + public Response get(@PathParam("optional") String endpoint, @PathParam("manufactor") String manufactor, + @PathParam("plate") String plate) throws Exception { + return get(new Car(manufactor, plate), endpoint); + } + + private Response get(Car car, String endpoint) throws Exception { + LOG.info("bean-validator: " + car.getManufacturer() + "/" + car.getLicensePlate()); + Exchange out = producerTemplate.request("direct:" + endpoint, e -> e.getMessage().setBody(car)); + if (messageInterpolator.getCount() == 0) { + return Response.status(500, "Interpolator was not used.").build(); + } if (out.isFailed()) { return Response.status(400, "Invalid car").build(); } else { return Response.status(200, "OK").build(); } } - } diff --git a/integration-tests/bean-validator/src/main/java/org/apache/camel/quarkus/component/bean/validator/it/BeanValidatorRoute.java b/integration-tests/bean-validator/src/main/java/org/apache/camel/quarkus/component/bean/validator/it/BeanValidatorRoute.java index 76a7c7d..cc4e30e 100644 --- a/integration-tests/bean-validator/src/main/java/org/apache/camel/quarkus/component/bean/validator/it/BeanValidatorRoute.java +++ b/integration-tests/bean-validator/src/main/java/org/apache/camel/quarkus/component/bean/validator/it/BeanValidatorRoute.java @@ -26,5 +26,9 @@ public class BeanValidatorRoute extends RouteBuilder { from("direct:start") .to("bean-validator:car"); + + from("direct:optional") + .to("bean-validator:xmlCar?group=org.apache.camel.quarkus.component.bean.validator.it.model.OptionalChecks"); } + } diff --git a/integration-tests/bean-validator/src/main/java/org/apache/camel/quarkus/component/bean/validator/it/ValidatorFactoryCustomizer.java b/integration-tests/bean-validator/src/main/java/org/apache/camel/quarkus/component/bean/validator/it/ValidatorFactoryCustomizer.java new file mode 100644 index 0000000..6e797f4 --- /dev/null +++ b/integration-tests/bean-validator/src/main/java/org/apache/camel/quarkus/component/bean/validator/it/ValidatorFactoryCustomizer.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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 org.apache.camel.quarkus.component.bean.validator.it; + +import java.util.Locale; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Produces; +import javax.inject.Singleton; +import javax.validation.MessageInterpolator; + +@ApplicationScoped +public class ValidatorFactoryCustomizer { + + @Produces + @Singleton + public MyMessageInterpolator createMyMessageInterpolator() { + return new MyMessageInterpolator(); + } + + class MyMessageInterpolator implements MessageInterpolator { + + private int count = 0; + + @Override + public String interpolate(String messageTemplate, Context context) { + count++; + return null; + } + + @Override + public String interpolate(String messageTemplate, Context context, Locale locale) { + count++; + return null; + } + + public int getCount() { + return count; + } + } +} diff --git a/integration-tests/bean-validator/src/main/java/org/apache/camel/quarkus/component/bean/validator/it/model/Car.java b/integration-tests/bean-validator/src/main/java/org/apache/camel/quarkus/component/bean/validator/it/model/Car.java index 19db6f8..a184516 100644 --- a/integration-tests/bean-validator/src/main/java/org/apache/camel/quarkus/component/bean/validator/it/model/Car.java +++ b/integration-tests/bean-validator/src/main/java/org/apache/camel/quarkus/component/bean/validator/it/model/Car.java @@ -25,7 +25,7 @@ public class Car { private String manufacturer; @NotNull - @Size(min = 5, max = 14) + @Size(min = 5, max = 14, groups = OptionalChecks.class) private String licensePlate; public Car(String manufacturer, String licencePlate) { @@ -48,5 +48,4 @@ public class Car { public void setLicensePlate(String licensePlate) { this.licensePlate = licensePlate; } - } diff --git a/integration-tests/bean-validator/src/main/java/org/apache/camel/quarkus/component/bean/validator/it/BeanValidatorRoute.java b/integration-tests/bean-validator/src/main/java/org/apache/camel/quarkus/component/bean/validator/it/model/OptionalChecks.java similarity index 70% copy from integration-tests/bean-validator/src/main/java/org/apache/camel/quarkus/component/bean/validator/it/BeanValidatorRoute.java copy to integration-tests/bean-validator/src/main/java/org/apache/camel/quarkus/component/bean/validator/it/model/OptionalChecks.java index 76a7c7d..fc40952 100644 --- a/integration-tests/bean-validator/src/main/java/org/apache/camel/quarkus/component/bean/validator/it/BeanValidatorRoute.java +++ b/integration-tests/bean-validator/src/main/java/org/apache/camel/quarkus/component/bean/validator/it/model/OptionalChecks.java @@ -14,17 +14,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.quarkus.component.bean.validator.it; +package org.apache.camel.quarkus.component.bean.validator.it.model; -import org.apache.camel.builder.RouteBuilder; +import io.quarkus.runtime.annotations.RegisterForReflection; -public class BeanValidatorRoute extends RouteBuilder { - - @Override - public void configure() throws Exception { - errorHandler(noErrorHandler()); - - from("direct:start") - .to("bean-validator:car"); - } +@RegisterForReflection +public interface OptionalChecks { } diff --git a/integration-tests/bean-validator/src/test/java/org/apache/camel/quarkus/component/bean/validator/it/BeanValidatorTest.java b/integration-tests/bean-validator/src/test/java/org/apache/camel/quarkus/component/bean/validator/it/BeanValidatorTest.java index 8e91c67..9a31d80 100644 --- a/integration-tests/bean-validator/src/test/java/org/apache/camel/quarkus/component/bean/validator/it/BeanValidatorTest.java +++ b/integration-tests/bean-validator/src/test/java/org/apache/camel/quarkus/component/bean/validator/it/BeanValidatorTest.java @@ -25,9 +25,15 @@ class BeanValidatorTest { @Test public void test() { - RestAssured.get("/bean-validator/get/honda/123").then().statusCode(400); + //forced optional check + RestAssured.get("/bean-validator/get/optional/honda/123").then().statusCode(400); + //forced optional check + RestAssured.get("/bean-validator/get/optional/honda/DD-AB-123").then().statusCode(200); + //not-forced optional check + RestAssured.get("/bean-validator/get/start/honda/123").then().statusCode(200); + //not-forced optional check + RestAssured.get("/bean-validator/get/start/honda/DD-AB-12").then().statusCode(200); - RestAssured.get("/bean-validator/get/honda/DD-AB-123").then().statusCode(200); } }
