Skip to content

Commit

Permalink
xImprove camel-quarkus-bean-validator test coverage apache#3567
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriOndrusek committed Mar 1, 2022
1 parent 6fe10c5 commit 1e9f1ed
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 10 deletions.
30 changes: 30 additions & 0 deletions docs/modules/ROOT/pages/reference/extensions/bean-validator.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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]).

Original file line number Diff line number Diff line change
@@ -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]).
20 changes: 20 additions & 0 deletions extensions/bean-validator/runtime/src/main/doc/usage.adoc
Original file line number Diff line number Diff line change
@@ -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 {
}
----
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@ public void configure() throws Exception {

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");
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -48,5 +48,4 @@ public String getLicensePlate() {
public void setLicensePlate(String licensePlate) {
this.licensePlate = licensePlate;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.model;

import io.quarkus.runtime.annotations.RegisterForReflection;

@RegisterForReflection
public interface OptionalChecks {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

0 comments on commit 1e9f1ed

Please sign in to comment.