Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add camel jetty integration test #59

Merged
merged 8 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -94,13 +94,18 @@ public Option[] config() {
@Before
public void init() throws Exception {
String testComponentName = getTestComponentName();
installBundle("file://%s/%s-%s.jar".formatted(getBaseDir(), testComponentName, getVersion()),true);
installRequiredFeatures();
installBundle("file://%s/%s-%s.jar".formatted(getBaseDir(), testComponentName, getVersion()), true);
assertBundleInstalled(testComponentName);
assertBundleInstalledAndRunning(testComponentName);
initCamelContext();
initProducerTemplate();
}

protected void installRequiredFeatures() throws Exception{
//default do nothing
}

private void initCamelContext() {
this.context = bundleContext.getService(bundleContext.getServiceReference(CamelContext.class));
}
Expand Down Expand Up @@ -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"));
}
}
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,71 @@
/*
* 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/test".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/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);
}
}
}

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

}
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