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 75ca3c0  js: cleanup loader and tests
75ca3c0 is described below

commit 75ca3c0d44facd8237cab0e0e4b102e0c3a65a99
Author: lburgazzoli <lburgazz...@gmail.com>
AuthorDate: Wed Aug 19 14:49:54 2020 +0200

    js: cleanup loader and tests
---
 camel-k-loader-js/pom.xml                          |  18 +++
 .../k/loader/js/JavaScriptSourceLoaderTest.groovy  | 105 ++++++++++++++++
 .../camel/k/loader/js/support/TestRuntime.groovy   |  78 ++++++++++++
 .../apache/camel/k/loader/js/RoutesLoaderTest.java |  95 --------------
 .../camel/k/loader/js/dsl/IntegrationTest.java     | 139 ---------------------
 5 files changed, 201 insertions(+), 234 deletions(-)

diff --git a/camel-k-loader-js/pom.xml b/camel-k-loader-js/pom.xml
index 540ed2d..ff1b0c3 100644
--- a/camel-k-loader-js/pom.xml
+++ b/camel-k-loader-js/pom.xml
@@ -110,6 +110,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-js/src/test/groovy/org/apache/camel/k/loader/js/JavaScriptSourceLoaderTest.groovy
 
b/camel-k-loader-js/src/test/groovy/org/apache/camel/k/loader/js/JavaScriptSourceLoaderTest.groovy
new file mode 100644
index 0000000..1d71575
--- /dev/null
+++ 
b/camel-k-loader-js/src/test/groovy/org/apache/camel/k/loader/js/JavaScriptSourceLoaderTest.groovy
@@ -0,0 +1,105 @@
+/*
+ * 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.js
+
+import org.apache.camel.component.seda.SedaComponent
+import org.apache.camel.k.loader.js.support.TestRuntime
+import org.apache.camel.model.FromDefinition
+import org.apache.camel.model.ToDefinition
+import org.apache.camel.model.TransformDefinition
+import org.apache.camel.model.rest.GetVerbDefinition
+import spock.lang.AutoCleanup
+import spock.lang.Specification
+
+class JavaScriptSourceLoaderTest extends Specification {
+    @AutoCleanup
+    def runtime = new TestRuntime()
+
+    def 'load'(location) {
+        expect:
+            runtime.loadRoutes(location)
+
+            with(runtime.context.routeDefinitions) {
+                it[0].input.endpointUri ==~ /timer:.*tick/
+                it[0].outputs[0] instanceof ToDefinition
+            }
+        where:
+            location << [
+                    'classpath:routes.js',
+                    'classpath:routes-with-endpoint-dsl.js',
+                    
'classpath:routes-compressed.js.gz.b64?language=js&compression=true',
+                    'classpath:routes.mytype?language=js'
+            ]
+
+    }
+
+    def 'load routes with component configuration'() {
+        when:
+            
runtime.loadRoutes('classpath:routes-with-component-configuration.js')
+        then:
+            with(runtime.context.getComponent('seda', SedaComponent.class)) {
+                queueSize == 1234
+            }
+    }
+
+    def 'load routes with rest configuration'() {
+        when:
+            runtime.loadRoutes('classpath:routes-with-rest-configuration.js')
+        then:
+            with(runtime.context.restConfiguration) {
+                component == 'undertow'
+                port == 1234
+            }
+    }
+
+    def 'load routes with rest dsl'() {
+        when:
+            runtime.loadRoutes('classpath:routes-with-rest-dsl.js')
+        then:
+            runtime.context.restDefinitions.size() == 1
+            runtime.context.routeDefinitions.size() == 1
+
+            with(runtime.context.restDefinitions[0]) {
+                produces == 'text/plain'
+                with(verbs[0], GetVerbDefinition) {
+                    uri == '/say/hello'
+                }
+            }
+            with(runtime.context.routeDefinitions[0]) {
+                input instanceof FromDefinition
+                outputs[0] instanceof TransformDefinition
+            }
+    }
+
+    def 'load routes with processors'() {
+        when:
+            runtime.loadRoutes('classpath:routes-with-processors.js')
+            runtime.start()
+        then:
+            'arrow' == 
runtime.template.to('direct:arrow').request(String.class)
+            'wrapper' == 
runtime.template.to('direct:wrapper').request(String.class)
+            'function' == 
runtime.template.to('direct:function').request(String.class)
+    }
+
+    def 'load routes with context configuration'() {
+        when:
+            
runtime.loadRoutes('classpath:routes-with-context-configuration.js')
+        then:
+            runtime.context.isTypeConverterStatisticsEnabled()
+
+    }
+}
diff --git 
a/camel-k-loader-js/src/test/groovy/org/apache/camel/k/loader/js/support/TestRuntime.groovy
 
b/camel-k-loader-js/src/test/groovy/org/apache/camel/k/loader/js/support/TestRuntime.groovy
new file mode 100644
index 0000000..8040d45
--- /dev/null
+++ 
b/camel-k-loader-js/src/test/groovy/org/apache/camel/k/loader/js/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.js.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-js/src/test/java/org/apache/camel/k/loader/js/RoutesLoaderTest.java
 
b/camel-k-loader-js/src/test/java/org/apache/camel/k/loader/js/RoutesLoaderTest.java
deleted file mode 100644
index 846daad..0000000
--- 
a/camel-k-loader-js/src/test/java/org/apache/camel/k/loader/js/RoutesLoaderTest.java
+++ /dev/null
@@ -1,95 +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.js;
-
-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.listener.RoutesConfigurer;
-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 = RoutesConfigurer.load(runtime, 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()).matches("timer:/*tick");
-        
assertThat(routes.get(0).getOutputs().get(0)).isInstanceOf(ToDefinition.class);
-    }
-
-    static Stream<Arguments> parameters() {
-        return Stream.of(
-            Arguments.arguments("classpath:routes.js", 
JavaScriptSourceLoader.class),
-            Arguments.arguments("classpath:routes-with-endpoint-dsl.js", 
JavaScriptSourceLoader.class),
-            
Arguments.arguments("classpath:routes-compressed.js.gz.b64?language=js&compression=true",
 JavaScriptSourceLoader.class),
-            Arguments.arguments("classpath:routes.mytype?language=js", 
JavaScriptSourceLoader.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) {
-        }
-    }
-}
diff --git 
a/camel-k-loader-js/src/test/java/org/apache/camel/k/loader/js/dsl/IntegrationTest.java
 
b/camel-k-loader-js/src/test/java/org/apache/camel/k/loader/js/dsl/IntegrationTest.java
deleted file mode 100644
index 326ddc5..0000000
--- 
a/camel-k-loader-js/src/test/java/org/apache/camel/k/loader/js/dsl/IntegrationTest.java
+++ /dev/null
@@ -1,139 +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.js.dsl;
-
-import java.util.List;
-
-import org.apache.camel.CamelContext;
-import org.apache.camel.ProducerTemplate;
-import org.apache.camel.component.seda.SedaComponent;
-import org.apache.camel.impl.DefaultCamelContext;
-import org.apache.camel.k.Runtime;
-import org.apache.camel.k.listener.RoutesConfigurer;
-import org.apache.camel.model.FromDefinition;
-import org.apache.camel.model.ModelCamelContext;
-import org.apache.camel.model.RouteDefinition;
-import org.apache.camel.model.TransformDefinition;
-import org.apache.camel.model.rest.GetVerbDefinition;
-import org.apache.camel.model.rest.RestDefinition;
-import org.apache.camel.spi.RestConfiguration;
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class IntegrationTest {
-    private CamelContext context;
-    private Runtime runtime;
-
-    @BeforeEach
-    public void setUp() {
-        this.context = new DefaultCamelContext();
-        this.runtime = Runtime.on(context);
-    }
-
-    @AfterEach
-    public void shutDown() {
-        if (this.context != null) {
-            this.context.stop();
-        }
-    }
-
-    private void configureRoutes(String... routes) {
-        
RoutesConfigurer.forRoutes(routes).accept(Runtime.Phase.ConfigureRoutes, 
runtime);
-    }
-
-    @Test
-    public void testComponentConfiguration() {
-        configureRoutes(
-            "classpath:routes-with-component-configuration.js"
-        );
-
-        SedaComponent seda = context.getComponent("seda", SedaComponent.class);
-
-        assertThat(seda).isNotNull();
-        assertThat(seda).hasFieldOrPropertyWithValue("queueSize", 1234);
-    }
-
-    @Test
-    public void testRestConfiguration() {
-        configureRoutes(
-            "classpath:routes-with-rest-configuration.js"
-        );
-
-        RestConfiguration conf = context.getRestConfiguration();
-
-        assertThat(conf).isNotNull();
-        assertThat(conf).hasFieldOrPropertyWithValue("component", "undertow");
-        assertThat(conf).hasFieldOrPropertyWithValue("port", 1234);
-    }
-
-    @Test
-    public void testRestDSL() {
-        configureRoutes(
-            "classpath:routes-with-rest-dsl.js"
-        );
-
-        ModelCamelContext mcc = context.adapt(ModelCamelContext.class);
-        List<RestDefinition> rests = mcc.getRestDefinitions();
-        List<RouteDefinition> routes = mcc.getRouteDefinitions();
-
-        assertThat(rests).hasSize(1);
-        assertThat(rests).first().hasFieldOrPropertyWithValue("produces", 
"text/plain");
-        assertThat(rests).first().satisfies(definition -> {
-            assertThat(definition.getVerbs()).hasSize(1);
-            
assertThat(definition.getVerbs()).first().isInstanceOfSatisfying(GetVerbDefinition.class,
 get -> {
-                assertThat(get).hasFieldOrPropertyWithValue("uri", 
"/say/hello");
-            });
-        });
-
-        assertThat(routes).hasSize(1);
-        assertThat(routes).first().satisfies(definition -> {
-            
assertThat(definition.getInput()).isInstanceOf(FromDefinition.class);
-            assertThat(definition.getOutputs()).hasSize(1);
-            assertThat(definition.getOutputs()).first().satisfies(output -> {
-                assertThat(output).isInstanceOf(TransformDefinition.class);
-            });
-        });
-    }
-
-    @Test
-    public void testProcessors() {
-        configureRoutes(
-            "classpath:routes-with-processors.js"
-        );
-
-        context.start();
-
-        ProducerTemplate template = context.createProducerTemplate();
-
-        assertThat(template.requestBody("direct:arrow", 
"")).isEqualTo("arrow");
-        assertThat(template.requestBody("direct:wrapper", 
"")).isEqualTo("wrapper");
-        assertThat(template.requestBody("direct:function", 
"")).isEqualTo("function");
-    }
-
-
-    @Test
-    public void testContextConfiguration() {
-        configureRoutes(
-            "classpath:routes-with-context-configuration.js"
-        );
-
-        assertThat(context.isTypeConverterStatisticsEnabled()).isTrue();
-    }
-}

Reply via email to