From f0e16fa02ae0c268634ceaa01e7219aad83ecc7f Mon Sep 17 00:00:00 2001 From: JiriOndrusek Date: Mon, 13 Jun 2022 11:36:06 +0200 Subject: [PATCH] CamelTestSupport style of testing #3511 --- .../ROOT/examples/components/mybatis-bean.yml | 13 -- .../ROOT/examples/components/mybatis.yml | 13 -- docs/modules/ROOT/nav.adoc | 1 - .../pages/reference/extensions/mybatis.adoc | 53 ----- .../ROOT/pages/user-guide/testing.adoc | 13 ++ extensions-core/core/deployment/pom.xml | 4 + .../deployment/InjectionPointsProcessor.java | 55 +++++- extensions/pom.xml | 1 - integration-tests/pom.xml | 1 - pom.xml | 5 + poms/bom/pom.xml | 178 +++++++++-------- test-framework/junit5/pom.xml | 72 +++++++ .../camel/quarkus/test/AfterAllCallback.java | 32 +++ .../camel/quarkus/test/AfterEachCallback.java | 32 +++ .../quarkus/test/BeforeEachCallback.java | 52 +++++ .../camel/quarkus/test/CallbackUtil.java | 181 +++++++++++++++++ .../quarkus/test/CamelQuarkusTestSupport.java | 147 ++++++++++++++ ...junit.callback.QuarkusTestAfterAllCallback | 1 + ...unit.callback.QuarkusTestAfterEachCallback | 1 + ...nit.callback.QuarkusTestBeforeEachCallback | 1 + .../src/main/resources/application.properties | 14 ++ .../quarkus/test/AbstractCallbacksTest.java | 184 ++++++++++++++++++ .../quarkus/test/AdviceWithLambdaTest.java | 55 ++++++ .../test/CallbacksPerTestFalseTest.java | 43 ++++ .../test/CallbacksPerTestTrueTest.java | 42 ++++ .../quarkus/test/DebugJUnit5Test.java | 95 +++++++++ .../test/DebugNoLazyTypeConverterTest.java | 91 +++++++++ .../apachencamel/quarkus/test/DebugTest.java | 98 ++++++++++ .../FilterCreateCamelContextPerClassTest.java | 63 ++++++ .../test/FilterFluentTemplateTest.java | 81 ++++++++ .../quarkus/test/FilterJUnit5Test.java | 75 +++++++ .../apachencamel/quarkus/test/FilterTest.java | 81 ++++++++ .../quarkus/test/GetMockEndpointTest.java | 47 +++++ .../IsMockEndpointsAndSkipJUnit5Test.java | 72 +++++++ .../quarkus/test/IsMockEndpointsFileTest.java | 73 +++++++ .../test/IsMockEndpointsJUnit5Test.java | 76 ++++++++ .../quarkus/test/IsMockEndpointsTest.java | 67 +++++++ .../test/MockEndpointFailNoHeaderTest.java | 65 +++++++ .../quarkus/test/MyProduceBean.java | 32 +++ .../apachencamel/quarkus/test/MySender.java | 25 +++ .../quarkus/test/ProduceBeanTest.java | 45 +++++ .../RouteBuilderConfigureExceptionTest.java | 56 ++++++ .../RouteProcessorDumpRouteCoverageTest.java | 74 +++++++ .../quarkus/test/SimpleMockEndpointsTest.java | 57 ++++++ .../quarkus/test/SimpleMockTest.java | 53 +++++ .../quarkus/test/SimpleNotifyBuilderTest.java | 55 ++++++ .../test/SimpleWeaveAddMockLastTest.java | 65 +++++++ ...PropertiesWithPropertiesComponentTest.java | 79 ++++++++ test-framework/pom.xml | 68 +++++++ 49 files changed, 2618 insertions(+), 169 deletions(-) delete mode 100644 docs/modules/ROOT/examples/components/mybatis-bean.yml delete mode 100644 docs/modules/ROOT/examples/components/mybatis.yml delete mode 100644 docs/modules/ROOT/pages/reference/extensions/mybatis.adoc create mode 100644 test-framework/junit5/pom.xml create mode 100644 test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/AfterAllCallback.java create mode 100644 test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/AfterEachCallback.java create mode 100644 test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/BeforeEachCallback.java create mode 100644 test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/CallbackUtil.java create mode 100644 test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/CamelQuarkusTestSupport.java create mode 100644 test-framework/junit5/src/main/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestAfterAllCallback create mode 100644 test-framework/junit5/src/main/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestAfterEachCallback create mode 100644 test-framework/junit5/src/main/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestBeforeEachCallback create mode 100644 test-framework/junit5/src/main/resources/application.properties create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/AbstractCallbacksTest.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/AdviceWithLambdaTest.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/CallbacksPerTestFalseTest.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/CallbacksPerTestTrueTest.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/DebugJUnit5Test.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/DebugNoLazyTypeConverterTest.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/DebugTest.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/FilterCreateCamelContextPerClassTest.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/FilterFluentTemplateTest.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/FilterJUnit5Test.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/FilterTest.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/GetMockEndpointTest.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/IsMockEndpointsAndSkipJUnit5Test.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/IsMockEndpointsFileTest.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/IsMockEndpointsJUnit5Test.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/IsMockEndpointsTest.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/MockEndpointFailNoHeaderTest.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/MyProduceBean.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/MySender.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/ProduceBeanTest.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/RouteBuilderConfigureExceptionTest.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/RouteProcessorDumpRouteCoverageTest.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/SimpleMockEndpointsTest.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/SimpleMockTest.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/SimpleNotifyBuilderTest.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/SimpleWeaveAddMockLastTest.java create mode 100644 test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/UseOverridePropertiesWithPropertiesComponentTest.java create mode 100644 test-framework/pom.xml diff --git a/docs/modules/ROOT/examples/components/mybatis-bean.yml b/docs/modules/ROOT/examples/components/mybatis-bean.yml deleted file mode 100644 index 5e1384613cfa..000000000000 --- a/docs/modules/ROOT/examples/components/mybatis-bean.yml +++ /dev/null @@ -1,13 +0,0 @@ -# Do not edit directly! -# This file was generated by camel-quarkus-maven-plugin:update-extension-doc-page -cqArtifactId: camel-quarkus-mybatis -cqArtifactIdBase: mybatis -cqNativeSupported: true -cqStatus: Stable -cqDeprecated: false -cqJvmSince: 1.1.0 -cqNativeSince: 2.8.0 -cqCamelPartName: mybatis-bean -cqCamelPartTitle: MyBatis Bean -cqCamelPartDescription: Perform queries, inserts, updates or deletes in a relational database using MyBatis. -cqExtensionPageTitle: MyBatis diff --git a/docs/modules/ROOT/examples/components/mybatis.yml b/docs/modules/ROOT/examples/components/mybatis.yml deleted file mode 100644 index 942d96ff42e1..000000000000 --- a/docs/modules/ROOT/examples/components/mybatis.yml +++ /dev/null @@ -1,13 +0,0 @@ -# Do not edit directly! -# This file was generated by camel-quarkus-maven-plugin:update-extension-doc-page -cqArtifactId: camel-quarkus-mybatis -cqArtifactIdBase: mybatis -cqNativeSupported: true -cqStatus: Stable -cqDeprecated: false -cqJvmSince: 1.1.0 -cqNativeSince: 2.8.0 -cqCamelPartName: mybatis -cqCamelPartTitle: MyBatis -cqCamelPartDescription: Performs a query, poll, insert, update or delete in a relational database using MyBatis. -cqExtensionPageTitle: MyBatis diff --git a/docs/modules/ROOT/nav.adoc b/docs/modules/ROOT/nav.adoc index 05380b3c999b..977b3639cf8b 100644 --- a/docs/modules/ROOT/nav.adoc +++ b/docs/modules/ROOT/nav.adoc @@ -212,7 +212,6 @@ *** xref:reference/extensions/mongodb.adoc[MongoDB] *** xref:reference/extensions/mongodb-gridfs.adoc[MongoDB GridFS] *** xref:reference/extensions/mustache.adoc[Mustache] -*** xref:reference/extensions/mybatis.adoc[MyBatis] *** xref:reference/extensions/nats.adoc[Nats] *** xref:reference/extensions/netty.adoc[Netty] *** xref:reference/extensions/netty-http.adoc[Netty HTTP] diff --git a/docs/modules/ROOT/pages/reference/extensions/mybatis.adoc b/docs/modules/ROOT/pages/reference/extensions/mybatis.adoc deleted file mode 100644 index 8f7ad4f18a6b..000000000000 --- a/docs/modules/ROOT/pages/reference/extensions/mybatis.adoc +++ /dev/null @@ -1,53 +0,0 @@ -// Do not edit directly! -// This file was generated by camel-quarkus-maven-plugin:update-extension-doc-page -= MyBatis -:linkattrs: -:cq-artifact-id: camel-quarkus-mybatis -:cq-native-supported: true -:cq-status: Stable -:cq-status-deprecation: Stable -:cq-description: Performs a query, poll, insert, update or delete in a relational database using MyBatis. -:cq-deprecated: false -:cq-jvm-since: 1.1.0 -:cq-native-since: 2.8.0 - -[.badges] -[.badge-key]##JVM since##[.badge-supported]##1.1.0## [.badge-key]##Native since##[.badge-supported]##2.8.0## - -Performs a query, poll, insert, update or delete in a relational database using MyBatis. - -== What's inside - -* xref:{cq-camel-components}::mybatis-component.adoc[MyBatis component], URI syntax: `mybatis:statement` -* xref:{cq-camel-components}::mybatis-bean-component.adoc[MyBatis Bean component], URI syntax: `mybatis-bean:beanName:methodName` - -Please refer to the above links for usage and configuration details. - -== Maven coordinates - -https://code.quarkus.io/?extension-search=camel-quarkus-mybatis[Create a new project with this extension on code.quarkus.io, window="_blank"] - -Or add the coordinates to your existing project: - -[source,xml] ----- - - org.apache.camel.quarkus - camel-quarkus-mybatis - ----- - -Check the xref:user-guide/index.adoc[User guide] for more information about writing Camel Quarkus applications. - -== Additional Camel Quarkus configuration - -Please refer to https://quarkiverse.github.io/quarkiverse-docs/quarkus-mybatis/dev/index.html[Quarkus MyBatis] for configuration. It must enable the following options - -[source, properties] ----- -quarkus.mybatis.xmlconfig.enable=true -quarkus.mybatis.xmlconfig.path=SqlMapConfig.xml ----- -TIP: `quarkus.mybatis.xmlconfig.path` must be the same with `configurationUri` param in the mybatis endpoint. - - diff --git a/docs/modules/ROOT/pages/user-guide/testing.adoc b/docs/modules/ROOT/pages/user-guide/testing.adoc index 24688eb851c6..46036434ed7c 100644 --- a/docs/modules/ROOT/pages/user-guide/testing.adoc +++ b/docs/modules/ROOT/pages/user-guide/testing.adoc @@ -226,3 +226,16 @@ class MyTest { ---- More examples of WireMock usage can be found in the Camel Quarkus integration test source tree such as https://github.com/apache/camel-quarkus/tree/main/integration-tests/geocoder[Geocoder]. + +== Testing with `CamelTestSupport` in JVM mode + +Sometimes you want to use test constructs from Camel (like `MockEndpoint`, `AdviceWith` , ...) in *JVM* mode. + +`CamelTestSupport` is not designed to work in Quarkus, you can use `CamelQuarkusTestSupport` instead. Test has to be annotated by `@QuarkusTest` and extend `CamelQuarkusTest` to allow you to use Camel test constructs. Support is based on component `camel-test-junit5`, see https://camel.apache.org/components/3.17.x/others/test-junit5.html[documentation] for more information. + +There are several limitations: + +* - JUnit does not about Qaurkus, different lifecycle, so the instances in the callbacks are different. Use Quarkus callbacks (see the https://quarkus.io/guides/getting-started-testing#enrichment-via-quarkustestcallback[documentation]). TODO is it the only solution? +* CamelQuarkus lifecycle does not allow to start/stop Camel context. Context is started before execution of the first test and closed after the last one. Test has to be written with consideration of this limitation. If it is not possible to write a test with such limitation, `@TestProfile` could be used. Test profile forces quarkus to restart its engine, therefore it creates a new Camel context (see the https://quarkus.io/guides/getting-started-testing#testing_different_profiles/[documentation] about this feature). +* Camel Quarkus executes the production of beans during the build phase. Because all the tests are build together, exclusion behavior is impl;emented into `CamelQuarkusTestSupport`. If a producer of the specific type and name is used in one tests, the instance will be the same for the rest of the tests. + diff --git a/extensions-core/core/deployment/pom.xml b/extensions-core/core/deployment/pom.xml index 765250a7dee4..065008c38e8b 100644 --- a/extensions-core/core/deployment/pom.xml +++ b/extensions-core/core/deployment/pom.xml @@ -37,6 +37,10 @@ io.quarkus quarkus-arc-deployment + + io.quarkus.arc + arc-processor + diff --git a/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/InjectionPointsProcessor.java b/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/InjectionPointsProcessor.java index e8ced1788acf..6d924f1b982b 100644 --- a/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/InjectionPointsProcessor.java +++ b/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/InjectionPointsProcessor.java @@ -78,6 +78,8 @@ public class InjectionPointsProcessor { .createSimple(EndpointInject.class.getName()); private static final DotName PRODUCE_ANNOTATION = DotName .createSimple(Produce.class.getName()); + private static final DotName TEST_SUPPORT_CLASS_NAME = DotName + .createSimple("org.apache.camel.quarkus.test.CamelQuarkusTestSupport"); private static SyntheticBeanBuildItem syntheticBean(DotName name, Supplier creator) { return SyntheticBeanBuildItem.configure(name) @@ -226,12 +228,16 @@ void syntheticBeans( BuildProducer syntheticBeans, BuildProducer proxyDefinitions) { + Set alreadyCreated = new HashSet<>(); + for (AnnotationInstance annot : index.getIndex().getAnnotations(ENDPOINT_INJECT_ANNOTATION)) { final AnnotationTarget target = annot.target(); switch (target.kind()) { case FIELD: { final FieldInfo field = target.asField(); - endpointInjectBeans(recorder, syntheticBeans, index.getIndex(), annot, field.type().name()); + if (!excludeTestSyntheticBeanDuplicities(annot, alreadyCreated, field.declaringClass(), index.getIndex())) { + endpointInjectBeans(recorder, syntheticBeans, index.getIndex(), annot, field.type().name()); + } break; } case METHOD: { @@ -251,8 +257,10 @@ void syntheticBeans( switch (target.kind()) { case FIELD: { final FieldInfo field = target.asField(); - produceBeans(recorder, capabilities, syntheticBeans, proxyDefinitions, beanCapabilityAvailable, - index.getIndex(), annot, field.type().name(), field.name(), field.declaringClass().name()); + if (!excludeTestSyntheticBeanDuplicities(annot, alreadyCreated, field.declaringClass(), index.getIndex())) { + produceBeans(recorder, capabilities, syntheticBeans, proxyDefinitions, beanCapabilityAvailable, + index.getIndex(), annot, field.type().name(), field.name(), field.declaringClass().name()); + } break; } case METHOD: { @@ -266,6 +274,47 @@ void syntheticBeans( } } + private boolean excludeTestSyntheticBeanDuplicities(AnnotationInstance annot, Set alreadyCreated, + ClassInfo declaringClass, IndexView index) { + String identifier = annot.toString(false) + ":" + getTargetClass(annot).toString(); + + if (extendsCamelQuarkusTest(declaringClass, index)) { + if (alreadyCreated.contains(identifier)) { + // System.out.println(">>>>>>>>>> already exists: " + identifier); + return true; + } else { + // System.out.println(">>>>>>>>>> creating: " + identifier); + alreadyCreated.add(identifier); + } + } + return false; + } + + private DotName getTargetClass(AnnotationInstance annot) { + switch (annot.target().kind()) { + case FIELD: + return annot.target().asField().type().name(); + case METHOD: + return annot.target().asMethod().returnType().name(); + default: + return null; + } + } + + private boolean extendsCamelQuarkusTest(ClassInfo declaringClass, IndexView indexView) { + if (declaringClass == null) { + return false; + } + + if (TEST_SUPPORT_CLASS_NAME.equals(declaringClass.name())) { + return true; + } + + //iterate over parent until found CamelQuarkusTest or null + return (declaringClass.superName() != null && + extendsCamelQuarkusTest(indexView.getClassByName(declaringClass.superName()), indexView)); + } + void produceBeans(CamelRecorder recorder, List capabilities, BuildProducer syntheticBeans, BuildProducer proxyDefinitions, diff --git a/extensions/pom.xml b/extensions/pom.xml index 64d77a1b9d16..d896fac39267 100644 --- a/extensions/pom.xml +++ b/extensions/pom.xml @@ -166,7 +166,6 @@ mongodb mongodb-gridfs mustache - mybatis nats netty netty-http diff --git a/integration-tests/pom.xml b/integration-tests/pom.xml index a0e80c281938..ba04015c413e 100644 --- a/integration-tests/pom.xml +++ b/integration-tests/pom.xml @@ -148,7 +148,6 @@ mllp mongodb-grouped mustache - mybatis nats netty nitrite diff --git a/pom.xml b/pom.xml index ecd259d87236..66a027ea000d 100644 --- a/pom.xml +++ b/pom.xml @@ -222,6 +222,7 @@ integration-test-groups docs integration-tests-jvm + test-framework @@ -586,6 +587,10 @@ integration-tests-support camel-quarkus-integration-test-support- + + test-framework + camel-quarkus-test-framework + diff --git a/poms/bom/pom.xml b/poms/bom/pom.xml index 679a37770633..931d11337cc3 100644 --- a/poms/bom/pom.xml +++ b/poms/bom/pom.xml @@ -5299,6 +5299,11 @@ + + org.apache.camel + camel-test-junit5 + ${camel.version} + org.apache.camel camel-threadpoolfactory-vertx @@ -7602,6 +7607,11 @@ camel-quarkus-jta-deployment ${camel-quarkus.version} + + org.apache.camel.quarkus + camel-quarkus-junit5 + ${camel-quarkus.version} + org.apache.camel.quarkus camel-quarkus-kafka @@ -10199,92 +10209,92 @@ - - org.l2x6.cq - cq-maven-plugin - - - flatten-bom - - flatten-bom - - process-resources - - - - - - io.quarkus:quarkus-bom - - - - - - org.apache.camel.quarkus:* - ca.uhn.hapi:* - net.openhft:affinity - - - - org.amqphub.quarkus:quarkus-qpid-jms - org.apache.geronimo.specs:geronimo-jms_2.0_spec - - - com.datastax.oss:java-driver-core - com.google.code.findbugs:jsr305 - - - com.datastax.oss:java-driver-query-builder - com.google.code.findbugs:jsr305 - - - io.quarkiverse.minio:quarkus-minio - com.google.code.findbugs:jsr305 - - - software.amazon.awssdk:apache-client - commons-logging:commons-logging - - - io.debezium:debezium-embedded:1.6.1.Final:jar - javax.activation:activation,javax.servlet:javax.servlet-api,log4j:log4j,org.apache.kafka:kafka-log4j-appender,org.apache.kafka:connect-runtime,org.apache.kafka:connect-file - - - org.glassfish.jaxb:jaxb-runtime - jakarta.xml.bind:jakarta.xml.bind-api - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - com.google.http-client:google-http-client - com.google.code.findbugs:jsr305 - - - io.debezium:debezium-embedded - javax.ws.rs:javax.ws.rs-api - - - io.grpc:grpc-netty - com.google.code.findbugs:jsr305 - - - org.apache.httpcomponents:* - commons-logging:commons-logging - - - org.apache.kafka:connect-runtime - javax.activation:activation,javax.servlet:javax.servlet-api,log4j:log4j - - - com.fasterxml.jackson.module:jackson-module-jaxb-annotations - jakarta.activation:jakarta.activation-api,jakarta.xml.bind:jakarta.xml.bind-api - + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - + + + + + diff --git a/test-framework/junit5/pom.xml b/test-framework/junit5/pom.xml new file mode 100644 index 000000000000..3311436e05d5 --- /dev/null +++ b/test-framework/junit5/pom.xml @@ -0,0 +1,72 @@ + + + + + org.apache.camel.quarkus + camel-quarkus-test-framework + 2.11.0-SNAPSHOT + ../pom.xml + + 4.0.0 + + camel-quarkus-junit5 + Camel Quarkus :: Test Framework :: Junit5 + + + + org.slf4j + slf4j-api + + + io.quarkus + quarkus-junit5 + + + org.assertj + assertj-core + + + org.junit.jupiter + junit-jupiter-engine + + + org.apache.camel.quarkus + camel-quarkus-integration-test-support + + + org.apache.camel.quarkus + camel-quarkus-core + + + org.apache.camel + camel-test-junit5 + + + org.apache.camel.quarkus + camel-quarkus-bean + test + + + org.apache.camel.quarkus + camel-quarkus-management + + + + diff --git a/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/AfterAllCallback.java b/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/AfterAllCallback.java new file mode 100644 index 000000000000..764c9b769899 --- /dev/null +++ b/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/AfterAllCallback.java @@ -0,0 +1,32 @@ +/* + * 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.test; + +import io.quarkus.test.junit.callback.QuarkusTestAfterAllCallback; +import io.quarkus.test.junit.callback.QuarkusTestContext; + +public class AfterAllCallback implements QuarkusTestAfterAllCallback { + + @Override + public void afterAll(QuarkusTestContext context) { + CamelQuarkusTestSupport testInstance = (CamelQuarkusTestSupport) context.getTestInstance(); + + if (CallbackUtil.isPerClass(testInstance)) { + CallbackUtil.resetContext(testInstance); + } + } +} diff --git a/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/AfterEachCallback.java b/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/AfterEachCallback.java new file mode 100644 index 000000000000..22e8e3af3599 --- /dev/null +++ b/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/AfterEachCallback.java @@ -0,0 +1,32 @@ +/* + * 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.test; + +import io.quarkus.test.junit.callback.QuarkusTestAfterEachCallback; +import io.quarkus.test.junit.callback.QuarkusTestMethodContext; + +public class AfterEachCallback implements QuarkusTestAfterEachCallback { + + @Override + public void afterEach(QuarkusTestMethodContext context) { + CamelQuarkusTestSupport testInstance = (CamelQuarkusTestSupport) context.getTestInstance(); + + if (!CallbackUtil.isPerClass(testInstance)) { + CallbackUtil.resetContext(testInstance); + } + } +} diff --git a/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/BeforeEachCallback.java b/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/BeforeEachCallback.java new file mode 100644 index 000000000000..fbded7f4c217 --- /dev/null +++ b/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/BeforeEachCallback.java @@ -0,0 +1,52 @@ +/* + * 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.test; + +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.stream.Collectors; + +import io.quarkus.test.junit.callback.QuarkusTestBeforeEachCallback; +import io.quarkus.test.junit.callback.QuarkusTestMethodContext; +import org.junit.jupiter.api.extension.ExtensionContext; + +public class BeforeEachCallback implements QuarkusTestBeforeEachCallback { + + @Override + public void beforeEach(QuarkusTestMethodContext context) { + CamelQuarkusTestSupport testInstance = (CamelQuarkusTestSupport) context.getTestInstance(); + ExtensionContext mockContext = new CallbackUtil.MockExtensionContext(CallbackUtil.getLifecycle(testInstance), + getDisplayName(context.getTestMethod())); + + try { + testInstance.mockBeforeEach(mockContext); + } catch (Exception e) { + throw new RuntimeException(e); + } + testInstance.mockBeforeAll(mockContext); + } + + private String getDisplayName(Method method) { + StringBuilder sb = new StringBuilder(method.getName()); + sb.append("("); + sb.append(Arrays.stream(method.getParameterTypes()).map(c -> c.getSimpleName()).collect(Collectors.joining(", "))); + sb.append(")"); + + return sb.toString(); + } + +} diff --git a/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/CallbackUtil.java b/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/CallbackUtil.java new file mode 100644 index 000000000000..84da04eef08d --- /dev/null +++ b/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/CallbackUtil.java @@ -0,0 +1,181 @@ +/* + * 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.test; + +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Method; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.function.Function; + +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.TestInstances; +import org.junit.jupiter.api.parallel.ExecutionMode; +import org.junit.jupiter.engine.execution.ExtensionValuesStore; +import org.junit.jupiter.engine.execution.NamespaceAwareStore; + +public class CallbackUtil { + + static boolean isPerClass(CamelQuarkusTestSupport testSupport) { + return getLifecycle(testSupport).filter(lc -> lc.equals(TestInstance.Lifecycle.PER_CLASS)).isPresent(); + } + + static Optional getLifecycle(CamelQuarkusTestSupport testSupport) { + if (testSupport.getClass().getAnnotation(TestInstance.class) != null) { + return Optional.of(testSupport.getClass().getAnnotation(TestInstance.class).value()); + } + + return Optional.empty(); + } + + static void resetContext(CamelQuarkusTestSupport testInstance) { + + try { + testInstance.context.getRouteController().stopAllRoutes(); + testInstance.context.getRouteController().removeAllRoutes(); + + } catch (Exception e) { + throw new RuntimeException(e); + } + + testInstance.context.getComponentNames().forEach(cn -> testInstance.context.removeComponent(cn)); + MockEndpoint.resetMocks(testInstance.context); + + // + // // if jmx object for context already exists. unmanage it + // if (testInstance.context.getManagementName() != null) { + // Object me = testInstance.context.getManagementStrategy().getManagementObjectStrategy() + // .getManagedObjectForCamelContext(testInstance.context); + // if (testInstance.context.getManagementStrategy().isManaged(me)) { + // try { + // testInstance.context.getManagementStrategy().unmanageObject(me); + // } catch (Exception e) { + // throw new RuntimeException(e); + // } + // System.out.println(">>>>> unmanaging: " + testInstance.getClass().getName()); + // } else { + // System.out.println(">>>>> no need: " + testInstance.getClass().getName()); + // } + // } else { + // System.out.println(">>>>> no managed name: " + testInstance.getClass().getName()); + // } + } + + static class MockExtensionContext implements ExtensionContext { + + private final Optional lifecycle; + private final String currentTestName; + private final ExtensionContext.Store globalStore; + + public MockExtensionContext(Optional lifecycle, String currentTestName) { + this.lifecycle = lifecycle; + this.currentTestName = currentTestName; + this.globalStore = new NamespaceAwareStore(new ExtensionValuesStore(null), ExtensionContext.Namespace.GLOBAL); + } + + @Override + public Optional getParent() { + return Optional.empty(); + } + + @Override + public ExtensionContext getRoot() { + return null; + } + + @Override + public String getUniqueId() { + return null; + } + + @Override + public String getDisplayName() { + return currentTestName; + } + + @Override + public Set getTags() { + return null; + } + + @Override + public Optional getElement() { + return Optional.empty(); + } + + @Override + public Optional> getTestClass() { + return Optional.empty(); + } + + @Override + public Optional getTestInstanceLifecycle() { + return lifecycle; + } + + @Override + public Optional getTestInstance() { + return Optional.empty(); + } + + @Override + public Optional getTestInstances() { + return Optional.empty(); + } + + @Override + public Optional getTestMethod() { + return Optional.empty(); + } + + @Override + public Optional getExecutionException() { + return Optional.empty(); + } + + @Override + public Optional getConfigurationParameter(String key) { + return Optional.empty(); + } + + @Override + public Optional getConfigurationParameter(String key, Function transformer) { + return Optional.empty(); + } + + @Override + public void publishReportEntry(Map map) { + + } + + @Override + public Store getStore(Namespace namespace) { + if (namespace == Namespace.GLOBAL) { + return globalStore; + } + return null; + } + + @Override + public ExecutionMode getExecutionMode() { + return null; + } + } +} diff --git a/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/CamelQuarkusTestSupport.java b/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/CamelQuarkusTestSupport.java new file mode 100644 index 000000000000..58f5434441e9 --- /dev/null +++ b/test-framework/junit5/src/main/java/org/apache/camel/quarkus/test/CamelQuarkusTestSupport.java @@ -0,0 +1,147 @@ +/* + * 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.test; + +import java.util.List; + +import javax.inject.Inject; + +import io.quarkus.test.junit.QuarkusTestProfile; +import org.apache.camel.CamelContext; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.Service; +import org.apache.camel.model.ModelCamelContext; +import org.apache.camel.model.RouteDefinition; +import org.apache.camel.quarkus.core.FastCamelContext; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.junit.jupiter.api.extension.ExtensionContext; + +public class CamelQuarkusTestSupport extends CamelTestSupport + implements QuarkusTestProfile { + + private boolean initialized; + + @Inject + protected CamelContext context; + + @Override + protected CamelContext createCamelContext() throws Exception { + return this.context; + } + + @Override + public void beforeAll(ExtensionContext context) { + //replaced by quarkus callback (beforeEach) + } + + @Override + public void beforeEach(ExtensionContext context) throws Exception { + //replaced by quarkus callback (beforeEach) + } + + @Override + public void afterAll(ExtensionContext context) { + //in camel-quarkus, junit5 uses different classloader, necessaryu code was moved into quarkus's callback + try { + doPostTearDown(); + cleanupResources(); + } catch (Exception e) { + // ignore + } + } + + @Override + public void afterEach(ExtensionContext context) throws Exception { + //in camel-quarkus, junit5 uses different classloader, necessaryu code was moved into quarkus's callback + } + + @Override + public void afterTestExecution(ExtensionContext context) throws Exception { + //in camel-quarkus, junit5 uses different classloader, necessaryu code was moved into quarkus's callback + } + + @Override + protected void stopCamelContext() throws Exception { + //context is started and stopped via quarkus lifecycle + } + + @Override + protected void doQuarkusCheck() { + //can run on Quarkus + } + + public void mockBeforeAll(ExtensionContext context) { + super.beforeAll(context); + } + + public void mockBeforeEach(ExtensionContext context) throws Exception { + super.beforeEach(context); + } + + @Override + public boolean isUseRouteBuilder() { + if (context.getRoutes().isEmpty()) { + return super.isUseRouteBuilder(); + } + return false; + } + + @Override + protected void doSetUp() throws Exception { + context.getManagementStrategy(); + if (!initialized) { + super.doSetUp(); + initialized = true; + } + } + + @Override + protected void doPreSetup() throws Exception { + if (isUseAdviceWith() || isUseDebugger()) { + ((FastCamelContext) context).suspend(); + } + super.doPreSetup(); + } + + @Override + protected void doPostSetup() throws Exception { + if (isUseAdviceWith() || isUseDebugger()) { + ((FastCamelContext) context).resume(); + if (isUseDebugger()) { + ModelCamelContext mcc = context.adapt(ModelCamelContext.class); + List rdfs = mcc.getRouteDefinitions(); + //addition causes removal -> it starts routes, which were added during setUp, when context was suspended + mcc.addRouteDefinitions(rdfs); + } + } + super.doPostSetup(); + } + + RoutesBuilder getRouteBuilder() throws Exception { + return createRouteBuilder(); + } + + @Override + protected void doStopCamelContext(CamelContext context, Service camelContextService) { + //don't stop + } + + @Override + protected void startCamelContext() { + //context has already started + } +} diff --git a/test-framework/junit5/src/main/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestAfterAllCallback b/test-framework/junit5/src/main/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestAfterAllCallback new file mode 100644 index 000000000000..23f8280e310e --- /dev/null +++ b/test-framework/junit5/src/main/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestAfterAllCallback @@ -0,0 +1 @@ +org.apache.camel.quarkus.test.AfterAllCallback diff --git a/test-framework/junit5/src/main/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestAfterEachCallback b/test-framework/junit5/src/main/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestAfterEachCallback new file mode 100644 index 000000000000..8749fd5f220c --- /dev/null +++ b/test-framework/junit5/src/main/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestAfterEachCallback @@ -0,0 +1 @@ +org.apache.camel.quarkus.test.AfterEachCallback diff --git a/test-framework/junit5/src/main/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestBeforeEachCallback b/test-framework/junit5/src/main/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestBeforeEachCallback new file mode 100644 index 000000000000..0887c9994488 --- /dev/null +++ b/test-framework/junit5/src/main/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestBeforeEachCallback @@ -0,0 +1 @@ +org.apache.camel.quarkus.test.BeforeEachCallback diff --git a/test-framework/junit5/src/main/resources/application.properties b/test-framework/junit5/src/main/resources/application.properties new file mode 100644 index 000000000000..2d6dd016cca6 --- /dev/null +++ b/test-framework/junit5/src/main/resources/application.properties @@ -0,0 +1,14 @@ +#quarkus.log.file.enable=true +# +#quarkus.log.file.path=target/camel-test.log +#quarkus.log.file.level=INFO +#quarkus.log.console.format = %d %-5p %c{1} - %m %n + +# no console logging +quarkus.log.console.enable=false + +# INFO file logging +quarkus.log.file.enable=true +quarkus.log.file.path=target/camel-test.log +quarkus.log.file.level=INFO +quarkus.log.file.format=%d %-5p %c{1} - %m %n \ No newline at end of file diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/AbstractCallbacksTest.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/AbstractCallbacksTest.java new file mode 100644 index 000000000000..5af96c19d0a9 --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/AbstractCallbacksTest.java @@ -0,0 +1,184 @@ +package org.apachencamel.quarkus.test; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.function.BiConsumer; +import java.util.stream.Collectors; + +import org.apache.camel.CamelContext; +import org.apache.camel.Produce; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.apache.camel.util.StopWatch; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public abstract class AbstractCallbacksTest extends CamelQuarkusTestSupport { + + public enum Callback { + postTearDown, + doSetup, + preSetup, + postSetup, + contextCreation; + } + + private final String testName; + + @Produce("direct:start") + protected ProducerTemplate template; + + public AbstractCallbacksTest(String testName) { + this.testName = testName; + } + + @Override + protected CamelContext createCamelContext() throws Exception { + createTmpFile(testName, Callback.contextCreation); + return super.createCamelContext(); + } + + @Override + protected void doPreSetup() throws Exception { + createTmpFile(testName, Callback.preSetup); + super.doPostSetup(); + } + + @Override + protected void doSetUp() throws Exception { + createTmpFile(testName, Callback.doSetup); + super.doSetUp(); + } + + @Override + protected void doPostSetup() throws Exception { + createTmpFile(testName, Callback.postSetup); + super.doPostSetup(); + } + + @Override + protected void doPostTearDown() throws Exception { + createTmpFile(testName, Callback.postTearDown); + super.doPostTearDown(); + } + + @Test + public void testMock() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Hello World"); + template.sendBody("direct:start", "Hello World"); + assertMockEndpointsSatisfied(); + } + + @Test + public void testMock2() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Hello World 2"); + template.sendBody("direct:start", "Hello World 2"); + assertMockEndpointsSatisfied(); + } + + @Override + protected RoutesBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:start").to("mock:result"); + } + }; + } + + static void assertCount(int expectedCount, Long count, Callback c, String testName) { + Assertions.assertEquals(expectedCount, count, + c.name() + " should be called exactly " + expectedCount + " times in " + testName); + } + + static void testAfterAll(String testName, BiConsumer consumer) { + // we are called before doPostTearDown so lets wait for that to be + // called + Runnable r = () -> { + Map counts = new HashMap<>(); + try { + StopWatch watch = new StopWatch(); + while (watch.taken() < 5000) { + //LOG.debug("Checking for tear down called correctly"); + try { + for (AbstractCallbacksTest.Callback c : AbstractCallbacksTest.Callback.values()) { + long count = doesTmpFileExist(testName, c); + if (count > 0) { + counts.put(c, count); + } + } + } catch (Exception e) { + //ignore + } + + if (counts.size() == AbstractCallbacksTest.Callback.values().length) { + break; + } else { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + break; + } + } + } + } finally { + // LOG.info("Should only call postTearDown 1 time per test class, called: " + POST_TEAR_DOWN.get()); + for (Callback c : Callback.values()) { + consumer.accept(c, counts.get(c)); + } + } + + }; + Thread t = new Thread(r); + t.setDaemon(false); + t.setName("shouldTearDown checker"); + t.start(); + } + + private static void createTmpFile(String testName, Callback callback) throws Exception { + Set testDirs = Arrays.stream(Paths.get("target").toFile().listFiles()) + .filter(f -> f.isDirectory() && f.getName().startsWith(testName)) + .collect(Collectors.toSet()); //todo only if there is onw + + Path tmpDir; + if (testDirs.size() == 1) { + tmpDir = testDirs.stream().findFirst().get().toPath(); + } else if (testDirs.size() > 1) { + throw new RuntimeException(); + } else { + tmpDir = Files.createTempDirectory(Paths.get("target"), testName); + tmpDir.toFile().deleteOnExit(); + } + + Path tmpFile = Files.createTempFile(tmpDir, callback.name(), ".log"); + tmpFile.toFile().deleteOnExit(); + } + + private static long doesTmpFileExist(String testName, Callback callback) throws Exception { + //find test dir + Set testDirs = Arrays.stream(Paths.get("target").toFile().listFiles()) + .filter(f -> f.isDirectory() && f.getName().startsWith(testName)) + .collect(Collectors.toSet()); //todo only if there is onw + + if (testDirs.size() > 1) { + //todo log + return -1; + } + if (testDirs.isEmpty()) { + //todo log + return 0; + } + + return Arrays.stream(testDirs.stream().findFirst().get().listFiles()) + .filter(f -> f.getName().startsWith(callback.name())) + .count(); + } +} diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/AdviceWithLambdaTest.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/AdviceWithLambdaTest.java new file mode 100644 index 000000000000..019901b09c41 --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/AdviceWithLambdaTest.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.apachencamel.quarkus.test; + +import io.quarkus.test.junit.QuarkusTest; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.AdviceWith; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.Test; + +@QuarkusTest +public class AdviceWithLambdaTest extends CamelQuarkusTestSupport { + + @Override + public boolean isUseAdviceWith() { + return true; + } + + @Test + public void testAdviceWith() throws Exception { + getMockEndpoint("mock:result").expectedMessageCount(1); + + // advice the route in one line + AdviceWith.adviceWith(context, "foo", a -> a.weaveAddLast().to("mock:result")); + + template.sendBody("direct:start", "Bye World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RoutesBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:start").routeId("foo").to("log:foo"); + } + }; + } +} diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/CallbacksPerTestFalseTest.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/CallbacksPerTestFalseTest.java new file mode 100644 index 000000000000..c9f3c13f4279 --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/CallbacksPerTestFalseTest.java @@ -0,0 +1,43 @@ +package org.apachencamel.quarkus.test; + +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.TestInstance; + +// replaces CreateCamelContextPerTestTrueTest +//tdoo add check of all callbacks and context creation + the same check with perClass +@QuarkusTest +@TestInstance(TestInstance.Lifecycle.PER_METHOD) +@TestProfile(CallbacksPerTestFalseTest.class) +public class CallbacksPerTestFalseTest extends AbstractCallbacksTest { + + public CallbacksPerTestFalseTest() { + super(CallbacksPerTestFalseTest.class.getSimpleName()); + } + + @AfterAll + public static void shouldTearDown() { + testAfterAll(CallbacksPerTestFalseTest.class.getSimpleName(), (callback, count) -> { + switch (callback) { + case doSetup: + assertCount(2, count, callback, CallbacksPerTestFalseTest.class.getSimpleName()); + break; + case contextCreation: + assertCount(2, count, callback, CallbacksPerTestFalseTest.class.getSimpleName()); + break; + case postSetup: + assertCount(2, count, callback, CallbacksPerTestFalseTest.class.getSimpleName()); + break; + case postTearDown: + assertCount(2, count, callback, CallbacksPerTestFalseTest.class.getSimpleName()); + break; + case preSetup: + assertCount(2, count, callback, CallbacksPerTestFalseTest.class.getSimpleName()); + break; + default: + throw new IllegalArgumentException(); + } + }); + } +} diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/CallbacksPerTestTrueTest.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/CallbacksPerTestTrueTest.java new file mode 100644 index 000000000000..b8c0d78eda42 --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/CallbacksPerTestTrueTest.java @@ -0,0 +1,42 @@ +package org.apachencamel.quarkus.test; + +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.TestInstance; + +// replaces CreateCamelContextPerTestTrueTest +@QuarkusTest +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@TestProfile(CallbacksPerTestTrueTest.class) +public class CallbacksPerTestTrueTest extends AbstractCallbacksTest { + + public CallbacksPerTestTrueTest() { + super(CallbacksPerTestTrueTest.class.getSimpleName()); + } + + @AfterAll + public static void shouldTearDown() { + testAfterAll(CallbacksPerTestTrueTest.class.getSimpleName(), (callback, count) -> { + switch (callback) { + case doSetup: + assertCount(1, count, callback, CallbacksPerTestTrueTest.class.getSimpleName()); + break; + case contextCreation: + assertCount(1, count, callback, CallbacksPerTestTrueTest.class.getSimpleName()); + break; + case postSetup: + assertCount(1, count, callback, CallbacksPerTestTrueTest.class.getSimpleName()); + break; + case postTearDown: + assertCount(1, count, callback, CallbacksPerTestTrueTest.class.getSimpleName()); + break; + case preSetup: + assertCount(1, count, callback, CallbacksPerTestTrueTest.class.getSimpleName()); + break; + default: + throw new IllegalArgumentException(); + } + }); + } +} diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/DebugJUnit5Test.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/DebugJUnit5Test.java new file mode 100644 index 000000000000..a8e70d2c1697 --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/DebugJUnit5Test.java @@ -0,0 +1,95 @@ +/* + * 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.apachencamel.quarkus.test; + +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.model.ProcessorDefinition; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@QuarkusTest +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@TestProfile(DebugJUnit5Test.class) +public class DebugJUnit5Test extends CamelQuarkusTestSupport { + + private static final Logger LOG = LoggerFactory.getLogger(DebugJUnit5Test.class); + + // START SNIPPET: e1 + @Override + public boolean isUseDebugger() { + // must enable debugger + return true; + } + + @Override + protected void debugBefore( + Exchange exchange, Processor processor, ProcessorDefinition definition, String id, String shortName) { + // this method is invoked before we are about to enter the given + // processor + // from your Java editor you can just add a breakpoint in the code line + // below + LOG.info("Before " + definition + " with body " + exchange.getIn().getBody()); + } + // END SNIPPET: e1 + + @Test + public void testDebugger() throws Exception { + // set mock expectations + getMockEndpoint("mock:a").expectedMessageCount(1); + getMockEndpoint("mock:b").expectedMessageCount(1); + + // send a message + template.sendBody("direct:start", "World"); + + // assert mocks + assertMockEndpointsSatisfied(); + } + + @Test + public void testTwo() throws Exception { + // set mock expectations + getMockEndpoint("mock:a").expectedMessageCount(2); + getMockEndpoint("mock:b").expectedMessageCount(2); + + // send a message + template.sendBody("direct:start", "World"); + template.sendBody("direct:start", "Camel"); + + // assert mocks + assertMockEndpointsSatisfied(); + } + + // START SNIPPET: e2 + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + // this is the route we want to debug + from("direct:start").to("mock:a").transform(body().prepend("Hello ")).to("mock:b"); + } + }; + } + // END SNIPPET: e2 +} diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/DebugNoLazyTypeConverterTest.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/DebugNoLazyTypeConverterTest.java new file mode 100644 index 000000000000..3ae972b4795a --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/DebugNoLazyTypeConverterTest.java @@ -0,0 +1,91 @@ +/* + * 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.apachencamel.quarkus.test; + +import io.quarkus.test.junit.QuarkusTest; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.model.ProcessorDefinition; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@QuarkusTest +public class DebugNoLazyTypeConverterTest extends CamelQuarkusTestSupport { + + private static final Logger LOG = LoggerFactory.getLogger(DebugNoLazyTypeConverterTest.class); + + // START SNIPPET: e1 + @Override + public boolean isUseDebugger() { + // must enable debugger + return true; + } + + @Override + protected void debugBefore( + Exchange exchange, Processor processor, ProcessorDefinition definition, String id, String shortName) { + // this method is invoked before we are about to enter the given + // processor + // from your Java editor you can just add a breakpoint in the code line + // below + LOG.info("Before " + definition + " with body " + exchange.getIn().getBody()); + } + // END SNIPPET: e1 + + @Test + public void testDebugger() throws Exception { + // set mock expectations + getMockEndpoint("mock:a").expectedMessageCount(1); + getMockEndpoint("mock:b").expectedMessageCount(1); + + // send a message + template.sendBody("direct:start", "World"); + + // assert mocks + assertMockEndpointsSatisfied(); + } + + @Test + public void testTwo() throws Exception { + // set mock expectations + getMockEndpoint("mock:a").expectedMessageCount(2); + getMockEndpoint("mock:b").expectedMessageCount(2); + + // send a message + template.sendBody("direct:start", "World"); + template.sendBody("direct:start", "Camel"); + + // assert mocks + assertMockEndpointsSatisfied(); + } + + // START SNIPPET: e2 + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + // this is the route we want to debug + from("direct:start").to("mock:a").transform(body().prepend("Hello ")).to("mock:b"); + } + }; + } + // END SNIPPET: e2 +} diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/DebugTest.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/DebugTest.java new file mode 100644 index 000000000000..f0237b95ac9a --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/DebugTest.java @@ -0,0 +1,98 @@ +/* + * 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.apachencamel.quarkus.test; + +import io.quarkus.test.junit.QuarkusTest; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.model.ProcessorDefinition; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@QuarkusTest +public class DebugTest extends CamelQuarkusTestSupport { + + private static final Logger LOG = LoggerFactory.getLogger(DebugTest.class); + + @Override + public boolean isUseAdviceWith() { + return true; + } + + // START SNIPPET: e1 + @Override + public boolean isUseDebugger() { + // must enable debugger + return true; + } + + @Override + protected void debugBefore( + Exchange exchange, Processor processor, ProcessorDefinition definition, String id, String shortName) { + // this method is invoked before we are about to enter the given + // processor + // from your Java editor you can just add a breakpoint in the code line + // below + LOG.info("Before " + definition + " with body " + exchange.getIn().getBody()); + } + // END SNIPPET: e1 + + @Test + public void testDebugger() throws Exception { + + // set mock expectations + getMockEndpoint("mock:a").expectedMessageCount(1); + getMockEndpoint("mock:b").expectedMessageCount(1); + + // send a message + template.sendBody("direct:start", "World"); + + // assert mocks + assertMockEndpointsSatisfied(); + } + + @Test + public void testTwo() throws Exception { + + // set mock expectations + getMockEndpoint("mock:a").expectedMessageCount(2); + getMockEndpoint("mock:b").expectedMessageCount(2); + + // send a message + template.sendBody("direct:start", "World"); + template.sendBody("direct:start", "Camel"); + + // assert mocks + assertMockEndpointsSatisfied(); + } + + // START SNIPPET: e2 + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + // this is the route we want to debug + from("direct:start").to("mock:a").transform(body().prepend("Hello ")).to("mock:b").routeId("foo"); + } + }; + } + // END SNIPPET: e2 +} diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/FilterCreateCamelContextPerClassTest.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/FilterCreateCamelContextPerClassTest.java new file mode 100644 index 000000000000..517af6fb9550 --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/FilterCreateCamelContextPerClassTest.java @@ -0,0 +1,63 @@ +/* + * 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.apachencamel.quarkus.test; + +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; + +/** + * Tests filtering using Camel Test + */ +// START SNIPPET: example +@QuarkusTest +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@TestProfile(FilterCreateCamelContextPerClassTest.class) +public class FilterCreateCamelContextPerClassTest extends CamelQuarkusTestSupport { + + @Test + public void testSendMatchingMessage() throws Exception { + String expectedBody = ""; + + getMockEndpoint("mock:result").expectedBodiesReceived(expectedBody); + + template.sendBodyAndHeader("direct:start", expectedBody, "foo", "bar"); + + assertMockEndpointsSatisfied(); + } + + @Test + public void testSendNotMatchingMessage() throws Exception { + getMockEndpoint("mock:result").expectedMessageCount(0); + + template.sendBodyAndHeader("direct:start", "", "foo", "notMatchedHeaderValue"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result"); + } + }; + } +} diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/FilterFluentTemplateTest.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/FilterFluentTemplateTest.java new file mode 100644 index 000000000000..a69b4bf51696 --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/FilterFluentTemplateTest.java @@ -0,0 +1,81 @@ +/* + * 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.apachencamel.quarkus.test; + +import io.quarkus.test.junit.QuarkusTest; +import org.apache.camel.EndpointInject; +import org.apache.camel.FluentProducerTemplate; +import org.apache.camel.Produce; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.Test; + +/** + * Tests filtering using Camel Test + */ +// START SNIPPET: example +// tag::example[] +@QuarkusTest +public class FilterFluentTemplateTest extends CamelQuarkusTestSupport { + + @EndpointInject("mock:result") + protected MockEndpoint resultEndpoint; + + @Produce("direct:startFluent") + protected FluentProducerTemplate fluentTemplate; + + @Override + public boolean isDumpRouteCoverage() { + return true; + } + + @Test + public void testSendMatchingMessage() throws Exception { + String expectedBody = ""; + + resultEndpoint.expectedBodiesReceived(expectedBody); + + fluentTemplate.withBody(expectedBody).withHeader("foo", "bar").send(); + + resultEndpoint.assertIsSatisfied(); + + resultEndpoint.reset(); + } + + @Test + public void testSendNotMatchingMessage() throws Exception { + resultEndpoint.expectedMessageCount(0); + + fluentTemplate.withBody("").withHeader("foo", "notMatchedHeaderValue").send(); + + resultEndpoint.assertIsSatisfied(); + + resultEndpoint.reset(); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + from("direct:startFluent").filter(header("foo").isEqualTo("bar")).to("mock:result"); + } + }; + } +} +// end::example[] +// END SNIPPET: example diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/FilterJUnit5Test.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/FilterJUnit5Test.java new file mode 100644 index 000000000000..081fbee5981a --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/FilterJUnit5Test.java @@ -0,0 +1,75 @@ +/* + * 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.apachencamel.quarkus.test; + +import io.quarkus.test.junit.QuarkusTest; +import org.apache.camel.EndpointInject; +import org.apache.camel.Produce; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.Test; + +/** + * Tests filtering using Camel Test + */ +// START SNIPPET: example +@QuarkusTest +public class FilterJUnit5Test extends CamelQuarkusTestSupport { + + @EndpointInject("mock:result") + protected MockEndpoint resultEndpoint; + + @Produce("direct:start") + protected ProducerTemplate template; + + @Test + public void testSendMatchingMessage() throws Exception { + String expectedBody = ""; + + resultEndpoint.expectedBodiesReceived(expectedBody); + + template.sendBodyAndHeader(expectedBody, "foo", "bar"); + + resultEndpoint.assertIsSatisfied(); + + resultEndpoint.reset(); + } + + @Test + public void testSendNotMatchingMessage() throws Exception { + resultEndpoint.expectedMessageCount(0); + + template.sendBodyAndHeader("", "foo", "notMatchedHeaderValue"); + + resultEndpoint.assertIsSatisfied(); + + resultEndpoint.reset(); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result"); + } + }; + } + +} +// END SNIPPET: example diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/FilterTest.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/FilterTest.java new file mode 100644 index 000000000000..5ce3b4443f85 --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/FilterTest.java @@ -0,0 +1,81 @@ +/* + * 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.apachencamel.quarkus.test; + +import io.quarkus.test.junit.QuarkusTest; +import org.apache.camel.EndpointInject; +import org.apache.camel.Produce; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.Test; + +/** + * Tests filtering using Camel Test + */ +// START SNIPPET: example +// tag::example[] +@QuarkusTest +public class FilterTest extends CamelQuarkusTestSupport { + + @EndpointInject("mock:result") + protected MockEndpoint resultEndpoint; + + @Produce("direct:start") + protected ProducerTemplate template; + + @Override + public boolean isDumpRouteCoverage() { + return true; + } + + @Test + public void testSendMatchingMessage() throws Exception { + String expectedBody = ""; + + resultEndpoint.expectedBodiesReceived(expectedBody); + + template.sendBodyAndHeader(expectedBody, "foo", "bar"); + + resultEndpoint.assertIsSatisfied(); + + resultEndpoint.reset(); + } + + @Test + public void testSendNotMatchingMessage() throws Exception { + resultEndpoint.expectedMessageCount(0); + + template.sendBodyAndHeader("", "foo", "notMatchedHeaderValue"); + + resultEndpoint.assertIsSatisfied(); + + resultEndpoint.reset(); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result"); + } + }; + } +} +// end::example[] +// END SNIPPET: example diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/GetMockEndpointTest.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/GetMockEndpointTest.java new file mode 100644 index 000000000000..88d81121d13d --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/GetMockEndpointTest.java @@ -0,0 +1,47 @@ +/* + * 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.apachencamel.quarkus.test; + +import io.quarkus.test.junit.QuarkusTest; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.Test; + +@QuarkusTest +public class GetMockEndpointTest extends CamelQuarkusTestSupport { + + @Test + public void testMock() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Hello World"); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RoutesBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:start").to("mock:result?failFast=false"); + } + }; + } + +} diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/IsMockEndpointsAndSkipJUnit5Test.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/IsMockEndpointsAndSkipJUnit5Test.java new file mode 100644 index 000000000000..464274f9380d --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/IsMockEndpointsAndSkipJUnit5Test.java @@ -0,0 +1,72 @@ +/* + * 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.apachencamel.quarkus.test; + +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.seda.SedaEndpoint; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +// START SNIPPET: e1 +// tag::e1[] +@QuarkusTest +@TestProfile(IsMockEndpointsAndSkipJUnit5Test.class) +public class IsMockEndpointsAndSkipJUnit5Test extends CamelQuarkusTestSupport { + + @Override + public String isMockEndpointsAndSkip() { + // override this method and return the pattern for which endpoints to + // mock, + // and skip sending to the original endpoint. + return "direct:foo"; + } + + @Test + public void testMockEndpointAndSkip() throws Exception { + // notice we have automatic mocked the direct:foo endpoints and the name + // of the endpoints is "mock:uri" + getMockEndpoint("mock:result").expectedBodiesReceived("Hello World"); + getMockEndpoint("mock:direct:foo").expectedMessageCount(1); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + + // the message was not send to the direct:foo route and thus not sent to + // the seda endpoint + SedaEndpoint seda = context.getEndpoint("seda:foo", SedaEndpoint.class); + assertEquals(0, seda.getCurrentQueueSize()); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:start").to("direct:foo").to("mock:result"); + + from("direct:foo").transform(constant("Bye World")).to("seda:foo"); + } + }; + } +} +// end::e1[] +// END SNIPPET: e1 diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/IsMockEndpointsFileTest.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/IsMockEndpointsFileTest.java new file mode 100644 index 000000000000..4be68fe22bf3 --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/IsMockEndpointsFileTest.java @@ -0,0 +1,73 @@ +/* + * 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.apachencamel.quarkus.test; + +import io.quarkus.test.junit.QuarkusTest; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.apache.camel.test.junit5.TestSupport.deleteDirectory; + +@QuarkusTest +public class IsMockEndpointsFileTest extends CamelQuarkusTestSupport { + + @Override + @BeforeEach + public void setUp() throws Exception { + deleteDirectory("target/input"); + deleteDirectory("target/messages"); + super.setUp(); + } + + @Override + public String isMockEndpoints() { + // override this method and return the pattern for which endpoints to + // mock. + return "file:target*"; + } + + @Test + public void testMockFileEndpoints() throws Exception { + // notice we have automatic mocked all endpoints and the name of the + // endpoints is "mock:uri" + MockEndpoint camel = getMockEndpoint("mock:file:target/messages/camel"); + camel.expectedMessageCount(1); + + MockEndpoint other = getMockEndpoint("mock:file:target/messages/others"); + other.expectedMessageCount(1); + + template.sendBodyAndHeader("file:target/input", "Hello Camel", Exchange.FILE_NAME, "camel.txt"); + template.sendBodyAndHeader("file:target/input", "Hello World", Exchange.FILE_NAME, "world.txt"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("file:target/input").choice().when(bodyAs(String.class).contains("Camel")).to("file:target/messages/camel") + .otherwise().to("file:target/messages/others"); + } + }; + } +} diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/IsMockEndpointsJUnit5Test.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/IsMockEndpointsJUnit5Test.java new file mode 100644 index 000000000000..8f0e99b9f5cb --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/IsMockEndpointsJUnit5Test.java @@ -0,0 +1,76 @@ +/* + * 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.apachencamel.quarkus.test; + +import io.quarkus.test.junit.QuarkusTest; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +// START SNIPPET: e1 +// tag::e1[] +@QuarkusTest +public class IsMockEndpointsJUnit5Test extends CamelQuarkusTestSupport { + + @Override + public String isMockEndpoints() { + // override this method and return the pattern for which endpoints to + // mock. + // use * to indicate all + return "*"; + } + + @Test + public void testMockAllEndpoints() throws Exception { + // notice we have automatic mocked all endpoints and the name of the + // endpoints is "mock:uri" + getMockEndpoint("mock:direct:start").expectedBodiesReceived("Hello World"); + getMockEndpoint("mock:direct:foo").expectedBodiesReceived("Hello World"); + getMockEndpoint("mock:log:foo").expectedBodiesReceived("Bye World"); + getMockEndpoint("mock:result").expectedBodiesReceived("Bye World"); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + + // additional test to ensure correct endpoints in registry + assertNotNull(context.hasEndpoint("direct:start")); + assertNotNull(context.hasEndpoint("direct:foo")); + assertNotNull(context.hasEndpoint("log:foo")); + assertNotNull(context.hasEndpoint("mock:result")); + // all the endpoints was mocked + assertNotNull(context.hasEndpoint("mock:direct:start")); + assertNotNull(context.hasEndpoint("mock:direct:foo")); + assertNotNull(context.hasEndpoint("mock:log:foo")); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:start").to("direct:foo").to("log:foo").to("mock:result"); + + from("direct:foo").transform(constant("Bye World")); + } + }; + } +} +// end::e1[] +// END SNIPPET: e1 diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/IsMockEndpointsTest.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/IsMockEndpointsTest.java new file mode 100644 index 000000000000..114c7934aa2d --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/IsMockEndpointsTest.java @@ -0,0 +1,67 @@ +/* + * 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.apachencamel.quarkus.test; + +import io.quarkus.test.junit.QuarkusTest; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@QuarkusTest +public class IsMockEndpointsTest extends CamelQuarkusTestSupport { + + @Override + public String isMockEndpoints() { + return "*"; + } + + @Test + public void testMockAllEndpoints() throws Exception { + getMockEndpoint("mock:direct:start").expectedBodiesReceived("Hello World"); + getMockEndpoint("mock:direct:foo").expectedBodiesReceived("Hello World"); + getMockEndpoint("mock:log:foo").expectedBodiesReceived("Bye World"); + getMockEndpoint("mock:result").expectedBodiesReceived("Bye World"); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + + // additional test to ensure correct endpoints in registry + assertNotNull(context.hasEndpoint("direct:start")); + assertNotNull(context.hasEndpoint("direct:foo")); + assertNotNull(context.hasEndpoint("log:foo")); + assertNotNull(context.hasEndpoint("mock:result")); + // all the endpoints was mocked + assertNotNull(context.hasEndpoint("mock:direct:start")); + assertNotNull(context.hasEndpoint("mock:direct:foo")); + assertNotNull(context.hasEndpoint("mock:log:foo")); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:start").to("direct:foo").to("log:foo").to("mock:result"); + + from("direct:foo").transform(constant("Bye World")); + } + }; + } +} diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/MockEndpointFailNoHeaderTest.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/MockEndpointFailNoHeaderTest.java new file mode 100644 index 000000000000..6fc23bbc8959 --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/MockEndpointFailNoHeaderTest.java @@ -0,0 +1,65 @@ +/* + * 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.apachencamel.quarkus.test; + +import io.quarkus.test.junit.QuarkusTest; +import org.apache.camel.EndpointInject; +import org.apache.camel.Produce; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.Test; + +@QuarkusTest +public class MockEndpointFailNoHeaderTest extends CamelQuarkusTestSupport { + + @EndpointInject("mock:result") + protected MockEndpoint resultEndpoint; + + @Produce("direct:start") + protected ProducerTemplate template; + + @Override + public boolean isDumpRouteCoverage() { + return true; + } + + @Test + public void withHeaderTestCase() throws InterruptedException { + String expectedBody = ""; + resultEndpoint.expectedHeaderReceived("foo", "bar"); + template.sendBodyAndHeader(expectedBody, "foo", "bar"); + resultEndpoint.assertIsSatisfied(); + } + + @Test + public void noHeaderTestCase() throws InterruptedException { + resultEndpoint.expectedHeaderReceived("foo", "bar"); + resultEndpoint.setResultWaitTime(1); // speedup test + resultEndpoint.assertIsNotSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result"); + } + }; + } +} diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/MyProduceBean.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/MyProduceBean.java new file mode 100644 index 000000000000..978c134e8564 --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/MyProduceBean.java @@ -0,0 +1,32 @@ +/* + * 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.apachencamel.quarkus.test; + +import org.apache.camel.Produce; + +/** + * + */ +public class MyProduceBean { + + @Produce("mock:result") + MySender sender; + + public void doSomething(String body) { + sender.send(body); + } +} diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/MySender.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/MySender.java new file mode 100644 index 000000000000..29212acd0fc6 --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/MySender.java @@ -0,0 +1,25 @@ +/* + * 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.apachencamel.quarkus.test; + +/** + * + */ +public interface MySender { + + void send(String body); +} diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/ProduceBeanTest.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/ProduceBeanTest.java new file mode 100644 index 000000000000..31d1f77f38b1 --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/ProduceBeanTest.java @@ -0,0 +1,45 @@ +/* + * 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.apachencamel.quarkus.test; + +import io.quarkus.test.junit.QuarkusTest; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.Test; + +@QuarkusTest +public class ProduceBeanTest extends CamelQuarkusTestSupport { + + @Test + public void testProduceBean() throws Exception { + getMockEndpoint("mock:result").expectedMessageCount(1); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:start").bean(MyProduceBean.class, "doSomething"); + } + }; + } +} diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/RouteBuilderConfigureExceptionTest.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/RouteBuilderConfigureExceptionTest.java new file mode 100644 index 000000000000..396f3e72466c --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/RouteBuilderConfigureExceptionTest.java @@ -0,0 +1,56 @@ +/* + * 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.apachencamel.quarkus.test; + +import io.quarkus.test.junit.QuarkusTest; +import org.apache.camel.Predicate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.fail; + +@QuarkusTest +public class RouteBuilderConfigureExceptionTest extends CamelQuarkusTestSupport { + + private Predicate iAmNull; + + @Override + @BeforeEach + public void setUp() { + try { + super.setUp(); + fail("Should have thrown exception"); + } catch (Exception e) { + // expected + } + } + + @Test + public void testFoo() { + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + from("direct:start").choice().when(iAmNull).to("mock:dead"); + } + }; + } +} diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/RouteProcessorDumpRouteCoverageTest.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/RouteProcessorDumpRouteCoverageTest.java new file mode 100644 index 000000000000..44205fc9e7a9 --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/RouteProcessorDumpRouteCoverageTest.java @@ -0,0 +1,74 @@ +/* + * 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.apachencamel.quarkus.test; + +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; +import org.junit.jupiter.api.TestReporter; + +import static org.apache.camel.test.junit5.TestSupport.assertFileExists; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@QuarkusTest +@TestProfile(RouteProcessorDumpRouteCoverageTest.class) +public class RouteProcessorDumpRouteCoverageTest extends CamelQuarkusTestSupport { + + @Override + public boolean isDumpRouteCoverage() { + return true; + } + + @Test + public void testProcessorJunit5() { + String out = template.requestBody("direct:start", "Hello World", String.class); + assertEquals("Bye World", out); + } + + @Test + public void testProcessorJunit5WithTestParameterInjection(TestInfo info, TestReporter testReporter) { + assertNotNull(info); + assertNotNull(testReporter); + String out = template.requestBody("direct:start", "Hello World", String.class); + assertEquals("Bye World", out); + } + + @AfterAll + public static void checkDumpFilesCreatedAfterTests() { + // should create that file when test is done + assertFileExists("target/camel-route-coverage/RouteProcessorDumpRouteCoverageTest-testProcessorJunit5.xml"); + assertFileExists( + "target/camel-route-coverage/RouteProcessorDumpRouteCoverageTest-testProcessorJunit5WithTestParameterInjection.xml"); + } + + @Override + protected RoutesBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:start").process(exchange -> exchange.getMessage().setBody("Bye World")); + } + }; + } + +} diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/SimpleMockEndpointsTest.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/SimpleMockEndpointsTest.java new file mode 100644 index 000000000000..8fea209c0be0 --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/SimpleMockEndpointsTest.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.apachencamel.quarkus.test; + +import io.quarkus.test.junit.QuarkusTest; +import org.apache.camel.Produce; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.Test; + +@QuarkusTest +public class SimpleMockEndpointsTest extends CamelQuarkusTestSupport { + + @Produce("direct:start") + protected ProducerTemplate template; + + @Override + public String isMockEndpointsAndSkip() { + return "seda:queue"; + } + + @Test + public void testMockAndSkip() throws Exception { + getMockEndpoint("mock:seda:queue").expectedBodiesReceived("Bye Camel"); + + template.sendBody("seda:start", "Camel"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RoutesBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("seda:start").transform(simple("Bye ${body}")).to("seda:queue"); + } + }; + } + +} diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/SimpleMockTest.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/SimpleMockTest.java new file mode 100644 index 000000000000..c37847cb616f --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/SimpleMockTest.java @@ -0,0 +1,53 @@ +/* + * 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.apachencamel.quarkus.test; + +import io.quarkus.test.junit.QuarkusTest; +import org.apache.camel.Produce; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.Test; + +@QuarkusTest +public class SimpleMockTest extends CamelQuarkusTestSupport { + + @Produce("direct:start") + // @Inject + protected ProducerTemplate template; + + @Test + public void testMock() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Hello World"); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RoutesBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:start").to("mock:result"); + } + }; + } + +} diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/SimpleNotifyBuilderTest.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/SimpleNotifyBuilderTest.java new file mode 100644 index 000000000000..6bccb1198c3f --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/SimpleNotifyBuilderTest.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.apachencamel.quarkus.test; + +import java.util.concurrent.TimeUnit; + +import io.quarkus.test.junit.QuarkusTest; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.NotifyBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +@QuarkusTest +public class SimpleNotifyBuilderTest extends CamelQuarkusTestSupport { + + @Test + public void testNotifyBuilder() { + NotifyBuilder notify = new NotifyBuilder(context).from("seda:start").wereSentTo("seda:queue").whenDone(10).create(); + + for (int i = 0; i < 10; i++) { + template.sendBody("seda:start", "Camel" + i); + } + + boolean matches = notify.matches(10, TimeUnit.SECONDS); + assertTrue(matches); + } + + @Override + protected RoutesBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("seda:start").transform(simple("Bye ${body}")).to("seda:queue"); + } + }; + } + +} diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/SimpleWeaveAddMockLastTest.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/SimpleWeaveAddMockLastTest.java new file mode 100644 index 000000000000..9498243d7ce8 --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/SimpleWeaveAddMockLastTest.java @@ -0,0 +1,65 @@ +/* + * 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.apachencamel.quarkus.test; + +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.AdviceWith; +import org.apache.camel.builder.AdviceWithRouteBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.model.Model; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.Test; + +@QuarkusTest +@TestProfile(SimpleWeaveAddMockLastTest.class) +public class SimpleWeaveAddMockLastTest extends CamelQuarkusTestSupport { + + public boolean isUseAdviceWith() { + return true; + } + + @Test + public void testWeaveAddMockLast() throws Exception { + AdviceWith.adviceWith(context.getExtension(Model.class).getRouteDefinitions().get(0), context, + new AdviceWithRouteBuilder() { + @Override + public void configure() { + weaveAddLast().to("mock:result"); + } + }); + context.start(); + + getMockEndpoint("mock:result").expectedBodiesReceived("Bye Camel"); + + template.sendBody("seda:start", "Camel"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RoutesBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("seda:start").transform(simple("Bye ${body}")).to("seda:queue"); + } + }; + } + +} diff --git a/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/UseOverridePropertiesWithPropertiesComponentTest.java b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/UseOverridePropertiesWithPropertiesComponentTest.java new file mode 100644 index 000000000000..00ed8451ab05 --- /dev/null +++ b/test-framework/junit5/src/test/java/org/apachencamel/quarkus/test/UseOverridePropertiesWithPropertiesComponentTest.java @@ -0,0 +1,79 @@ +/* + * 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.apachencamel.quarkus.test; + +import java.util.Properties; + +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import org.apache.camel.builder.AdviceWith; +import org.apache.camel.builder.AdviceWithRouteBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.model.ModelCamelContext; +import org.apache.camel.quarkus.test.CamelQuarkusTestSupport; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +@QuarkusTest +@TestProfile(UseOverridePropertiesWithPropertiesComponentTest.class) +public class UseOverridePropertiesWithPropertiesComponentTest extends CamelQuarkusTestSupport { + + @Override + public boolean isUseAdviceWith() { + return true; + } + + @BeforeEach + public void doSomethingBefore() throws Exception { + AdviceWithRouteBuilder mocker = new AdviceWithRouteBuilder() { + @Override + public void configure() throws Exception { + replaceFromWith("direct:sftp"); + + interceptSendToEndpoint("file:*").skipSendToOriginalEndpoint().to("mock:file"); + } + }; + AdviceWith.adviceWith(this.context.adapt(ModelCamelContext.class).getRouteDefinition("myRoute"), this.context, mocker); + } + + @Override + protected Properties useOverridePropertiesWithPropertiesComponent() { + Properties pc = new Properties(); + pc.put("ftp.username", "scott"); + pc.put("ftp.password", "tiger"); + return pc; + } + + @Test + public void testOverride() throws Exception { + getMockEndpoint("mock:file").expectedMessageCount(1); + + template.sendBody("direct:sftp", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + from("ftp:somepath?username={{ftp.username}}&password={{ftp.password}}").routeId("myRoute") + .to("file:target/out"); + } + }; + } +} diff --git a/test-framework/pom.xml b/test-framework/pom.xml new file mode 100644 index 000000000000..4f2b2c92e23a --- /dev/null +++ b/test-framework/pom.xml @@ -0,0 +1,68 @@ + + + + + 4.0.0 + + org.apache.camel.quarkus + camel-quarkus-build-parent + 2.11.0-SNAPSHOT + ../poms/build-parent/pom.xml + + + camel-quarkus-test-framework + pom + + Camel Quarkus :: Test Framework :: Parent + Ancillary modules required by some tests. Hosted outside the integration-tests directory + so that we can keep a flat hierarchy in the integration-tests directory. + + + + junit5 + + + + + + io.quarkus + quarkus-bom + ${quarkus.version} + pom + import + + + org.apache.camel.quarkus + camel-quarkus-bom + ${project.version} + pom + import + + + org.apache.camel.quarkus + camel-quarkus-bom-test + ${project.version} + pom + import + + + + +