This is an automated email from the ASF dual-hosted git repository.

nfilotto pushed a commit to branch 276/integration-test-framework
in repository https://gitbox.apache.org/repos/asf/camel-karaf.git

commit ade50998b32708dd7e616b3aa04ddcbcd7434c10
Author: François de Parscau <116000379+f2p...@users.noreply.github.com>
AuthorDate: Mon Apr 29 16:26:57 2024 +0200

    Add camel jetty integration test (#276)
---
 tests/camel-integration-test/pom.xml               |  4 +-
 .../karaf/camel/itests/AbstractCamelComponent.java |  4 ++
 .../camel/itests/AbstractCamelKarafITest.java      | 16 +++--
 tests/components/camel-jetty/pom.xml               | 34 +++++++++++
 .../karaf/camel/test/CamelJettyComponent.java      | 69 ++++++++++++++++++++++
 .../apache/karaf/camel/itests/CamelJettyITest.java | 38 ++++++++++++
 tests/components/pom.xml                           |  1 +
 7 files changed, 161 insertions(+), 5 deletions(-)

diff --git a/tests/camel-integration-test/pom.xml 
b/tests/camel-integration-test/pom.xml
index eca2692b..36670d7d 100644
--- a/tests/camel-integration-test/pom.xml
+++ b/tests/camel-integration-test/pom.xml
@@ -76,8 +76,10 @@
                         <Import-Package>
                             
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*
+                            org.awaitility,
+                            org.ops4j.pax.swissbox.tracker
                         </Import-Package>
                     </instructions>
                     
<excludeDependencies>geronimo-atinject_1.0_spec</excludeDependencies>
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 47ca0254..21bb7f75 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 @@ public abstract class AbstractCamelComponent {
         }
         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 8d4c6025..c210924e 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.CamelContext;
 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;
@@ -94,15 +95,22 @@ public abstract class AbstractCamelKarafITest extends 
KarafTestSupport {
     @Before
     public void init() throws Exception {
         String testComponentName = getTestComponentName();
-        installBundle("file://%s/%s-%s.jar".formatted(getBaseDir(), 
testComponentName, getVersion()),true);
-        assertBundleInstalled(testComponentName);
+        installRequiredFeatures();
+        installBundle("file://%s/%s-%s.jar".formatted(getBaseDir(), 
testComponentName, getVersion()), true);
         assertBundleInstalledAndRunning(testComponentName);
         initCamelContext();
         initProducerTemplate();
     }
 
+    protected void installRequiredFeatures() throws Exception {
+        String featureName = 
toKebabCase(this.getClass().getSimpleName()).replace("-itest", "");
+        if (null != featureService.getFeature(featureName)) {
+            installAndAssertFeature(featureName);
+        }
+    }
+
     private void initCamelContext() {
-        this.context = 
bundleContext.getService(bundleContext.getServiceReference(CamelContext.class));
+        this.context = ServiceLookup.getService(bundleContext, 
CamelContext.class);
     }
 
     private void initProducerTemplate() {
@@ -154,7 +162,7 @@ public abstract class AbstractCamelKarafITest extends 
KarafTestSupport {
         //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 00000000..705cba93
--- /dev/null
+++ b/tests/components/camel-jetty/pom.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0";
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.apache.camel.karaf</groupId>
+        <artifactId>camel-karaf-components-test</artifactId>
+        <version>4.5.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>camel-jetty-test</artifactId>
+    <name>Apache Camel :: Karaf :: Tests :: Components :: Jetty</name>
+
+
+</project>
\ 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 00000000..0bd93e9a
--- /dev/null
+++ 
b/tests/components/camel-jetty/src/main/java/org/apache/karaf/camel/test/CamelJettyComponent.java
@@ -0,0 +1,69 @@
+/*
+ * 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<RouteBuilder, RouteDefinition> consumerRoute() {
+        return builder -> 
builder.from("jetty://http://localhost:%s/jettyTest".formatted(port)).transform(builder.constant("OK"));
+    }
+
+    @Override
+    protected void configureProducer(RouteBuilder builder, RouteDefinition 
producerRoute) {
+        producerRoute.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/jettyTest".formatted(port));
+
+            // Create a HttpRequest
+            HttpRequest request = HttpRequest.newBuilder()
+                    .uri(uri)
+                    .build();
+
+            client.send(request, HttpResponse.BodyHandlers.ofString());
+        }
+    }
+}
+
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 00000000..f904fb2c
--- /dev/null
+++ 
b/tests/components/camel-jetty/src/test/java/org/apache/karaf/camel/itests/CamelJettyITest.java
@@ -0,0 +1,38 @@
+/*
+ * 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 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 787ba916..e26df75d 100644
--- a/tests/components/pom.xml
+++ b/tests/components/pom.xml
@@ -39,6 +39,7 @@
     <modules>
         <module>camel-file</module>
         <module>camel-seda</module>
+        <module>camel-jetty</module>
     </modules>
 
     <dependencyManagement>

Reply via email to