This is an automated email from the ASF dual-hosted git repository. ffang pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-spring-boot.git
The following commit(s) were added to refs/heads/main by this push: new 0cb02870576 [CAMEL-18036]add tests in camel-bean-validator-starter (#552) 0cb02870576 is described below commit 0cb02870576ac14ccea26de7d2dc573c4e152d5f Author: Freeman(Yue) Fang <freeman.f...@gmail.com> AuthorDate: Wed May 4 14:05:36 2022 -0400 [CAMEL-18036]add tests in camel-bean-validator-starter (#552) --- .../springboot/BeanValidatorConfigurationTest.java | 167 +++++++++ ...anValidatorIgnoreXMLConfigurationRouteTest.java | 61 ++++ .../springboot/BeanValidatorRouteTest.java | 392 +++++++++++++++++++++ .../component/bean/validator/springboot/Car.java | 28 ++ .../validator/springboot/CarWithAnnotations.java | 55 +++ .../springboot/CarWithRedefinedDefaultGroup.java | 57 +++ .../CustomValidationProviderResolverTest.java | 118 +++++++ .../HibernateValidationProviderResolverTest.java | 113 ++++++ .../bean/validator/springboot/OptionalChecks.java | 20 ++ .../bean/validator/springboot/OrderedChecks.java | 24 ++ .../bean/validator/springboot/RequiredChecks.java | 20 ++ .../springboot/ValidatorFactoryAutowireTest.java | 97 +++++ .../springboot/ValidatorFactoryRegistryTest.java | 107 ++++++ .../validator/springboot/ValidatorFactoryTest.java | 68 ++++ 14 files changed, 1327 insertions(+) diff --git a/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/BeanValidatorConfigurationTest.java b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/BeanValidatorConfigurationTest.java new file mode 100644 index 00000000000..8b7b38842b7 --- /dev/null +++ b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/BeanValidatorConfigurationTest.java @@ -0,0 +1,167 @@ +/* + * 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.component.bean.validator.springboot; + + + + + +import java.lang.annotation.ElementType; +import java.util.Locale; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorFactory; +import javax.validation.MessageInterpolator; +import javax.validation.Path; +import javax.validation.TraversableResolver; +import javax.validation.Path.Node; + +import org.apache.camel.CamelContext; +import org.apache.camel.EndpointInject; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.component.bean.validator.BeanValidatorEndpoint; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.spring.boot.CamelAutoConfiguration; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.condition.OS.AIX; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; + + +@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) +@CamelSpringBootTest +@SpringBootTest( + classes = { + CamelAutoConfiguration.class, + BeanValidatorConfigurationTest.class + } +) +public class BeanValidatorConfigurationTest { + + + @Autowired + ProducerTemplate template; + + @Autowired + CamelContext context; + + @EndpointInject("mock:result") + MockEndpoint mock; + + + private static MessageInterpolator messageInterpolator; + + private static TraversableResolver traversableResolver; + + private static ConstraintValidatorFactory constraintValidatorFactory; + + + @Bean("myMessageInterpolator") + private MessageInterpolator getMessageInterpolator() { + messageInterpolator = new MyMessageInterpolator(); + return messageInterpolator; + } + @Bean("myTraversableResolver") + private TraversableResolver getTraversableResolver() { + traversableResolver = new MyTraversableResolver(); + return traversableResolver; + } + @Bean("myConstraintValidatorFactory") + private ConstraintValidatorFactory getConstraintValidatorFactory() { + constraintValidatorFactory = new MyConstraintValidatorFactory(); + return constraintValidatorFactory; + } + + + @DisabledOnOs(AIX) + @Test + void configureWithDefaults() { + BeanValidatorEndpoint endpoint = context.getEndpoint("bean-validator://x", BeanValidatorEndpoint.class); + assertNull(endpoint.getGroup()); + } + + @DisabledOnOs(AIX) + @Test + void configureBeanValidator() { + BeanValidatorEndpoint endpoint = context + .getEndpoint("bean-validator://x" + "?group=org.apache.camel.component.bean.validator.OptionalChecks" + + "&messageInterpolator=#myMessageInterpolator" + + "&traversableResolver=#myTraversableResolver" + + "&constraintValidatorFactory=#myConstraintValidatorFactory", + BeanValidatorEndpoint.class); + + assertEquals("org.apache.camel.component.bean.validator.OptionalChecks", endpoint.getGroup()); + assertSame(messageInterpolator, endpoint.getMessageInterpolator()); + assertSame(traversableResolver, endpoint.getTraversableResolver()); + assertSame(constraintValidatorFactory, endpoint.getConstraintValidatorFactory()); + } + + class MyMessageInterpolator implements MessageInterpolator { + + @Override + public String interpolate(String messageTemplate, Context context) { + return null; + } + + @Override + public String interpolate(String messageTemplate, Context context, Locale locale) { + return null; + } + } + + class MyTraversableResolver implements TraversableResolver { + + @Override + public boolean isCascadable( + Object traversableObject, Node traversableProperty, Class<?> rootBeanType, Path pathToTraversableObject, + ElementType elementType) { + return false; + } + + @Override + public boolean isReachable( + Object traversableObject, Node traversableProperty, Class<?> rootBeanType, Path pathToTraversableObject, + ElementType elementType) { + return false; + } + } + + class MyConstraintValidatorFactory implements ConstraintValidatorFactory { + + @Override + public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) { + return null; + } + + @Override + public void releaseInstance(ConstraintValidator<?, ?> arg0) { + // noop + } + } + +} diff --git a/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/BeanValidatorIgnoreXMLConfigurationRouteTest.java b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/BeanValidatorIgnoreXMLConfigurationRouteTest.java new file mode 100644 index 00000000000..81f10a06a2e --- /dev/null +++ b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/BeanValidatorIgnoreXMLConfigurationRouteTest.java @@ -0,0 +1,61 @@ +/* + * 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.component.bean.validator.springboot; + + + + + + +import org.apache.camel.CamelContext; +import org.apache.camel.component.bean.validator.BeanValidatorComponent; +import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.apache.camel.spring.boot.CamelContextConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; + + +@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) +@CamelSpringBootTest +@SpringBootTest( + classes = { + CamelAutoConfiguration.class, + BeanValidatorIgnoreXMLConfigurationRouteTest.class + } +) +public class BeanValidatorIgnoreXMLConfigurationRouteTest extends BeanValidatorRouteTest { + + + @Bean + CamelContextConfiguration contextConfiguration() { + return new CamelContextConfiguration() { + @Override + public void beforeApplicationStart(CamelContext context) { + context.getComponent("bean-validator", BeanValidatorComponent.class).setIgnoreXmlConfiguration(true); + } + @Override + public void afterApplicationStart(CamelContext camelContext) { + //do nothing here + } + + }; + } + +} diff --git a/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/BeanValidatorRouteTest.java b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/BeanValidatorRouteTest.java new file mode 100644 index 00000000000..7fd349371d7 --- /dev/null +++ b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/BeanValidatorRouteTest.java @@ -0,0 +1,392 @@ +/* + * 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.component.bean.validator.springboot; + + + + + +import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; + +import java.util.Arrays; +import java.util.Locale; +import java.util.Set; +import java.util.stream.Stream; + +import javax.validation.ConstraintViolation; + + +import org.apache.camel.CamelContext; +import org.apache.camel.CamelExecutionException; +import org.apache.camel.EndpointInject; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.component.bean.validator.BeanValidationException; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.spring.boot.CamelAutoConfiguration; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +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.junit.jupiter.api.Assertions.assertEquals; +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; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; + + +@DirtiesContext +@CamelSpringBootTest +@SpringBootTest( + classes = { + CamelAutoConfiguration.class, + BeanValidatorRouteTest.class + } +) +public class BeanValidatorRouteTest { + + + @Autowired + ProducerTemplate template; + + @Autowired + CamelContext context; + + @EndpointInject("mock:result") + MockEndpoint mock; + + + private Locale origLocale; + + @BeforeEach + public void setLanguage() { + origLocale = Locale.getDefault(); + Locale.setDefault(Locale.US); + } + + @AfterEach + public void restoreLanguage() { + Locale.setDefault(origLocale); + } + + //@DisabledOnOs(AIX) + @ParameterizedTest + @MethodSource("provideValidCars") + void validateShouldSuccessWithImpliciteDefaultGroup(Object cars) { + + Exchange exchange = template.request("bean-validator://x", new Processor() { + public void process(Exchange exchange) { + exchange.getIn().setBody(cars); + } + }); + + assertNotNull(exchange); + } + + @DisabledOnOs(AIX) + @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(cars); + } + }); + + assertNotNull(exchange); + } + + @DisabledOnOs(AIX) + @ParameterizedTest + @MethodSource("provideInvalidCarsWithoutLicensePlate") + void validateShouldFailWithImpliciteDefaultGroup(Object cars, int numberOfViolations) { + + final String url = "bean-validator://x"; + + try { + template.requestBody(url, cars); + fail("should throw exception"); + } catch (CamelExecutionException e) { + assertIsInstanceOf(BeanValidationException.class, e.getCause()); + + BeanValidationException exception = (BeanValidationException) e.getCause(); + Set<ConstraintViolation<Object>> constraintViolations = exception.getConstraintViolations(); + + assertEquals(numberOfViolations, constraintViolations.size()); + constraintViolations.forEach(cv -> { + assertEquals("licensePlate", cv.getPropertyPath().toString()); + assertEquals(null, cv.getInvalidValue()); + assertEquals("must not be null", cv.getMessage()); + }); + } + + setLicensePlates(cars, "D-A"); + + Exchange exchange = template.request(url, new Processor() { + public void process(Exchange exchange) { + exchange.getIn().setBody(cars); + } + }); + + assertNotNull(exchange); + } + + @DisabledOnOs(AIX) + @ParameterizedTest + @MethodSource("provideInvalidCarsWithoutLicensePlate") + void validateShouldFailWithExpliciteDefaultGroup(Object cars, int numberOfViolations) { + + final String url = "bean-validator://x?group=javax.validation.groups.Default"; + + try { + template.requestBody(url, cars); + fail("should throw exception"); + } catch (CamelExecutionException e) { + assertIsInstanceOf(BeanValidationException.class, e.getCause()); + + BeanValidationException exception = (BeanValidationException) e.getCause(); + Set<ConstraintViolation<Object>> constraintViolations = exception.getConstraintViolations(); + + assertEquals(numberOfViolations, constraintViolations.size()); + constraintViolations.forEach(cv -> { + assertEquals("licensePlate", cv.getPropertyPath().toString()); + assertEquals(null, cv.getInvalidValue()); + assertEquals("must not be null", cv.getMessage()); + }); + } + + setLicensePlates(cars, "D-A"); + + Exchange exchange = template.request(url, new Processor() { + public void process(Exchange exchange) { + exchange.getIn().setBody(cars); + } + }); + + assertNotNull(exchange); + } + + @DisabledOnOs(AIX) + @ParameterizedTest + @MethodSource("provideInvalidCarsWithShortLicensePlate") + void validateShouldFailWithOptionalChecksGroup(Object cars, int numberOfViolations) { + + final String url = "bean-validator://x?group=org.apache.camel.component.bean.validator.springboot.OptionalChecks"; + + try { + template.requestBody(url, cars); + fail("should throw exception"); + } catch (CamelExecutionException e) { + assertIsInstanceOf(BeanValidationException.class, e.getCause()); + + BeanValidationException exception = (BeanValidationException) e.getCause(); + Set<ConstraintViolation<Object>> constraintViolations = exception.getConstraintViolations(); + + 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()); + }); + } + + setLicensePlates(cars, "DD-AB-123"); + + Exchange exchange = template.request(url, new Processor() { + public void process(Exchange exchange) { + exchange.getIn().setBody(cars); + } + }); + + assertNotNull(exchange); + } + + @DisabledOnOs(AIX) + @ParameterizedTest + @MethodSource("provideInvalidCarsWithoutManufacturer") + void validateShouldFailWithOrderedChecksGroup(Object cars, int numberOfViolations) { + + final String url = "bean-validator://x?group=org.apache.camel.component.bean.validator.springboot.OrderedChecks"; + + try { + template.requestBody(url, cars); + fail("should throw exception"); + } catch (CamelExecutionException e) { + assertIsInstanceOf(BeanValidationException.class, e.getCause()); + + BeanValidationException exception = (BeanValidationException) e.getCause(); + Set<ConstraintViolation<Object>> constraintViolations = exception.getConstraintViolations(); + + assertEquals(numberOfViolations, constraintViolations.size()); + constraintViolations.forEach(cv -> { + assertEquals("manufacturer", cv.getPropertyPath().toString()); + assertEquals(null, cv.getInvalidValue()); + assertEquals("must not be null", cv.getMessage()); + }); + } + + setManufacturer(cars, "BMW"); + + try { + template.requestBody(url, cars); + fail("should throw exception"); + } catch (CamelExecutionException e) { + assertIsInstanceOf(BeanValidationException.class, e.getCause()); + + BeanValidationException exception = (BeanValidationException) e.getCause(); + Set<ConstraintViolation<Object>> constraintViolations = exception.getConstraintViolations(); + + 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()); + }); + } + + setLicensePlates(cars, "DD-AB-123"); + + Exchange exchange = template.request(url, new Processor() { + public void process(Exchange exchange) { + exchange.getIn().setBody(cars); + } + }); + + assertNotNull(exchange); + } + + @DisabledOnOs(AIX) + @ParameterizedTest + @MethodSource("provideCarsWithRedefinedDefaultGroup") + void validateShouldSuccessWithRedefinedDefaultGroup(Object cars) { + + final String url = "bean-validator://x"; + + Exchange exchange = template.request(url, new Processor() { + public void process(Exchange exchange) { + exchange.getIn().setBody(cars); + } + }); + + assertNotNull(exchange); + } + + @DisabledOnOs(AIX) + @ParameterizedTest + @MethodSource("provideCarsWithRedefinedDefaultGroupAndShortLicencePlate") + void validateShouldFailWithRedefinedDefaultGroup(Object cars, int numberOfViolations) { + + final String url = "bean-validator://x"; + + try { + template.requestBody(url, cars); + fail("should throw exception"); + } catch (CamelExecutionException e) { + assertIsInstanceOf(BeanValidationException.class, e.getCause()); + + BeanValidationException exception = (BeanValidationException) e.getCause(); + Set<ConstraintViolation<Object>> constraintViolations = exception.getConstraintViolations(); + + 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()); + }); + } + } + + static Car createCar(String manufacturer, String licencePlate) { + return new CarWithAnnotations(manufacturer, licencePlate); + } + + private static 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 static Stream<Arguments> provideInvalidCarsWithoutLicensePlate() { + return Stream.of( + Arguments.of(createCar("BMW", null), 1), + Arguments.of(Arrays.asList( + createCar("BMW", null), + createCar("VW", null)), 2)); + } + + private static 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 static 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 static 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 static 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)); + } + } + + +} diff --git a/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/Car.java b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/Car.java new file mode 100644 index 00000000000..7205ece6ad5 --- /dev/null +++ b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/Car.java @@ -0,0 +1,28 @@ +/* + * 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.component.bean.validator.springboot; + +public interface Car { + + String getManufacturer(); + + void setManufacturer(String manufacturer); + + String getLicensePlate(); + + void setLicensePlate(String licensePlate); +} diff --git a/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/CarWithAnnotations.java b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/CarWithAnnotations.java new file mode 100644 index 00000000000..aba7ebf41f4 --- /dev/null +++ b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/CarWithAnnotations.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.component.bean.validator.springboot; + +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, groups = OptionalChecks.class) + 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; + } +} diff --git a/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/CarWithRedefinedDefaultGroup.java b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/CarWithRedefinedDefaultGroup.java new file mode 100644 index 00000000000..1889dd36d55 --- /dev/null +++ b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/CarWithRedefinedDefaultGroup.java @@ -0,0 +1,57 @@ +/* + * 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.component.bean.validator.springboot; + +import javax.validation.GroupSequence; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; + +@GroupSequence({ CarWithRedefinedDefaultGroup.class, OptionalChecks.class }) +public class CarWithRedefinedDefaultGroup implements Car { + + @NotNull(groups = RequiredChecks.class) + private String manufacturer; + + @NotNull(groups = RequiredChecks.class) + @Size(min = 5, max = 14, groups = OptionalChecks.class) + private String licensePlate; + + public CarWithRedefinedDefaultGroup(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; + } +} diff --git a/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/CustomValidationProviderResolverTest.java b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/CustomValidationProviderResolverTest.java new file mode 100644 index 00000000000..241e2996978 --- /dev/null +++ b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/CustomValidationProviderResolverTest.java @@ -0,0 +1,118 @@ +/* + * 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.component.bean.validator.springboot; + + + + +import static java.util.Arrays.asList; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import java.util.List; + +import javax.validation.ValidationProviderResolver; +import javax.validation.spi.ValidationProvider; + +import org.apache.camel.CamelContext; +import org.apache.camel.EndpointInject; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.spring.boot.CamelAutoConfiguration; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + + + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; +import org.hibernate.validator.HibernateValidator; + + +@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) +@CamelSpringBootTest +@SpringBootTest( + classes = { + CamelAutoConfiguration.class, + CustomValidationProviderResolverTest.class, + CustomValidationProviderResolverTest.TestConfiguration.class + } +) +public class CustomValidationProviderResolverTest { + + + @Autowired + ProducerTemplate template; + + @Autowired + CamelContext context; + + @EndpointInject("mock:test") + MockEndpoint mockEndpoint; + + + static ValidationProviderResolver validationProviderResolver = mock(ValidationProviderResolver.class); + + @Bean("myValidationProviderResolver") + private ValidationProviderResolver getValidationProviderResolver() { + return validationProviderResolver; + } + + @BeforeAll + protected static void doPreSetup() throws Exception { + List<ValidationProvider<?>> validationProviders = asList(new HibernateValidator()); + given(validationProviderResolver.getValidationProviders()).willReturn(validationProviders); + + } + + @Test + void shouldResolveCustomValidationProviderResolver() { + verify(validationProviderResolver, atLeastOnce()).getValidationProviders(); + } + + + // ************************************* + // Config + // ************************************* + + @Configuration + public class TestConfiguration { + + @Bean + public RouteBuilder routeBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:test").to( + "bean-validator://ValidationProviderResolverTest?validationProviderResolver=#myValidationProviderResolver"); + } + }; + } + } + + + +} diff --git a/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/HibernateValidationProviderResolverTest.java b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/HibernateValidationProviderResolverTest.java new file mode 100644 index 00000000000..bec14a956a7 --- /dev/null +++ b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/HibernateValidationProviderResolverTest.java @@ -0,0 +1,113 @@ +/* + * 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.component.bean.validator.springboot; + + + + +import javax.validation.ValidationProviderResolver; + +import org.apache.camel.CamelContext; +import org.apache.camel.EndpointInject; +import org.apache.camel.Message; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.bean.validator.BeanValidationException; +import org.apache.camel.component.bean.validator.HibernateValidationProviderResolver; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.spring.boot.CamelAutoConfiguration; + +import org.junit.jupiter.api.Test; + + + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; + + +@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) +@CamelSpringBootTest +@SpringBootTest( + classes = { + CamelAutoConfiguration.class, + HibernateValidationProviderResolverTest.class, + HibernateValidationProviderResolverTest.TestConfiguration.class + } +) +public class HibernateValidationProviderResolverTest { + + + @Autowired + ProducerTemplate template; + + @Autowired + CamelContext context; + + @EndpointInject("mock:test") + MockEndpoint mockEndpoint; + + @Bean("myValidationProviderResolver") + ValidationProviderResolver getValidationProviderResolver() { + return new HibernateValidationProviderResolver(); + } + + @Test + void shouldResolveHibernateValidationProviderResolver() throws InterruptedException { + // Given + mockEndpoint.expectedMessageCount(1); + mockEndpoint.message(0).body().isInstanceOf(CarWithAnnotations.class); + CarWithAnnotations carWithNullFields = new CarWithAnnotations(null, null); + + template.send("direct:test", exchange -> { + Message in = exchange.getIn(); + in.setBody(carWithNullFields); + }); + + + // Then + mockEndpoint.assertIsSatisfied(); + } + + // ************************************* + // Config + // ************************************* + + @Configuration + public class TestConfiguration { + + @Bean + public RouteBuilder routeBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + onException(BeanValidationException.class).to(mockEndpoint); + + from("direct:test").to( + "bean-validator://ValidationProviderResolverTest?validationProviderResolver=#myValidationProviderResolver"); + } + }; + } + } + + + +} diff --git a/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/OptionalChecks.java b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/OptionalChecks.java new file mode 100644 index 00000000000..7b9c1e10bb5 --- /dev/null +++ b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/OptionalChecks.java @@ -0,0 +1,20 @@ +/* + * 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.component.bean.validator.springboot; + +public interface OptionalChecks { +} diff --git a/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/OrderedChecks.java b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/OrderedChecks.java new file mode 100644 index 00000000000..7710eac3d67 --- /dev/null +++ b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/OrderedChecks.java @@ -0,0 +1,24 @@ +/* + * 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.component.bean.validator.springboot; + +import javax.validation.GroupSequence; +import javax.validation.groups.Default; + +@GroupSequence({ Default.class, OptionalChecks.class }) +public interface OrderedChecks { +} diff --git a/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/RequiredChecks.java b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/RequiredChecks.java new file mode 100644 index 00000000000..343cb3be554 --- /dev/null +++ b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/RequiredChecks.java @@ -0,0 +1,20 @@ +/* + * 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.component.bean.validator.springboot; + +public interface RequiredChecks { +} diff --git a/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/ValidatorFactoryAutowireTest.java b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/ValidatorFactoryAutowireTest.java new file mode 100644 index 00000000000..7b7b330820e --- /dev/null +++ b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/ValidatorFactoryAutowireTest.java @@ -0,0 +1,97 @@ +/* + * 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.component.bean.validator.springboot; + + + + + + +import javax.validation.Validation; +import javax.validation.ValidatorFactory; +import javax.validation.bootstrap.GenericBootstrap; + +import org.apache.camel.CamelContext; +import org.apache.camel.EndpointInject; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.component.bean.validator.BeanValidatorEndpoint; +import org.apache.camel.component.bean.validator.BeanValidatorProducer; +import org.apache.camel.component.bean.validator.HibernateValidationProviderResolver; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.spring.boot.CamelAutoConfiguration; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; + +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.condition.OS.AIX; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; + + +@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) +@CamelSpringBootTest +@SpringBootTest( + classes = { + CamelAutoConfiguration.class, + ValidatorFactoryAutowireTest.class + } +) +public class ValidatorFactoryAutowireTest { + + + @Autowired + ProducerTemplate template; + + @Autowired + CamelContext context; + + @EndpointInject("mock:result") + MockEndpoint mock; + + + + private static ValidatorFactory validatorFactory; + + @Bean("myValidatorFactory") + private ValidatorFactory getValidatorFactory() { + GenericBootstrap bootstrap = Validation.byDefaultProvider(); + bootstrap.providerResolver(new HibernateValidationProviderResolver()); + + validatorFactory = bootstrap.configure().buildValidatorFactory(); + return validatorFactory; + } + + + + @DisabledOnOs(AIX) + @Test + void configureValidatorFactoryAutowired() throws Exception { + BeanValidatorEndpoint endpoint + = context.getEndpoint("bean-validator:dummy", BeanValidatorEndpoint.class); + BeanValidatorProducer producer = (BeanValidatorProducer) endpoint.createProducer(); + + assertSame(validatorFactory, endpoint.getValidatorFactory()); + assertSame(validatorFactory, producer.getValidatorFactory()); + } + +} diff --git a/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/ValidatorFactoryRegistryTest.java b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/ValidatorFactoryRegistryTest.java new file mode 100644 index 00000000000..d3217b5e5d3 --- /dev/null +++ b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/ValidatorFactoryRegistryTest.java @@ -0,0 +1,107 @@ +/* + * 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.component.bean.validator.springboot; + + + + + +import javax.validation.Validation; +import javax.validation.ValidatorFactory; +import javax.validation.bootstrap.GenericBootstrap; + +import org.apache.camel.CamelContext; +import org.apache.camel.EndpointInject; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.component.bean.validator.BeanValidatorEndpoint; +import org.apache.camel.component.bean.validator.BeanValidatorProducer; +import org.apache.camel.component.bean.validator.HibernateValidationProviderResolver; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.spring.boot.CamelAutoConfiguration; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; + +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.condition.OS.AIX; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; + + +@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) +@CamelSpringBootTest +@SpringBootTest( + classes = { + CamelAutoConfiguration.class, + ValidatorFactoryRegistryTest.class + } +) +public class ValidatorFactoryRegistryTest { + + + @Autowired + ProducerTemplate template; + + @Autowired + CamelContext context; + + @EndpointInject("mock:result") + MockEndpoint mock; + + + + private static ValidatorFactory validatorFactory; + + + private static ValidatorFactory otherValidatorFactory; + + @Bean("myValidatorFactory") + private ValidatorFactory getMyValidatorFactory() { + GenericBootstrap bootstrap = Validation.byDefaultProvider(); + bootstrap.providerResolver(new HibernateValidationProviderResolver()); + + validatorFactory = bootstrap.configure().buildValidatorFactory(); + return validatorFactory; + } + + @Bean("otherValidatorFactory") + private ValidatorFactory getOtherValidatorFactory() { + GenericBootstrap bootstrap = Validation.byDefaultProvider(); + bootstrap.providerResolver(new HibernateValidationProviderResolver()); + + otherValidatorFactory = bootstrap.configure().buildValidatorFactory(); + return otherValidatorFactory; + } + + @DisabledOnOs(AIX) + @Test + void configureValidatorFactoryFromRegistry() throws Exception { + BeanValidatorEndpoint endpoint + = context.getEndpoint("bean-validator:dummy?validatorFactory=#otherValidatorFactory", + BeanValidatorEndpoint.class); + BeanValidatorProducer producer = (BeanValidatorProducer) endpoint.createProducer(); + + assertSame(otherValidatorFactory, endpoint.getValidatorFactory()); + assertSame(otherValidatorFactory, producer.getValidatorFactory()); + } + +} diff --git a/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/ValidatorFactoryTest.java b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/ValidatorFactoryTest.java new file mode 100644 index 00000000000..f393b4023ab --- /dev/null +++ b/components-starter/camel-bean-validator-starter/src/test/java/org/apache/camel/component/bean/validator/springboot/ValidatorFactoryTest.java @@ -0,0 +1,68 @@ +/* + * 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.component.bean.validator.springboot; + + +import org.apache.camel.CamelContext; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.component.bean.validator.BeanValidatorEndpoint; +import org.apache.camel.component.bean.validator.BeanValidatorProducer; +import org.apache.camel.spring.boot.CamelAutoConfiguration; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.condition.OS.AIX; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; + + +@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) +@CamelSpringBootTest +@SpringBootTest( + classes = { + CamelAutoConfiguration.class, + ValidatorFactoryTest.class + } +) +public class ValidatorFactoryTest { + + + @Autowired + ProducerTemplate template; + + @Autowired + CamelContext context; + + + @DisabledOnOs(AIX) + @Test + void configureValidatorFactory() throws Exception { + + BeanValidatorEndpoint endpoint = context.getEndpoint("bean-validator:dummy", BeanValidatorEndpoint.class); + BeanValidatorProducer producer = (BeanValidatorProducer) endpoint.createProducer(); + + assertNull(endpoint.getValidatorFactory()); + assertNotNull(producer.getValidatorFactory()); + } +}