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 7fdd4ac groovy: cleanup loader and tests 7fdd4ac is described below commit 7fdd4ac8984168dee5e9c81d42113193b0858a76 Author: lburgazzoli <lburgazz...@gmail.com> AuthorDate: Wed Aug 19 09:23:34 2020 +0200 groovy: cleanup loader and tests --- .../k/loader/groovy/dsl/BeanConfiguration.groovy | 2 - .../groovy/dsl/ComponentsConfiguration.groovy | 2 - .../groovy/dsl/IntegrationConfiguration.groovy | 4 +- .../camel/k/loader/groovy/dsl/Support.groovy | 10 +- .../CamelContextExtensions.groovy} | 29 +++--- .../extension/ExpressionClauseExtensions.groovy | 3 +- .../groovy/extension/LogComponentExtension.groovy | 2 + .../extension/ProcessorDefinitionExtensions.groovy | 4 +- .../org.codehaus.groovy.runtime.ExtensionModule | 3 +- .../apache/camel/k/loader/groovy/LoaderTest.groovy | 59 +++--------- .../k/loader/groovy/dsl/IntegrationTest.groovy | 105 +++++++++------------ .../extension/ExpressionClauseExtensionTest.groovy | 64 +++++-------- .../k/loader/groovy/support/TestRuntime.groovy | 66 +++++++++++++ .../org/apache/camel/k/support/RouteBuilders.java | 12 ++- 14 files changed, 187 insertions(+), 178 deletions(-) diff --git a/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/BeanConfiguration.groovy b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/BeanConfiguration.groovy index feb0657..a8a4461 100644 --- a/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/BeanConfiguration.groovy +++ b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/BeanConfiguration.groovy @@ -16,12 +16,10 @@ */ package org.apache.camel.k.loader.groovy.dsl -import groovy.util.logging.Slf4j import org.apache.camel.CamelContext import org.apache.camel.ExtendedCamelContext import org.apache.camel.support.PropertyBindingSupport -@Slf4j class BeanConfiguration { private final CamelContext context private final Object target diff --git a/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/ComponentsConfiguration.groovy b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/ComponentsConfiguration.groovy index ed69798..9eae49f 100644 --- a/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/ComponentsConfiguration.groovy +++ b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/ComponentsConfiguration.groovy @@ -16,11 +16,9 @@ */ package org.apache.camel.k.loader.groovy.dsl -import groovy.util.logging.Slf4j import org.apache.camel.CamelContext import org.apache.camel.Component -@Slf4j class ComponentsConfiguration { private final CamelContext context diff --git a/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/IntegrationConfiguration.groovy b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/IntegrationConfiguration.groovy index ba9ca16..0e7432f 100644 --- a/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/IntegrationConfiguration.groovy +++ b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/IntegrationConfiguration.groovy @@ -16,7 +16,6 @@ */ package org.apache.camel.k.loader.groovy.dsl - import org.apache.camel.builder.BuilderSupport import org.apache.camel.builder.EndpointConsumerBuilder import org.apache.camel.builder.ErrorHandlerBuilder @@ -28,6 +27,7 @@ import org.apache.camel.model.InterceptSendToEndpointDefinition import org.apache.camel.model.OnCompletionDefinition import org.apache.camel.model.OnExceptionDefinition import org.apache.camel.model.RouteDefinition +import org.apache.camel.model.rest.RestConfigurationDefinition import org.apache.camel.model.rest.RestDefinition import org.apache.camel.spi.Registry @@ -66,7 +66,7 @@ class IntegrationConfiguration extends BuilderSupport implements Support, Endpoi return builder.rest() } - RestConfiguration restConfiguration() { + RestConfigurationDefinition restConfiguration() { builder.restConfiguration(); } diff --git a/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/Support.groovy b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/Support.groovy index 5a6cce7..80f0f09 100644 --- a/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/Support.groovy +++ b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/Support.groovy @@ -17,19 +17,21 @@ package org.apache.camel.k.loader.groovy.dsl import org.apache.camel.Exchange +import org.apache.camel.Predicate +import org.apache.camel.Processor trait Support { def processor(@DelegatesTo(Exchange) Closure<?> callable) { return { - callable.resolveStrategy = Closure.DELEGATE_FIRST + callable.resolveStrategy = DELEGATE_FIRST callable.call(it) - } as org.apache.camel.Processor + } as Processor } def predicate(@DelegatesTo(Exchange) Closure<?> callable) { return { - callable.resolveStrategy = Closure.DELEGATE_FIRST + callable.resolveStrategy = DELEGATE_FIRST return callable.call(it) - } as org.apache.camel.Predicate + } as Predicate } } \ No newline at end of file diff --git a/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/RegistryConfiguration.groovy b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/extension/CamelContextExtensions.groovy similarity index 59% rename from camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/RegistryConfiguration.groovy rename to camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/extension/CamelContextExtensions.groovy index 248c567..307b8d0 100644 --- a/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/dsl/RegistryConfiguration.groovy +++ b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/extension/CamelContextExtensions.groovy @@ -14,23 +14,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.k.loader.groovy.dsl +package org.apache.camel.k.loader.groovy.extension -import org.apache.camel.spi.Registry +import groovy.transform.CompileStatic +import org.apache.camel.CamelContext +import org.apache.camel.builder.RouteBuilder +import org.apache.camel.k.support.RouteBuilders - -class RegistryConfiguration { - private final Registry registry - - RegistryConfiguration(Registry registry) { - this.registry = registry - } - - def bind(String name, value) { - registry.bind(name, value) - } - - def propertyMissing(String name, value) { - registry.bind(name, value) +@CompileStatic +class CamelContextExtensions { + static void addRoutes(CamelContext self, @DelegatesTo(RouteBuilder.class) Closure<?> callable) { + self.addRoutes(RouteBuilders.endpoint { + callable.resolveStrategy = Closure.DELEGATE_ONLY + callable.delegate = it + callable.call() + }) } } diff --git a/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/extension/ExpressionClauseExtensions.groovy b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/extension/ExpressionClauseExtensions.groovy index e5034b3..e4a06fe 100644 --- a/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/extension/ExpressionClauseExtensions.groovy +++ b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/extension/ExpressionClauseExtensions.groovy @@ -16,13 +16,14 @@ */ package org.apache.camel.k.loader.groovy.extension +import groovy.transform.CompileStatic import org.apache.camel.Message import org.apache.camel.builder.ExpressionClause import java.util.function.Function +@CompileStatic class ExpressionClauseExtensions { - static <T> T body(ExpressionClause<T> self, Closure<?> callable) { return self.body(new Function<Object, Object>() { @Override diff --git a/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/extension/LogComponentExtension.groovy b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/extension/LogComponentExtension.groovy index 386b463..8b8427f 100644 --- a/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/extension/LogComponentExtension.groovy +++ b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/extension/LogComponentExtension.groovy @@ -16,10 +16,12 @@ */ package org.apache.camel.k.loader.groovy.extension +import groovy.transform.CompileStatic import org.apache.camel.Exchange import org.apache.camel.component.log.LogComponent import org.apache.camel.spi.ExchangeFormatter +@CompileStatic class LogComponentExtension { static void formatter(LogComponent self, Closure callable) { self.exchangeFormatter = new ExchangeFormatter() { diff --git a/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/extension/ProcessorDefinitionExtensions.groovy b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/extension/ProcessorDefinitionExtensions.groovy index 8a514cb..e3e7eb1 100644 --- a/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/extension/ProcessorDefinitionExtensions.groovy +++ b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/extension/ProcessorDefinitionExtensions.groovy @@ -16,6 +16,7 @@ */ package org.apache.camel.k.loader.groovy.extension +import groovy.transform.CompileStatic import org.apache.camel.Processor; import java.util.function.Function; @@ -23,8 +24,7 @@ import java.util.function.Function; import org.apache.camel.Exchange; import org.apache.camel.model.ProcessorDefinition; -// Workaround for https://issues.apache.org/jira/browse/CAMEL-14300 -// TODO: remove once fixed on camel side +@CompileStatic class ProcessorDefinitionExtensions { static <T extends ProcessorDefinition<T>> T setBody(ProcessorDefinition<T> self, Closure<?> callable) { return self.setBody(new Function<Exchange, Object>() { diff --git a/camel-k-loader-groovy/src/main/resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule b/camel-k-loader-groovy/src/main/resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule index d0e5874..57da804 100644 --- a/camel-k-loader-groovy/src/main/resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule +++ b/camel-k-loader-groovy/src/main/resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule @@ -3,4 +3,5 @@ moduleVersion=1.0.0 extensionClasses=\ org.apache.camel.k.loader.groovy.extension.LogComponentExtension,\ org.apache.camel.k.loader.groovy.extension.ExpressionClauseExtensions,\ - org.apache.camel.k.loader.groovy.extension.ProcessorDefinitionExtensions + org.apache.camel.k.loader.groovy.extension.ProcessorDefinitionExtensions,\ + org.apache.camel.k.loader.groovy.extension.CamelContextExtensions diff --git a/camel-k-loader-groovy/src/test/groovy/org/apache/camel/k/loader/groovy/LoaderTest.groovy b/camel-k-loader-groovy/src/test/groovy/org/apache/camel/k/loader/groovy/LoaderTest.groovy index c637fda..e11a5e2 100644 --- a/camel-k-loader-groovy/src/test/groovy/org/apache/camel/k/loader/groovy/LoaderTest.groovy +++ b/camel-k-loader-groovy/src/test/groovy/org/apache/camel/k/loader/groovy/LoaderTest.groovy @@ -16,14 +16,12 @@ */ package org.apache.camel.k.loader.groovy -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.Sources import org.apache.camel.k.listener.RoutesConfigurer +import org.apache.camel.k.loader.groovy.support.TestRuntime import org.apache.camel.model.FromDefinition +import org.apache.camel.model.ModelCamelContext import org.apache.camel.model.ToDefinition import spock.lang.Specification @@ -39,18 +37,12 @@ class LoaderTest extends Specification { then: loader instanceof GroovySourceLoader - runtime.builders.size() == 1 - runtime.builders[0] instanceof RouteBuilder - - with(runtime.builders[0], RouteBuilder) { - it.setContext(runtime.camelContext) - it.configure() - def routes = it.routeCollection.routes + with(runtime.getCamelContext(ModelCamelContext.class).routeDefinitions) { + it.size() == 1 - routes.size() == 1 - routes[0].outputs[0] instanceof ToDefinition - routes[0].input.endpointUri == 'timer:tick' + it[0].outputs[0] instanceof ToDefinition + it[0].input.endpointUri == 'timer:tick' } } @@ -64,45 +56,16 @@ class LoaderTest extends Specification { then: loader instanceof GroovySourceLoader - runtime.builders.size() == 1 - - with(runtime.builders[0], RouteBuilder) { - it.setContext(runtime.camelContext) - it.configure() - def routes = it.routeCollection.routes - routes.size() == 1 + with(runtime.getCamelContext(ModelCamelContext.class).routeDefinitions) { + it.size() == 1 - with(routes[0].input, FromDefinition) { + with(it[0].input, FromDefinition) { it.endpointUri == 'timer://tick?period=1s' } - with(routes[0].outputs[0], ToDefinition) { + with(it[0].outputs[0], ToDefinition) { it.endpointUri == 'log://info' } } } - - static class TestRuntime implements Runtime { - private final CamelContext camelContext - private final List<RoutesBuilder> builders - - TestRuntime() { - this.camelContext = new DefaultCamelContext() - this.builders = new ArrayList<>() - } - - @Override - CamelContext getCamelContext() { - return this.camelContext - } - - @Override - void addRoutes(RoutesBuilder builder) { - this.builders.add(builder) - } - - @Override - void setPropertiesLocations(Collection<String> locations) { - } - } } diff --git a/camel-k-loader-groovy/src/test/groovy/org/apache/camel/k/loader/groovy/dsl/IntegrationTest.groovy b/camel-k-loader-groovy/src/test/groovy/org/apache/camel/k/loader/groovy/dsl/IntegrationTest.groovy index 11f29ac..5e5119a 100644 --- a/camel-k-loader-groovy/src/test/groovy/org/apache/camel/k/loader/groovy/dsl/IntegrationTest.groovy +++ b/camel-k-loader-groovy/src/test/groovy/org/apache/camel/k/loader/groovy/dsl/IntegrationTest.groovy @@ -18,13 +18,12 @@ package org.apache.camel.k.loader.groovy.dsl import org.apache.camel.Predicate import org.apache.camel.Processor +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.impl.DefaultCamelContext -import org.apache.camel.k.Runtime +import org.apache.camel.k.loader.groovy.support.TestRuntime import org.apache.camel.language.bean.BeanLanguage -import org.apache.camel.model.ModelCamelContext import org.apache.camel.model.rest.GetVerbDefinition import org.apache.camel.model.rest.PostVerbDefinition import org.apache.camel.processor.FatalFallbackErrorHandler @@ -32,40 +31,25 @@ import org.apache.camel.processor.SendProcessor import org.apache.camel.processor.channel.DefaultChannel import org.apache.camel.spi.HeaderFilterStrategy import org.apache.camel.support.DefaultHeaderFilterStrategy +import spock.lang.AutoCleanup import spock.lang.Specification import javax.sql.DataSource -import static org.apache.camel.k.listener.RoutesConfigurer.forRoutes - class IntegrationTest extends Specification { - - private ModelCamelContext context - private Runtime runtime - - def setup() { - this.context = new DefaultCamelContext() - this.runtime = Runtime.on(context) - } - - - def cleanup() { - if (this.context != null) { - this.context.stop() - } - } + @AutoCleanup + def runtime = new TestRuntime() def "load integration with rest"() { when: - forRoutes('classpath:routes-with-rest.groovy').accept(Runtime.Phase.ConfigureRoutes, runtime) + runtime.loadRoutes('classpath:routes-with-rest.groovy') then: - context.restConfiguration.host == 'my-host' - context.restConfiguration.port == 9192 - - context.restDefinitions.size() == 2 + runtime.context.restConfiguration.host == 'my-host' + runtime.context.restConfiguration.port == 9192 + runtime.context.restDefinitions.size() == 2 - with(context.restDefinitions.find {it.path == '/my/path'}) { + with(runtime.context.restDefinitions.find {it.path == '/my/path'}) { verbs.size() == 1 with(verbs.first(), GetVerbDefinition) { @@ -79,7 +63,7 @@ class IntegrationTest extends Specification { } } - with(context.restDefinitions.find {it.path == '/post'}) { + with(runtime.context.restDefinitions.find {it.path == '/post'}) { verbs.size() == 1 with(verbs.first(), PostVerbDefinition) { @@ -96,46 +80,47 @@ class IntegrationTest extends Specification { def "load integration with beans"() { when: - forRoutes('classpath:routes-with-beans.groovy').accept(Runtime.Phase.ConfigureRoutes, runtime) + runtime.loadRoutes('classpath:routes-with-beans.groovy') then: - context.registry.findByType(DataSource).size() == 1 - context.registry.lookupByName('dataSource') instanceof DataSource - context.registry.findByType(HeaderFilterStrategy).size() == 1 - context.registry.lookupByName('filterStrategy') instanceof DefaultHeaderFilterStrategy - - context.registry.lookupByName('myProcessor') instanceof Processor - context.registry.lookupByName('myPredicate') instanceof Predicate + with(runtime.context.registry) { + it.findByType(DataSource).size() == 1 + it.lookupByName('dataSource') instanceof DataSource + it.findByType(HeaderFilterStrategy).size() == 1 + it.lookupByName('filterStrategy') instanceof DefaultHeaderFilterStrategy + it.lookupByName('myProcessor') instanceof Processor + it.lookupByName('myPredicate') instanceof Predicate + } } def "load integration with components configuration"() { when: - forRoutes('classpath:routes-with-components-configuration.groovy').accept(Runtime.Phase.ConfigureRoutes, runtime) + runtime.loadRoutes('classpath:routes-with-components-configuration.groovy') then: - with(context.getComponent('seda', SedaComponent)) { + with(runtime.context.getComponent('seda', SedaComponent)) { queueSize == 1234 concurrentConsumers == 12 } - with(context.getComponent('mySeda', SedaComponent)) { + with(runtime.context.getComponent('mySeda', SedaComponent)) { queueSize == 4321 concurrentConsumers == 21 } - with(context.getComponent('log', LogComponent)) { + with(runtime.context.getComponent('log', LogComponent)) { exchangeFormatter != null } } def "load integration with languages configuration"() { when: - forRoutes('classpath:routes-with-languages-configuration.groovy').accept(Runtime.Phase.ConfigureRoutes, runtime) + runtime.loadRoutes('classpath:routes-with-languages-configuration.groovy') then: - with(context.resolveLanguage('bean'), BeanLanguage) { + with(runtime.context.resolveLanguage('bean'), BeanLanguage) { beanType == String.class method == "toUpperCase" } - with(context.resolveLanguage('myBean'), BeanLanguage) { + with(runtime.context.resolveLanguage('myBean'), BeanLanguage) { beanType == String.class method == "toLowerCase" } @@ -143,48 +128,47 @@ class IntegrationTest extends Specification { def "load integration with dataformats configuration"() { when: - forRoutes('classpath:routes-with-dataformats-configuration.groovy').accept(Runtime.Phase.ConfigureRoutes, runtime) + runtime.loadRoutes('classpath:routes-with-dataformats-configuration.groovy') then: - with(context.resolveDataFormat('json-jackson'), JacksonDataFormat) { + with(runtime.context.resolveDataFormat('json-jackson'), JacksonDataFormat) { unmarshalType == Map.class - prettyPrint == true + prettyPrint } - with(context.resolveDataFormat('my-jackson'), JacksonDataFormat) { + with(runtime.context.resolveDataFormat('my-jackson'), JacksonDataFormat) { unmarshalType == String.class - prettyPrint == false + (!prettyPrint) } } def "load integration with component error property configuration"() { when: - forRoutes('classpath:routes-with-component-wrong-property-configuration.groovy').accept(Runtime.Phase.ConfigureRoutes, runtime) + runtime.loadRoutes('classpath:routes-with-component-wrong-property-configuration.groovy') then: - def e = thrown org.apache.camel.RuntimeCamelException + def e = thrown RuntimeCamelException assert e.message.contains("No such property: queueNumber for class: org.apache.camel.component.seda.SedaComponent") } def "load integration with component error method configuration"() { when: - forRoutes('classpath:routes-with-component-wrong-method-configuration.groovy').accept(Runtime.Phase.ConfigureRoutes, runtime) + runtime.loadRoutes('classpath:routes-with-component-wrong-method-configuration.groovy') then: - def e = thrown org.apache.camel.RuntimeCamelException + def e = thrown RuntimeCamelException assert e.message.contains("No signature of method: org.apache.camel.component.seda.SedaComponent.queueNumber()") } def "load integration with error handler"() { when: - forRoutes('classpath:routes-with-error-handler.groovy').accept(Runtime.Phase.ConfigureRoutes, runtime) - - context.start() + runtime.loadRoutes('classpath:routes-with-error-handler.groovy') + runtime.start() then: - context.routes?.size() == 1 - context.routes[0].getOnException('my-on-exception') != null - context.routes[0].getOnException('my-on-exception') instanceof FatalFallbackErrorHandler + runtime.context.routes?.size() == 1 + runtime.context.routes[0].getOnException('my-on-exception') != null + runtime.context.routes[0].getOnException('my-on-exception') instanceof FatalFallbackErrorHandler - def eh = context.routes[0].getOnException('my-on-exception') as FatalFallbackErrorHandler + def eh = runtime.context.routes[0].getOnException('my-on-exception') as FatalFallbackErrorHandler def ch = eh.processor as DefaultChannel ch.output instanceof SendProcessor @@ -193,9 +177,8 @@ class IntegrationTest extends Specification { // Test groovy eip extension, relates to https://issues.apache.org/jira/browse/CAMEL-14300 def "load integration with eip"() { when: - forRoutes('classpath:routes-with-eip.groovy').accept(Runtime.Phase.ConfigureRoutes, runtime) - - context.start() + runtime.loadRoutes('classpath:routes-with-eip.groovy') + runtime.start() then: 1 == 1 } diff --git a/camel-k-loader-groovy/src/test/groovy/org/apache/camel/k/loader/groovy/dsl/extension/ExpressionClauseExtensionTest.groovy b/camel-k-loader-groovy/src/test/groovy/org/apache/camel/k/loader/groovy/dsl/extension/ExpressionClauseExtensionTest.groovy index 34f353d..255ae90 100644 --- a/camel-k-loader-groovy/src/test/groovy/org/apache/camel/k/loader/groovy/dsl/extension/ExpressionClauseExtensionTest.groovy +++ b/camel-k-loader-groovy/src/test/groovy/org/apache/camel/k/loader/groovy/dsl/extension/ExpressionClauseExtensionTest.groovy @@ -16,7 +16,7 @@ */ package org.apache.camel.k.loader.groovy.dsl.extension -import org.apache.camel.builder.RouteBuilder + import org.apache.camel.impl.DefaultCamelContext import spock.lang.Specification @@ -25,13 +25,10 @@ class ExpressionClauseExtensionTest extends Specification { def "invoke extension method - untyped body expression"() { given: def ctx = new DefaultCamelContext() - ctx.addRoutes(new RouteBuilder() { - @Override - void configure() throws Exception { - from('direct:start') - .transform().body { it.toString() } - } - }) + ctx.addRoutes { + from('direct:start') + .transform().body { it.toString() } + } ctx.start() when: @@ -47,13 +44,10 @@ class ExpressionClauseExtensionTest extends Specification { def "invoke extension method - typed body expression"() { given: def ctx = new DefaultCamelContext() - ctx.addRoutes(new RouteBuilder() { - @Override - void configure() throws Exception { - from('direct:start') - .transform().body(String.class, { it.toUpperCase() }) - } - }) + ctx.addRoutes { + from('direct:start') + .transform().body(String.class, { it.toUpperCase() }) + } ctx.start() when: @@ -70,13 +64,10 @@ class ExpressionClauseExtensionTest extends Specification { def "invoke extension method - message expression"() { given: def ctx = new DefaultCamelContext() - ctx.addRoutes(new RouteBuilder() { - @Override - void configure() throws Exception { - from('direct:start') - .transform().message { it.body.toUpperCase() } - } - }) + ctx.addRoutes { + from('direct:start') + .transform().message { it.body.toUpperCase() } + } ctx.start() when: @@ -93,22 +84,19 @@ class ExpressionClauseExtensionTest extends Specification { def "invoke extension method - cbr"() { given: def ctx = new DefaultCamelContext() - ctx.addRoutes(new RouteBuilder() { - @Override - void configure() throws Exception { - from('direct:start') - .choice() - .when().body(String.class, { it == '1'}) - .setBody().constant('case-1') - .endChoice() - .when().body(String.class, { it == '2'}) - .setBody().constant('case-2') - .endChoice() - .otherwise() - .setBody().constant('default') - .end() - } - }) + ctx.addRoutes { + from('direct:start') + .choice() + .when().body(String.class, { it == '1'}) + .setBody().constant('case-1') + .endChoice() + .when().body(String.class, { it == '2'}) + .setBody().constant('case-2') + .endChoice() + .otherwise() + .setBody().constant('default') + .end() + } ctx.start() when: diff --git a/camel-k-loader-groovy/src/test/groovy/org/apache/camel/k/loader/groovy/support/TestRuntime.groovy b/camel-k-loader-groovy/src/test/groovy/org/apache/camel/k/loader/groovy/support/TestRuntime.groovy new file mode 100644 index 0000000..33ec1e9 --- /dev/null +++ b/camel-k-loader-groovy/src/test/groovy/org/apache/camel/k/loader/groovy/support/TestRuntime.groovy @@ -0,0 +1,66 @@ +/* + * 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.groovy.support + +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.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 + + TestRuntime() { + this.context = new DefaultCamelContext() + this.builders = new ArrayList<>() + } + + @Override + CamelContext getCamelContext() { + return this.context + } + + @Override + void addRoutes(RoutesBuilder builder) { + this.builders << builder + this.context.addRoutes(builder) + } + + 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-runtime-core/src/main/java/org/apache/camel/k/support/RouteBuilders.java b/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/RouteBuilders.java index 94d1c89..63b9e70 100644 --- a/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/RouteBuilders.java +++ b/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/RouteBuilders.java @@ -18,6 +18,7 @@ package org.apache.camel.k.support; import java.io.Reader; import java.util.function.BiConsumer; +import java.util.function.Consumer; import org.apache.camel.builder.endpoint.EndpointRouteBuilder; import org.apache.camel.k.Source; @@ -35,5 +36,14 @@ public final class RouteBuilders { } } }; - }; + } + + public static EndpointRouteBuilder endpoint(Consumer<EndpointRouteBuilder> consumer) { + return new EndpointRouteBuilder() { + @Override + public void configure() throws Exception { + consumer.accept(this); + } + }; + } }