Skip to content

Commit

Permalink
Improve camel-quarkus-bean-validator test coverage #3567
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriOndrusek committed Feb 28, 2022
1 parent 6fe10c5 commit 67610a9
Show file tree
Hide file tree
Showing 12 changed files with 347 additions and 36 deletions.
25 changes: 25 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,28 @@ 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 https://quarkus.io/guides/validation[Quarkus extension].

Therefore it is not possible to configure ValidatorFactory by Camel's properties ('constraintValidatorFactory`, `messageInterpolator`, `traversableResolver`, `validationProviderResolver` and `validatorFactory`).

You can configure ValidatorFactory by creation of beans which will be injected into the default validatorFactory (created by Quarkus).
See https://quarkus.io/guides/validation#hibernate-validator-extension-and-cdi[documentation] for more information.

=== Custom validation groups in native mode

For native mode, it is necessary to register definitions of custom validation groups for reflection.
To do this, annotate interface definition with `@RegisterForReflection`.

Example:
[source,java]
----
@RegisterForReflection
public interface OptionalChecks {
}
----

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 @@ -28,6 +28,8 @@
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.quarkus.component.bean.validator.it.model.Car;
import org.apache.camel.quarkus.component.bean.validator.it.model.CarWithAnnotations;
import org.apache.camel.quarkus.component.bean.validator.it.model.CarWithoutAnnotations;
import org.jboss.logging.Logger;

@Path("/bean-validator")
Expand All @@ -39,13 +41,36 @@ public class BeanValidatorResource {
@Inject
ProducerTemplate producerTemplate;

@Path("/get/{manufactor}/{plate}")
@Inject
ValidatorFactoryCustomizer.MyMessageInterpolator messageInterpolator;

@Path("/get/xml/{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 getXml(@PathParam("manufactor") String manufactor, @PathParam("plate") String plate) throws Exception {
return get(new CarWithAnnotations(manufactor, plate), "start");
}

@Path("/get/anot/{manufactor}/{plate}")
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response getAnot(@PathParam("manufactor") String manufactor, @PathParam("plate") String plate) throws Exception {
return get(new CarWithAnnotations(manufactor, plate), "start");
}

@Path("/get/optional/{manufactor}/{plate}")
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response getOptional(@PathParam("manufactor") String manufactor, @PathParam("plate") String plate) throws Exception {
return get(new CarWithoutAnnotations(manufactor, plate), "optional");
}

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 {
Expand Down
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 @@ -16,37 +16,13 @@
*/
package org.apache.camel.quarkus.component.bean.validator.it.model;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public interface Car {

public class Car {
String getManufacturer();

@NotNull
private String manufacturer;
void setManufacturer(String manufacturer);

@NotNull
@Size(min = 5, max = 14)
private String licensePlate;

public Car(String manufacturer, String licencePlate) {
this.manufacturer = manufacturer;
this.licensePlate = licencePlate;
}

public String getManufacturer() {
return manufacturer;
}

public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}

public String getLicensePlate() {
return licensePlate;
}

public void setLicensePlate(String licensePlate) {
this.licensePlate = licensePlate;
}
String getLicensePlate();

void setLicensePlate(String licensePlate);
}
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.model;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class CarWithAnnotations implements Car {

@NotNull
private String manufacturer;

@NotNull
@Size(min = 5, max = 14)
private String licensePlate;

public CarWithAnnotations(String manufacturer, String licencePlate) {
this.manufacturer = manufacturer;
this.licensePlate = licencePlate;
}

@Override
public String getManufacturer() {
return manufacturer;
}

@Override
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}

@Override
public String getLicensePlate() {
return licensePlate;
}

@Override
public void setLicensePlate(String licensePlate) {
this.licensePlate = licensePlate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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;

public class CarWithoutAnnotations implements Car {

private String manufacturer;

private String licensePlate;

public CarWithoutAnnotations(String manufacturer, String licencePlate) {
this.manufacturer = manufacturer;
this.licensePlate = licencePlate;
}

@Override
public String getManufacturer() {
return manufacturer;
}

@Override
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}

@Override
public String getLicensePlate() {
return licensePlate;
}

@Override
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
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<validation-config xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration">

<default-provider>org.hibernate.validator.HibernateValidator</default-provider>
<message-interpolator>org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator</message-interpolator>
<constraint-mapping>/constraints-car.xml</constraint-mapping>

</validation-config>

0 comments on commit 67610a9

Please sign in to comment.