greyp9 commented on code in PR #6172:
URL: https://github.com/apache/nifi/pull/6172#discussion_r918267934


##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestFTPCharsetIT.java:
##########
@@ -0,0 +1,304 @@
+/*
+ * 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.nifi.processors.standard;
+
+import org.apache.ftpserver.FtpServer;
+import org.apache.ftpserver.FtpServerFactory;
+import org.apache.ftpserver.ftplet.FileSystemFactory;
+import org.apache.ftpserver.ftplet.FileSystemView;
+import org.apache.ftpserver.ftplet.FtpException;
+import org.apache.ftpserver.ftplet.FtpFile;
+import org.apache.ftpserver.ftplet.UserManager;
+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.nifi.processor.Processor;
+import org.apache.nifi.processor.util.list.AbstractListProcessor;
+import org.apache.nifi.processors.standard.util.FTPTransfer;
+import org.apache.nifi.remote.io.socket.NetworkUtils;
+import org.apache.nifi.util.MockFlowFile;
+import org.apache.nifi.util.TestRunner;
+import org.apache.nifi.util.TestRunners;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestMethodOrder;
+import org.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Properties;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.params.provider.Arguments.arguments;
+
+/**
+ * Seed an ASCII charset folder in FTP server with files using i18n known 
filenames.  Iterate through a set of i18n folder
+ * names, copying the known content into each new target.  Afterwards, verify 
that the last folder contains all
+ * files with the expected filenames.
+ * <p>
+ * To test against a live FTP server, run test with system property set
+ * <code>TestFTPCharset=hostname,port,user,password</code>, like
+ * <code>TestFTPCharset=localhost,21,ftpuser,ftppassword</code>.
+ */
+@TestMethodOrder(MethodOrderer.MethodName.class)
+public class TestFTPCharsetIT {
+    private static final String SERVER_OVERRIDE = 
System.getProperty(TestFTPCharsetIT.class.getSimpleName());
+    private static final boolean EMBED_FTP_SERVER = (SERVER_OVERRIDE == null);
+    private static FtpServer FTP_SERVER;
+
+    private static final String USE_UTF8 = Boolean.TRUE.toString();
+    private static final String HOSTNAME = "localhost";
+    private static final String PORT = 
Integer.toString(NetworkUtils.getAvailableTcpPort());
+    private static final String USER = "ftpuser";
+    private static final String PASSWORD = "admin";
+
+    @TempDir
+    private static File FOLDER_FTP;
+    @TempDir
+    private static File FOLDER_USER_PROPERTIES;
+
+    public static Arguments serverParametersProvider() {
+        final String override = 
System.getProperty(TestFTPCharsetIT.class.getSimpleName());
+        if (override == null) {
+            return arguments(HOSTNAME, PORT, USER, PASSWORD);
+        } else {
+            return arguments((Object[]) override.split(","));
+        }
+    }
+
+    public static Stream<Arguments> folderNamesProvider() {
+        return Stream.of(
+                arguments("folder1", "folder2"),
+                arguments("folder2", "æøå"),
+                arguments("æøå", "folder3"),
+                arguments("folder3", "اختبار"),
+                arguments("اختبار", "folder4"),
+                arguments("folder4", "Госагїzатїой"),
+                arguments("Госагїzатїой", "folder5"),
+                arguments("folder5", "し回亡丹し工z丹卞工回几"),
+                arguments("し回亡丹し工z丹卞工回几", "folder6")
+        );
+    }
+
+    public static Stream<String> filenamesProvider() {
+        return Stream.of(
+                "1.txt",
+                "æøå.txt",
+                "اختبار.txt",
+                "Госагїzатїой.txt",
+                "し回亡丹し工z丹卞工回几.txt");
+    }
+
+    @BeforeAll
+    static void startEmbeddedServer() throws IOException, FtpException {
+        if (EMBED_FTP_SERVER) {
+            // setup ftp user
+            final Properties userProperties = new Properties();
+            userProperties.setProperty("ftpserver.user.ftpuser.idletime", "0");
+            userProperties.setProperty("ftpserver.user.ftpuser.enableflag", 
"true");
+            userProperties.setProperty("ftpserver.user.ftpuser.userpassword", 
"admin");
+            
userProperties.setProperty("ftpserver.user.ftpuser.writepermission", "true");
+            userProperties.setProperty("ftpserver.user.ftpuser.homedirectory", 
FOLDER_FTP.getAbsolutePath());
+            final File userPropertiesFile = new File(FOLDER_USER_PROPERTIES, 
"user.properties");
+            try (final FileOutputStream fos = new 
FileOutputStream(userPropertiesFile)) {
+                userProperties.store(fos, "ftp-user-properties");
+            }
+            final PropertiesUserManagerFactory userManagerFactory = new 
PropertiesUserManagerFactory();
+            userManagerFactory.setUrl(userPropertiesFile.toURI().toURL());
+            userManagerFactory.setPasswordEncryptor(new 
ClearTextPasswordEncryptor());
+            final UserManager userManager = 
userManagerFactory.createUserManager();
+            final BaseUser ftpuser = (BaseUser) 
userManager.getUserByName(USER);
+            // setup embedded ftp server
+            final FtpServerFactory serverFactory = new FtpServerFactory();
+            serverFactory.setUserManager(userManager);
+            final FileSystemFactory fileSystemFactory = 
serverFactory.getFileSystem();
+            final FileSystemView view = 
fileSystemFactory.createFileSystemView(ftpuser);
+            final FtpFile workingDirectory = view.getWorkingDirectory();
+            final Object physicalFile = workingDirectory.getPhysicalFile();
+            assertInstanceOf(File.class, physicalFile);
+            assertEquals(FOLDER_FTP.getAbsolutePath(), ((File) 
physicalFile).getAbsolutePath());
+            final ListenerFactory factory = new ListenerFactory();
+            factory.setPort(Integer.parseInt(PORT));
+            serverFactory.addListener("default", factory.createListener());
+            FTP_SERVER = serverFactory.createServer();
+            FTP_SERVER.start();
+        }
+    }
+
+    @AfterAll
+    static void stopEmbeddedServer() throws InterruptedException {
+        if (EMBED_FTP_SERVER) {
+            FTP_SERVER.stop();
+            while (!FTP_SERVER.isStopped()) {
+                Thread.sleep(100L);
+            }

Review Comment:
   Thanks.  In an earlier iteration I was starting an FTP server for each test 
case; when done once, this is unneeded.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@nifi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to