This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new b1e8f08 CAMEL-16946: bean-validator, validate all elements if body
is of type Iterable (#6214)
b1e8f08 is described below
commit b1e8f08f416b0f8b5c6d0117b9f75b7063ddf7d2
Author: mschnitzler <[email protected]>
AuthorDate: Wed Oct 6 06:13:39 2021 +0200
CAMEL-16946: bean-validator, validate all elements if body is of type
Iterable (#6214)
* CAMEL-16946: validate all elements if body is of type Iterable
* CAMEL-16946: minor readability improvement
---
.../bean/validator/BeanValidatorProducer.java | 23 ++-
.../bean/validator/BeanValidatorRouteTest.java | 223 ++++++++++++++-------
2 files changed, 169 insertions(+), 77 deletions(-)
diff --git
a/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorProducer.java
b/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorProducer.java
index bda334e..f5f7361 100644
---
a/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorProducer.java
+++
b/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorProducer.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel.component.bean.validator;
+import java.util.HashSet;
import java.util.Set;
import javax.validation.ConstraintViolation;
@@ -39,13 +40,13 @@ public class BeanValidatorProducer extends DefaultProducer {
@Override
public void process(Exchange exchange) throws Exception {
- Object bean = exchange.getIn().getBody();
- Set<ConstraintViolation<Object>> constraintViolations;
+ Set<ConstraintViolation<Object>> constraintViolations = new
HashSet<>();
- if (this.group != null) {
- constraintViolations =
validatorFactory.getValidator().validate(bean, group);
+ if (exchange.getIn().getBody() instanceof Iterable) {
+ Iterable<?> body = exchange.getIn().getBody(Iterable.class);
+ body.forEach(b ->
constraintViolations.addAll(getConstraintViolationsForSingleBean(b,
this.group)));
} else {
- constraintViolations =
validatorFactory.getValidator().validate(bean);
+
constraintViolations.addAll(getConstraintViolationsForSingleBean(exchange.getIn().getBody(),
this.group));
}
if (!constraintViolations.isEmpty()) {
@@ -69,4 +70,16 @@ public class BeanValidatorProducer extends DefaultProducer {
this.group = group;
}
+ private Set<ConstraintViolation<Object>>
getConstraintViolationsForSingleBean(Object bean, Class<?> group) {
+ Set<ConstraintViolation<Object>> constraintViolations;
+
+ if (this.group != null) {
+ constraintViolations =
validatorFactory.getValidator().validate(bean, group);
+ } else {
+ constraintViolations =
validatorFactory.getValidator().validate(bean);
+ }
+
+ return constraintViolations;
+ }
+
}
diff --git
a/components/camel-bean-validator/src/test/java/org/apache/camel/component/bean/validator/BeanValidatorRouteTest.java
b/components/camel-bean-validator/src/test/java/org/apache/camel/component/bean/validator/BeanValidatorRouteTest.java
index 6b4a260..b4c9214 100644
---
a/components/camel-bean-validator/src/test/java/org/apache/camel/component/bean/validator/BeanValidatorRouteTest.java
+++
b/components/camel-bean-validator/src/test/java/org/apache/camel/component/bean/validator/BeanValidatorRouteTest.java
@@ -16,8 +16,10 @@
*/
package org.apache.camel.component.bean.validator;
+import java.util.Arrays;
import java.util.Locale;
import java.util.Set;
+import java.util.stream.Stream;
import javax.validation.ConstraintViolation;
@@ -27,8 +29,11 @@ import org.apache.camel.Processor;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.condition.DisabledOnOs;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -36,7 +41,8 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.condition.OS.AIX;
-public class BeanValidatorRouteTest extends CamelTestSupport {
+@TestInstance(TestInstance.Lifecycle.PER_CLASS)
+class BeanValidatorRouteTest extends CamelTestSupport {
private Locale origLocale;
@BeforeEach
@@ -51,12 +57,13 @@ public class BeanValidatorRouteTest extends
CamelTestSupport {
}
@DisabledOnOs(AIX)
- @Test
- void validateShouldSuccessWithImpliciteDefaultGroup() {
+ @ParameterizedTest
+ @MethodSource("provideValidCars")
+ void validateShouldSuccessWithImpliciteDefaultGroup(Object cars) {
Exchange exchange = template.request("bean-validator://x", new
Processor() {
public void process(Exchange exchange) {
- exchange.getIn().setBody(createCar("BMW", "DD-AB-123"));
+ exchange.getIn().setBody(cars);
}
});
@@ -64,12 +71,13 @@ public class BeanValidatorRouteTest extends
CamelTestSupport {
}
@DisabledOnOs(AIX)
- @Test
- void validateShouldSuccessWithExpliciteDefaultGroup() {
+ @ParameterizedTest
+ @MethodSource("provideValidCars")
+ void validateShouldSuccessWithExpliciteDefaultGroup(Object cars) {
Exchange exchange =
template.request("bean-validator://x?group=javax.validation.groups.Default",
new Processor() {
public void process(Exchange exchange) {
- exchange.getIn().setBody(createCar("BMW", "DD-AB-123"));
+ exchange.getIn().setBody(cars);
}
});
@@ -77,14 +85,14 @@ public class BeanValidatorRouteTest extends
CamelTestSupport {
}
@DisabledOnOs(AIX)
- @Test
- void validateShouldFailWithImpliciteDefaultGroup() {
+ @ParameterizedTest
+ @MethodSource("provideInvalidCarsWithoutLicensePlate")
+ void validateShouldFailWithImpliciteDefaultGroup(Object cars, int
numberOfViolations) {
final String url = "bean-validator://x";
- final Car car = createCar("BMW", null);
try {
- template.requestBody(url, car);
+ template.requestBody(url, cars);
fail("should throw exception");
} catch (CamelExecutionException e) {
assertIsInstanceOf(BeanValidationException.class, e.getCause());
@@ -92,18 +100,19 @@ public class BeanValidatorRouteTest extends
CamelTestSupport {
BeanValidationException exception = (BeanValidationException)
e.getCause();
Set<ConstraintViolation<Object>> constraintViolations =
exception.getConstraintViolations();
- assertEquals(1, constraintViolations.size());
- ConstraintViolation<Object> constraintViolation =
constraintViolations.iterator().next();
- assertEquals("licensePlate",
constraintViolation.getPropertyPath().toString());
- assertEquals(null, constraintViolation.getInvalidValue());
- assertEquals("must not be null", constraintViolation.getMessage());
+ assertEquals(numberOfViolations, constraintViolations.size());
+ constraintViolations.forEach(cv -> {
+ assertEquals("licensePlate", cv.getPropertyPath().toString());
+ assertEquals(null, cv.getInvalidValue());
+ assertEquals("must not be null", cv.getMessage());
+ });
}
- car.setLicensePlate("D-A");
+ setLicensePlates(cars, "D-A");
Exchange exchange = template.request(url, new Processor() {
public void process(Exchange exchange) {
- exchange.getIn().setBody(car);
+ exchange.getIn().setBody(cars);
}
});
@@ -111,14 +120,14 @@ public class BeanValidatorRouteTest extends
CamelTestSupport {
}
@DisabledOnOs(AIX)
- @Test
- void validateShouldFailWithExpliciteDefaultGroup() {
+ @ParameterizedTest
+ @MethodSource("provideInvalidCarsWithoutLicensePlate")
+ void validateShouldFailWithExpliciteDefaultGroup(Object cars, int
numberOfViolations) {
final String url =
"bean-validator://x?group=javax.validation.groups.Default";
- final Car car = createCar("BMW", null);
try {
- template.requestBody(url, car);
+ template.requestBody(url, cars);
fail("should throw exception");
} catch (CamelExecutionException e) {
assertIsInstanceOf(BeanValidationException.class, e.getCause());
@@ -126,18 +135,19 @@ public class BeanValidatorRouteTest extends
CamelTestSupport {
BeanValidationException exception = (BeanValidationException)
e.getCause();
Set<ConstraintViolation<Object>> constraintViolations =
exception.getConstraintViolations();
- assertEquals(1, constraintViolations.size());
- ConstraintViolation<Object> constraintViolation =
constraintViolations.iterator().next();
- assertEquals("licensePlate",
constraintViolation.getPropertyPath().toString());
- assertEquals(null, constraintViolation.getInvalidValue());
- assertEquals("must not be null", constraintViolation.getMessage());
+ assertEquals(numberOfViolations, constraintViolations.size());
+ constraintViolations.forEach(cv -> {
+ assertEquals("licensePlate", cv.getPropertyPath().toString());
+ assertEquals(null, cv.getInvalidValue());
+ assertEquals("must not be null", cv.getMessage());
+ });
}
- car.setLicensePlate("D-A");
+ setLicensePlates(cars, "D-A");
Exchange exchange = template.request(url, new Processor() {
public void process(Exchange exchange) {
- exchange.getIn().setBody(car);
+ exchange.getIn().setBody(cars);
}
});
@@ -145,14 +155,14 @@ public class BeanValidatorRouteTest extends
CamelTestSupport {
}
@DisabledOnOs(AIX)
- @Test
- void validateShouldFailWithOptionalChecksGroup() {
+ @ParameterizedTest
+ @MethodSource("provideInvalidCarsWithShortLicensePlate")
+ void validateShouldFailWithOptionalChecksGroup(Object cars, int
numberOfViolations) {
final String url =
"bean-validator://x?group=org.apache.camel.component.bean.validator.OptionalChecks";
- final Car car = createCar("BMW", "D-A");
try {
- template.requestBody(url, car);
+ template.requestBody(url, cars);
fail("should throw exception");
} catch (CamelExecutionException e) {
assertIsInstanceOf(BeanValidationException.class, e.getCause());
@@ -160,18 +170,19 @@ public class BeanValidatorRouteTest extends
CamelTestSupport {
BeanValidationException exception = (BeanValidationException)
e.getCause();
Set<ConstraintViolation<Object>> constraintViolations =
exception.getConstraintViolations();
- assertEquals(1, constraintViolations.size());
- ConstraintViolation<Object> constraintViolation =
constraintViolations.iterator().next();
- assertEquals("licensePlate",
constraintViolation.getPropertyPath().toString());
- assertEquals("D-A", constraintViolation.getInvalidValue());
- assertEquals("size must be between 5 and 14",
constraintViolation.getMessage());
+ assertEquals(numberOfViolations, constraintViolations.size());
+ constraintViolations.forEach(cv -> {
+ assertEquals("licensePlate", cv.getPropertyPath().toString());
+ assertEquals("D-A", cv.getInvalidValue());
+ assertEquals("size must be between 5 and 14", cv.getMessage());
+ });
}
- car.setLicensePlate("DD-AB-123");
+ setLicensePlates(cars, "DD-AB-123");
Exchange exchange = template.request(url, new Processor() {
public void process(Exchange exchange) {
- exchange.getIn().setBody(car);
+ exchange.getIn().setBody(cars);
}
});
@@ -179,14 +190,14 @@ public class BeanValidatorRouteTest extends
CamelTestSupport {
}
@DisabledOnOs(AIX)
- @Test
- void validateShouldFailWithOrderedChecksGroup() {
+ @ParameterizedTest
+ @MethodSource("provideInvalidCarsWithoutManufacturer")
+ void validateShouldFailWithOrderedChecksGroup(Object cars, int
numberOfViolations) {
final String url =
"bean-validator://x?group=org.apache.camel.component.bean.validator.OrderedChecks";
- final Car car = createCar(null, "D-A");
try {
- template.requestBody(url, car);
+ template.requestBody(url, cars);
fail("should throw exception");
} catch (CamelExecutionException e) {
assertIsInstanceOf(BeanValidationException.class, e.getCause());
@@ -194,17 +205,18 @@ public class BeanValidatorRouteTest extends
CamelTestSupport {
BeanValidationException exception = (BeanValidationException)
e.getCause();
Set<ConstraintViolation<Object>> constraintViolations =
exception.getConstraintViolations();
- assertEquals(1, constraintViolations.size());
- ConstraintViolation<Object> constraintViolation =
constraintViolations.iterator().next();
- assertEquals("manufacturer",
constraintViolation.getPropertyPath().toString());
- assertEquals(null, constraintViolation.getInvalidValue());
- assertEquals("must not be null", constraintViolation.getMessage());
+ assertEquals(numberOfViolations, constraintViolations.size());
+ constraintViolations.forEach(cv -> {
+ assertEquals("manufacturer", cv.getPropertyPath().toString());
+ assertEquals(null, cv.getInvalidValue());
+ assertEquals("must not be null", cv.getMessage());
+ });
}
- car.setManufacturer("BMW");
+ setManufacturer(cars, "BMW");
try {
- template.requestBody(url, car);
+ template.requestBody(url, cars);
fail("should throw exception");
} catch (CamelExecutionException e) {
assertIsInstanceOf(BeanValidationException.class, e.getCause());
@@ -212,18 +224,19 @@ public class BeanValidatorRouteTest extends
CamelTestSupport {
BeanValidationException exception = (BeanValidationException)
e.getCause();
Set<ConstraintViolation<Object>> constraintViolations =
exception.getConstraintViolations();
- assertEquals(1, constraintViolations.size());
- ConstraintViolation<Object> constraintViolation =
constraintViolations.iterator().next();
- assertEquals("licensePlate",
constraintViolation.getPropertyPath().toString());
- assertEquals("D-A", constraintViolation.getInvalidValue());
- assertEquals("size must be between 5 and 14",
constraintViolation.getMessage());
+ assertEquals(numberOfViolations, constraintViolations.size());
+ constraintViolations.forEach(cv -> {
+ assertEquals("licensePlate", cv.getPropertyPath().toString());
+ assertEquals("D-A", cv.getInvalidValue());
+ assertEquals("size must be between 5 and 14", cv.getMessage());
+ });
}
- car.setLicensePlate("DD-AB-123");
+ setLicensePlates(cars, "DD-AB-123");
Exchange exchange = template.request(url, new Processor() {
public void process(Exchange exchange) {
- exchange.getIn().setBody(car);
+ exchange.getIn().setBody(cars);
}
});
@@ -231,15 +244,15 @@ public class BeanValidatorRouteTest extends
CamelTestSupport {
}
@DisabledOnOs(AIX)
- @Test
- void validateShouldSuccessWithRedefinedDefaultGroup() {
+ @ParameterizedTest
+ @MethodSource("provideCarsWithRedefinedDefaultGroup")
+ void validateShouldSuccessWithRedefinedDefaultGroup(Object cars) {
final String url = "bean-validator://x";
- final Car car = new CarWithRedefinedDefaultGroup(null, "DD-AB-123");
Exchange exchange = template.request(url, new Processor() {
public void process(Exchange exchange) {
- exchange.getIn().setBody(car);
+ exchange.getIn().setBody(cars);
}
});
@@ -247,14 +260,14 @@ public class BeanValidatorRouteTest extends
CamelTestSupport {
}
@DisabledOnOs(AIX)
- @Test
- void validateShouldFailWithRedefinedDefaultGroup() {
+ @ParameterizedTest
+ @MethodSource("provideCarsWithRedefinedDefaultGroupAndShortLicencePlate")
+ void validateShouldFailWithRedefinedDefaultGroup(Object cars, int
numberOfViolations) {
final String url = "bean-validator://x";
- final Car car = new CarWithRedefinedDefaultGroup(null, "D-A");
try {
- template.requestBody(url, car);
+ template.requestBody(url, cars);
fail("should throw exception");
} catch (CamelExecutionException e) {
assertIsInstanceOf(BeanValidationException.class, e.getCause());
@@ -262,15 +275,81 @@ public class BeanValidatorRouteTest extends
CamelTestSupport {
BeanValidationException exception = (BeanValidationException)
e.getCause();
Set<ConstraintViolation<Object>> constraintViolations =
exception.getConstraintViolations();
- assertEquals(1, constraintViolations.size());
- ConstraintViolation<Object> constraintViolation =
constraintViolations.iterator().next();
- assertEquals("licensePlate",
constraintViolation.getPropertyPath().toString());
- assertEquals("D-A", constraintViolation.getInvalidValue());
- assertEquals("size must be between 5 and 14",
constraintViolation.getMessage());
+ assertEquals(numberOfViolations, constraintViolations.size());
+ constraintViolations.forEach(cv -> {
+ assertEquals("licensePlate", cv.getPropertyPath().toString());
+ assertEquals("D-A", cv.getInvalidValue());
+ assertEquals("size must be between 5 and 14", cv.getMessage());
+ });
}
}
Car createCar(String manufacturer, String licencePlate) {
return new CarWithAnnotations(manufacturer, licencePlate);
}
+
+ private Stream<Arguments> provideValidCars() {
+ return Stream.of(
+ Arguments.of(createCar("BMW", "DD-AB-123")),
+ Arguments.of(Arrays.asList(
+ createCar("BMW", "DD-AB-123"),
+ createCar("VW", "XX-YZ-789"))));
+ }
+
+ private Stream<Arguments> provideInvalidCarsWithoutLicensePlate() {
+ return Stream.of(
+ Arguments.of(createCar("BMW", null), 1),
+ Arguments.of(Arrays.asList(
+ createCar("BMW", null),
+ createCar("VW", null)), 2));
+ }
+
+ private Stream<Arguments> provideInvalidCarsWithShortLicensePlate() {
+ return Stream.of(
+ Arguments.of(createCar("BMW", "D-A"), 1),
+ Arguments.of(Arrays.asList(
+ createCar("BMW", "D-A"),
+ createCar("VW", "D-A")), 2));
+ }
+
+ private Stream<Arguments> provideInvalidCarsWithoutManufacturer() {
+ return Stream.of(
+ Arguments.of(createCar(null, "D-A"), 1),
+ Arguments.of(Arrays.asList(
+ createCar(null, "D-A"),
+ createCar(null, "D-A")), 2));
+ }
+
+ private Stream<Arguments> provideCarsWithRedefinedDefaultGroup() {
+ return Stream.of(
+ Arguments.of(new CarWithRedefinedDefaultGroup(null,
"DD-AB-123")),
+ Arguments.of(Arrays.asList(
+ new CarWithRedefinedDefaultGroup(null, "DD-AB-123")),
+ new CarWithRedefinedDefaultGroup(null, "XX-YZ-789")));
+ }
+
+ private Stream<Arguments>
provideCarsWithRedefinedDefaultGroupAndShortLicencePlate() {
+ return Stream.of(
+ Arguments.of(new CarWithRedefinedDefaultGroup(null, "D-A"), 1),
+ Arguments.of(Arrays.asList(
+ new CarWithRedefinedDefaultGroup(null, "D-A"),
+ new CarWithRedefinedDefaultGroup(null, "D-A")), 2));
+ }
+
+ private void setLicensePlates(Object cars, String licensePlate) {
+ if (cars instanceof Car) {
+ ((Car) cars).setLicensePlate(licensePlate);
+ } else {
+ ((Iterable) cars).forEach(car -> ((Car)
car).setLicensePlate(licensePlate));
+ }
+ }
+
+ private void setManufacturer(Object cars, String manufacturer) {
+ if (cars instanceof Car) {
+ ((Car) cars).setManufacturer(manufacturer);
+ } else {
+ ((Iterable) cars).forEach(car -> ((Car)
car).setManufacturer(manufacturer));
+ }
+ }
+
}