Skip to content

Commit 81ea53d

Browse files
authored
Add camel jetty integration test (#59)
1 parent 77ca73d commit 81ea53d

File tree

7 files changed

+161
-5
lines changed

7 files changed

+161
-5
lines changed

tests/camel-integration-test/pom.xml

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@
7676
<Import-Package>
7777
org.apache.karaf.itests,org.ops4j.pax.exam,org.osgi.framework,org.junit,
7878
org.apache.camel*;${camel.osgi.import.camel.version},
79+
org.apache.karaf.features,
7980
org.osgi.service.*,
80-
org.awaitility*
81+
org.awaitility,
82+
org.ops4j.pax.swissbox.tracker
8183
</Import-Package>
8284
</instructions>
8385
<excludeDependencies>geronimo-atinject_1.0_spec</excludeDependencies>

tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelComponent.java

+4
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,8 @@ protected void configureConsumer(RouteDefinition consumerRoute) {
9494
}
9595
consumerRoute.routeId("consumer-%s".formatted(getTestComponentName()));
9696
}
97+
98+
public int getNextAvailablePort() {
99+
return AbstractCamelKarafITest.getAvailablePort(30000, 40000);
100+
}
97101
}

tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/AbstractCamelKarafITest.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apache.camel.ProducerTemplate;
2121
import org.junit.After;
2222
import org.junit.Before;
23+
import org.ops4j.pax.swissbox.tracker.ServiceLookup;
2324
import org.osgi.framework.Bundle;
2425

2526
import java.net.InetAddress;
@@ -94,15 +95,22 @@ public Option[] config() {
9495
@Before
9596
public void init() throws Exception {
9697
String testComponentName = getTestComponentName();
97-
installBundle("file://%s/%s-%s.jar".formatted(getBaseDir(), testComponentName, getVersion()),true);
98-
assertBundleInstalled(testComponentName);
98+
installRequiredFeatures();
99+
installBundle("file://%s/%s-%s.jar".formatted(getBaseDir(), testComponentName, getVersion()), true);
99100
assertBundleInstalledAndRunning(testComponentName);
100101
initCamelContext();
101102
initProducerTemplate();
102103
}
103104

105+
protected void installRequiredFeatures() throws Exception {
106+
String featureName = toKebabCase(this.getClass().getSimpleName()).replace("-itest", "");
107+
if (null != featureService.getFeature(featureName)) {
108+
installAndAssertFeature(featureName);
109+
}
110+
}
111+
104112
private void initCamelContext() {
105-
this.context = bundleContext.getService(bundleContext.getServiceReference(CamelContext.class));
113+
this.context = ServiceLookup.getService(bundleContext, CamelContext.class);
106114
}
107115

108116
private void initProducerTemplate() {
@@ -154,7 +162,7 @@ protected void assertBundleInstalledAndRunning(String name) {
154162
//need to check with the command because the status may be Active while it's displayed as Waiting in the console
155163
//because of an exception for instance
156164
String bundles = executeCommand("bundle:list -s -t 0 | grep %s".formatted(name));
157-
Assert.assertTrue("bundle%s is in state %d /%s".formatted(bundle.getSymbolicName(), bundle.getState(), bundles),
165+
Assert.assertTrue("bundle %s is in state %d /%s".formatted(bundle.getSymbolicName(), bundle.getState(), bundles),
158166
bundles.contains("Active"));
159167
}
160168
}

tests/components/camel-jetty/pom.xml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Licensed to the Apache Software Foundation (ASF) under one or more
5+
contributor license agreements. See the NOTICE file distributed with
6+
this work for additional information regarding copyright ownership.
7+
The ASF licenses this file to You under the Apache License, Version 2.0
8+
(the "License"); you may not use this file except in compliance with
9+
the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
<project xmlns="http://maven.apache.org/POM/4.0.0"
21+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
23+
<modelVersion>4.0.0</modelVersion>
24+
<parent>
25+
<groupId>org.apache.camel.karaf</groupId>
26+
<artifactId>camel-karaf-components-test</artifactId>
27+
<version>4.5.0-SNAPSHOT</version>
28+
</parent>
29+
30+
<artifactId>camel-jetty-test</artifactId>
31+
<name>Apache Camel :: Karaf :: Tests :: Components :: Jetty</name>
32+
33+
34+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.karaf.camel.test;
17+
18+
import java.net.URI;
19+
import java.net.http.HttpClient;
20+
import java.net.http.HttpRequest;
21+
import java.net.http.HttpResponse;
22+
import java.util.function.Function;
23+
24+
import org.apache.camel.Exchange;
25+
import org.apache.camel.Processor;
26+
import org.apache.camel.builder.RouteBuilder;
27+
import org.apache.camel.model.RouteDefinition;
28+
import org.apache.karaf.camel.itests.AbstractCamelComponentResultMockBased;
29+
import org.osgi.service.component.annotations.Component;
30+
31+
@Component(
32+
name = "karaf-camel-jetty-test",
33+
immediate = true
34+
)
35+
public class CamelJettyComponent extends AbstractCamelComponentResultMockBased {
36+
37+
private final int port = getNextAvailablePort();
38+
39+
@Override
40+
protected Function<RouteBuilder, RouteDefinition> consumerRoute() {
41+
return builder -> builder.from("jetty://http://localhost:%s/jettyTest".formatted(port)).transform(builder.constant("OK"));
42+
}
43+
44+
@Override
45+
protected void configureProducer(RouteBuilder builder, RouteDefinition producerRoute) {
46+
producerRoute.log("calling http endpoint")
47+
.process(new HttpClientProcessor());
48+
}
49+
50+
class HttpClientProcessor implements Processor {
51+
52+
@Override
53+
public void process(Exchange exchange) throws Exception {
54+
55+
HttpClient client = HttpClient.newHttpClient();
56+
57+
// Create a URI for the request
58+
URI uri = URI.create("http://localhost:%s/jettyTest".formatted(port));
59+
60+
// Create a HttpRequest
61+
HttpRequest request = HttpRequest.newBuilder()
62+
.uri(uri)
63+
.build();
64+
65+
client.send(request, HttpResponse.BodyHandlers.ofString());
66+
}
67+
}
68+
}
69+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package org.apache.karaf.camel.itests;
15+
16+
import org.apache.camel.component.mock.MockEndpoint;
17+
import org.junit.Test;
18+
import org.junit.runner.RunWith;
19+
import org.ops4j.pax.exam.junit.PaxExam;
20+
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
21+
import org.ops4j.pax.exam.spi.reactors.PerClass;
22+
23+
24+
@RunWith(PaxExam.class)
25+
@ExamReactorStrategy(PerClass.class)
26+
public class CamelJettyITest extends AbstractCamelKarafResultMockBasedITest {
27+
28+
@Override
29+
protected void configureMock(MockEndpoint mock) {
30+
mock.expectedBodiesReceived("OK");
31+
}
32+
33+
@Test
34+
public void testResultMock() throws Exception {
35+
assertMockEndpointsSatisfied();
36+
}
37+
38+
}

tests/components/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<modules>
4040
<module>camel-file</module>
4141
<module>camel-seda</module>
42+
<module>camel-jetty</module>
4243
</modules>
4344

4445
<dependencyManagement>

0 commit comments

Comments
 (0)