This is an automated email from the ASF dual-hosted git repository. lburgazzoli pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k-runtime.git
The following commit(s) were added to refs/heads/master by this push: new dbd3cf2 xml: cleanup loader and tests dbd3cf2 is described below commit dbd3cf2961a975422e6290f64cc17bc3a6f6e0b0 Author: lburgazzoli <lburgazz...@gmail.com> AuthorDate: Wed Aug 19 15:34:21 2020 +0200 xml: cleanup loader and tests --- camel-k-loader-xml/pom.xml | 18 ++++ .../camel/k/loader/xml/XmlSourceLoaderTest.groovy | 48 ++++++++++ .../camel/k/loader/xml/support/TestRuntime.groovy | 78 ++++++++++++++++ .../camel/k/loader/xml/RoutesLoaderTest.java | 102 --------------------- 4 files changed, 144 insertions(+), 102 deletions(-) diff --git a/camel-k-loader-xml/pom.xml b/camel-k-loader-xml/pom.xml index 9a2565d..bbd7e4f 100644 --- a/camel-k-loader-xml/pom.xml +++ b/camel-k-loader-xml/pom.xml @@ -107,6 +107,24 @@ <build> <plugins> <plugin> + <groupId>org.codehaus.gmavenplus</groupId> + <artifactId>gmavenplus-plugin</artifactId> + <version>${gmavenplus-plugin.version}</version> + <executions> + <execution> + <goals> + <goal>addSources</goal> + <goal>addTestSources</goal> + <goal>compile</goal> + <goal>compileTests</goal> + </goals> + </execution> + </executions> + <configuration> + <invokeDynamic>true</invokeDynamic> + </configuration> + </plugin> + <plugin> <groupId>org.jboss.jandex</groupId> <artifactId>jandex-maven-plugin</artifactId> <executions> diff --git a/camel-k-loader-xml/src/test/groovy/org/apache/camel/k/loader/xml/XmlSourceLoaderTest.groovy b/camel-k-loader-xml/src/test/groovy/org/apache/camel/k/loader/xml/XmlSourceLoaderTest.groovy new file mode 100644 index 0000000..a3eadd0 --- /dev/null +++ b/camel-k-loader-xml/src/test/groovy/org/apache/camel/k/loader/xml/XmlSourceLoaderTest.groovy @@ -0,0 +1,48 @@ +/* + * 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.camel.k.loader.xml + +import org.apache.camel.k.loader.xml.support.TestRuntime +import org.apache.camel.model.ToDefinition +import spock.lang.AutoCleanup +import spock.lang.Specification + +class XmlSourceLoaderTest extends Specification { + @AutoCleanup + def runtime = new TestRuntime() + + def 'load routes'() { + when: + runtime.loadRoutes('classpath:routes.xml') + then: + with(runtime.context.routeDefinitions) { + it[0].input.endpointUri ==~ /timer:.*tick/ + it[0].outputs[0] instanceof ToDefinition + } + + } + + def 'load rests'() { + when: + runtime.loadRoutes('classpath:rests.xml') + then: + with(runtime.context.restDefinitions) { + size() == 1 + } + + } +} diff --git a/camel-k-loader-xml/src/test/groovy/org/apache/camel/k/loader/xml/support/TestRuntime.groovy b/camel-k-loader-xml/src/test/groovy/org/apache/camel/k/loader/xml/support/TestRuntime.groovy new file mode 100644 index 0000000..b62e678 --- /dev/null +++ b/camel-k-loader-xml/src/test/groovy/org/apache/camel/k/loader/xml/support/TestRuntime.groovy @@ -0,0 +1,78 @@ +/* + * 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.camel.k.loader.xml.support + +import org.apache.camel.CamelContext +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.Runtime +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 + final List<RoutesBuilder> builders + final List<Object> configurations + + TestRuntime() { + this.context = new DefaultCamelContext() + this.context.setApplicationContextClassLoader(new CompositeClassloader()) + this.template = this.context.createFluentProducerTemplate() + this.builders = [] + this.configurations = [] + } + + @Override + CamelContext getCamelContext() { + return this.context + } + + @Override + void addRoutes(RoutesBuilder builder) { + this.builders << builder + this.context.addRoutes(builder) + } + + @Override + void addConfiguration(Object configuration) { + this.configurations.add(configuration) + } + + void loadRoutes(String... routes) { + routes.each { + forRoutes(it).accept(Phase.ConfigureRoutes, this) + } + } + + void start() { + context.start() + } + + @Override + void stop() { + context.stop() + } + + @Override + void close() { + stop() + } +} diff --git a/camel-k-loader-xml/src/test/java/org/apache/camel/k/loader/xml/RoutesLoaderTest.java b/camel-k-loader-xml/src/test/java/org/apache/camel/k/loader/xml/RoutesLoaderTest.java deleted file mode 100644 index 9a5c42a..0000000 --- a/camel-k-loader-xml/src/test/java/org/apache/camel/k/loader/xml/RoutesLoaderTest.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * 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.camel.k.loader.xml; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.stream.Stream; - -import org.apache.camel.CamelContext; -import org.apache.camel.RoutesBuilder; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.impl.DefaultCamelContext; -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.support.RuntimeSupport; -import org.apache.camel.model.RouteDefinition; -import org.apache.camel.model.ToDefinition; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - -import static org.assertj.core.api.Assertions.assertThat; - -public class RoutesLoaderTest { - @ParameterizedTest - @MethodSource("parameters") - public void testLoaders(String location, Class<? extends SourceLoader> type) throws Exception { - TestRuntime runtime = new TestRuntime(); - Source source = Sources.fromURI(location); - SourceLoader loader = runtime.load(source); - - assertThat(loader).isInstanceOf(type); - assertThat(runtime.builders).hasSize(1); - assertThat(runtime.builders).first().isInstanceOf(RouteBuilder.class); - - RouteBuilder builder = (RouteBuilder)runtime.builders.get(0); - builder.setContext(runtime.getCamelContext()); - builder.configure(); - - List<RouteDefinition> routes = builder.getRouteCollection().getRoutes(); - assertThat(routes).hasSize(1); - assertThat(routes.get(0).getInput().getEndpointUri()).isEqualTo("timer:tick"); - assertThat(routes.get(0).getOutputs().get(0)).isInstanceOf(ToDefinition.class); - } - - static Stream<Arguments> parameters() { - return Stream.of( - Arguments.arguments("classpath:routes.xml", XmlSourceLoader.class) - ); - } - - static class TestRuntime implements Runtime { - private final CamelContext camelContext; - private final List<RoutesBuilder> builders; - - public TestRuntime() { - this.camelContext = new DefaultCamelContext(); - this.builders = new ArrayList<>(); - } - - @Override - public CamelContext getCamelContext() { - return this.camelContext; - } - - @Override - public void addRoutes(RoutesBuilder builder) { - this.builders.add(builder); - } - - @Override - public void setPropertiesLocations(Collection<String> locations) { - } - - public SourceLoader load(Source source) throws Exception { - SourceLoader loader = RuntimeSupport.loaderFor(camelContext, source); - SourceLoader.Result result = loader.load(this, source); - - result.builder().ifPresent(this::addRoutes); - result.configuration().ifPresent(this::addConfiguration); - - return loader; - } - } -}