Skip to content

Commit

Permalink
quarkus: improve runtime set-up
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Sep 17, 2020
1 parent 77ae741 commit a1b2668
Show file tree
Hide file tree
Showing 25 changed files with 745 additions and 153 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ jobs:
matrix:
native-image-project:
- :camel-k-quarkus-itests-core
- :camel-k-quarkus-itests-runtime
- :camel-k-quarkus-itests-cron
- :camel-k-quarkus-itests-master
- :camel-k-quarkus-itests-kamelet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,22 @@
<artifactId>camel-k-quarkus-itests-cron</artifactId>

<dependencies>
<dependency>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-runtime-quarkus</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-quarkus-cron</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-quarkus-loader-yaml</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-timer</artifactId>
</dependency>

<!-- quarkus dependencies -->
<dependency>
Expand Down Expand Up @@ -68,6 +80,13 @@
<artifactId>hamcrest-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>${awaitility-version}</version>
</dependency>

</dependencies>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,38 @@
*/
package org.apache.camel.k.quarkus.cron.deployment;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import io.quarkus.arc.Unremovable;
import org.apache.camel.CamelContext;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.k.Constants;
import org.apache.camel.k.Runtime;
import org.apache.camel.k.Source;
import org.apache.camel.k.SourceLoader;
import org.apache.camel.k.Sources;
import org.apache.camel.k.cron.CronSourceLoaderInterceptor;
import org.apache.camel.k.loader.yaml.YamlSourceLoader;

@Path("/test")
@ApplicationScoped
public class Application {
@Inject
CamelContext context;
@Inject
Runtime runtime;

private final AtomicBoolean stopped = new AtomicBoolean();

@GET
@Path("/find-cron-interceptor")
Expand All @@ -43,4 +59,54 @@ public String findCronInterceptor() {
.map(Class::getName)
.orElse("");
}

@GET
@Path("/load")
@Produces(MediaType.TEXT_PLAIN)
public String load() throws Exception {
final String code = ""
+ "\n- from:"
+ "\n uri: \"timer:tick?period=1&delay=60000\""
+ "\n steps:"
+ "\n - log: \"${body}\"";

final SourceLoader loader = new YamlSourceLoader();
final Source source = Sources.fromBytes("my-cron", "yaml", null, List.of("cron"), code.getBytes(StandardCharsets.UTF_8));

final CronSourceLoaderInterceptor interceptor = new CronSourceLoaderInterceptor();
interceptor.setRuntime(runtime);
interceptor.setOverridableComponents("timer");

SourceLoader.Result result = interceptor.afterLoad(
loader,
source,
loader.load(runtime, source));

result.builder().ifPresent(b -> {
try {
context.addRoutes(b);
} catch (Exception e) {
throw new RuntimeException(e);
}
});

return "" + context.getRoutesSize();
}

@GET
@Path("/stopped")
@Produces(MediaType.TEXT_PLAIN)
public String stopped() {
return "" + stopped.get();
}

/*
* Override the default ShutdownTask for testing purpose.
*/
@Unremovable
@Singleton
@javax.enterprise.inject.Produces
org.apache.camel.k.quarkus.Application.ShutdownTask shutdownTask() {
return () -> stopped.set(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ quarkus.banner.enabled = false
#
quarkus.camel.routes-discovery.enabled = false



Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,43 @@
*/
package org.apache.camel.k.quarkus.cron.deployment;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import io.quarkus.test.junit.QuarkusTest;
import org.apache.camel.k.cron.CronSourceLoaderInterceptor;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.when;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.Matchers.is;

@QuarkusTest
public class CronTest {
@Test
public void cronInterceptorIsRegistered() throws IOException {
public void cronInterceptorIsRegistered() {
when()
.get("/test/find-cron-interceptor")
.then()
.statusCode(200)
.body(is(CronSourceLoaderInterceptor.class.getName()));
}

@Test
public void cronInvokesShutdown() {
when()
.get("/test/load")
.then()
.statusCode(200)
.body(is("1"));

await().atMost(10, TimeUnit.SECONDS).until(() -> {
String result = when()
.get("/test/stopped")
.then()
.statusCode(200)
.extract().body().asString();

return "true".equals(result);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ public class Application {
@GET
@Path("/inspect")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject findCronInterceptor() {
KubernetesClusterService service = context.hasService(KubernetesClusterService.class);
public JsonObject inspect() {
var service = context.hasService(KubernetesClusterService.class);

return Json.createObjectBuilder()
.add("cluster-service", service != null ? service.getClass().getName() : "")
.add("cluster-service-cm", service != null ? service.getConfigMapName() : "")

.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#
# Quarkus
#
quarkus.log.console.enable = true
quarkus.log.console.enable = false
quarkus.banner.enabled = false

#
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?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">
<parent>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-quarkus-itests</artifactId>
<version>1.5.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>camel-k-quarkus-itests-runtime</artifactId>

<dependencies>
<dependency>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-runtime-quarkus</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jsonb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>


<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${build-helper-maven-plugin-version}</version>
<executions>
<execution>
<id>reserve-network-port</id>
<goals>
<goal>reserve-network-port</goal>
</goals>
<phase>process-resources</phase>
<configuration>
<portNames>
<portName>test.http.port.jvm</portName>
<portName>test.http.port.native</portName>
</portNames>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus-version}</version>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<environmentVariables>
<CAMEL_K_CONF>${project.basedir}/src/test/resources/test.properties</CAMEL_K_CONF>
</environmentVariables>
<systemProperties>
<quarkus.http.test-port>${test.http.port.jvm}</quarkus.http.test-port>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<camel.k.conf>${project.basedir}/src/test/resources/test.properties</camel.k.conf>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<environmentVariables>
<CAMEL_K_CONF>${project.basedir}/src/test/resources/test.properties</CAMEL_K_CONF>
</environmentVariables>
<systemProperties>
<quarkus.http.test-port>${test.http.port.native}</quarkus.http.test-port>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
</systemProperties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
Loading

0 comments on commit a1b2668

Please sign in to comment.