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 2cb49aa groovy: add extension methods for expression clause
2cb49aa is described below
commit 2cb49aa99a835662853a2b496c35802a0399728d
Author: lburgazzoli <[email protected]>
AuthorDate: Mon Sep 2 19:43:14 2019 +0200
groovy: add extension methods for expression clause
---
.../extension/ExpressionClauseExtensions.groovy | 55 +++++++++
.../org.codehaus.groovy.runtime.ExtensionModule | 4 +-
.../extension/ExpressionClauseExtensionTest.groovy | 130 +++++++++++++++++++++
3 files changed, 187 insertions(+), 2 deletions(-)
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
new file mode 100644
index 0000000..e5034b3
--- /dev/null
+++
b/camel-k-loader-groovy/src/main/groovy/org/apache/camel/k/loader/groovy/extension/ExpressionClauseExtensions.groovy
@@ -0,0 +1,55 @@
+/*
+ * 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.extension
+
+import org.apache.camel.Message
+import org.apache.camel.builder.ExpressionClause
+
+import java.util.function.Function
+
+class ExpressionClauseExtensions {
+
+ static <T> T body(ExpressionClause<T> self, Closure<?> callable) {
+ return self.body(new Function<Object, Object>() {
+ @Override
+ Object apply(Object body) {
+ callable.resolveStrategy = Closure.DELEGATE_ONLY
+ return callable.call(body)
+ }
+ })
+ }
+
+ static <T, B> T body(ExpressionClause<T> self, Class<B> type, Closure<?>
callable) {
+ return self.body(type, new Function<B, Object>() {
+ @Override
+ Object apply(B body) {
+ callable.resolveStrategy = Closure.DELEGATE_ONLY
+ return callable.call(body)
+ }
+ })
+ }
+
+ static <T> T message(ExpressionClause<T> self, @DelegatesTo(Message)
Closure<?> callable) {
+ return self.message(new Function<Message, Object>() {
+ @Override
+ Object apply(Message body) {
+ callable.resolveStrategy = Closure.DELEGATE_ONLY
+ return callable.call(body)
+ }
+ })
+ }
+}
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 09569be..bc78fb3 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
@@ -1,4 +1,4 @@
moduleName=camel-k-loader-groovy
-moduleVersion=0.0.3-SNAPSHOT
-extensionClasses=org.apache.camel.k.loader.groovy.extension.LogComponentExtension
+moduleVersion=1.0.0
+extensionClasses=org.apache.camel.k.loader.groovy.extension.LogComponentExtension,org.apache.camel.k.loader.groovy.extension.ExpressionClauseExtensions
#staticExtensionClasses=support.StaticStringExtension
\ No newline at end of file
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
new file mode 100644
index 0000000..34f353d
--- /dev/null
+++
b/camel-k-loader-groovy/src/test/groovy/org/apache/camel/k/loader/groovy/dsl/extension/ExpressionClauseExtensionTest.groovy
@@ -0,0 +1,130 @@
+/*
+ * 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.dsl.extension
+
+import org.apache.camel.builder.RouteBuilder
+import org.apache.camel.impl.DefaultCamelContext
+import spock.lang.Specification
+
+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.start()
+ when:
+ def result =
ctx.createProducerTemplate().requestBody('direct:start', 1)
+
+ then:
+ result instanceof String
+
+ cleanup:
+ ctx.stop()
+ }
+
+ 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.start()
+ when:
+ def result =
ctx.createProducerTemplate().requestBody('direct:start', 'a')
+
+ then:
+ result instanceof String
+ result == 'A'
+
+ cleanup:
+ ctx.stop()
+ }
+
+ 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.start()
+ when:
+ def result =
ctx.createProducerTemplate().requestBody('direct:start', 'a')
+
+ then:
+ result instanceof String
+ result == 'A'
+
+ cleanup:
+ ctx.stop()
+ }
+
+ 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.start()
+ when:
+ def r1 = ctx.createProducerTemplate().requestBody('direct:start',
'1')
+ def r2 = ctx.createProducerTemplate().requestBody('direct:start',
'2')
+ def r3 = ctx.createProducerTemplate().requestBody('direct:start',
'3')
+
+ then:
+ r1 instanceof String
+ r1 == 'case-1'
+ r2 instanceof String
+ r2 == 'case-2'
+ r3 instanceof String
+ r3 == 'default'
+
+ cleanup:
+ ctx.stop()
+ }
+}