You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2024/08/19 16:55:00 UTC

(camel) branch main updated: CAMEL-20966: camel-kamelet - Added unit test

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new ffbfb90b69b CAMEL-20966: camel-kamelet - Added unit test
ffbfb90b69b is described below

commit ffbfb90b69b386e3de1193ba2c36cb39f26d4349
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Aug 19 18:54:49 2024 +0200

    CAMEL-20966: camel-kamelet - Added unit test
---
 components/camel-kamelet/pom.xml                   |  28 +++++-
 .../camel/component/kamelet/KameletEnvTest.java    | 111 +++++++++++++++++++++
 .../src/test/resources/double-kamelet.xml          |  27 +++++
 3 files changed, 165 insertions(+), 1 deletion(-)

diff --git a/components/camel-kamelet/pom.xml b/components/camel-kamelet/pom.xml
index b680a6085ca..62dffd11340 100644
--- a/components/camel-kamelet/pom.xml
+++ b/components/camel-kamelet/pom.xml
@@ -17,7 +17,8 @@
     limitations under the License.
 
 -->
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
 
     <parent>
@@ -108,4 +109,29 @@
 
     </dependencies>
 
+    <profiles>
+        <profile>
+            <id>full</id>
+            <activation>
+                <property>
+                    <name>!quickly</name>
+                </property>
+            </activation>
+            <build>
+                <plugins>
+                    <plugin>
+                        <artifactId>maven-surefire-plugin</artifactId>
+                        <configuration>
+                            <!-- needed for testing -->
+                            <environmentVariables>
+                                <NUMBER>4</NUMBER>
+                            </environmentVariables>
+                        </configuration>
+                    </plugin>
+                </plugins>
+            </build>
+        </profile>
+    </profiles>
+
+
 </project>
diff --git a/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletEnvTest.java b/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletEnvTest.java
new file mode 100644
index 00000000000..cb195c991e7
--- /dev/null
+++ b/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletEnvTest.java
@@ -0,0 +1,111 @@
+/*
+ * 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.component.kamelet;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spi.Resource;
+import org.apache.camel.spi.RoutesBuilderLoader;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class KameletEnvTest extends CamelTestSupport {
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        context.getRegistry().bind("routes-builder-loader-xml", new MyRoutesLoader());
+        return context;
+    }
+
+    @Test
+    public void testCalc() throws Exception {
+        Assertions.assertEquals("4", System.getenv().get("NUMBER"));
+
+        // OS ENV is 4, but the kamelet should use the parameter as-is which is 3
+        // and as such the expected result would be 2 x 3 = 6
+        getMockEndpoint("mock:result").expectedBodiesReceived("6");
+        template.sendBody("direct:start", "3");
+        MockEndpoint.assertIsSatisfied(context);
+    }
+
+    // we cannot use camel-xml-io-dsl to load the XML route so we fool Camel
+    // and use this class that has the route template hardcoded from java
+    public class MyRoutesLoader implements RoutesBuilderLoader {
+
+        private CamelContext camelContext;
+
+        @Override
+        public CamelContext getCamelContext() {
+            return camelContext;
+        }
+
+        @Override
+        public void setCamelContext(CamelContext camelContext) {
+            this.camelContext = camelContext;
+        }
+
+        @Override
+        public String getSupportedExtension() {
+            return "xml";
+        }
+
+        @Override
+        public RoutesBuilder loadRoutesBuilder(Resource resource) {
+            return new RouteBuilder() {
+                @Override
+                public void configure() {
+                    routeTemplate("double")
+                            .templateParameter("number")
+                            .from("kamelet:source")
+                            .transform().groovy("2 * {{number}}");
+                }
+            };
+        }
+
+        @Override
+        public void start() {
+            // noop
+        }
+
+        @Override
+        public void stop() {
+            // noop
+        }
+    }
+
+    // **********************************************
+    //
+    // test set-up
+    //
+    // **********************************************
+
+    @Override
+    protected RoutesBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:start")
+                        .kamelet("double?location=file:src/test/resources/double-kamelet.xml&number=3")
+                        .to("mock:result");
+            }
+        };
+    }
+}
diff --git a/components/camel-kamelet/src/test/resources/double-kamelet.xml b/components/camel-kamelet/src/test/resources/double-kamelet.xml
new file mode 100644
index 00000000000..14dc8ef9bbd
--- /dev/null
+++ b/components/camel-kamelet/src/test/resources/double-kamelet.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<routeTemplate id="double">
+    <route>
+        <from uri="kamelet:source"/>
+        <transform>
+            <groovy>2 * {{number}}</groovy>
+        </transform>
+    </route>
+</routeTemplate>
\ No newline at end of file