From f35b9cff9559bff870a703255c23698b31cb01be Mon Sep 17 00:00:00 2001 From: Jan Malkiewicz Date: Thu, 23 Jul 2020 09:04:34 +0200 Subject: Removed support for FTPS protocol (from now only FTPeS and sFTP will be supported). Issue-ID: DCAEGEN2-2367 Signed-off-by: Jan Malkiewicz Change-Id: Ifada2899431c56bdbfcf3aad73fdc9b376ae9c4f --- .../collectors/datafile/ftp/FtpesClient.java | 239 +++++++++++++++++++++ .../collectors/datafile/ftp/FtpsClient.java | 238 -------------------- .../dcaegen2/collectors/datafile/ftp/Scheme.java | 9 +- .../datafile/ftp/SftpClientSettings.java | 4 +- .../collectors/datafile/tasks/FileCollector.java | 13 +- .../collectors/datafile/ftp/FtpesClientTest.java | 237 ++++++++++++++++++++ .../collectors/datafile/ftp/FtpsClientTest.java | 236 -------------------- .../collectors/datafile/ftp/SchemeTest.java | 24 +-- .../collectors/datafile/model/FileDataTest.java | 7 +- .../datafile/service/JsonMessageParserTest.java | 9 +- .../datafile/tasks/DMaaPMessageConsumerTest.java | 7 +- .../datafile/tasks/FileCollectorTest.java | 41 ++-- .../datafile/tasks/ScheduledTasksTest.java | 5 +- 13 files changed, 538 insertions(+), 531 deletions(-) create mode 100644 datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpesClient.java delete mode 100644 datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClient.java create mode 100644 datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpesClientTest.java delete mode 100644 datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClientTest.java diff --git a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpesClient.java b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpesClient.java new file mode 100644 index 00000000..a91d46ae --- /dev/null +++ b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpesClient.java @@ -0,0 +1,239 @@ +/*- + * ============LICENSE_START====================================================================== + * Copyright (C) 2018-2019 Nordix Foundation. All rights reserved. + * Copyright (C) 2020 Nokia. All rights reserved. + * =============================================================================================== + * 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. + * ============LICENSE_END======================================================================== + */ + +package org.onap.dcaegen2.collectors.datafile.ftp; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.GeneralSecurityException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.UnrecoverableKeyException; +import java.security.cert.CertificateException; +import java.util.Optional; + +import javax.net.ssl.KeyManager; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.TrustManagerFactory; + +import org.apache.commons.net.ftp.FTP; +import org.apache.commons.net.ftp.FTPReply; +import org.apache.commons.net.ftp.FTPSClient; +import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException; +import org.onap.dcaegen2.collectors.datafile.exceptions.NonRetryableDatafileTaskException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.io.FileSystemResource; + +/** + * Gets file from PNF with FTPS protocol. + * + * @author Martin Yan + */ +public class FtpesClient implements FileCollectClient { + private static final Logger logger = LoggerFactory.getLogger(FtpesClient.class); + + private static final int DEFAULT_PORT = 21; + + FTPSClient realFtpsClient = new FTPSClient(); + private final FileServerData fileServerData; + private static TrustManager theTrustManager = null; + private static KeyManager theKeyManager = null; + + private final Path keyCertPath; + private final String keyCertPasswordPath; + private final Path trustedCaPath; + private final String trustedCaPasswordPath; + + /** + * Constructor. + * + * @param fileServerData info needed to connect to the PNF. + * @param keyCertPath path to DFC's key cert. + * @param keyCertPasswordPath path of file containing password for DFC's key cert. + * @param trustedCaPath path to the PNF's trusted keystore. + * @param trustedCaPasswordPath path of file containing password for the PNF's trusted keystore. + */ + public FtpesClient(FileServerData fileServerData, Path keyCertPath, String keyCertPasswordPath, Path trustedCaPath, + String trustedCaPasswordPath) { + this.fileServerData = fileServerData; + this.keyCertPath = keyCertPath; + this.keyCertPasswordPath = keyCertPasswordPath; + this.trustedCaPath = trustedCaPath; + this.trustedCaPasswordPath = trustedCaPasswordPath; + } + + @Override + public void open() throws DatafileTaskException { + try { + realFtpsClient.setNeedClientAuth(true); + realFtpsClient.setKeyManager(getKeyManager(keyCertPath, keyCertPasswordPath)); + realFtpsClient.setTrustManager(getTrustManager(trustedCaPath, trustedCaPasswordPath)); + setUpConnection(); + } catch (DatafileTaskException e) { + throw e; + } catch (Exception e) { + throw new DatafileTaskException("Could not open connection: " + e, e); + } + } + + @Override + public void close() { + logger.trace("starting to closeDownConnection"); + if (realFtpsClient.isConnected()) { + try { + boolean logOut = realFtpsClient.logout(); + logger.trace("logOut: {}", logOut); + } catch (Exception e) { + logger.trace("Unable to logout connection.", e); + } + try { + realFtpsClient.disconnect(); + logger.trace("disconnected!"); + } catch (Exception e) { + logger.trace("Unable to disconnect connection.", e); + } + } + } + + @Override + public void collectFile(String remoteFileName, Path localFileName) throws DatafileTaskException { + logger.trace("collectFile called"); + + try (OutputStream output = createOutputStream(localFileName)) { + logger.trace("begin to retrieve from xNF."); + if (!realFtpsClient.retrieveFile(remoteFileName, output)) { + throw new NonRetryableDatafileTaskException( + "Could not retrieve file. No retry attempts will be done, file :" + remoteFileName); + } + } catch (IOException e) { + throw new DatafileTaskException("Could not fetch file: " + e, e); + } + logger.trace("collectFile fetched: {}", localFileName); + } + + private static int getPort(Optional port) { + return port.isPresent() ? port.get() : DEFAULT_PORT; + } + + private void setUpConnection() throws DatafileTaskException, IOException { + + realFtpsClient.connect(fileServerData.serverAddress(), getPort(fileServerData.port())); + logger.trace("after ftp connect"); + + if (!realFtpsClient.login(fileServerData.userId(), fileServerData.password())) { + throw new DatafileTaskException("Unable to log in to xNF. " + fileServerData.serverAddress()); + } + + if (FTPReply.isPositiveCompletion(realFtpsClient.getReplyCode())) { + realFtpsClient.enterLocalPassiveMode(); + realFtpsClient.setFileType(FTP.BINARY_FILE_TYPE); + // Set protection buffer size + realFtpsClient.execPBSZ(0); + // Set data channel protection to private + realFtpsClient.execPROT("P"); + realFtpsClient.setBufferSize(1024 * 1024); + } else { + throw new DatafileTaskException("Unable to connect to xNF. " + fileServerData.serverAddress() + + " xNF reply code: " + realFtpsClient.getReplyCode()); + } + + logger.trace("setUpConnection successfully!"); + } + + private TrustManager createTrustManager(Path trustedCaPath, String trustedCaPassword) + throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException { + logger.trace("Creating trust manager from file: {}", trustedCaPath); + try (InputStream fis = createInputStream(trustedCaPath)) { + KeyStore keyStore = KeyStore.getInstance("JKS"); + keyStore.load(fis, trustedCaPassword.toCharArray()); + TrustManagerFactory factory = TrustManagerFactory.getInstance("SunX509"); + factory.init(keyStore); + return factory.getTrustManagers()[0]; + } + } + + protected InputStream createInputStream(Path localFileName) throws IOException { + FileSystemResource realResource = new FileSystemResource(localFileName); + return realResource.getInputStream(); + } + + protected OutputStream createOutputStream(Path localFileName) throws IOException { + File localFile = localFileName.toFile(); + if (!localFile.createNewFile()) { + logger.warn("Local file {} already created", localFileName); + } + OutputStream output = new FileOutputStream(localFile); + logger.trace("File {} opened xNF", localFileName); + return output; + } + + protected TrustManager getTrustManager(Path trustedCaPath, String trustedCaPasswordPath) + throws KeyStoreException, NoSuchAlgorithmException, IOException, CertificateException { + String trustedCaPassword = ""; + try { + trustedCaPassword = new String(Files.readAllBytes(Paths.get(trustedCaPasswordPath))); + } catch (IOException e) { + logger.error("Truststore password file at path: {} cannot be opened ", trustedCaPasswordPath); + e.printStackTrace(); + } + synchronized (FtpesClient.class) { + if (theTrustManager == null) { + theTrustManager = createTrustManager(trustedCaPath, trustedCaPassword); + } + return theTrustManager; + } + } + + protected KeyManager getKeyManager(Path keyCertPath, String keyCertPasswordPath) + throws IOException, GeneralSecurityException { + String keyCertPassword = ""; + try { + keyCertPassword = new String(Files.readAllBytes(Paths.get(keyCertPasswordPath))); + } catch (IOException e) { + logger.error("Keystore password file at path: {} cannot be opened ", keyCertPasswordPath); + e.printStackTrace(); + } + + synchronized (FtpesClient.class) { + if (theKeyManager == null) { + theKeyManager = createKeyManager(keyCertPath, keyCertPassword); + } + return theKeyManager; + } + } + + private KeyManager createKeyManager(Path keyCertPath, String keyCertPassword) throws IOException, KeyStoreException, + NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException { + logger.trace("Creating key manager from file: {}", keyCertPath); + try (InputStream fis = createInputStream(keyCertPath)) { + KeyStore keyStore = KeyStore.getInstance("JKS"); + keyStore.load(fis, keyCertPassword.toCharArray()); + KeyManagerFactory factory = KeyManagerFactory.getInstance("SunX509"); + factory.init(keyStore, keyCertPassword.toCharArray()); + return factory.getKeyManagers()[0]; + } + } +} diff --git a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClient.java b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClient.java deleted file mode 100644 index fea578ba..00000000 --- a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClient.java +++ /dev/null @@ -1,238 +0,0 @@ -/*- - * ============LICENSE_START====================================================================== - * Copyright (C) 2018-2019 Nordix Foundation. All rights reserved. - * =============================================================================================== - * 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. - * ============LICENSE_END======================================================================== - */ - -package org.onap.dcaegen2.collectors.datafile.ftp; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.security.GeneralSecurityException; -import java.security.KeyStore; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.UnrecoverableKeyException; -import java.security.cert.CertificateException; -import java.util.Optional; - -import javax.net.ssl.KeyManager; -import javax.net.ssl.KeyManagerFactory; -import javax.net.ssl.TrustManager; -import javax.net.ssl.TrustManagerFactory; - -import org.apache.commons.net.ftp.FTP; -import org.apache.commons.net.ftp.FTPReply; -import org.apache.commons.net.ftp.FTPSClient; -import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException; -import org.onap.dcaegen2.collectors.datafile.exceptions.NonRetryableDatafileTaskException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.core.io.FileSystemResource; - -/** - * Gets file from PNF with FTPS protocol. - * - * @author Martin Yan - */ -public class FtpsClient implements FileCollectClient { - private static final Logger logger = LoggerFactory.getLogger(FtpsClient.class); - - private static final int FTPS_DEFAULT_PORT = 21; - - FTPSClient realFtpsClient = new FTPSClient(); - private final FileServerData fileServerData; - private static TrustManager theTrustManager = null; - private static KeyManager theKeyManager = null; - - private final Path keyCertPath; - private final String keyCertPasswordPath; - private final Path trustedCaPath; - private final String trustedCaPasswordPath; - - /** - * Constructor. - * - * @param fileServerData info needed to connect to the PNF. - * @param keyCertPath path to DFC's key cert. - * @param keyCertPasswordPath path of file containing password for DFC's key cert. - * @param trustedCaPath path to the PNF's trusted keystore. - * @param trustedCaPasswordPath path of file containing password for the PNF's trusted keystore. - */ - public FtpsClient(FileServerData fileServerData, Path keyCertPath, String keyCertPasswordPath, Path trustedCaPath, - String trustedCaPasswordPath) { - this.fileServerData = fileServerData; - this.keyCertPath = keyCertPath; - this.keyCertPasswordPath = keyCertPasswordPath; - this.trustedCaPath = trustedCaPath; - this.trustedCaPasswordPath = trustedCaPasswordPath; - } - - @Override - public void open() throws DatafileTaskException { - try { - realFtpsClient.setNeedClientAuth(true); - realFtpsClient.setKeyManager(getKeyManager(keyCertPath, keyCertPasswordPath)); - realFtpsClient.setTrustManager(getTrustManager(trustedCaPath, trustedCaPasswordPath)); - setUpConnection(); - } catch (DatafileTaskException e) { - throw e; - } catch (Exception e) { - throw new DatafileTaskException("Could not open connection: " + e, e); - } - } - - @Override - public void close() { - logger.trace("starting to closeDownConnection"); - if (realFtpsClient.isConnected()) { - try { - boolean logOut = realFtpsClient.logout(); - logger.trace("logOut: {}", logOut); - } catch (Exception e) { - logger.trace("Unable to logout connection.", e); - } - try { - realFtpsClient.disconnect(); - logger.trace("disconnected!"); - } catch (Exception e) { - logger.trace("Unable to disconnect connection.", e); - } - } - } - - @Override - public void collectFile(String remoteFileName, Path localFileName) throws DatafileTaskException { - logger.trace("collectFile called"); - - try (OutputStream output = createOutputStream(localFileName)) { - logger.trace("begin to retrieve from xNF."); - if (!realFtpsClient.retrieveFile(remoteFileName, output)) { - throw new NonRetryableDatafileTaskException( - "Could not retrieve file. No retry attempts will be done, file :" + remoteFileName); - } - } catch (IOException e) { - throw new DatafileTaskException("Could not fetch file: " + e, e); - } - logger.trace("collectFile fetched: {}", localFileName); - } - - private static int getPort(Optional port) { - return port.isPresent() ? port.get() : FTPS_DEFAULT_PORT; - } - - private void setUpConnection() throws DatafileTaskException, IOException { - - realFtpsClient.connect(fileServerData.serverAddress(), getPort(fileServerData.port())); - logger.trace("after ftp connect"); - - if (!realFtpsClient.login(fileServerData.userId(), fileServerData.password())) { - throw new DatafileTaskException("Unable to log in to xNF. " + fileServerData.serverAddress()); - } - - if (FTPReply.isPositiveCompletion(realFtpsClient.getReplyCode())) { - realFtpsClient.enterLocalPassiveMode(); - realFtpsClient.setFileType(FTP.BINARY_FILE_TYPE); - // Set protection buffer size - realFtpsClient.execPBSZ(0); - // Set data channel protection to private - realFtpsClient.execPROT("P"); - realFtpsClient.setBufferSize(1024 * 1024); - } else { - throw new DatafileTaskException("Unable to connect to xNF. " + fileServerData.serverAddress() - + " xNF reply code: " + realFtpsClient.getReplyCode()); - } - - logger.trace("setUpConnection successfully!"); - } - - private TrustManager createTrustManager(Path trustedCaPath, String trustedCaPassword) - throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException { - logger.trace("Creating trust manager from file: {}", trustedCaPath); - try (InputStream fis = createInputStream(trustedCaPath)) { - KeyStore keyStore = KeyStore.getInstance("JKS"); - keyStore.load(fis, trustedCaPassword.toCharArray()); - TrustManagerFactory factory = TrustManagerFactory.getInstance("SunX509"); - factory.init(keyStore); - return factory.getTrustManagers()[0]; - } - } - - protected InputStream createInputStream(Path localFileName) throws IOException { - FileSystemResource realResource = new FileSystemResource(localFileName); - return realResource.getInputStream(); - } - - protected OutputStream createOutputStream(Path localFileName) throws IOException { - File localFile = localFileName.toFile(); - if (!localFile.createNewFile()) { - logger.warn("Local file {} already created", localFileName); - } - OutputStream output = new FileOutputStream(localFile); - logger.trace("File {} opened xNF", localFileName); - return output; - } - - protected TrustManager getTrustManager(Path trustedCaPath, String trustedCaPasswordPath) - throws KeyStoreException, NoSuchAlgorithmException, IOException, CertificateException { - String trustedCaPassword = ""; - try { - trustedCaPassword = new String(Files.readAllBytes(Paths.get(trustedCaPasswordPath))); - } catch (IOException e) { - logger.error("Truststore password file at path: {} cannot be opened ", trustedCaPasswordPath); - e.printStackTrace(); - } - synchronized (FtpsClient.class) { - if (theTrustManager == null) { - theTrustManager = createTrustManager(trustedCaPath, trustedCaPassword); - } - return theTrustManager; - } - } - - protected KeyManager getKeyManager(Path keyCertPath, String keyCertPasswordPath) - throws IOException, GeneralSecurityException { - String keyCertPassword = ""; - try { - keyCertPassword = new String(Files.readAllBytes(Paths.get(keyCertPasswordPath))); - } catch (IOException e) { - logger.error("Keystore password file at path: {} cannot be opened ", keyCertPasswordPath); - e.printStackTrace(); - } - - synchronized (FtpsClient.class) { - if (theKeyManager == null) { - theKeyManager = createKeyManager(keyCertPath, keyCertPassword); - } - return theKeyManager; - } - } - - private KeyManager createKeyManager(Path keyCertPath, String keyCertPassword) throws IOException, KeyStoreException, - NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException { - logger.trace("Creating key manager from file: {}", keyCertPath); - try (InputStream fis = createInputStream(keyCertPath)) { - KeyStore keyStore = KeyStore.getInstance("JKS"); - keyStore.load(fis, keyCertPassword.toCharArray()); - KeyManagerFactory factory = KeyManagerFactory.getInstance("SunX509"); - factory.init(keyStore, keyCertPassword.toCharArray()); - return factory.getKeyManagers()[0]; - } - } -} diff --git a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/Scheme.java b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/Scheme.java index c10c0f1e..b20feb82 100644 --- a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/Scheme.java +++ b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/Scheme.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. All rights reserved. + * Copyright (C) 2020 Nokia. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,10 +28,10 @@ import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException; * */ public enum Scheme { - FTPS, SFTP; + FTPES, SFTP; public static final String DFC_DOES_NOT_SUPPORT_PROTOCOL_ERROR_MSG = "DFC does not support protocol "; - public static final String SUPPORTED_PROTOCOLS_ERROR_MESSAGE = ". Supported protocols are FTPES, FTPS, and SFTP"; + public static final String SUPPORTED_PROTOCOLS_ERROR_MESSAGE = ". Supported protocols are FTPeS and sFTP"; /** * Get a Scheme from a string. @@ -41,8 +42,8 @@ public enum Scheme { */ public static Scheme getSchemeFromString(String schemeString) throws DatafileTaskException { Scheme result; - if ("FTPS".equalsIgnoreCase(schemeString) || "FTPES".equalsIgnoreCase(schemeString)) { - result = Scheme.FTPS; + if ("FTPES".equalsIgnoreCase(schemeString)) { + result = Scheme.FTPES; } else if ("SFTP".equalsIgnoreCase(schemeString)) { result = Scheme.SFTP; } else { diff --git a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClientSettings.java b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClientSettings.java index 8cab4327..a4c5cb3f 100644 --- a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClientSettings.java +++ b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClientSettings.java @@ -53,10 +53,10 @@ public class SftpClientSettings { private void logUsageOfStrictHostCheckingFlag(boolean strictHostKeyChecking, String filePath) { if (strictHostKeyChecking) { - logger.info("StrictHostKeyChecking will be enabled with KNOW_HOSTS_FILE_PATH [{}].", filePath); + logger.info("StrictHostKeyChecking will be enabled with KNOWN_HOSTS_FILE_PATH [{}].", filePath); } else { logger.warn( - "StrictHostKeyChecking is enabled but environment variable KNOW_HOSTS_FILE_PATH is not set or points to not existing file [{}] --> falling back to StrictHostKeyChecking='no'.", + "StrictHostKeyChecking is enabled but environment variable KNOWN_HOSTS_FILE_PATH is not set or points to not existing file [{}] --> falling back to StrictHostKeyChecking='no'.", filePath); } } diff --git a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/tasks/FileCollector.java b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/tasks/FileCollector.java index e9c4aceb..b151b042 100644 --- a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/tasks/FileCollector.java +++ b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/tasks/FileCollector.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START====================================================================== * Copyright (C) 2018-2019 Nordix Foundation. All rights reserved. + * Copyright (C) 2020 Nokia. All rights reserved. * =============================================================================================== * 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 @@ -27,7 +28,7 @@ import org.onap.dcaegen2.collectors.datafile.configuration.FtpesConfig; import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException; import org.onap.dcaegen2.collectors.datafile.exceptions.NonRetryableDatafileTaskException; import org.onap.dcaegen2.collectors.datafile.ftp.FileCollectClient; -import org.onap.dcaegen2.collectors.datafile.ftp.FtpsClient; +import org.onap.dcaegen2.collectors.datafile.ftp.FtpesClient; import org.onap.dcaegen2.collectors.datafile.ftp.SftpClient; import org.onap.dcaegen2.collectors.datafile.ftp.SftpClientSettings; import org.onap.dcaegen2.collectors.datafile.model.Counters; @@ -124,10 +125,10 @@ public class FileCollector { switch (fileData.scheme()) { case SFTP: return createSftpClient(fileData); - case FTPS: - return createFtpsClient(fileData); + case FTPES: + return createFtpesClient(fileData); default: - throw new DatafileTaskException("Unhandeled protocol: " + fileData.scheme()); + throw new DatafileTaskException("Unhandled protocol: " + fileData.scheme()); } } @@ -158,9 +159,9 @@ public class FileCollector { new SftpClientSettings(datafileAppConfig.getSftpConfiguration())); } - protected FtpsClient createFtpsClient(FileData fileData) { + protected FtpesClient createFtpesClient(FileData fileData) { FtpesConfig config = datafileAppConfig.getFtpesConfiguration(); - return new FtpsClient(fileData.fileServerData(), Paths.get(config.keyCert()), config.keyPasswordPath(), + return new FtpesClient(fileData.fileServerData(), Paths.get(config.keyCert()), config.keyPasswordPath(), Paths.get(config.trustedCa()), config.trustedCaPasswordPath()); } } diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpesClientTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpesClientTest.java new file mode 100644 index 00000000..8e6ff947 --- /dev/null +++ b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpesClientTest.java @@ -0,0 +1,237 @@ +/* + * ============LICENSE_START====================================================================== + * Copyright (C) 2018-2019 Nordix Foundation. All rights reserved. + * Copyright (C) 2020 Nokia. All rights reserved. + * =============================================================================================== + * 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. + * ============LICENSE_END======================================================================== + */ + +package org.onap.dcaegen2.collectors.datafile.ftp; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Path; +import java.nio.file.Paths; + +import javax.net.ssl.KeyManager; +import javax.net.ssl.TrustManager; + +import org.apache.commons.net.ftp.FTP; +import org.apache.commons.net.ftp.FTPSClient; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentMatchers; +import org.springframework.http.HttpStatus; + +public class FtpesClientTest { + + private static final String REMOTE_FILE_PATH = "/dir/sample.txt"; + private static final Path LOCAL_FILE_PATH = Paths.get("target/sample.txt"); + private static final String XNF_ADDRESS = "127.0.0.1"; + private static final int PORT = 8021; + private static final String FTP_KEY_PATH = "ftpKeyPath"; + private static final String FTP_KEY_PASSWORD = "ftpKeyPassword"; + private static final Path TRUSTED_CA_PATH = Paths.get("trustedCaPath"); + private static final String TRUSTED_CA_PASSWORD = "trustedCaPassword"; + + private static final String USERNAME = "bob"; + private static final String PASSWORD = "123"; + + private FTPSClient ftpsClientMock = mock(FTPSClient.class); + private KeyManager keyManagerMock = mock(KeyManager.class); + private TrustManager trustManagerMock = mock(TrustManager.class); + private InputStream inputStreamMock = mock(InputStream.class); + private OutputStream outputStreamMock = mock(OutputStream.class); + + FtpesClient clientUnderTestSpy; + + private ImmutableFileServerData createFileServerData() { + return ImmutableFileServerData.builder() // + .serverAddress(XNF_ADDRESS) // + .userId(USERNAME).password(PASSWORD) // + .port(PORT) // + .build(); + } + + @BeforeEach + protected void setUp() throws Exception { + clientUnderTestSpy = spy(new FtpesClient(createFileServerData(), Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD, + TRUSTED_CA_PATH, TRUSTED_CA_PASSWORD)); + clientUnderTestSpy.realFtpsClient = ftpsClientMock; + } + + private void verifyFtpsClientMock_openOk() throws Exception { + doReturn(outputStreamMock).when(clientUnderTestSpy).createOutputStream(LOCAL_FILE_PATH); + + when(ftpsClientMock.retrieveFile(ArgumentMatchers.eq(REMOTE_FILE_PATH), + ArgumentMatchers.any(OutputStream.class))).thenReturn(true); + verify(ftpsClientMock).setNeedClientAuth(true); + verify(ftpsClientMock).setKeyManager(keyManagerMock); + verify(ftpsClientMock).setTrustManager(trustManagerMock); + verify(ftpsClientMock).connect(XNF_ADDRESS, PORT); + verify(ftpsClientMock).login(USERNAME, PASSWORD); + verify(ftpsClientMock).getReplyCode(); + verify(ftpsClientMock, times(1)).enterLocalPassiveMode(); + verify(ftpsClientMock).execPBSZ(0); + verify(ftpsClientMock).execPROT("P"); + verify(ftpsClientMock).setFileType(FTP.BINARY_FILE_TYPE); + verify(ftpsClientMock).setBufferSize(1024 * 1024); + } + + @Test + public void collectFile_allOk() throws Exception { + + doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD); + doReturn(trustManagerMock).when(clientUnderTestSpy).getTrustManager(TRUSTED_CA_PATH, TRUSTED_CA_PASSWORD); + doReturn(outputStreamMock).when(clientUnderTestSpy).createOutputStream(LOCAL_FILE_PATH); + doReturn(true).when(ftpsClientMock).login(USERNAME, PASSWORD); + doReturn(HttpStatus.OK.value()).when(ftpsClientMock).getReplyCode(); + + clientUnderTestSpy.open(); + + doReturn(true).when(ftpsClientMock).retrieveFile(REMOTE_FILE_PATH, outputStreamMock); + clientUnderTestSpy.collectFile(REMOTE_FILE_PATH, LOCAL_FILE_PATH); + + doReturn(true).when(ftpsClientMock).isConnected(); + clientUnderTestSpy.close(); + + verifyFtpsClientMock_openOk(); + verify(ftpsClientMock, times(1)).isConnected(); + verify(ftpsClientMock, times(1)).logout(); + verify(ftpsClientMock, times(1)).disconnect(); + verify(ftpsClientMock, times(1)).retrieveFile(ArgumentMatchers.eq(REMOTE_FILE_PATH), any()); + verifyNoMoreInteractions(ftpsClientMock); + } + + @Test + public void collectFileFaultyOwnKey_shouldFail() throws Exception { + + doReturn(outputStreamMock).when(clientUnderTestSpy).createOutputStream(LOCAL_FILE_PATH); + assertThatThrownBy(() -> clientUnderTestSpy.open()) + .hasMessageContaining("Could not open connection: java.io.FileNotFoundException:"); + + verify(ftpsClientMock).setNeedClientAuth(true); + + doReturn(false).when(ftpsClientMock).isConnected(); + clientUnderTestSpy.close(); + verify(ftpsClientMock).isConnected(); + verifyNoMoreInteractions(ftpsClientMock); + } + + @Test + public void collectFileFaultTrustedCA_shouldFail_no_trustedCA_file() throws Exception { + + doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD); + doThrow(new IOException("problem")).when(clientUnderTestSpy).createInputStream(TRUSTED_CA_PATH); + + assertThatThrownBy(() -> clientUnderTestSpy.open()) + .hasMessage("Could not open connection: java.io.IOException: problem"); + } + + @Test + public void collectFileFaultTrustedCA_shouldFail_empty_trustedCA_file() throws Exception { + + doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD); + doReturn(inputStreamMock).when(clientUnderTestSpy).createInputStream(TRUSTED_CA_PATH); + + assertThatThrownBy(() -> clientUnderTestSpy.open()) + .hasMessage("Could not open connection: java.io.EOFException"); + } + + @Test + public void collectFileFaultyLogin_shouldFail() throws Exception { + + doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD); + doReturn(trustManagerMock).when(clientUnderTestSpy).getTrustManager(TRUSTED_CA_PATH, TRUSTED_CA_PASSWORD); + doReturn(outputStreamMock).when(clientUnderTestSpy).createOutputStream(LOCAL_FILE_PATH); + doReturn(false).when(ftpsClientMock).login(USERNAME, PASSWORD); + + assertThatThrownBy(() -> clientUnderTestSpy.open()).hasMessage("Unable to log in to xNF. 127.0.0.1"); + + verify(ftpsClientMock).setNeedClientAuth(true); + verify(ftpsClientMock).setKeyManager(keyManagerMock); + verify(ftpsClientMock).setTrustManager(trustManagerMock); + verify(ftpsClientMock).connect(XNF_ADDRESS, PORT); + verify(ftpsClientMock).login(USERNAME, PASSWORD); + } + + @Test + public void collectFileBadRequestResponse_shouldFail() throws Exception { + doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD); + doReturn(trustManagerMock).when(clientUnderTestSpy).getTrustManager(TRUSTED_CA_PATH, TRUSTED_CA_PASSWORD); + doReturn(outputStreamMock).when(clientUnderTestSpy).createOutputStream(LOCAL_FILE_PATH); + doReturn(true).when(ftpsClientMock).login(USERNAME, PASSWORD); + doReturn(503).when(ftpsClientMock).getReplyCode(); + + assertThatThrownBy(() -> clientUnderTestSpy.open()) + .hasMessage("Unable to connect to xNF. 127.0.0.1 xNF reply code: 503"); + + verify(ftpsClientMock).setNeedClientAuth(true); + verify(ftpsClientMock).setKeyManager(keyManagerMock); + verify(ftpsClientMock).setTrustManager(trustManagerMock); + verify(ftpsClientMock).connect(XNF_ADDRESS, PORT); + verify(ftpsClientMock).login(USERNAME, PASSWORD); + verify(ftpsClientMock, times(2)).getReplyCode(); + verifyNoMoreInteractions(ftpsClientMock); + } + + @Test + public void collectFile_shouldFail() throws Exception { + doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD); + doReturn(trustManagerMock).when(clientUnderTestSpy).getTrustManager(TRUSTED_CA_PATH, TRUSTED_CA_PASSWORD); + doReturn(outputStreamMock).when(clientUnderTestSpy).createOutputStream(LOCAL_FILE_PATH); + doReturn(true).when(ftpsClientMock).login(USERNAME, PASSWORD); + doReturn(HttpStatus.OK.value()).when(ftpsClientMock).getReplyCode(); + clientUnderTestSpy.open(); + + doReturn(false).when(ftpsClientMock).retrieveFile(REMOTE_FILE_PATH, outputStreamMock); + + assertThatThrownBy(() -> clientUnderTestSpy.collectFile(REMOTE_FILE_PATH, LOCAL_FILE_PATH)) + .hasMessageContaining(REMOTE_FILE_PATH).hasMessageContaining("No retry"); + + verifyFtpsClientMock_openOk(); + verify(ftpsClientMock, times(1)).retrieveFile(ArgumentMatchers.eq(REMOTE_FILE_PATH), any()); + verifyNoMoreInteractions(ftpsClientMock); + } + + @Test + public void collectFile_shouldFail_ioexception() throws Exception { + doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD); + doReturn(trustManagerMock).when(clientUnderTestSpy).getTrustManager(TRUSTED_CA_PATH, TRUSTED_CA_PASSWORD); + doReturn(outputStreamMock).when(clientUnderTestSpy).createOutputStream(LOCAL_FILE_PATH); + doReturn(true).when(ftpsClientMock).login(USERNAME, PASSWORD); + doReturn(HttpStatus.OK.value()).when(ftpsClientMock).getReplyCode(); + clientUnderTestSpy.open(); + when(ftpsClientMock.isConnected()).thenReturn(false); + + doThrow(new IOException("problem")).when(ftpsClientMock).retrieveFile(REMOTE_FILE_PATH, outputStreamMock); + + assertThatThrownBy(() -> clientUnderTestSpy.collectFile(REMOTE_FILE_PATH, LOCAL_FILE_PATH)) + .hasMessage("Could not fetch file: java.io.IOException: problem"); + + verifyFtpsClientMock_openOk(); + verify(ftpsClientMock, times(1)).retrieveFile(ArgumentMatchers.eq(REMOTE_FILE_PATH), any()); + verifyNoMoreInteractions(ftpsClientMock); + } +} diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClientTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClientTest.java deleted file mode 100644 index f64cf433..00000000 --- a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClientTest.java +++ /dev/null @@ -1,236 +0,0 @@ -/* - * ============LICENSE_START====================================================================== - * Copyright (C) 2018-2019 Nordix Foundation. All rights reserved. - * =============================================================================================== - * 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. - * ============LICENSE_END======================================================================== - */ - -package org.onap.dcaegen2.collectors.datafile.ftp; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.file.Path; -import java.nio.file.Paths; - -import javax.net.ssl.KeyManager; -import javax.net.ssl.TrustManager; - -import org.apache.commons.net.ftp.FTP; -import org.apache.commons.net.ftp.FTPSClient; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentMatchers; -import org.springframework.http.HttpStatus; - -public class FtpsClientTest { - - private static final String REMOTE_FILE_PATH = "/dir/sample.txt"; - private static final Path LOCAL_FILE_PATH = Paths.get("target/sample.txt"); - private static final String XNF_ADDRESS = "127.0.0.1"; - private static final int PORT = 8021; - private static final String FTP_KEY_PATH = "ftpKeyPath"; - private static final String FTP_KEY_PASSWORD = "ftpKeyPassword"; - private static final Path TRUSTED_CA_PATH = Paths.get("trustedCaPath"); - private static final String TRUSTED_CA_PASSWORD = "trustedCaPassword"; - - private static final String USERNAME = "bob"; - private static final String PASSWORD = "123"; - - private FTPSClient ftpsClientMock = mock(FTPSClient.class); - private KeyManager keyManagerMock = mock(KeyManager.class); - private TrustManager trustManagerMock = mock(TrustManager.class); - private InputStream inputStreamMock = mock(InputStream.class); - private OutputStream outputStreamMock = mock(OutputStream.class); - - FtpsClient clientUnderTestSpy; - - private ImmutableFileServerData createFileServerData() { - return ImmutableFileServerData.builder() // - .serverAddress(XNF_ADDRESS) // - .userId(USERNAME).password(PASSWORD) // - .port(PORT) // - .build(); - } - - @BeforeEach - protected void setUp() throws Exception { - clientUnderTestSpy = spy(new FtpsClient(createFileServerData(), Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD, - TRUSTED_CA_PATH, TRUSTED_CA_PASSWORD)); - clientUnderTestSpy.realFtpsClient = ftpsClientMock; - } - - private void verifyFtpsClientMock_openOk() throws Exception { - doReturn(outputStreamMock).when(clientUnderTestSpy).createOutputStream(LOCAL_FILE_PATH); - - when(ftpsClientMock.retrieveFile(ArgumentMatchers.eq(REMOTE_FILE_PATH), - ArgumentMatchers.any(OutputStream.class))).thenReturn(true); - verify(ftpsClientMock).setNeedClientAuth(true); - verify(ftpsClientMock).setKeyManager(keyManagerMock); - verify(ftpsClientMock).setTrustManager(trustManagerMock); - verify(ftpsClientMock).connect(XNF_ADDRESS, PORT); - verify(ftpsClientMock).login(USERNAME, PASSWORD); - verify(ftpsClientMock).getReplyCode(); - verify(ftpsClientMock, times(1)).enterLocalPassiveMode(); - verify(ftpsClientMock).execPBSZ(0); - verify(ftpsClientMock).execPROT("P"); - verify(ftpsClientMock).setFileType(FTP.BINARY_FILE_TYPE); - verify(ftpsClientMock).setBufferSize(1024 * 1024); - } - - @Test - public void collectFile_allOk() throws Exception { - - doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD); - doReturn(trustManagerMock).when(clientUnderTestSpy).getTrustManager(TRUSTED_CA_PATH, TRUSTED_CA_PASSWORD); - doReturn(outputStreamMock).when(clientUnderTestSpy).createOutputStream(LOCAL_FILE_PATH); - doReturn(true).when(ftpsClientMock).login(USERNAME, PASSWORD); - doReturn(HttpStatus.OK.value()).when(ftpsClientMock).getReplyCode(); - - clientUnderTestSpy.open(); - - doReturn(true).when(ftpsClientMock).retrieveFile(REMOTE_FILE_PATH, outputStreamMock); - clientUnderTestSpy.collectFile(REMOTE_FILE_PATH, LOCAL_FILE_PATH); - - doReturn(true).when(ftpsClientMock).isConnected(); - clientUnderTestSpy.close(); - - verifyFtpsClientMock_openOk(); - verify(ftpsClientMock, times(1)).isConnected(); - verify(ftpsClientMock, times(1)).logout(); - verify(ftpsClientMock, times(1)).disconnect(); - verify(ftpsClientMock, times(1)).retrieveFile(ArgumentMatchers.eq(REMOTE_FILE_PATH), any()); - verifyNoMoreInteractions(ftpsClientMock); - } - - @Test - public void collectFileFaultyOwnKey_shouldFail() throws Exception { - - doReturn(outputStreamMock).when(clientUnderTestSpy).createOutputStream(LOCAL_FILE_PATH); - assertThatThrownBy(() -> clientUnderTestSpy.open()) - .hasMessageContaining("Could not open connection: java.io.FileNotFoundException:"); - - verify(ftpsClientMock).setNeedClientAuth(true); - - doReturn(false).when(ftpsClientMock).isConnected(); - clientUnderTestSpy.close(); - verify(ftpsClientMock).isConnected(); - verifyNoMoreInteractions(ftpsClientMock); - } - - @Test - public void collectFileFaultTrustedCA_shouldFail_no_trustedCA_file() throws Exception { - - doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD); - doThrow(new IOException("problem")).when(clientUnderTestSpy).createInputStream(TRUSTED_CA_PATH); - - assertThatThrownBy(() -> clientUnderTestSpy.open()) - .hasMessage("Could not open connection: java.io.IOException: problem"); - } - - @Test - public void collectFileFaultTrustedCA_shouldFail_empty_trustedCA_file() throws Exception { - - doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD); - doReturn(inputStreamMock).when(clientUnderTestSpy).createInputStream(TRUSTED_CA_PATH); - - assertThatThrownBy(() -> clientUnderTestSpy.open()) - .hasMessage("Could not open connection: java.io.EOFException"); - } - - @Test - public void collectFileFaultyLogin_shouldFail() throws Exception { - - doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD); - doReturn(trustManagerMock).when(clientUnderTestSpy).getTrustManager(TRUSTED_CA_PATH, TRUSTED_CA_PASSWORD); - doReturn(outputStreamMock).when(clientUnderTestSpy).createOutputStream(LOCAL_FILE_PATH); - doReturn(false).when(ftpsClientMock).login(USERNAME, PASSWORD); - - assertThatThrownBy(() -> clientUnderTestSpy.open()).hasMessage("Unable to log in to xNF. 127.0.0.1"); - - verify(ftpsClientMock).setNeedClientAuth(true); - verify(ftpsClientMock).setKeyManager(keyManagerMock); - verify(ftpsClientMock).setTrustManager(trustManagerMock); - verify(ftpsClientMock).connect(XNF_ADDRESS, PORT); - verify(ftpsClientMock).login(USERNAME, PASSWORD); - } - - @Test - public void collectFileBadRequestResponse_shouldFail() throws Exception { - doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD); - doReturn(trustManagerMock).when(clientUnderTestSpy).getTrustManager(TRUSTED_CA_PATH, TRUSTED_CA_PASSWORD); - doReturn(outputStreamMock).when(clientUnderTestSpy).createOutputStream(LOCAL_FILE_PATH); - doReturn(true).when(ftpsClientMock).login(USERNAME, PASSWORD); - doReturn(503).when(ftpsClientMock).getReplyCode(); - - assertThatThrownBy(() -> clientUnderTestSpy.open()) - .hasMessage("Unable to connect to xNF. 127.0.0.1 xNF reply code: 503"); - - verify(ftpsClientMock).setNeedClientAuth(true); - verify(ftpsClientMock).setKeyManager(keyManagerMock); - verify(ftpsClientMock).setTrustManager(trustManagerMock); - verify(ftpsClientMock).connect(XNF_ADDRESS, PORT); - verify(ftpsClientMock).login(USERNAME, PASSWORD); - verify(ftpsClientMock, times(2)).getReplyCode(); - verifyNoMoreInteractions(ftpsClientMock); - } - - @Test - public void collectFile_shouldFail() throws Exception { - doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD); - doReturn(trustManagerMock).when(clientUnderTestSpy).getTrustManager(TRUSTED_CA_PATH, TRUSTED_CA_PASSWORD); - doReturn(outputStreamMock).when(clientUnderTestSpy).createOutputStream(LOCAL_FILE_PATH); - doReturn(true).when(ftpsClientMock).login(USERNAME, PASSWORD); - doReturn(HttpStatus.OK.value()).when(ftpsClientMock).getReplyCode(); - clientUnderTestSpy.open(); - - doReturn(false).when(ftpsClientMock).retrieveFile(REMOTE_FILE_PATH, outputStreamMock); - - assertThatThrownBy(() -> clientUnderTestSpy.collectFile(REMOTE_FILE_PATH, LOCAL_FILE_PATH)) - .hasMessageContaining(REMOTE_FILE_PATH).hasMessageContaining("No retry"); - - verifyFtpsClientMock_openOk(); - verify(ftpsClientMock, times(1)).retrieveFile(ArgumentMatchers.eq(REMOTE_FILE_PATH), any()); - verifyNoMoreInteractions(ftpsClientMock); - } - - @Test - public void collectFile_shouldFail_ioexception() throws Exception { - doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD); - doReturn(trustManagerMock).when(clientUnderTestSpy).getTrustManager(TRUSTED_CA_PATH, TRUSTED_CA_PASSWORD); - doReturn(outputStreamMock).when(clientUnderTestSpy).createOutputStream(LOCAL_FILE_PATH); - doReturn(true).when(ftpsClientMock).login(USERNAME, PASSWORD); - doReturn(HttpStatus.OK.value()).when(ftpsClientMock).getReplyCode(); - clientUnderTestSpy.open(); - when(ftpsClientMock.isConnected()).thenReturn(false); - - doThrow(new IOException("problem")).when(ftpsClientMock).retrieveFile(REMOTE_FILE_PATH, outputStreamMock); - - assertThatThrownBy(() -> clientUnderTestSpy.collectFile(REMOTE_FILE_PATH, LOCAL_FILE_PATH)) - .hasMessage("Could not fetch file: java.io.IOException: problem"); - - verifyFtpsClientMock_openOk(); - verify(ftpsClientMock, times(1)).retrieveFile(ArgumentMatchers.eq(REMOTE_FILE_PATH), any()); - verifyNoMoreInteractions(ftpsClientMock); - } -} diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/SchemeTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/SchemeTest.java index 82b5b229..b695f106 100644 --- a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/SchemeTest.java +++ b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/SchemeTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. All rights reserved. + * Copyright (C) 2020 Nokia. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +19,6 @@ package org.onap.dcaegen2.collectors.datafile.ftp; -import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -26,22 +26,20 @@ import org.junit.jupiter.api.Test; import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException; public class SchemeTest { - @Test - public void getSchemeFromString_properScheme() throws DatafileTaskException { - - Scheme actualScheme = Scheme.getSchemeFromString("FTPES"); - assertEquals(Scheme.FTPS, actualScheme); - actualScheme = Scheme.getSchemeFromString("FTPS"); - assertEquals(Scheme.FTPS, actualScheme); + @Test + public void shouldReturnSchemeForSupportedProtocol() throws DatafileTaskException { + assertEquals(Scheme.FTPES, Scheme.getSchemeFromString("FTPES")); + assertEquals(Scheme.SFTP, Scheme.getSchemeFromString("SFTP")); + } - actualScheme = Scheme.getSchemeFromString("SFTP"); - assertEquals(Scheme.SFTP, actualScheme); + @Test + public void shouldThrowExceptionForUnsupportedProtocol() { + assertThrows(DatafileTaskException.class, () -> Scheme.getSchemeFromString("FTPS")); } @Test - public void getSchemeFromString_invalidScheme() { - assertTrue(assertThrows(DatafileTaskException.class, () -> Scheme.getSchemeFromString("invalid")).getMessage() - .startsWith("DFC does not support protocol invalid")); + public void shouldThrowExceptionForInvalidProtocol() { + assertThrows(DatafileTaskException.class, () -> Scheme.getSchemeFromString("invalid")); } } diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/model/FileDataTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/model/FileDataTest.java index e4b528a9..99bf1cd4 100644 --- a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/model/FileDataTest.java +++ b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/model/FileDataTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018-2019 Nordix Foundation. All rights reserved. + * Copyright (C) 2020 Nokia. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -62,7 +63,7 @@ public class FileDataTest { .compression("comp") // .fileFormatType("type") // .fileFormatVersion("version") // - .scheme(Scheme.FTPS) // + .scheme(Scheme.FTPES) // .messageMetaData(messageMetaData()) // .build(); } @@ -74,7 +75,7 @@ public class FileDataTest { .compression("comp") // .fileFormatType("type") // .fileFormatVersion("version") // - .scheme(Scheme.FTPS) // + .scheme(Scheme.FTPES) // .messageMetaData(messageMetaData()) // .build(); } @@ -86,7 +87,7 @@ public class FileDataTest { .compression("comp") // .fileFormatType("type") // .fileFormatVersion("version") // - .scheme(Scheme.FTPS) // + .scheme(Scheme.FTPES) // .messageMetaData(messageMetaData()) // .build(); } diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/service/JsonMessageParserTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/service/JsonMessageParserTest.java index cf0cfecf..8fb8c364 100644 --- a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/service/JsonMessageParserTest.java +++ b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/service/JsonMessageParserTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 NOKIA Intellectual Property, 2018-2019 Nordix Foundation. All rights reserved. + * Copyright (C) 2020 Nokia. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -105,7 +106,7 @@ class JsonMessageParserTest { FileData expectedFileData = ImmutableFileData.builder() // .name(PM_FILE_NAME) // .location(LOCATION) // - .scheme(Scheme.FTPS) // + .scheme(Scheme.FTPES) // .compression(GZIP_COMPRESSION) // .fileFormatType(FILE_FORMAT_TYPE) // .fileFormatVersion(FILE_FORMAT_VERSION) // @@ -157,7 +158,7 @@ class JsonMessageParserTest { FileData expectedFileData = ImmutableFileData.builder() // .name(PM_FILE_NAME) // .location(LOCATION) // - .scheme(Scheme.FTPS) // + .scheme(Scheme.FTPES) // .compression(GZIP_COMPRESSION) // .fileFormatType(FILE_FORMAT_TYPE) // .fileFormatVersion(FILE_FORMAT_VERSION) // @@ -277,7 +278,7 @@ class JsonMessageParserTest { FileData expectedFileData = ImmutableFileData.builder() // .name(PM_FILE_NAME) // .location(LOCATION) // - .scheme(Scheme.FTPS) // + .scheme(Scheme.FTPES) // .compression(GZIP_COMPRESSION) // .fileFormatType(FILE_FORMAT_TYPE) // .fileFormatVersion(FILE_FORMAT_VERSION) // @@ -489,7 +490,7 @@ class JsonMessageParserTest { FileData expectedFileData = ImmutableFileData.builder() // .name(PM_FILE_NAME) // .location(LOCATION) // - .scheme(Scheme.FTPS) // + .scheme(Scheme.FTPES) // .compression(GZIP_COMPRESSION) // .fileFormatType(FILE_FORMAT_TYPE) // .fileFormatVersion(FILE_FORMAT_VERSION) // diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/DMaaPMessageConsumerTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/DMaaPMessageConsumerTest.java index a4319d37..d4dd89f0 100644 --- a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/DMaaPMessageConsumerTest.java +++ b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/DMaaPMessageConsumerTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2018 NOKIA Intellectual Property, 2018-2019 Nordix Foundation. + * Copyright (C) 2018 NOKIA Intellectual Property, 2018-2019 Nordix Foundation. + * Copyright (C) 2020 Nokia. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -147,7 +148,7 @@ public class DMaaPMessageConsumerTest { ftpesFileData = ImmutableFileData.builder() // .name(PM_FILE_NAME) // .location(FTPES_LOCATION) // - .scheme(Scheme.FTPS) // + .scheme(Scheme.FTPES) // .compression(GZIP_COMPRESSION) // .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE) // .fileFormatVersion(FILE_FORMAT_VERSION) // @@ -178,7 +179,7 @@ public class DMaaPMessageConsumerTest { sftpFileData = ImmutableFileData.builder() // .name(PM_FILE_NAME) // .location(SFTP_LOCATION) // - .scheme(Scheme.FTPS) // + .scheme(Scheme.FTPES) // .compression(GZIP_COMPRESSION) // .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE) // .fileFormatVersion(FILE_FORMAT_VERSION) // diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/FileCollectorTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/FileCollectorTest.java index e5523251..b7b69e8d 100644 --- a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/FileCollectorTest.java +++ b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/FileCollectorTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START====================================================================== * Copyright (C) 2018-2019 Nordix Foundation. All rights reserved. + * Copyright (C) 2020 Nokia. All rights reserved. * =============================================================================================== * 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 @@ -40,7 +41,7 @@ import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig; import org.onap.dcaegen2.collectors.datafile.configuration.FtpesConfig; import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException; import org.onap.dcaegen2.collectors.datafile.exceptions.NonRetryableDatafileTaskException; -import org.onap.dcaegen2.collectors.datafile.ftp.FtpsClient; +import org.onap.dcaegen2.collectors.datafile.ftp.FtpesClient; import org.onap.dcaegen2.collectors.datafile.ftp.Scheme; import org.onap.dcaegen2.collectors.datafile.ftp.SftpClient; import org.onap.dcaegen2.collectors.datafile.model.Counters; @@ -91,7 +92,7 @@ public class FileCollectorTest { private static AppConfig appConfigMock = mock(AppConfig.class); private static FtpesConfig ftpesConfigMock = mock(FtpesConfig.class); - private FtpsClient ftpsClientMock = mock(FtpsClient.class); + private FtpesClient ftpesClientMock = mock(FtpesClient.class); private SftpClient sftpClientMock = mock(SftpClient.class); private final Map contextMap = new HashMap<>(); @@ -159,9 +160,9 @@ public class FileCollectorTest { @Test public void whenFtpesFile_returnCorrectResponse() throws Exception { FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters)); - doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any()); + doReturn(ftpesClientMock).when(collectorUndetTest).createFtpesClient(any()); - FileData fileData = createFileData(FTPES_LOCATION_NO_PORT, Scheme.FTPS); + FileData fileData = createFileData(FTPES_LOCATION_NO_PORT, Scheme.FTPES); FilePublishInformation expectedfilePublishInformation = createExpectedFilePublishInformation(FTPES_LOCATION_NO_PORT); @@ -170,10 +171,10 @@ public class FileCollectorTest { .expectNext(expectedfilePublishInformation) // .verifyComplete(); - verify(ftpsClientMock, times(1)).open(); - verify(ftpsClientMock, times(1)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION); - verify(ftpsClientMock, times(1)).close(); - verifyNoMoreInteractions(ftpsClientMock); + verify(ftpesClientMock, times(1)).open(); + verify(ftpesClientMock, times(1)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION); + verify(ftpesClientMock, times(1)).close(); + verifyNoMoreInteractions(ftpesClientMock); assertEquals("collectedFiles should have been 1", 1, counters.getNoOfCollectedFiles()); assertEquals("failedFtpAttempts should have been 0", 0, counters.getNoOfFailedFtpAttempts()); @@ -211,17 +212,17 @@ public class FileCollectorTest { @Test public void whenFtpesFileAlwaysFail_retryAndFail() throws Exception { FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters)); - doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any()); + doReturn(ftpesClientMock).when(collectorUndetTest).createFtpesClient(any()); - FileData fileData = createFileData(FTPES_LOCATION, Scheme.FTPS); - doThrow(new DatafileTaskException("Unable to collect file.")).when(ftpsClientMock) + FileData fileData = createFileData(FTPES_LOCATION, Scheme.FTPES); + doThrow(new DatafileTaskException("Unable to collect file.")).when(ftpesClientMock) .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION); StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap)) .expectErrorMessage("Retries exhausted: 3/3") // .verify(); - verify(ftpsClientMock, times(4)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION); + verify(ftpesClientMock, times(4)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION); assertEquals("collectedFiles should have been 0", 0, counters.getNoOfCollectedFiles()); assertEquals("failedFtpAttempts should have been 4", 4, counters.getNoOfFailedFtpAttempts()); @@ -230,17 +231,17 @@ public class FileCollectorTest { @Test public void whenFtpesFileAlwaysFail_failWithoutRetry() throws Exception { FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters)); - doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any()); + doReturn(ftpesClientMock).when(collectorUndetTest).createFtpesClient(any()); - FileData fileData = createFileData(FTPES_LOCATION, Scheme.FTPS); - doThrow(new NonRetryableDatafileTaskException("Unable to collect file.")).when(ftpsClientMock) + FileData fileData = createFileData(FTPES_LOCATION, Scheme.FTPES); + doThrow(new NonRetryableDatafileTaskException("Unable to collect file.")).when(ftpesClientMock) .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION); StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap)) .expectErrorMessage("Non retryable file transfer failure") // .verify(); - verify(ftpsClientMock, times(1)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION); + verify(ftpesClientMock, times(1)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION); assertEquals("collectedFiles should have been 0", 0, counters.getNoOfCollectedFiles()); assertEquals("failedFtpAttempts should have been 1", 1, counters.getNoOfFailedFtpAttempts()); @@ -249,20 +250,20 @@ public class FileCollectorTest { @Test public void whenFtpesFileFailOnce_retryAndReturnCorrectResponse() throws Exception { FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters)); - doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any()); - doThrow(new DatafileTaskException("Unable to collect file.")).doNothing().when(ftpsClientMock) + doReturn(ftpesClientMock).when(collectorUndetTest).createFtpesClient(any()); + doThrow(new DatafileTaskException("Unable to collect file.")).doNothing().when(ftpesClientMock) .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION); FilePublishInformation expectedfilePublishInformation = createExpectedFilePublishInformation(FTPES_LOCATION_NO_PORT); - FileData fileData = createFileData(FTPES_LOCATION_NO_PORT, Scheme.FTPS); + FileData fileData = createFileData(FTPES_LOCATION_NO_PORT, Scheme.FTPES); StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap)) .expectNext(expectedfilePublishInformation) // .verifyComplete(); - verify(ftpsClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION); + verify(ftpesClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION); assertEquals("collectedFiles should have been 1", 1, counters.getNoOfCollectedFiles()); assertEquals("failedFtpAttempts should have been 1", 1, counters.getNoOfFailedFtpAttempts()); diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/ScheduledTasksTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/ScheduledTasksTest.java index c77f5fc2..1cb79bcf 100644 --- a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/ScheduledTasksTest.java +++ b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/ScheduledTasksTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2020 Nokia. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -149,7 +150,7 @@ public class ScheduledTasksTest { .fileFormatType("") // .fileFormatVersion("") // .location("ftpes://192.168.0.101/ftp/rop/" + PM_FILE_NAME + instanceNumber) // - .scheme(Scheme.FTPS) // + .scheme(Scheme.FTPES) // .compression("") // .messageMetaData(messageMetaData()) // .build(); -- cgit 1.2.3-korg