diff options
author | 2019-03-26 10:22:23 +0100 | |
---|---|---|
committer | 2019-04-11 08:55:15 +0200 | |
commit | 314a1e4310545e5b70ff64e328f3e7eae281c5b4 (patch) | |
tree | 0a96b6a05676f219db1dbaaa8fa65e36b534f325 /datafile-dmaap-client/src | |
parent | 5983d76f162aef34740a05ae4e78c7d9e2b3c20a (diff) |
Housekeeping
No functional changes made in this commit.
Removed CheckStyle and Sonar warnings.
Formatted code.
Renamed methods and classes for better understanding.
Removed unnecessary classes.
Moved all code to single project.
Change-Id: Ie3feb6c6a985e94a382812aa083dcf57bc46c7b3
Issue-ID: DCAEGEN2-1367
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
Diffstat (limited to 'datafile-dmaap-client/src')
18 files changed, 0 insertions, 1663 deletions
diff --git a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FileCollectClient.java b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FileCollectClient.java deleted file mode 100644 index de50f24a..00000000 --- a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FileCollectClient.java +++ /dev/null @@ -1,29 +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.nio.file.Path; - -import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException; - -/** - * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a> - */ -public interface FileCollectClient extends AutoCloseable { - public void collectFile(String remoteFile, Path localFile) throws DatafileTaskException; - public void open() throws DatafileTaskException; -} diff --git a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FileServerData.java b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FileServerData.java deleted file mode 100644 index b080c320..00000000 --- a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FileServerData.java +++ /dev/null @@ -1,33 +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.util.Optional; - -import org.immutables.value.Value; - -/** - * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a> - * - */ -@Value.Immutable -public interface FileServerData { - public String serverAddress(); - public String userId(); - public String password(); - public Optional<Integer> port(); -} diff --git a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClient.java b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClient.java deleted file mode 100644 index 492768c2..00000000 --- a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClient.java +++ /dev/null @@ -1,189 +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.Path; -import java.security.GeneralSecurityException; -import java.security.KeyStore; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.cert.CertificateException; -import java.util.Optional; - -import javax.net.ssl.KeyManager; -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.apache.commons.net.util.KeyManagerUtils; -import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.core.io.FileSystemResource; - -/** - * Gets file from PNF with FTPS protocol. - * - * @author <a href="mailto:martin.c.yan@est.tech">Martin Yan</a> - */ -public class FtpsClient implements FileCollectClient { - private static final Logger logger = LoggerFactory.getLogger(FtpsClient.class); - FTPSClient realFtpsClient = new FTPSClient(); - private final FileServerData fileServerData; - private static TrustManager theTrustManager = null; - - private final String keyCertPath; - private final String keyCertPassword; - private final Path trustedCAPath; - private final String trustedCAPassword; - - public FtpsClient(FileServerData fileServerData, String keyCertPath, String keyCertPassword, Path trustedCAPath, - String trustedCAPassword) { - this.fileServerData = fileServerData; - this.keyCertPath = keyCertPath; - this.keyCertPassword = keyCertPassword; - this.trustedCAPath = trustedCAPath; - this.trustedCAPassword = trustedCAPassword; - } - - @Override - public void open() throws DatafileTaskException { - try { - realFtpsClient.setNeedClientAuth(true); - realFtpsClient.setKeyManager(createKeyManager(keyCertPath, keyCertPassword)); - realFtpsClient.setTrustManager(getTrustManager(trustedCAPath, trustedCAPassword)); - 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 DatafileTaskException("Could not retrieve file " + remoteFileName); - } - } catch (IOException e) { - throw new DatafileTaskException("Could not fetch file: " + e, e); - } - logger.trace("collectFile fetched: {}", localFileName); - } - - private int getPort(Optional<Integer> port) { - final int FTPS_DEFAULT_PORT = 21; - 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.debug("File {} opened xNF", localFileName); - return output; - } - - protected TrustManager getTrustManager(Path trustedCAPath, String trustedCAPassword) - throws KeyStoreException, NoSuchAlgorithmException, IOException, CertificateException { - synchronized (FtpsClient.class) { - if (theTrustManager == null) { - theTrustManager = createTrustManager(trustedCAPath, trustedCAPassword); - } - return theTrustManager; - } - } - - protected KeyManager createKeyManager(String keyCertPath, String keyCertPassword) - throws IOException, GeneralSecurityException { - return KeyManagerUtils.createClientKeyManager(new File(keyCertPath), keyCertPassword); - } -} diff --git a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/Scheme.java b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/Scheme.java deleted file mode 100644 index d469da66..00000000 --- a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/Scheme.java +++ /dev/null @@ -1,51 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 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 org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException; - -/** - * Enum specifying the schemes that DFC support for downloading files. - * - * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a> - * - */ -public enum Scheme { - FTPS, SFTP; - - /** - * Get a <code>Scheme</code> from a string. - * - * @param schemeString the string to convert to <code>Scheme</code>. - * @return The corresponding <code>Scheme</code> - * @throws Exception if the value of the string doesn't match any defined scheme. - */ - public static Scheme getSchemeFromString(String schemeString) throws DatafileTaskException { - Scheme result; - if ("FTPS".equalsIgnoreCase(schemeString) || "FTPES".equalsIgnoreCase(schemeString)) { - result = Scheme.FTPS; - } else if ("SFTP".equalsIgnoreCase(schemeString)) { - result = Scheme.SFTP; - } else { - throw new DatafileTaskException("DFC does not support protocol " + schemeString - + ". Supported protocols are FTPES , FTPS, and SFTP"); - } - return result; - } -} diff --git a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClient.java b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClient.java deleted file mode 100644 index 4517a755..00000000 --- a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClient.java +++ /dev/null @@ -1,108 +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 com.jcraft.jsch.Channel; -import com.jcraft.jsch.ChannelSftp; -import com.jcraft.jsch.JSch; -import com.jcraft.jsch.JSchException; -import com.jcraft.jsch.Session; - -import java.nio.file.Path; -import java.util.Optional; - -import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Gets file from xNF with SFTP protocol. - * - * @author <a href="mailto:martin.c.yan@est.tech">Martin Yan</a> - * - */ -public class SftpClient implements FileCollectClient { - private static final Logger logger = LoggerFactory.getLogger(SftpClient.class); - private final FileServerData fileServerData; - private Session session = null; - private ChannelSftp sftpChannel = null; - - public SftpClient(FileServerData fileServerData) { - this.fileServerData = fileServerData; - } - - @Override - public void collectFile(String remoteFile, Path localFile) throws DatafileTaskException { - logger.trace("collectFile {}", localFile); - - try { - sftpChannel.get(remoteFile, localFile.toString()); - logger.debug("File {} Download Successfull from xNF", localFile.getFileName()); - } catch (Exception e) { - throw new DatafileTaskException("Unable to get file from xNF. Data: " + fileServerData, e); - } - - logger.trace("collectFile OK"); - } - - @Override - public void close() { - logger.trace("closing sftp session"); - if (sftpChannel != null) { - sftpChannel.exit(); - sftpChannel = null; - } - if (session != null) { - session.disconnect(); - session = null; - } - } - - @Override - public void open() throws DatafileTaskException { - try { - if (session == null) { - session = setUpSession(fileServerData); - sftpChannel = getChannel(session); - } - } catch (JSchException e) { - throw new DatafileTaskException("Could not open Sftp client" + e, e); - } - } - - private int getPort(Optional<Integer> port) { - final int FTPS_DEFAULT_PORT = 22; - return port.isPresent() ? port.get() : FTPS_DEFAULT_PORT; - } - - private Session setUpSession(FileServerData fileServerData) throws JSchException { - JSch jsch = new JSch(); - - Session newSession = jsch.getSession(fileServerData.userId(), fileServerData.serverAddress(), - getPort(fileServerData.port())); - newSession.setConfig("StrictHostKeyChecking", "no"); - newSession.setPassword(fileServerData.password()); - newSession.connect(); - return newSession; - } - - private ChannelSftp getChannel(Session session) throws JSchException { - Channel channel = session.openChannel("sftp"); - channel.connect(); - return (ChannelSftp) channel; - } -} diff --git a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/http/HttpAsyncClientBuilderWrapper.java b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/http/HttpAsyncClientBuilderWrapper.java deleted file mode 100644 index e01a941b..00000000 --- a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/http/HttpAsyncClientBuilderWrapper.java +++ /dev/null @@ -1,67 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.dcaegen2.collectors.datafile.http; - -import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.SSLContext; - -import org.apache.http.client.RedirectStrategy; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; -import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; -import org.apache.http.impl.nio.client.HttpAsyncClients; - -/** - * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a> - */ -public class HttpAsyncClientBuilderWrapper implements IHttpAsyncClientBuilder { - HttpAsyncClientBuilder builder = HttpAsyncClients.custom(); - - @Override - public IHttpAsyncClientBuilder setRedirectStrategy(RedirectStrategy redirectStrategy) { - builder.setRedirectStrategy(redirectStrategy); - return this; - } - - @Override - public IHttpAsyncClientBuilder setSSLContext(SSLContext sslcontext) { - builder.setSSLContext(sslcontext); - return this; - } - - @Override - public IHttpAsyncClientBuilder setSSLHostnameVerifier(HostnameVerifier hostnameVerifier) { - builder.setSSLHostnameVerifier(hostnameVerifier); - return this; - } - - @Override - public IHttpAsyncClientBuilder setDefaultRequestConfig(RequestConfig config) { - builder.setDefaultRequestConfig(config); - return this; - } - - @Override - public CloseableHttpAsyncClient build() { - return builder.build(); - } - -} diff --git a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/http/IHttpAsyncClientBuilder.java b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/http/IHttpAsyncClientBuilder.java deleted file mode 100644 index e0a51a80..00000000 --- a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/http/IHttpAsyncClientBuilder.java +++ /dev/null @@ -1,43 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.dcaegen2.collectors.datafile.http; - -import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.SSLContext; - -import org.apache.http.client.RedirectStrategy; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; - -/** - * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a> - */ -public interface IHttpAsyncClientBuilder { - public IHttpAsyncClientBuilder setRedirectStrategy(final RedirectStrategy redirectStrategy); - - public IHttpAsyncClientBuilder setSSLContext(final SSLContext sslcontext); - - public IHttpAsyncClientBuilder setSSLHostnameVerifier(final HostnameVerifier hostnameVerifier); - - public IHttpAsyncClientBuilder setDefaultRequestConfig(final RequestConfig config); - - public CloseableHttpAsyncClient build(); -} diff --git a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/service/DmaapReactiveWebClient.java b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/service/DmaapReactiveWebClient.java deleted file mode 100644 index 23fd0bc7..00000000 --- a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/service/DmaapReactiveWebClient.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * ============LICENSE_START====================================================================== - * Copyright (C) 2018 NOKIA Intellectual Property, 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.service; - -import static org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext.RESPONSE_CODE; -import static org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext.SERVICE_NAME; -import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication; - -import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapCustomConfig; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.MDC; -import org.springframework.http.HttpHeaders; -import org.springframework.web.reactive.function.client.ExchangeFilterFunction; -import org.springframework.web.reactive.function.client.WebClient; -import org.springframework.web.reactive.function.client.WebClient.Builder; - -import reactor.core.publisher.Mono; - -/** - * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18 - */ -public class DmaapReactiveWebClient { - - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - - private String dmaaPContentType; - private String dmaaPUserName; - private String dmaaPUserPassword; - - /** - * Creating DmaapReactiveWebClient passing to them basic DmaapConfig. - * - * @param dmaapCustomConfig - configuration object - * @return DmaapReactiveWebClient - */ - public DmaapReactiveWebClient fromConfiguration(DmaapCustomConfig dmaapCustomConfig) { - this.dmaaPContentType = dmaapCustomConfig.dmaapContentType(); - return this; - } - - /** - * Construct Reactive WebClient with appropriate settings. - * - * @return WebClient - */ - public WebClient build() { - Builder webClientBuilder = WebClient.builder() - .defaultHeader(HttpHeaders.CONTENT_TYPE, dmaaPContentType) // - .filter(logRequest()) // - .filter(logResponse()); - if (dmaaPUserName != null && !dmaaPUserName.isEmpty() && dmaaPUserPassword != null - && !dmaaPUserPassword.isEmpty()) { - webClientBuilder.filter(basicAuthentication(dmaaPUserName, dmaaPUserPassword)); - } - return webClientBuilder.build(); - } - - private ExchangeFilterFunction logResponse() { - return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> { - MDC.put(RESPONSE_CODE, String.valueOf(clientResponse.statusCode())); - logger.trace("Response Status {}", clientResponse.statusCode()); - MDC.remove(RESPONSE_CODE); - return Mono.just(clientResponse); - }); - } - - private ExchangeFilterFunction logRequest() { - return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> { - MDC.put(SERVICE_NAME, String.valueOf(clientRequest.url())); - logger.trace("Request: {} {}", clientRequest.method(), clientRequest.url()); - clientRequest.headers() - .forEach((name, values) -> values.forEach(value -> logger.trace("{}={}", name, value))); - logger.trace("HTTP request headers: {}", clientRequest.headers()); - MDC.remove(SERVICE_NAME); - return Mono.just(clientRequest); - }); - } - -} diff --git a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/service/HttpUtils.java b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/service/HttpUtils.java deleted file mode 100644 index 1e1187ac..00000000 --- a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/service/HttpUtils.java +++ /dev/null @@ -1,30 +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.service; - -import org.apache.http.HttpStatus; - -public final class HttpUtils implements HttpStatus { - - private HttpUtils() {} - - public static boolean isSuccessfulResponseCode(Integer statusCode) { - return statusCode >= 200 && statusCode < 300; - } -} diff --git a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerHttpClient.java b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerHttpClient.java deleted file mode 100644 index b0904b29..00000000 --- a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerHttpClient.java +++ /dev/null @@ -1,152 +0,0 @@ -/* - * ============LICENSE_START====================================================================== - * Copyright (C) 2018 NOKIA Intellectual Property, 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.service.producer; - -import java.nio.charset.StandardCharsets; -import java.security.KeyManagementException; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.time.Duration; -import java.util.Map; -import java.util.concurrent.Future; - -import javax.net.ssl.SSLContext; - -import org.apache.commons.codec.binary.Base64; -import org.apache.http.HttpResponse; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.methods.HttpUriRequest; -import org.apache.http.conn.ssl.NoopHostnameVerifier; -import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; -import org.apache.http.ssl.SSLContextBuilder; -import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException; -import org.onap.dcaegen2.collectors.datafile.http.HttpAsyncClientBuilderWrapper; -import org.onap.dcaegen2.collectors.datafile.http.IHttpAsyncClientBuilder; -import org.onap.dcaegen2.collectors.datafile.web.PublishRedirectStrategy; -import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.MDC; -import org.slf4j.Marker; -import org.slf4j.MarkerFactory; -import org.springframework.web.util.DefaultUriBuilderFactory; -import org.springframework.web.util.UriBuilder; - -/** - * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18 - * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a> - */ -public class DmaapProducerHttpClient { - - private static final Duration DEFAULT_REQUEST_TIMEOUT = Duration.ofMinutes(2); - private static final Marker INVOKE = MarkerFactory.getMarker("INVOKE"); - private static final Marker INVOKE_RETURN = MarkerFactory.getMarker("INVOKE_RETURN"); - - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - - private final DmaapPublisherConfiguration configuration; - - /** - * Constructor DmaapProducerReactiveHttpClient. - * - * @param dmaapPublisherConfiguration - DMaaP producer configuration object - */ - public DmaapProducerHttpClient(DmaapPublisherConfiguration dmaapPublisherConfiguration) { - this.configuration = dmaapPublisherConfiguration; - } - - public HttpResponse getDmaapProducerResponseWithRedirect(HttpUriRequest request, Map<String, String> contextMap) - throws DatafileTaskException { - MDC.setContextMap(contextMap); - try (CloseableHttpAsyncClient webClient = createWebClient(true, DEFAULT_REQUEST_TIMEOUT)) { - webClient.start(); - - logger.trace(INVOKE, "Starting to produce to DR {}", request); - Future<HttpResponse> future = webClient.execute(request, null); - HttpResponse response = future.get(); - logger.trace(INVOKE_RETURN, "Response from DR {}", response); - return response; - } catch (Exception e) { - throw new DatafileTaskException("Unable to create web client.", e); - } - } - - public HttpResponse getDmaapProducerResponseWithCustomTimeout(HttpUriRequest request, Duration requestTimeout, - Map<String, String> contextMap) throws DatafileTaskException { - MDC.setContextMap(contextMap); - try (CloseableHttpAsyncClient webClient = createWebClient(false, requestTimeout)) { - webClient.start(); - - logger.trace(INVOKE, "Starting to produce to DR {}", request); - Future<HttpResponse> future = webClient.execute(request, null); - HttpResponse response = future.get(); - logger.trace(INVOKE_RETURN, "Response from DR {}", response); - return response; - } catch (Exception e) { - throw new DatafileTaskException("Unable to create web client.", e); - } - } - - public void addUserCredentialsToHead(HttpUriRequest request) { - String plainCreds = configuration.dmaapUserName() + ":" + configuration.dmaapUserPassword(); - byte[] plainCredsBytes = plainCreds.getBytes(StandardCharsets.ISO_8859_1); - byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes); - String base64Creds = new String(base64CredsBytes); - logger.trace("base64Creds...: {}", base64Creds); - request.addHeader("Authorization", "Basic " + base64Creds); - } - - public UriBuilder getBaseUri() { - return new DefaultUriBuilderFactory().builder() // - .scheme(configuration.dmaapProtocol()) // - .host(configuration.dmaapHostName()) // - .port(configuration.dmaapPortNumber()); - } - - private CloseableHttpAsyncClient createWebClient(boolean expectRedirect, Duration requestTimeout) - throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException { - SSLContext sslContext = - new SSLContextBuilder().loadTrustMaterial(null, (certificate, authType) -> true).build(); - - IHttpAsyncClientBuilder clientBuilder = getHttpClientBuilder(); - clientBuilder.setSSLContext(sslContext) // - .setSSLHostnameVerifier(new NoopHostnameVerifier()); - - if (expectRedirect) { - clientBuilder.setRedirectStrategy(PublishRedirectStrategy.INSTANCE); - } - - if (requestTimeout.toMillis() > 0) { - int millis = (int)requestTimeout.toMillis(); - RequestConfig requestConfig = RequestConfig.custom() // - .setSocketTimeout(millis) // - .setConnectTimeout(millis) // - .setConnectionRequestTimeout(millis) // - .build(); - - clientBuilder.setDefaultRequestConfig(requestConfig); - } else { - logger.error("WEB client without timeout created {}", requestTimeout); - } - - return clientBuilder.build(); - } - - IHttpAsyncClientBuilder getHttpClientBuilder() { - return new HttpAsyncClientBuilderWrapper(); - } -}
\ No newline at end of file diff --git a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/web/PublishRedirectStrategy.java b/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/web/PublishRedirectStrategy.java deleted file mode 100644 index e002c284..00000000 --- a/datafile-dmaap-client/src/main/java/org/onap/dcaegen2/collectors/datafile/web/PublishRedirectStrategy.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * ============LICENSE_START====================================================================== - * Copyright (C) 2018 NOKIA Intellectual Property, 2018 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.web; - -import java.net.URI; - -import org.apache.http.HttpRequest; -import org.apache.http.HttpResponse; -import org.apache.http.ProtocolException; -import org.apache.http.annotation.Contract; -import org.apache.http.annotation.ThreadingBehavior; -import org.apache.http.client.methods.HttpDelete; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpHead; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.methods.HttpPut; -import org.apache.http.client.methods.HttpUriRequest; -import org.apache.http.client.methods.RequestBuilder; -import org.apache.http.impl.client.DefaultRedirectStrategy; -import org.apache.http.protocol.HttpContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * PublishRedirectStrategy implementation - * that automatically redirects all HEAD, GET, POST, PUT, and DELETE requests. - * This strategy relaxes restrictions on automatic redirection of - * POST methods imposed by the HTTP specification. - * - */ -@Contract(threading = ThreadingBehavior.IMMUTABLE) -public class PublishRedirectStrategy extends DefaultRedirectStrategy { - - public static final PublishRedirectStrategy INSTANCE = new PublishRedirectStrategy(); - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - - /** - * Redirectable methods. - */ - private static final String[] REDIRECT_METHODS = new String[] { - HttpPut.METHOD_NAME, - HttpGet.METHOD_NAME, - HttpPost.METHOD_NAME, - HttpHead.METHOD_NAME, - HttpDelete.METHOD_NAME - }; - - @Override - protected boolean isRedirectable(final String method) { - for (final String m: REDIRECT_METHODS) { - if (m.equalsIgnoreCase(method)) { - return true; - } - } - return false; - } - - @Override - public HttpUriRequest getRedirect( - final HttpRequest request, - final HttpResponse response, - final HttpContext context) throws ProtocolException { - final URI uri = getLocationURI(request, response, context); - logger.trace("getRedirect...: {}", request); - return RequestBuilder.copy(request).setUri(uri).build(); - } - -} diff --git a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClientTest.java b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClientTest.java deleted file mode 100644 index 9e6c29f8..00000000 --- a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClientTest.java +++ /dev/null @@ -1,234 +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(), 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).createKeyManager(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).createKeyManager(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).createKeyManager(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).createKeyManager(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).createKeyManager(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).createKeyManager(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)) - .hasMessage("Could not retrieve file /dir/sample.txt"); - - 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).createKeyManager(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-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/SchemeTest.java b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/SchemeTest.java deleted file mode 100644 index 162a0e78..00000000 --- a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/SchemeTest.java +++ /dev/null @@ -1,51 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 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.junit.Assert.assertTrue; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import org.junit.jupiter.api.Test; -import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException; - -/** - * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a> - * - */ -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); - - actualScheme = Scheme.getSchemeFromString("SFTP"); - assertEquals(Scheme.SFTP, actualScheme); - } - - @Test - public void getSchemeFromString_invalidScheme() { - assertTrue(assertThrows(DatafileTaskException.class, () -> Scheme.getSchemeFromString("invalid")).getMessage() - .startsWith("DFC does not support protocol invalid")); - } -} diff --git a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClientTest.java b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClientTest.java deleted file mode 100644 index 85a0c090..00000000 --- a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClientTest.java +++ /dev/null @@ -1,135 +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 java.nio.charset.StandardCharsets.UTF_8; -import static org.apache.commons.io.IOUtils.toByteArray; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import com.github.stefanbirkner.fakesftpserver.rule.FakeSftpServerRule; -import com.jcraft.jsch.ChannelSftp; -import com.jcraft.jsch.JSch; -import com.jcraft.jsch.JSchException; -import com.jcraft.jsch.Session; -import com.jcraft.jsch.SftpException; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; - -import org.junit.Rule; -import org.junit.Test; -import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException; - -public class SftpClientTest { - private static final String USERNAME = "bob"; - private static final String PASSWORD = "123"; - private static final String DUMMY_CONTENT = "dummy content"; - private static final Path LOCAL_DUMMY_FILE = Paths.get("target/dummy.txt"); - private static final String REMOTE_DUMMY_FILE = "/dummy_directory/dummy_file.txt"; - private static final JSch JSCH = new JSch(); - private static final int TIMEOUT = 2000; - - @Rule - public final FakeSftpServerRule sftpServer = new FakeSftpServerRule().addUser(USERNAME, PASSWORD); - - @Test - public void collectFile_withOKresponse() - throws DatafileTaskException, IOException, JSchException, SftpException, Exception { - FileServerData expectedFileServerData = ImmutableFileServerData.builder().serverAddress("127.0.0.1") - .userId(USERNAME).password(PASSWORD).port(sftpServer.getPort()).build(); - try (SftpClient sftpClient = new SftpClient(expectedFileServerData)) { - sftpClient.open(); - sftpServer.putFile(REMOTE_DUMMY_FILE, DUMMY_CONTENT, UTF_8); - byte[] file = downloadFile(sftpServer, REMOTE_DUMMY_FILE); - - sftpClient.collectFile(REMOTE_DUMMY_FILE, LOCAL_DUMMY_FILE); - byte[] localFile = Files.readAllBytes(LOCAL_DUMMY_FILE.toFile().toPath()); - assertThat(new String(file, UTF_8)).isEqualTo(DUMMY_CONTENT); - assertThat(new String(localFile, UTF_8)).isEqualTo(DUMMY_CONTENT); - } - } - - @Test - public void collectFile_withWrongUserName_shouldFail() throws DatafileTaskException, IOException { - FileServerData expectedFileServerData = ImmutableFileServerData.builder().serverAddress("127.0.0.1") - .userId("wrong").password(PASSWORD).port(sftpServer.getPort()).build(); - try (SftpClient sftpClient = new SftpClient(expectedFileServerData)) { - - sftpServer.putFile(REMOTE_DUMMY_FILE, DUMMY_CONTENT, UTF_8); - - - assertThatThrownBy(() -> sftpClient.open()) - .hasMessageContaining("Could not open Sftp clientcom.jcraft.jsch.JSchException: Auth fail"); - } - } - - @Test - public void collectFile_withWrongFileName_shouldFail() - throws IOException, JSchException, SftpException, DatafileTaskException { - FileServerData expectedFileServerData = ImmutableFileServerData.builder().serverAddress("127.0.0.1") - .userId(USERNAME).password(PASSWORD).port(sftpServer.getPort()).build(); - try (SftpClient sftpClient = new SftpClient(expectedFileServerData)) { - sftpServer.putFile(REMOTE_DUMMY_FILE, DUMMY_CONTENT, UTF_8); - sftpClient.open(); - - assertThatThrownBy(() -> sftpClient.collectFile("wrong", LOCAL_DUMMY_FILE)).hasMessageStartingWith( - "Unable to get file from xNF. Data: FileServerData{serverAddress=127.0.0.1, " - + "userId=bob, password=123, port="); - } - } - - private static Session connectToServer(FakeSftpServerRule sftpServer) throws JSchException { - return connectToServerAtPort(sftpServer.getPort()); - } - - private static Session connectToServerAtPort(int port) throws JSchException { - Session session = createSessionWithCredentials(USERNAME, PASSWORD, port); - session.connect(TIMEOUT); - return session; - } - - private static ChannelSftp connectSftpChannel(Session session) throws JSchException { - ChannelSftp channel = (ChannelSftp) session.openChannel("sftp"); - channel.connect(); - return channel; - } - - private static Session createSessionWithCredentials(String username, String password, int port) - throws JSchException { - Session session = JSCH.getSession(username, "127.0.0.1", port); - session.setConfig("StrictHostKeyChecking", "no"); - session.setPassword(password); - return session; - } - - private static byte[] downloadFile(FakeSftpServerRule server, String path) - throws JSchException, SftpException, IOException { - Session session = connectToServer(server); - ChannelSftp channel = connectSftpChannel(session); - try { - InputStream is = channel.get(path); - return toByteArray(is); - } finally { - channel.disconnect(); - session.disconnect(); - } - } -} diff --git a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/service/DmaapReactiveWebClientTest.java b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/service/DmaapReactiveWebClientTest.java deleted file mode 100644 index 54db7401..00000000 --- a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/service/DmaapReactiveWebClientTest.java +++ /dev/null @@ -1,54 +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.service; - -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.Mock; - - -import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration; -import org.springframework.web.reactive.function.client.WebClient; - -class DmaapReactiveWebClientTest { - - @Mock - private DmaapConsumerConfiguration dmaapConsumerConfiguration; - - @BeforeEach - void setUp() { - dmaapConsumerConfiguration = mock(DmaapConsumerConfiguration.class); - } - - - @Test - void buildsDMaaPReactiveWebClientProperly() { - when(dmaapConsumerConfiguration.dmaapContentType()).thenReturn("*/*"); - WebClient dmaapReactiveWebClient = new DmaapReactiveWebClient() - .fromConfiguration(dmaapConsumerConfiguration) - .build(); - - verify(dmaapConsumerConfiguration, times(1)).dmaapContentType(); - assertNotNull(dmaapReactiveWebClient); - } -}
\ No newline at end of file diff --git a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/service/HttpUtilsTest.java b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/service/HttpUtilsTest.java deleted file mode 100644 index c973a120..00000000 --- a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/service/HttpUtilsTest.java +++ /dev/null @@ -1,35 +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.service; - -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import org.junit.jupiter.api.Test; - -class HttpUtilsTest { - - @Test - public void shouldReturnSuccessfulResponse() { - assertTrue(HttpUtils.isSuccessfulResponseCode(200)); - } - - @Test - public void shouldReturnBadResponse() { - assertFalse(HttpUtils.isSuccessfulResponseCode(404)); - } -}
\ No newline at end of file diff --git a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerHttpClientTest.java b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerHttpClientTest.java deleted file mode 100644 index 92a14997..00000000 --- a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerHttpClientTest.java +++ /dev/null @@ -1,202 +0,0 @@ -/* - * ============LICENSE_START====================================================================== - * Copyright (C) 2018 NOKIA Intellectual Property, 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.service.producer; - -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; - -import java.net.URI; -import java.nio.charset.StandardCharsets; -import java.security.KeyManagementException; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.time.Duration; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.Future; - -import javax.net.ssl.SSLContext; - -import org.apache.commons.codec.binary.Base64; -import org.apache.http.Header; -import org.apache.http.HttpResponse; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPut; -import org.apache.http.client.methods.HttpUriRequest; -import org.apache.http.conn.ssl.NoopHostnameVerifier; -import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; -import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException; -import org.onap.dcaegen2.collectors.datafile.http.IHttpAsyncClientBuilder; -import org.onap.dcaegen2.collectors.datafile.web.PublishRedirectStrategy; -import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration; - -/** - * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18 - * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a> - */ -class DmaapProducerHttpClientTest { - - private static final String HOST = "54.45.33.2"; - private static final String HTTPS_SCHEME = "https"; - private static final int PORT = 1234; - private static final String USER_NAME = "dradmin"; - private static final Duration TWO_SECOND_TIMEOUT = Duration.ofSeconds(2); - - private static final Map<String, String> CONTEXT_MAP = new HashMap<>(); - - - private DmaapProducerHttpClient producerClientUnderTestSpy; - - private DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock(DmaapPublisherConfiguration.class); - - private IHttpAsyncClientBuilder clientBuilderMock; - - private CloseableHttpAsyncClient clientMock; - @SuppressWarnings("unchecked") - private Future<HttpResponse> futureMock = mock(Future.class); - - @BeforeEach - void setUp() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException { - when(dmaapPublisherConfigurationMock.dmaapHostName()).thenReturn(HOST); - when(dmaapPublisherConfigurationMock.dmaapProtocol()).thenReturn(HTTPS_SCHEME); - when(dmaapPublisherConfigurationMock.dmaapPortNumber()).thenReturn(PORT); - when(dmaapPublisherConfigurationMock.dmaapUserName()).thenReturn("dradmin"); - when(dmaapPublisherConfigurationMock.dmaapUserPassword()).thenReturn("dradmin"); - - producerClientUnderTestSpy = spy(new DmaapProducerHttpClient(dmaapPublisherConfigurationMock)); - - clientBuilderMock = mock(IHttpAsyncClientBuilder.class); - clientMock = mock(CloseableHttpAsyncClient.class); - } - - @Test - void getHttpResponseWithRederict_Success() throws Exception { - doReturn(clientBuilderMock).when(producerClientUnderTestSpy).getHttpClientBuilder(); - when(clientBuilderMock.setSSLContext(any(SSLContext.class))).thenReturn(clientBuilderMock); - when(clientBuilderMock.setSSLHostnameVerifier(any(NoopHostnameVerifier.class))).thenReturn(clientBuilderMock); - when(clientBuilderMock.build()).thenReturn(clientMock); - when(clientMock.execute(any(HttpUriRequest.class), any())).thenReturn(futureMock); - HttpResponse responseMock = mock(HttpResponse.class); - when(futureMock.get()).thenReturn(responseMock); - - HttpGet request = new HttpGet(); - producerClientUnderTestSpy.getDmaapProducerResponseWithRedirect(request, CONTEXT_MAP); - - verify(clientBuilderMock).setSSLContext(any(SSLContext.class)); - verify(clientBuilderMock).setSSLHostnameVerifier(any(NoopHostnameVerifier.class)); - verify(clientBuilderMock).setRedirectStrategy(PublishRedirectStrategy.INSTANCE); - verify(clientBuilderMock).setDefaultRequestConfig(any()); - verify(clientBuilderMock).build(); - verifyNoMoreInteractions(clientBuilderMock); - - verify(clientMock).start(); - verify(clientMock).close(); - - verify(futureMock).get(); - verifyNoMoreInteractions(futureMock); - } - - @Test - void getHttpResponseWithCustomTimeout_Success() throws Exception { - doReturn(clientBuilderMock).when(producerClientUnderTestSpy).getHttpClientBuilder(); - when(clientBuilderMock.setSSLContext(any(SSLContext.class))).thenReturn(clientBuilderMock); - when(clientBuilderMock.setDefaultRequestConfig(any(RequestConfig.class))).thenReturn(clientBuilderMock); - when(clientBuilderMock.build()).thenReturn(clientMock); - when(clientMock.execute(any(HttpUriRequest.class), any())).thenReturn(futureMock); - HttpResponse responseMock = mock(HttpResponse.class); - when(futureMock.get()).thenReturn(responseMock); - - HttpGet request = new HttpGet(); - producerClientUnderTestSpy.getDmaapProducerResponseWithCustomTimeout(request, TWO_SECOND_TIMEOUT, CONTEXT_MAP); - - ArgumentCaptor<RequestConfig> requestConfigCaptor = ArgumentCaptor.forClass(RequestConfig.class); - verify(clientBuilderMock).setSSLContext(any(SSLContext.class)); - verify(clientBuilderMock).setSSLHostnameVerifier(any(NoopHostnameVerifier.class)); - verify(clientBuilderMock).setDefaultRequestConfig(requestConfigCaptor.capture()); - RequestConfig requestConfig = requestConfigCaptor.getValue(); - assertEquals(TWO_SECOND_TIMEOUT.toMillis(), requestConfig.getSocketTimeout()); - assertEquals(TWO_SECOND_TIMEOUT.toMillis(), requestConfig.getConnectTimeout()); - assertEquals(TWO_SECOND_TIMEOUT.toMillis(), requestConfig.getConnectionRequestTimeout()); - verify(clientBuilderMock).build(); - verifyNoMoreInteractions(clientBuilderMock); - - verify(clientMock).start(); - verify(clientMock).close(); - - verify(futureMock).get(); - verifyNoMoreInteractions(futureMock); - } - - @Test - public void getResponseWithException_throwsException() throws Exception { - doReturn(clientBuilderMock).when(producerClientUnderTestSpy).getHttpClientBuilder(); - when(clientBuilderMock.setDefaultRequestConfig(any(RequestConfig.class))).thenReturn(clientBuilderMock); - when(clientBuilderMock.setSSLContext(any(SSLContext.class))).thenReturn(clientBuilderMock); - when(clientBuilderMock.build()).thenReturn(clientMock); - HttpPut request = new HttpPut(); - when(clientMock.execute(any(HttpPut.class), any())).thenReturn(futureMock); - - try { - when(futureMock.get()).thenThrow(new InterruptedException("Interrupted")); - - producerClientUnderTestSpy.getDmaapProducerResponseWithCustomTimeout(request, TWO_SECOND_TIMEOUT, - CONTEXT_MAP); - - fail("Should have got an exception."); - } catch (DatafileTaskException e) { - assertTrue(e.getCause() instanceof InterruptedException); - assertEquals("Interrupted", e.getCause().getMessage()); - } catch (Exception e) { - fail("Wrong exception"); - } - - verify(clientMock).start(); - verify(clientMock).close(); - } - - @Test - public void addCredentialsToHead_success() { - HttpPut request = new HttpPut(); - - producerClientUnderTestSpy.addUserCredentialsToHead(request); - - String plainCreds = USER_NAME + ":" + USER_NAME; - byte[] plainCredsBytes = plainCreds.getBytes(StandardCharsets.ISO_8859_1); - byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes); - String base64Creds = "Basic " + new String(base64CredsBytes); - Header[] authorizationHeaders = request.getHeaders("Authorization"); - assertEquals(base64Creds, authorizationHeaders[0].getValue()); - } - - @Test - public void getBaseUri_success() { - URI uri = producerClientUnderTestSpy.getBaseUri().build(); - assertEquals(HTTPS_SCHEME + "://" + HOST + ":" + PORT, uri.toString()); - } -} diff --git a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/web/PublishRedirectStrategyTest.java b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/web/PublishRedirectStrategyTest.java deleted file mode 100644 index 0ed3c72e..00000000 --- a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/web/PublishRedirectStrategyTest.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * ============LICENSE_START====================================================================== - * Copyright (C) 2018 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.web; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import org.apache.http.Header; -import org.apache.http.HttpRequest; -import org.apache.http.HttpResponse; -import org.apache.http.ProtocolException; -import org.apache.http.RequestLine; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.methods.HttpUriRequest; -import org.apache.http.client.protocol.HttpClientContext; -import org.apache.http.protocol.HttpContext; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -class PublishRedirectStrategyTest { - - private static final String URI = "sftp://localhost:80/"; - - private static PublishRedirectStrategy publishRedirectStrategy; - - - @BeforeAll - static void setUp() { - publishRedirectStrategy = new PublishRedirectStrategy(); - } - - @Test - void isRedirectable_shouldReturnTrue() { - Assertions.assertTrue(publishRedirectStrategy.isRedirectable("Put")); - } - - @Test - void isRedirectable_shouldReturnFalse() { - Assertions.assertFalse(publishRedirectStrategy.isRedirectable("not valid method")); - } - - @Test - void getRedirect_shouldReturnCorrectUri() throws ProtocolException { - HttpRequest requestMock = mock(HttpRequest.class); - HttpResponse responseMock = mock(HttpResponse.class); - HttpContext contextMock = mock(HttpContext.class); - Header headerMock = mock(Header.class); - when(responseMock.getFirstHeader("location")).thenReturn(headerMock); - when(headerMock.getValue()).thenReturn(URI); - RequestConfig requestConfigMock = mock(RequestConfig.class); - when(contextMock.getAttribute(HttpClientContext.REQUEST_CONFIG)).thenReturn(requestConfigMock); - RequestLine requestLineMock = mock(RequestLine.class); - when(requestMock.getRequestLine()).thenReturn(requestLineMock); - when(requestLineMock.getUri()).thenReturn(URI); - - HttpUriRequest actualRedirect = publishRedirectStrategy.getRedirect(requestMock, responseMock, contextMock); - assertEquals(URI, actualRedirect.getURI().toString()); - } -} |