Skip to content

Commit

Permalink
Add camel jetty integration test (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
f2par0 authored Apr 29, 2024
1 parent 77ca73d commit 81ea53d
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 5 deletions.
4 changes: 3 additions & 1 deletion tests/camel-integration-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,8 @@ protected void configureConsumer(RouteDefinition consumerRoute) {
}
consumerRoute.routeId("consumer-%s".formatted(getTestComponentName()));
}

public int getNextAvailablePort() {
return AbstractCamelKarafITest.getAvailablePort(30000, 40000);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -94,15 +95,22 @@ public Option[] config() {
@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() {
Expand Down Expand Up @@ -154,7 +162,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"));
}
}
34 changes: 34 additions & 0 deletions tests/components/camel-jetty/pom.xml
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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());
}
}
}

Original file line number Diff line number Diff line change
@@ -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();
}

}
1 change: 1 addition & 0 deletions tests/components/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<modules>
<module>camel-file</module>
<module>camel-seda</module>
<module>camel-jetty</module>
</modules>

<dependencyManagement>
Expand Down

0 comments on commit 81ea53d

Please sign in to comment.