You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2024/06/19 12:24:28 UTC

(camel-karaf) branch main updated: Ref #364: Add camel-ftp integration test (#365)

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

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


The following commit(s) were added to refs/heads/main by this push:
     new 3d0513b6 Ref #364: Add camel-ftp integration test (#365)
3d0513b6 is described below

commit 3d0513b6a0f783c06a3a03c87906da836fbf6990
Author: François de Parscau <11...@users.noreply.github.com>
AuthorDate: Wed Jun 19 14:24:24 2024 +0200

    Ref #364: Add camel-ftp integration test (#365)
---
 .../itests/ExternalResourceWithPrerequisite.java   |  60 ++++++++++++
 .../camel/itests/GenericContainerResource.java     |   1 +
 tests/features/camel-ftp/pom.xml                   |  42 ++++++++
 .../karaf/camel/test/CamelFtpRouteSupplier.java    |  51 ++++++++++
 .../apache/karaf/camel/itest/CamelFtpITest.java    |  63 ++++++++++++
 .../karaf/camel/itest/FtpServerResource.java       | 107 +++++++++++++++++++++
 tests/features/pom.xml                             |   1 +
 7 files changed, 325 insertions(+)

diff --git a/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/ExternalResourceWithPrerequisite.java b/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/ExternalResourceWithPrerequisite.java
new file mode 100644
index 00000000..d930f959
--- /dev/null
+++ b/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/ExternalResourceWithPrerequisite.java
@@ -0,0 +1,60 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.karaf.camel.itests;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public abstract class ExternalResourceWithPrerequisite implements ExternalResource {
+
+    private final Map<String, String> properties = new HashMap<>();
+
+    @Override
+    public void before() {
+        for (ExternalResource prerequisite : getPrerequisites()) {
+            prerequisite.before();
+            prerequisite.properties().forEach(this::setProperty);
+        }
+        doStart();
+    }
+
+    @Override
+    public void after() {
+        doStop();
+        for (ExternalResource prerequisite : getPrerequisites()) {
+            prerequisite.after();
+        }
+    }
+
+    public void setProperty(String key, String value) {
+        properties.put(key, value);
+    }
+
+    @Override
+    public Map<String, String> properties() {
+        return properties;
+    }
+
+    protected abstract List<ExternalResource> getPrerequisites();
+
+    protected abstract void doStart();
+
+    protected abstract void doStop();
+
+}
diff --git a/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/GenericContainerResource.java b/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/GenericContainerResource.java
index 5adbbba9..39c4ad90 100644
--- a/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/GenericContainerResource.java
+++ b/tests/camel-integration-test/src/main/java/org/apache/karaf/camel/itests/GenericContainerResource.java
@@ -60,6 +60,7 @@ public class GenericContainerResource<T extends GenericContainer<T>> implements
         container.start();
         onStarted.accept(this);
         for (ExternalResource dependency : dependencies) {
+            dependency.before();
             dependency.properties().forEach(this::setProperty);
         }
         LOG.info("Container {} started", container.getDockerImageName());
