Skip to content

Commit

Permalink
kamelets: create a camel-kamelet component apache#375
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Sep 14, 2020
1 parent bdacdc3 commit 4521414
Show file tree
Hide file tree
Showing 86 changed files with 2,839 additions and 680 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ jobs:
native-image-project:
- :camel-k-quarkus-itests-core
- :camel-k-quarkus-itests-cron
- :camel-k-quarkus-itests-kamelet
- :camel-k-quarkus-itests-knative
- :camel-k-quarkus-itests-loader-xml
- :camel-k-quarkus-itests-loader-yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ import org.apache.camel.RuntimeCamelException
import org.apache.camel.component.jackson.JacksonDataFormat
import org.apache.camel.component.log.LogComponent
import org.apache.camel.component.seda.SedaComponent
import org.apache.camel.k.Sources
import org.apache.camel.k.listener.RoutesConfigurer
import org.apache.camel.k.loader.groovy.support.MyBean
import org.apache.camel.k.loader.groovy.support.TestRuntime
import org.apache.camel.language.bean.BeanLanguage
import org.apache.camel.model.FromDefinition
import org.apache.camel.model.ModelCamelContext
import org.apache.camel.model.ToDefinition
import org.apache.camel.model.rest.GetVerbDefinition
import org.apache.camel.model.rest.PostVerbDefinition
Expand All @@ -45,16 +42,10 @@ class GroovySourceLoaderTest extends Specification {
def runtime = new TestRuntime()

def "load routes"() {
given:
def source = Sources.fromURI("classpath:routes.groovy")

when:
def loader = RoutesConfigurer.load(runtime, source)

runtime.loadRoutes("classpath:routes.groovy")
then:
loader instanceof GroovySourceLoader

with(runtime.getCamelContext(ModelCamelContext.class).routeDefinitions) {
with(runtime.context.routeDefinitions) {
it.size() == 1

it[0].outputs[0] instanceof ToDefinition
Expand All @@ -63,16 +54,10 @@ class GroovySourceLoaderTest extends Specification {
}

def "load routes with endpoint dsl"() {
given:
def source = Sources.fromURI("classpath:routes-with-endpoint-dsl.groovy")

when:
def loader = RoutesConfigurer.load(runtime, source)

runtime.loadRoutes("classpath:routes-with-endpoint-dsl.groovy")
then:
loader instanceof GroovySourceLoader

with(runtime.getCamelContext(ModelCamelContext.class).routeDefinitions) {
with(runtime.context.routeDefinitions) {
it.size() == 1

with(it[0].input, FromDefinition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import org.apache.camel.CamelContext
import org.apache.camel.RoutesBuilder
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.k.Runtime
import org.apache.camel.k.support.SourcesSupport
import org.apache.camel.model.ModelCamelContext

import static org.apache.camel.k.listener.RoutesConfigurer.forRoutes

class TestRuntime implements Runtime, AutoCloseable {
public final ModelCamelContext context
public final List<RoutesBuilder> builders
Expand All @@ -45,9 +44,7 @@ class TestRuntime implements Runtime, AutoCloseable {
}

void loadRoutes(String... routes) {
routes.each {
forRoutes(it).accept(Phase.ConfigureRoutes, this)
}
SourcesSupport.loadSources(this, routes)
}

void start() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ import org.apache.camel.RoutesBuilder
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.k.CompositeClassloader
import org.apache.camel.k.Runtime
import org.apache.camel.k.support.SourcesSupport
import org.apache.camel.model.ModelCamelContext

import static org.apache.camel.k.listener.RoutesConfigurer.forRoutes

class TestRuntime implements Runtime, AutoCloseable {
final ModelCamelContext context
final List<RoutesBuilder> builders
Expand Down Expand Up @@ -54,9 +53,7 @@ class TestRuntime implements Runtime, AutoCloseable {
}

void loadRoutes(String... routes) {
routes.each {
forRoutes(it).accept(Phase.ConfigureRoutes, this)
}
SourcesSupport.loadSources(this, routes)
}

void start() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ import org.apache.camel.RoutesBuilder
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.k.CompositeClassloader
import org.apache.camel.k.Runtime
import org.apache.camel.k.support.SourcesSupport
import org.apache.camel.model.ModelCamelContext

import static org.apache.camel.k.listener.RoutesConfigurer.forRoutes

class TestRuntime implements Runtime, AutoCloseable {
final ModelCamelContext context
final FluentProducerTemplate template
Expand Down Expand Up @@ -57,9 +56,7 @@ class TestRuntime implements Runtime, AutoCloseable {
}

void loadRoutes(String... routes) {
routes.each {
forRoutes(it).accept(Phase.ConfigureRoutes, this)
}
SourcesSupport.loadSources(this, routes)
}

void start() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.apache.camel.k.Runtime;
import org.apache.camel.k.Source;
import org.apache.camel.k.Sources;
import org.apache.camel.k.listener.RoutesConfigurer;
import org.apache.camel.k.support.SourcesSupport;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -33,7 +33,7 @@ public void testLoad() throws Exception {
final Runtime runtime = Runtime.on(context);
final Source source = Sources.fromURI("classpath:routes.kts");

RoutesConfigurer.load(runtime, source);
SourcesSupport.load(runtime, source);

try {
context.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import org.apache.camel.FluentProducerTemplate
import org.apache.camel.RoutesBuilder
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.k.CompositeClassloader
import org.apache.camel.k.listener.RoutesConfigurer.forRoutes
import org.apache.camel.k.support.SourcesSupport
import org.apache.camel.model.ModelCamelContext
import java.util.ArrayList

Expand Down Expand Up @@ -54,9 +54,7 @@ class TestRuntime : Runtime {
}

fun loadRoutes(vararg routes: String) {
for (route in routes) {
forRoutes(route).accept(Runtime.Phase.ConfigureRoutes, this)
}
SourcesSupport.loadSources(this, *routes)
}

fun start() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ import org.apache.camel.RoutesBuilder
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.k.CompositeClassloader
import org.apache.camel.k.Runtime
import org.apache.camel.k.support.SourcesSupport
import org.apache.camel.model.ModelCamelContext

import static org.apache.camel.k.listener.RoutesConfigurer.forRoutes

class TestRuntime implements Runtime, AutoCloseable {
final ModelCamelContext context
final FluentProducerTemplate template
Expand Down Expand Up @@ -57,9 +56,7 @@ class TestRuntime implements Runtime, AutoCloseable {
}

void loadRoutes(String... routes) {
routes.each {
forRoutes(it).accept(Phase.ConfigureRoutes, this)
}
SourcesSupport.loadSources(this, routes)
}

void start() {
Expand Down
5 changes: 5 additions & 0 deletions camel-k-main/camel-k-runtime-main/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@
<artifactId>camel-knative-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-kamelet</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.k.CompositeClassloader;
import org.apache.camel.k.Runtime;
import org.apache.camel.k.support.PropertiesSupport;
import org.apache.camel.main.BaseMainSupport;
import org.apache.camel.main.MainSupport;
import org.apache.camel.main.RoutesCollector;
import org.apache.camel.model.RouteTemplatesDefinition;
import org.apache.camel.model.RoutesDefinition;
import org.apache.camel.model.rest.RestsDefinition;
import org.apache.camel.spi.HasId;
import org.apache.camel.util.function.ThrowingConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -103,16 +101,7 @@ public void addListeners(Iterable<Runtime.Listener> listeners) {
}

public void addListener(Runtime.Listener listener) {
if (listener instanceof HasId) {
String id = ((HasId) listener).getId();
if (!id.endsWith(".")) {
id = id + ".";
}

PropertiesSupport.bindProperties(getCamelContext(), listener, id);
}

LOGGER.info("Add listener: {}", listener);
LOGGER.debug("Add listener: {}", listener);

this.listeners.add(listener);
}
Expand Down Expand Up @@ -178,7 +167,7 @@ private void invokeListeners(Phase phase) {
.sorted(Comparator.comparingInt(Listener::getOrder))
.forEach(l -> {
if (l.accept(phase, ApplicationRuntime.this)) {
LOGGER.info("Listener {} executed in phase {}", l, phase);
LOGGER.debug("Listener {} executed in phase {}", l, phase);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import org.apache.camel.k.Runtime;
import org.apache.camel.k.http.PlatformHttpServiceContextCustomizer;
import org.apache.camel.k.listener.ContextConfigurer;
import org.apache.camel.k.listener.RoutesConfigurer;
import org.apache.camel.k.listener.SourcesConfigurer;
import org.apache.camel.k.main.support.MyBean;
import org.apache.camel.k.support.SourcesSupport;
import org.apache.camel.k.test.AvailablePortFinder;
import org.apache.camel.model.ModelCamelContext;
import org.apache.camel.model.ToDefinition;
Expand All @@ -53,10 +55,11 @@ public void cleanUp() throws Exception {
runtime.stop();
}
}

@Test
void testLoadMultipleRoutes() throws Exception {
runtime.addListener(new ContextConfigurer());
runtime.addListener(RoutesConfigurer.forRoutes("classpath:r1.js", "classpath:r2.mytype?language=js"));
runtime.addListener(SourcesSupport.forRoutes("classpath:r1.js", "classpath:r2.mytype?language=js"));
runtime.addListener(Runtime.Phase.Started, r -> {
CamelContext context = r.getCamelContext();
List<Route> routes = context.getRoutes();
Expand All @@ -74,12 +77,12 @@ void testLoadMultipleRoutes() throws Exception {
@Test
void testLoadRouteAndRest() throws Exception {
runtime.addListener(new ContextConfigurer());
runtime.addListener(RoutesConfigurer.forRoutes("classpath:routes.xml", "classpath:rests.xml"));
runtime.addListener(SourcesSupport.forRoutes("classpath:routes.xml", "classpath:rests.xml"));
runtime.addListener(Runtime.Phase.Started, r -> {
CamelContext context = r.getCamelContext();
ModelCamelContext context = r.getCamelContext(ModelCamelContext.class);

assertThat(context.adapt(ModelCamelContext.class).getRouteDefinitions()).isNotEmpty();
assertThat(context.adapt(ModelCamelContext.class).getRestDefinitions()).isNotEmpty();
assertThat(context.getRouteDefinitions()).isNotEmpty();
assertThat(context.getRestDefinitions()).isNotEmpty();

runtime.stop();
});
Expand All @@ -94,16 +97,55 @@ void testLoadRouteWithExpression() throws Exception {
));

runtime.addListener(new ContextConfigurer());
runtime.addListener(RoutesConfigurer.forRoutes("classpath:routes-with-expression.xml"));
runtime.addListener(Runtime.Phase.Started, r -> runtime.stop());
runtime.addListener(SourcesSupport.forRoutes("classpath:routes-with-expression.xml"));
runtime.addListener(Runtime.Phase.Started, Runtime::stop);
runtime.run();
}

@Test
public void testLoadJavaSource() throws Exception {
ApplicationRuntime runtime = new ApplicationRuntime();
runtime.addListener(RoutesConfigurer.forRoutes("classpath:MyRoutesWithBeans.java", "classpath:MyRoutesConfig.java"));
runtime.addListener(Runtime.Phase.Started, r -> runtime.stop());
runtime.addListener(SourcesSupport.forRoutes("classpath:MyRoutesWithBeans.java", "classpath:MyRoutesConfig.java"));
runtime.addListener(Runtime.Phase.Started, r -> {
assertThat(runtime.getCamelContext().getRoutes()).hasSize(1);
assertThat(runtime.getRegistry().lookupByName("my-processor")).isNotNull();
assertThat(runtime.getRegistry().lookupByName("my-bean")).isInstanceOfSatisfying(MyBean.class, b -> {
assertThat(b).hasFieldOrPropertyWithValue("name", "my-bean-name");
});
r.stop();
});
runtime.run();
}

@Test
public void testLoadJavaSourceFromProperties() throws Exception {
runtime.setInitialProperties(
"camel.k.sources[0].name", "MyRoutesWithBeans",
"camel.k.sources[0].location", "classpath:MyRoutesWithBeans.java",
"camel.k.sources[0].language", "java",
"camel.k.sources[1].name", "MyRoutesConfig",
"camel.k.sources[1].location", "classpath:MyRoutesConfig.java",
"camel.k.sources[1].language", "java"
);
runtime.addListener(new SourcesConfigurer());
runtime.addListener(Runtime.Phase.Started, r -> {
assertThat(runtime.getCamelContext().getRoutes()).hasSize(1);
assertThat(runtime.getRegistry().lookupByName("my-processor")).isNotNull();
assertThat(runtime.getRegistry().lookupByName("my-bean")).isInstanceOfSatisfying(MyBean.class, b -> {
assertThat(b).hasFieldOrPropertyWithValue("name", "my-bean-name");
});
r.stop();
});
runtime.run();
}

@Test
public void testLoadJavaSourceFromSimpleProperties() throws Exception {
runtime.setInitialProperties(
"camel.k.sources[0].location", "classpath:MyRoutesWithBeans.java",
"camel.k.sources[1].location", "classpath:MyRoutesConfig.java"
);
runtime.addListener(new SourcesConfigurer());
runtime.addListener(Runtime.Phase.Started, Runtime::stop);
runtime.run();

assertThat(runtime.getRegistry().lookupByName("my-processor")).isNotNull();
Expand All @@ -124,8 +166,8 @@ public void testLoadJavaSourceWrap() throws Exception {
phsc.apply(runtime.getCamelContext());

runtime.getCamelContext().addComponent("knative", component);
runtime.addListener(RoutesConfigurer.forRoutes("classpath:MyRoutesWithBeans.java?interceptors=knative-source"));
runtime.addListener(Runtime.Phase.Started, r -> runtime.stop());
runtime.addListener(SourcesSupport.forRoutes("classpath:MyRoutesWithBeans.java?interceptors=knative-source"));
runtime.addListener(Runtime.Phase.Started, Runtime::stop);
runtime.run();

assertThat(runtime.getRegistry().lookupByName("my-bean")).isInstanceOfSatisfying(MyBean.class, b -> {
Expand Down
Loading

0 comments on commit 4521414

Please sign in to comment.