Merge "Added support for using oauth token for Kafka"
[nonrtric/plt/ranpm.git] / datafilecollector / src / test / java / org / onap / dcaegen2 / collectors / datafile / ftp / SftpClientSettingsTest.java
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018-2019 Nordix Foundation. All rights reserved.
4  * Copyright (C) 2020 Nokia. All rights reserved.
5  * ===============================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
7  * except in compliance with the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software distributed under the
12  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific language governing permissions
14  * and limitations under the License.
15  * ============LICENSE_END========================================================================
16  */
17
18 package org.onap.dcaegen2.collectors.datafile.ftp;
19
20 import static org.assertj.core.api.Assertions.assertThat;
21
22 import java.io.File;
23 import java.nio.file.Path;
24
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.io.TempDir;
27 import org.onap.dcaegen2.collectors.datafile.configuration.SftpConfig;
28
29 public class SftpClientSettingsTest {
30
31     @Test
32     public void shouldUseFtpStrictHostChecking(@TempDir Path tempDir) throws Exception {
33         File knowHostsFile = new File(tempDir.toFile(), "known_hosts");
34         knowHostsFile.createNewFile();
35
36         SftpConfig config = createSampleSftpConfigWithStrictHostChecking(knowHostsFile.getAbsolutePath());
37         SftpClientSettings sftpClient = new SftpClientSettings(config);
38
39         assertThat(sftpClient.shouldUseStrictHostChecking()).isTrue();
40     }
41
42     @Test
43     public void shouldNotUseFtpStrictHostChecking_whenFileDoesNotExist() {
44         SftpConfig config = createSampleSftpConfigWithStrictHostChecking("unknown_file");
45         SftpClientSettings sftpClient = new SftpClientSettings(config);
46
47         sftpClient.shouldUseStrictHostChecking();
48         assertThat(sftpClient.shouldUseStrictHostChecking()).isFalse();
49     }
50
51     @Test
52     public void shouldNotUseFtpStrictHostChecking_whenExplicitlySwitchedOff() {
53         SftpClientSettings sftpClient = new SftpClientSettings(createSampleSftpConfigNoStrictHostChecking());
54         sftpClient.shouldUseStrictHostChecking();
55         assertThat(sftpClient.shouldUseStrictHostChecking()).isFalse();
56     }
57
58     private SftpConfig createSampleSftpConfigNoStrictHostChecking() {
59         return SftpConfig.builder() //
60             .strictHostKeyChecking(false).knownHostsFilePath("N/A").build();
61     }
62
63     private SftpConfig createSampleSftpConfigWithStrictHostChecking(String pathToKnownHostsFile) {
64         return SftpConfig.builder() //
65             .strictHostKeyChecking(true).knownHostsFilePath(pathToKnownHostsFile).build();
66     }
67 }