diff --git a/tests/features/camel-ftp/pom.xml b/tests/features/camel-ftp/pom.xml
new file mode 100644
index 00000000..a236b88e
--- /dev/null
+++ b/tests/features/camel-ftp/pom.xml
@@ -0,0 +1,42 @@
+<?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.
+
+-->
+<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>
+        <groupId>org.apache.camel.karaf</groupId>
+        <artifactId>camel-karaf-features-test</artifactId>
+        <version>4.6.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>camel-ftp-test</artifactId>
+    <name>Apache Camel :: Karaf :: Tests :: Features :: FTP</name>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.ftpserver</groupId>
+            <artifactId>ftpserver-core</artifactId>
+            <version>${ftpserver-version}</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+</project>
\ No newline at end of file
diff --git a/tests/features/camel-ftp/src/main/java/org/apache/karaf/camel/test/CamelFtpRouteSupplier.java b/tests/features/camel-ftp/src/main/java/org/apache/karaf/camel/test/CamelFtpRouteSupplier.java
new file mode 100644
index 00000000..286c235a
--- /dev/null
+++ b/tests/features/camel-ftp/src/main/java/org/apache/karaf/camel/test/CamelFtpRouteSupplier.java
@@ -0,0 +1,51 @@
+/*
+ * 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.karaf.camel.test;
+
+import java.util.function.Function;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.RouteDefinition;
+import org.apache.karaf.camel.itests.AbstractCamelSingleFeatureResultMockBasedRouteSupplier;
+import org.apache.karaf.camel.itests.CamelRouteSupplier;
+import org.osgi.service.component.annotations.Component;
+
+
+@Component(
+        name = "karaf-camel-ftp-test",
+        immediate = true,
+        service = CamelRouteSupplier.class
+)
+public class CamelFtpRouteSupplier extends AbstractCamelSingleFeatureResultMockBasedRouteSupplier {
+
+    @Override
+    protected void configureProducer(RouteBuilder builder, RouteDefinition producerRoute) {
+        //uploads a file "test.txt" containing "OK"
+        producerRoute.log("storing file in ftp")
+                .setBody(builder.constant("OK"))
+                .toF("ftp://localhost:%s/test.txt?username=%s&password=%s&binary=true&passiveMode=true",
+                        System.getProperty("ftp.port"), System.getProperty("ftp.username"), System.getProperty("ftp.password"));
+    }
+
+    @Override
+    protected Function<RouteBuilder, RouteDefinition> consumerRoute() {
+        //download the file "test.txt" and get the content
+        return builder -> builder.fromF("ftp://localhost:%s/test.txt?username=%s&password=%s&binary=true&passiveMode=true&delay=1000",
+                    System.getProperty("ftp.port"), System.getProperty("ftp.username"), System.getProperty("ftp.password"))
+                .convertBodyTo(String.class);
+    }
+}
+
diff --git a/tests/features/camel-ftp/src/test/java/org/apache/karaf/camel/itest/CamelFtpITest.java b/tests/features/camel-ftp/src/test/java/org/apache/karaf/camel/itest/CamelFtpITest.java
new file mode 100644
index 00000000..934bde78
--- /dev/null
+++ b/tests/features/camel-ftp/src/test/java/org/apache/karaf/camel/itest/CamelFtpITest.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed 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.karaf.camel.itest;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.ftpserver.ConnectionConfigFactory;
+import org.apache.ftpserver.FtpServerFactory;
+import org.apache.ftpserver.filesystem.nativefs.NativeFileSystemFactory;
+import org.apache.ftpserver.ftplet.FtpException;
+import org.apache.ftpserver.ftplet.UserManager;
+import org.apache.ftpserver.listener.Listener;
+import org.apache.ftpserver.listener.ListenerFactory;
+import org.apache.ftpserver.usermanager.ClearTextPasswordEncryptor;
+import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
+import org.apache.ftpserver.usermanager.impl.BaseUser;
+import org.apache.ftpserver.usermanager.impl.WritePermission;
+import org.apache.karaf.camel.itests.AbstractCamelSingleFeatureResultMockBasedRouteITest;
+import org.apache.karaf.camel.itests.AvailablePortProvider;
+import org.apache.karaf.camel.itests.CamelKarafTestHint;
+import org.apache.karaf.camel.itests.PaxExamWithExternalResource;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerClass;
+
+@CamelKarafTestHint(externalResourceProvider = CamelFtpITest.ExternalResourceProviders.class)
+@RunWith(PaxExamWithExternalResource.class)
+@ExamReactorStrategy(PerClass.class)
+public class CamelFtpITest extends AbstractCamelSingleFeatureResultMockBasedRouteITest {
+
+    public static final String FTP_PORT_PROPERTY = "ftp.port";
+
+    @Override
+    public void configureMock(MockEndpoint mock) {
+        mock.expectedBodiesReceived("OK");
+    }
+
+    @Test
+    public void testResultMock() throws Exception {
+        assertMockEndpointsSatisfied();
+    }
+
+    public static final class ExternalResourceProviders {
+
+        public static FtpServerResource createFtpServer() throws FtpException {
+            return new FtpServerResource();
+        }
+    }
+}
\ No newline at end of file
diff --git a/tests/features/camel-ftp/src/test/java/org/apache/karaf/camel/itest/FtpServerResource.java b/tests/features/camel-ftp/src/test/java/org/apache/karaf/camel/itest/FtpServerResource.java
new file mode 100644
index 00000000..243c9e2e
--- /dev/null
+++ b/tests/features/camel-ftp/src/test/java/org/apache/karaf/camel/itest/FtpServerResource.java
@@ -0,0 +1,107 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.karaf.camel.itest;
+
+import static org.apache.karaf.camel.itest.CamelFtpITest.FTP_PORT_PROPERTY;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.ftpserver.ConnectionConfigFactory;
+import org.apache.ftpserver.FtpServer;
+import org.apache.ftpserver.FtpServerFactory;
+import org.apache.ftpserver.filesystem.nativefs.NativeFileSystemFactory;
+import org.apache.ftpserver.ftplet.FtpException;
+import org.apache.ftpserver.ftplet.UserManager;
+import org.apache.ftpserver.listener.Listener;
+import org.apache.ftpserver.listener.ListenerFactory;
+import org.apache.ftpserver.usermanager.ClearTextPasswordEncryptor;
+import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
+import org.apache.ftpserver.usermanager.impl.BaseUser;
+import org.apache.ftpserver.usermanager.impl.WritePermission;
+import org.apache.karaf.camel.itests.AvailablePortProvider;
+import org.apache.karaf.camel.itests.ExternalResource;
+import org.apache.karaf.camel.itests.ExternalResourceWithPrerequisite;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class FtpServerResource extends ExternalResourceWithPrerequisite {
+
+    private static final Logger LOG = LoggerFactory.getLogger(FtpServerResource.class);
+    private static final String USER_NAME = "scott";
+    private static final String PASSWORD = "tiger";
+
+    private final AvailablePortProvider portProvider = new AvailablePortProvider(List.of(FTP_PORT_PROPERTY));
+    private FtpServer ftpServer;
+
+    @Override
+    protected void doStart() {
+        try {
+            NativeFileSystemFactory fsf = new NativeFileSystemFactory();
+            fsf.setCreateHome(true);
+
+            PropertiesUserManagerFactory pumf = new PropertiesUserManagerFactory();
+            pumf.setAdminName("admin");
+            pumf.setPasswordEncryptor(new ClearTextPasswordEncryptor());
+            pumf.setFile(null);
+
+            UserManager userMgr = pumf.createUserManager();
+
+            BaseUser user = new BaseUser();
+            user.setName(USER_NAME);
+            user.setPassword(PASSWORD);
+            user.setHomeDirectory(System.getProperty("project.target"));
+            user.setAuthorities(Collections.singletonList(new WritePermission()));
+            userMgr.save(user);
+
+            setProperty("ftp.username", USER_NAME);
+            setProperty("ftp.password", PASSWORD);
+
+            FtpServerFactory serverFactory = new FtpServerFactory();
+            serverFactory.setUserManager(userMgr);
+            serverFactory.setFileSystem(fsf);
+            serverFactory.setConnectionConfig(new ConnectionConfigFactory().createConnectionConfig());
+
+            ListenerFactory factory = new ListenerFactory();
+            factory.setPort(Integer.parseInt(properties().get(FTP_PORT_PROPERTY)));
+            factory.setServerAddress("127.0.0.1");
+
+            final Listener listener = factory.createListener();
+            serverFactory.addListener("default", listener);
+
+            ftpServer = serverFactory.createServer();
+            ftpServer.start();
+        } catch (FtpException e) {
+            LOG.error("Could not create FTP server",e);
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    protected void doStop() {
+        if (ftpServer != null) {
+            ftpServer.stop();
+        }
+    }
+
+    @Override
+    protected List<ExternalResource> getPrerequisites() {
+        return List.of(portProvider);
+    }
+
+}
diff --git a/tests/features/pom.xml b/tests/features/pom.xml
index 143907ed..648d1be4 100644
--- a/tests/features/pom.xml
+++ b/tests/features/pom.xml
@@ -45,6 +45,7 @@
         <module>camel-core</module>
         <module>camel-ehcache</module>
         <module>camel-elasticsearch</module>
+        <module>camel-ftp</module>
         <module>camel-hazelcast</module>
         <module>camel-http</module>
         <module>camel-jcache</module>