Skip to content

Commit

Permalink
chore: upgrade to Camel 3.6.0, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zregvart committed Oct 27, 2020
1 parent 2db8e2c commit 13b1028
Show file tree
Hide file tree
Showing 11 changed files with 351 additions and 42 deletions.
13 changes: 0 additions & 13 deletions .mvn/extensions.xml

This file was deleted.

2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM openjdk:11 AS build
WORKDIR /build
COPY mvnw pom.xml srcdeps.yaml ./
COPY mvnw pom.xml ./
COPY .mvn ./.mvn/
RUN ./mvnw -U -B -q dependency:go-offline
COPY src src
Expand Down
67 changes: 64 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
<parent>
<groupId>org.basepom</groupId>
<artifactId>basepom-oss</artifactId>
<version>32</version>
<version>33</version>
</parent>

<groupId>com.github.zregvart</groupId>
<artifactId>camel-netty-proxy</artifactId>
<version>0.0.1</version>

<properties>
<camel.version>3.4.0-SNAPSHOT</camel.version>
<camel.version>3.6.0</camel.version>
<fabric8.openshift.generateRoute>false</fabric8.openshift.generateRoute>
<project.build.targetJdk>11</project.build.targetJdk>
</properties>
Expand All @@ -47,6 +47,16 @@
<artifactId>camel-api</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-support</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId>
<version>4.1.53.Final</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-netty-http</artifactId>
Expand All @@ -59,6 +69,48 @@
<version>1.2.3</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.jqwik</groupId>
<artifactId>jqwik</artifactId>
<version>1.3.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.jqwik</groupId>
<artifactId>jqwik-api</artifactId>
<version>1.3.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.18.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ru.lanwen.wiremock</groupId>
<artifactId>wiremock-junit5</artifactId>
<version>1.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
<version>2.27.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -67,11 +119,20 @@
<groupId>org.basepom.maven</groupId>
<artifactId>duplicate-finder-maven-plugin</artifactId>
<configuration>
<ignoredResourcePatterns>
<ignoredResourcePatterns combine.children="append">
<ignoredResourcePattern>component.properties</ignoredResourcePattern>
</ignoredResourcePatterns>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Properties.java</include>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/github/zregvart/cnp/ProxyApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@
*/
package com.github.zregvart.cnp;

import io.netty.handler.logging.LoggingHandler;

import org.apache.camel.main.Main;

public class ProxyApp {
public static void main(final String[] args) throws Exception {
public static void main(final String[] args) {
final Main main = new Main();
launch(main);
}

static void launch(final Main main) {
try {
main.bind("logging-handler", new LoggingHandler());
main.configure().addRoutesBuilder(new ProxyRoute());
main.run();
} catch (final Exception e) {
throw new ExceptionInInitializerError(e);
} finally {
main.stop();
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/github/zregvart/cnp/ProxyRoute.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ProxyRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
final RouteDefinition from;
if (Files.exists(Path.of("/tls", "keystore.jks"))) {
if (Files.exists(keystorePath())) {
from = from("netty-http:proxy://0.0.0.0:8443?ssl=true&keyStoreFile=/tls/keystore.jks&passphrase=changeit&trustStoreFile=/tls/keystore.jks");
} else {
from = from("netty-http:proxy://0.0.0.0:8080");
Expand All @@ -46,6 +46,10 @@ public void configure() throws Exception {
.process(ProxyRoute::uppercase);
}

Path keystorePath() {
return Path.of("/tls", "keystore.jks");
}

public static void uppercase(final Exchange exchange) {
final Message message = exchange.getIn();
final String body = message.getBody(String.class);
Expand Down
106 changes: 106 additions & 0 deletions src/test/java/com/github/zregvart/cnp/ProxyAppIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* 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 com.github.zregvart.cnp;

import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ForkJoinPool;

import org.apache.camel.main.BaseMainSupport;
import org.apache.camel.main.Main;
import org.apache.camel.main.MainListenerSupport;
import org.apache.camel.main.SimpleMainShutdownStrategy;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import com.github.tomakehurst.wiremock.WireMockServer;

import static org.assertj.core.api.Assertions.assertThat;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.post;

import ru.lanwen.wiremock.ext.WiremockResolver;
import ru.lanwen.wiremock.ext.WiremockResolver.Wiremock;
import ru.lanwen.wiremock.ext.WiremockUriResolver;
import ru.lanwen.wiremock.ext.WiremockUriResolver.WiremockUri;

@ExtendWith({
WiremockResolver.class,
WiremockUriResolver.class
})
class ProxyAppIntegrationTest {

final static SimpleMainShutdownStrategy SHUTDOWN = new SimpleMainShutdownStrategy();

@Test
public void shouldProxyPlaintextHTTP(@Wiremock final WireMockServer server, @WiremockUri final String backend) throws Exception {
server.stubFor(post("/path?a=b")
.withQueryParam("a", equalTo("b"))
.withHost(equalTo("localhost"))
.withHeader("x", equalTo("y"))
.withRequestBody(equalTo("HI"))
.willReturn(aResponse().withBody("hello")));

final HttpClient client = HttpClient.newBuilder()
.proxy(ProxySelector.of(new InetSocketAddress("localhost", 8080)))
.build();

final HttpResponse<String> response = client.send(
HttpRequest.newBuilder(URI.create(backend).resolve("/path?a=b"))
.POST(BodyPublishers.ofString("hi"))
.header("x", "y")
.build(),
BodyHandlers.ofString());

assertThat(response.body()).isEqualTo("HELLO");
}

@BeforeAll
static void launchMain() throws InterruptedException {
final Main main = new Main();
final CountDownLatch started = new CountDownLatch(1);
main.addMainListener(new MainListenerSupport() {
@Override
public void afterStart(final BaseMainSupport given) {
started.countDown();
}

@Override
public void afterStop(final BaseMainSupport main) {
started.countDown();
}
});
main.setShutdownStrategy(SHUTDOWN);
ForkJoinPool.commonPool().submit(() -> ProxyApp.launch(main));
started.await();
}

@AfterAll
static void shutdownMain() {
SHUTDOWN.shutdown();
}
}
44 changes: 44 additions & 0 deletions src/test/java/com/github/zregvart/cnp/ProxyRouteProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* 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 com.github.zregvart.cnp;

import net.jqwik.api.ForAll;
import net.jqwik.api.Property;

import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.support.DefaultExchange;
import org.apache.camel.support.DefaultMessage;

import static org.assertj.core.api.Assertions.assertThat;

class ProxyRouteProperties {

private final CamelContext context = new DefaultCamelContext();

@Property
void messageBodiesShouldBeUppercased(@ForAll final String body) {
final DefaultMessage message = new DefaultMessage(context);
message.setBody(body);

final DefaultExchange exchange = new DefaultExchange(context);
exchange.setMessage(message);

ProxyRoute.uppercase(exchange);

assertThat(message.getBody(String.class)).isUpperCase();
}
}
Loading

0 comments on commit 13b1028

Please sign in to comment.