From 806ba032ec3fd1431545b2acd163342cfbb9b3a5 Mon Sep 17 00:00:00 2001 From: Francois de Parscau Date: Tue, 12 Mar 2024 12:09:42 +0100 Subject: [PATCH 1/8] Add camel jetty integration test --- .../camel/itests/AbstractCamelComponent.java | 4 ++ .../camel/itests/AbstractCamelKarafITest.java | 7 +- tests/components/camel-jetty/pom.xml | 34 +++++++++ .../karaf/camel/test/CamelJettyComponent.java | 72 +++++++++++++++++++ .../karaf/camel/itests/CamelJettyITest.java | 43 +++++++++++ tests/components/pom.xml | 1 + 6 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 tests/components/camel-jetty/pom.xml create mode 100644 tests/components/camel-jetty/src/main/java/org/apache/karaf/camel/test/CamelJettyComponent.java create mode 100644 tests/components/camel-jetty/src/test/java/org/apache/karaf/camel/itests/CamelJettyITest.java diff --git a/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelComponent.java b/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelComponent.java index 47ca0254d..d674d95d6 100644 --- a/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelComponent.java +++ b/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelComponent.java @@ -94,4 +94,8 @@ protected void configureConsumer(RouteDefinition consumerRoute) { } consumerRoute.routeId("consumer-%s".formatted(getTestComponentName())); } + + public int getNextAvailablePort() { + return AbstractCamelKarafITest.getAvailablePort(30000,40000); + } } diff --git a/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java b/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java index 8d4c6025d..8dc5f4edc 100644 --- a/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java +++ b/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java @@ -94,6 +94,7 @@ public Option[] config() { @Before public void init() throws Exception { String testComponentName = getTestComponentName(); + installRequiredFeatures(); installBundle("file://%s/%s-%s.jar".formatted(getBaseDir(), testComponentName, getVersion()),true); assertBundleInstalled(testComponentName); assertBundleInstalledAndRunning(testComponentName); @@ -101,6 +102,10 @@ public void init() throws Exception { initProducerTemplate(); } + protected void installRequiredFeatures() throws Exception{ + //default do nothing + } + private void initCamelContext() { this.context = bundleContext.getService(bundleContext.getServiceReference(CamelContext.class)); } @@ -154,7 +159,7 @@ protected void assertBundleInstalledAndRunning(String name) { //need to check with the command because the status may be Active while it's displayed as Waiting in the console //because of an exception for instance String bundles = executeCommand("bundle:list -s -t 0 | grep %s".formatted(name)); - Assert.assertTrue("bundle%s is in state %d /%s".formatted(bundle.getSymbolicName(), bundle.getState(), bundles), + Assert.assertTrue("bundle %s is in state %d /%s".formatted(bundle.getSymbolicName(), bundle.getState(), bundles), bundles.contains("Active")); } } \ No newline at end of file diff --git a/tests/components/camel-jetty/pom.xml b/tests/components/camel-jetty/pom.xml new file mode 100644 index 000000000..705cba937 --- /dev/null +++ b/tests/components/camel-jetty/pom.xml @@ -0,0 +1,34 @@ + + + + 4.0.0 + + org.apache.camel.karaf + camel-karaf-components-test + 4.5.0-SNAPSHOT + + + camel-jetty-test + Apache Camel :: Karaf :: Tests :: Components :: Jetty + + + \ No newline at end of file diff --git a/tests/components/camel-jetty/src/main/java/org/apache/karaf/camel/test/CamelJettyComponent.java b/tests/components/camel-jetty/src/main/java/org/apache/karaf/camel/test/CamelJettyComponent.java new file mode 100644 index 000000000..944005a77 --- /dev/null +++ b/tests/components/camel-jetty/src/main/java/org/apache/karaf/camel/test/CamelJettyComponent.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.apache.karaf.camel.test; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.function.Function; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.model.RouteDefinition; +import org.apache.karaf.camel.itests.AbstractCamelComponentResultMockBased; +import org.osgi.service.component.annotations.Component; + +@Component( + name = "karaf-camel-jetty-test", + immediate = true +) +public class CamelJettyComponent extends AbstractCamelComponentResultMockBased { + + private final int port = getNextAvailablePort(); + + @Override + protected Function consumerRoute() { + return builder -> builder.from("jetty://http://localhost:%s/test".formatted(port)).transform(builder.constant("OK")); + } + + @Override + protected void configureProducer(RouteBuilder builder, RouteDefinition producerRoute) { + producerRoute.setBody(builder.constant("OK")) + .log("calling http endpoint") + .process(new HttpClientProcessor()); + } + + class HttpClientProcessor implements Processor { + + @Override + public void process(Exchange exchange) throws Exception { + + HttpClient client = HttpClient.newHttpClient(); + + // Create a URI for the request + URI uri = URI.create("http://localhost:%s/test".formatted(port)); + + // Create a HttpRequest + HttpRequest request = HttpRequest.newBuilder() + .uri(uri) + .build(); + + client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) + .thenApply(HttpResponse::body) + .thenAccept(System.out::println); + } + } +} + diff --git a/tests/components/camel-jetty/src/test/java/org/apache/karaf/camel/itests/CamelJettyITest.java b/tests/components/camel-jetty/src/test/java/org/apache/karaf/camel/itests/CamelJettyITest.java new file mode 100644 index 000000000..05c2bf57b --- /dev/null +++ b/tests/components/camel-jetty/src/test/java/org/apache/karaf/camel/itests/CamelJettyITest.java @@ -0,0 +1,43 @@ +/* + * Licensed 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.karaf.camel.itests; + +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.ops4j.pax.exam.junit.PaxExam; +import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy; +import org.ops4j.pax.exam.spi.reactors.PerClass; + + +@RunWith(PaxExam.class) +@ExamReactorStrategy(PerClass.class) +public class CamelJettyITest extends AbstractCamelKarafResultMockBasedITest { + + @Override + protected void installRequiredFeatures() throws Exception { + installAndAssertFeature("camel-jetty"); + } + + @Override + protected void configureMock(MockEndpoint mock) { + mock.expectedBodiesReceived("OK"); + } + + @Test + public void testResultMock() throws Exception { + assertMockEndpointsSatisfied(); + } + +} \ No newline at end of file diff --git a/tests/components/pom.xml b/tests/components/pom.xml index 787ba9164..e26df75d9 100644 --- a/tests/components/pom.xml +++ b/tests/components/pom.xml @@ -39,6 +39,7 @@ camel-file camel-seda + camel-jetty From 38a374585436778e19c4fb469bed8be2b5514ebc Mon Sep 17 00:00:00 2001 From: Francois de Parscau Date: Wed, 17 Apr 2024 12:30:00 +0200 Subject: [PATCH 2/8] remove unecessary constant --- .../java/org/apache/karaf/camel/test/CamelJettyComponent.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/components/camel-jetty/src/main/java/org/apache/karaf/camel/test/CamelJettyComponent.java b/tests/components/camel-jetty/src/main/java/org/apache/karaf/camel/test/CamelJettyComponent.java index 944005a77..6a0edfc0f 100644 --- a/tests/components/camel-jetty/src/main/java/org/apache/karaf/camel/test/CamelJettyComponent.java +++ b/tests/components/camel-jetty/src/main/java/org/apache/karaf/camel/test/CamelJettyComponent.java @@ -43,8 +43,7 @@ protected Function consumerRoute() { @Override protected void configureProducer(RouteBuilder builder, RouteDefinition producerRoute) { - producerRoute.setBody(builder.constant("OK")) - .log("calling http endpoint") + producerRoute.log("calling http endpoint") .process(new HttpClientProcessor()); } From 9cbfa6f2d4511abe9e99ccedf00f21582dadd4a0 Mon Sep 17 00:00:00 2001 From: Nicolas Filotto Date: Thu, 18 Apr 2024 17:50:15 +0200 Subject: [PATCH 3/8] Update tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelComponent.java --- .../org/apache/karaf/camel/itests/AbstractCamelComponent.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelComponent.java b/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelComponent.java index d674d95d6..21bb7f750 100644 --- a/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelComponent.java +++ b/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelComponent.java @@ -96,6 +96,6 @@ protected void configureConsumer(RouteDefinition consumerRoute) { } public int getNextAvailablePort() { - return AbstractCamelKarafITest.getAvailablePort(30000,40000); + return AbstractCamelKarafITest.getAvailablePort(30000, 40000); } } From 6ef72377154b739c9a1062daec66b8c3a963bb36 Mon Sep 17 00:00:00 2001 From: Nicolas Filotto Date: Thu, 18 Apr 2024 18:12:56 +0200 Subject: [PATCH 4/8] Update tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java --- .../org/apache/karaf/camel/itests/AbstractCamelKarafITest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java b/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java index 8dc5f4edc..a524c28c2 100644 --- a/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java +++ b/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java @@ -95,7 +95,7 @@ public Option[] config() { public void init() throws Exception { String testComponentName = getTestComponentName(); installRequiredFeatures(); - installBundle("file://%s/%s-%s.jar".formatted(getBaseDir(), testComponentName, getVersion()),true); + installBundle("file://%s/%s-%s.jar".formatted(getBaseDir(), testComponentName, getVersion()), true); assertBundleInstalled(testComponentName); assertBundleInstalledAndRunning(testComponentName); initCamelContext(); From 567c68ec39a67c228d7ff3e0ed804a0b8c2421b4 Mon Sep 17 00:00:00 2001 From: Nicolas Filotto Date: Thu, 18 Apr 2024 18:15:03 +0200 Subject: [PATCH 5/8] Update tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java --- .../org/apache/karaf/camel/itests/AbstractCamelKarafITest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java b/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java index a524c28c2..183fd30cb 100644 --- a/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java +++ b/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java @@ -96,7 +96,6 @@ public void init() throws Exception { String testComponentName = getTestComponentName(); installRequiredFeatures(); installBundle("file://%s/%s-%s.jar".formatted(getBaseDir(), testComponentName, getVersion()), true); - assertBundleInstalled(testComponentName); assertBundleInstalledAndRunning(testComponentName); initCamelContext(); initProducerTemplate(); From b9fac4ab778061009ac8d0b85248f75b9dd131a9 Mon Sep 17 00:00:00 2001 From: Francois de Parscau Date: Fri, 19 Apr 2024 15:33:46 +0200 Subject: [PATCH 6/8] implement a default install feature based on class name --- tests/camel-integration-test/pom.xml | 1 + .../apache/karaf/camel/itests/AbstractCamelKarafITest.java | 7 +++++-- .../org/apache/karaf/camel/itests/CamelJettyITest.java | 5 ----- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/camel-integration-test/pom.xml b/tests/camel-integration-test/pom.xml index eca2692bd..85ac4ef73 100644 --- a/tests/camel-integration-test/pom.xml +++ b/tests/camel-integration-test/pom.xml @@ -76,6 +76,7 @@ org.apache.karaf.itests,org.ops4j.pax.exam,org.osgi.framework,org.junit, org.apache.camel*;${camel.osgi.import.camel.version}, + org.apache.karaf.features, org.osgi.service.*, org.awaitility* diff --git a/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java b/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java index 183fd30cb..b56fae440 100644 --- a/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java +++ b/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java @@ -101,8 +101,11 @@ public void init() throws Exception { initProducerTemplate(); } - protected void installRequiredFeatures() throws Exception{ - //default do nothing + protected void installRequiredFeatures() throws Exception { + String featureName = toKebabCase(this.getClass().getSimpleName()).replace("-itest", ""); + if (null != featureService.getFeature(featureName)) { + installAndAssertFeature(featureName); + } } private void initCamelContext() { diff --git a/tests/components/camel-jetty/src/test/java/org/apache/karaf/camel/itests/CamelJettyITest.java b/tests/components/camel-jetty/src/test/java/org/apache/karaf/camel/itests/CamelJettyITest.java index 05c2bf57b..f904fb2c6 100644 --- a/tests/components/camel-jetty/src/test/java/org/apache/karaf/camel/itests/CamelJettyITest.java +++ b/tests/components/camel-jetty/src/test/java/org/apache/karaf/camel/itests/CamelJettyITest.java @@ -25,11 +25,6 @@ @ExamReactorStrategy(PerClass.class) public class CamelJettyITest extends AbstractCamelKarafResultMockBasedITest { - @Override - protected void installRequiredFeatures() throws Exception { - installAndAssertFeature("camel-jetty"); - } - @Override protected void configureMock(MockEndpoint mock) { mock.expectedBodiesReceived("OK"); From fe52f00310e68577c27200d35d97c47b4445ce75 Mon Sep 17 00:00:00 2001 From: Francois de Parscau Date: Mon, 29 Apr 2024 16:14:02 +0200 Subject: [PATCH 7/8] remove async call --- .../org/apache/karaf/camel/test/CamelJettyComponent.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/components/camel-jetty/src/main/java/org/apache/karaf/camel/test/CamelJettyComponent.java b/tests/components/camel-jetty/src/main/java/org/apache/karaf/camel/test/CamelJettyComponent.java index 6a0edfc0f..0bd93e9a1 100644 --- a/tests/components/camel-jetty/src/main/java/org/apache/karaf/camel/test/CamelJettyComponent.java +++ b/tests/components/camel-jetty/src/main/java/org/apache/karaf/camel/test/CamelJettyComponent.java @@ -38,7 +38,7 @@ public class CamelJettyComponent extends AbstractCamelComponentResultMockBased { @Override protected Function consumerRoute() { - return builder -> builder.from("jetty://http://localhost:%s/test".formatted(port)).transform(builder.constant("OK")); + return builder -> builder.from("jetty://http://localhost:%s/jettyTest".formatted(port)).transform(builder.constant("OK")); } @Override @@ -55,16 +55,14 @@ public void process(Exchange exchange) throws Exception { HttpClient client = HttpClient.newHttpClient(); // Create a URI for the request - URI uri = URI.create("http://localhost:%s/test".formatted(port)); + URI uri = URI.create("http://localhost:%s/jettyTest".formatted(port)); // Create a HttpRequest HttpRequest request = HttpRequest.newBuilder() .uri(uri) .build(); - client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) - .thenApply(HttpResponse::body) - .thenAccept(System.out::println); + client.send(request, HttpResponse.BodyHandlers.ofString()); } } } From a46fd80de740bb34cdb2577c96570aa720e66507 Mon Sep 17 00:00:00 2001 From: Francois de Parscau Date: Mon, 29 Apr 2024 16:25:26 +0200 Subject: [PATCH 8/8] use ServiceLookup --- tests/camel-integration-test/pom.xml | 3 ++- .../org/apache/karaf/camel/itests/AbstractCamelKarafITest.java | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/camel-integration-test/pom.xml b/tests/camel-integration-test/pom.xml index 85ac4ef73..36670d7d9 100644 --- a/tests/camel-integration-test/pom.xml +++ b/tests/camel-integration-test/pom.xml @@ -78,7 +78,8 @@ org.apache.camel*;${camel.osgi.import.camel.version}, org.apache.karaf.features, org.osgi.service.*, - org.awaitility* + org.awaitility, + org.ops4j.pax.swissbox.tracker geronimo-atinject_1.0_spec diff --git a/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java b/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java index b56fae440..c210924e1 100644 --- a/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java +++ b/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java @@ -20,6 +20,7 @@ import org.apache.camel.ProducerTemplate; import org.junit.After; import org.junit.Before; +import org.ops4j.pax.swissbox.tracker.ServiceLookup; import org.osgi.framework.Bundle; import java.net.InetAddress; @@ -109,7 +110,7 @@ protected void installRequiredFeatures() throws Exception { } private void initCamelContext() { - this.context = bundleContext.getService(bundleContext.getServiceReference(CamelContext.class)); + this.context = ServiceLookup.getService(bundleContext, CamelContext.class); } private void initProducerTemplate() {