diff options
Diffstat (limited to 'datafile-app-server/src/test/java')
5 files changed, 167 insertions, 255 deletions
diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/configuration/CloudConfigParserTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/configuration/CloudConfigParserTest.java index 60968c05..119224b4 100644 --- a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/configuration/CloudConfigParserTest.java +++ b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/configuration/CloudConfigParserTest.java @@ -1,28 +1,25 @@ /* - * ============LICENSE_START======================================================= - * Copyright (C) 2018 NOKIA Intellectual Property. 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 + * ============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 + * 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========================================================= + * 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.configuration; - import static org.assertj.core.api.Assertions.assertThat; -import com.google.gson.Gson; import com.google.gson.JsonObject; + import org.junit.jupiter.api.Test; import org.onap.dcaegen2.collectors.datafile.config.DmaapConsumerConfiguration; import org.onap.dcaegen2.collectors.datafile.config.DmaapPublisherConfiguration; @@ -31,74 +28,74 @@ import org.onap.dcaegen2.collectors.datafile.config.ImmutableDmaapPublisherConfi class CloudConfigParserTest { - private static final String CORRECT_JSON = - "{\"dmaap.dmaapProducerConfiguration.dmaapTopicName\": \"/events/unauthenticated.VES_NOTIFICATION_OUTPUT\", " - + "\"dmaap.dmaapConsumerConfiguration.timeoutMS\": -1," - + " \"dmaap.dmaapConsumerConfiguration.dmaapHostName\": \"message-router.onap.svc.cluster.local\"," - + "\"dmaap.dmaapConsumerConfiguration.dmaapUserName\": \"admin\", " - + "\"dmaap.dmaapProducerConfiguration.dmaapPortNumber\": 3904, " - + "\"dmaap.dmaapConsumerConfiguration.dmaapUserPassword\": \"admin\", " - + "\"dmaap.dmaapProducerConfiguration.dmaapProtocol\": \"http\", " - + "\"dmaap.dmaapProducerConfiguration.dmaapContentType\": \"application/json\", " - + "\"dmaap.dmaapConsumerConfiguration.dmaapTopicName\": \"/events/unauthenticated.VES_NOTIFICATION_OUTPUT\", " - + "\"dmaap.dmaapConsumerConfiguration.dmaapPortNumber\": 3904, " - + "\"dmaap.dmaapConsumerConfiguration.dmaapContentType\": \"application/json\", " - + "\"dmaap.dmaapConsumerConfiguration.messageLimit\": -1, " - + "\"dmaap.dmaapConsumerConfiguration.dmaapProtocol\": \"http\", " - + "\"dmaap.dmaapConsumerConfiguration.consumerId\": \"c12\"," - + "\"dmaap.dmaapProducerConfiguration.dmaapHostName\": \"message-router.onap.svc.cluster.local\", " - + "\"dmaap.dmaapConsumerConfiguration.consumerGroup\": \"OpenDCAE-c12\", " - + "\"dmaap.dmaapProducerConfiguration.dmaapUserName\": \"admin\", " - + "\"dmaap.dmaapProducerConfiguration.dmaapUserPassword\": \"admin\"}"; - - private static final ImmutableDmaapConsumerConfiguration correctDmaapConsumerConfig = - new ImmutableDmaapConsumerConfiguration.Builder() - .timeoutMS(-1) - .dmaapHostName("message-router.onap.svc.cluster.local") - .dmaapUserName("admin") - .dmaapUserPassword("admin") - .dmaapTopicName("/events/unauthenticated.VES_NOTIFICATION_OUTPUT") - .dmaapPortNumber(3904) - .dmaapContentType("application/json") - .messageLimit(-1) - .dmaapProtocol("http") - .consumerId("c12") - .consumerGroup("OpenDCAE-c12") - .build(); - - private static final ImmutableDmaapPublisherConfiguration correctDmaapPublisherConfig = - new ImmutableDmaapPublisherConfiguration.Builder() - .dmaapTopicName("/events/unauthenticated.VES_NOTIFICATION_OUTPUT") - .dmaapUserPassword("admin") - .dmaapPortNumber(3904) - .dmaapProtocol("http") - .dmaapContentType("application/json") - .dmaapHostName("message-router.onap.svc.cluster.local") - .dmaapUserName("admin") - .build(); - - private CloudConfigParser cloudConfigParser = new CloudConfigParser( - new Gson().fromJson(CORRECT_JSON, JsonObject.class)); + private static final ImmutableDmaapConsumerConfiguration CORRECT_DMAAP_CONSUMER_CONFIG = + new ImmutableDmaapConsumerConfiguration.Builder().timeoutMS(-1) + .dmaapHostName("message-router.onap.svc.cluster.local").dmaapUserName("admin") + .dmaapUserPassword("admin").dmaapTopicName("/events/unauthenticated.VES_NOTIFICATION_OUTPUT") + .dmaapPortNumber(2222).dmaapContentType("application/json").messageLimit(-1).dmaapProtocol("http") + .consumerId("C12").consumerGroup("OpenDCAE-c12").build(); + + private static final ImmutableDmaapPublisherConfiguration CORRECT_DMAAP_PUBLISHER_CONFIG = + new ImmutableDmaapPublisherConfiguration.Builder().dmaapTopicName("publish").dmaapUserPassword("dradmin") + .dmaapPortNumber(3907).dmaapProtocol("https").dmaapContentType("application/json") + .dmaapHostName("message-router.onap.svc.cluster.local").dmaapUserName("dradmin").build(); + private static final ImmutableFtpesConfig CORRECT_FTPES_CONFIGURATION = + new ImmutableFtpesConfig.Builder().keyCert("/config/ftpKey.jks").keyPassword("secret") + .trustedCA("config/cacerts").trustedCAPassword("secret").build(); + + private CloudConfigParser cloudConfigParser = new CloudConfigParser(getCloudConfigJsonObject()); @Test public void shouldCreateDmaapConsumerConfigurationCorrectly() { - // when DmaapConsumerConfiguration dmaapConsumerConfig = cloudConfigParser.getDmaapConsumerConfig(); - // then assertThat(dmaapConsumerConfig).isNotNull(); - assertThat(dmaapConsumerConfig).isEqualToComparingFieldByField(correctDmaapConsumerConfig); + assertThat(dmaapConsumerConfig).isEqualToComparingFieldByField(CORRECT_DMAAP_CONSUMER_CONFIG); } - @Test public void shouldCreateDmaapPublisherConfigurationCorrectly() { - // when DmaapPublisherConfiguration dmaapPublisherConfig = cloudConfigParser.getDmaapPublisherConfig(); - // then assertThat(dmaapPublisherConfig).isNotNull(); - assertThat(dmaapPublisherConfig).isEqualToComparingFieldByField(correctDmaapPublisherConfig); + assertThat(dmaapPublisherConfig).isEqualToComparingFieldByField(CORRECT_DMAAP_PUBLISHER_CONFIG); + } + + @Test + public void shouldCreateFtpesConfigurationCorrectly() { + FtpesConfig ftpesConfig = cloudConfigParser.getFtpesConfig(); + + assertThat(ftpesConfig).isNotNull(); + assertThat(ftpesConfig).isEqualToComparingFieldByField(CORRECT_FTPES_CONFIGURATION); + } + + public JsonObject getCloudConfigJsonObject() { + JsonObject config = new JsonObject(); + config.addProperty("dmaap.dmaapConsumerConfiguration.timeoutMS", -1); + config.addProperty("dmaap.dmaapConsumerConfiguration.dmaapHostName", "message-router.onap.svc.cluster.local"); + config.addProperty("dmaap.dmaapConsumerConfiguration.dmaapUserName", "admin"); + config.addProperty("dmaap.dmaapConsumerConfiguration.dmaapUserPassword", "admin"); + config.addProperty("dmaap.dmaapConsumerConfiguration.dmaapTopicName", + "/events/unauthenticated.VES_NOTIFICATION_OUTPUT"); + config.addProperty("dmaap.dmaapConsumerConfiguration.dmaapPortNumber", 2222); + config.addProperty("dmaap.dmaapConsumerConfiguration.dmaapContentType", "application/json"); + config.addProperty("dmaap.dmaapConsumerConfiguration.messageLimit", -1); + config.addProperty("dmaap.dmaapConsumerConfiguration.dmaapProtocol", "http"); + config.addProperty("dmaap.dmaapConsumerConfiguration.consumerId", "C12"); + config.addProperty("dmaap.dmaapConsumerConfiguration.consumerGroup", "OpenDCAE-c12"); + config.addProperty("dmaap.dmaapProducerConfiguration.dmaapTopicName", "publish"); + config.addProperty("dmaap.dmaapProducerConfiguration.dmaapProtocol", "https"); + config.addProperty("dmaap.dmaapProducerConfiguration.dmaapContentType", "application/json"); + config.addProperty("dmaap.dmaapProducerConfiguration.dmaapHostName", "message-router.onap.svc.cluster.local"); + config.addProperty("dmaap.dmaapProducerConfiguration.dmaapPortNumber", 3907); + config.addProperty("dmaap.dmaapProducerConfiguration.dmaapUserName", "dradmin"); + config.addProperty("dmaap.dmaapProducerConfiguration.dmaapUserPassword", "dradmin"); + config.addProperty("dmaap.ftpesConfig.keyCert", "/config/ftpKey.jks"); + config.addProperty("dmaap.ftpesConfig.keyPassword", "secret"); + config.addProperty("dmaap.ftpesConfig.trustedCA", "config/cacerts"); + config.addProperty("dmaap.ftpesConfig.trustedCAPassword", "secret"); + + return config; } -}
\ No newline at end of file +} diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/configuration/DatafileAppConfigTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/configuration/DatafileAppConfigTest.java index 12c8c7e7..1238a472 100644 --- a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/configuration/DatafileAppConfigTest.java +++ b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/configuration/DatafileAppConfigTest.java @@ -26,6 +26,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import com.google.gson.JsonElement; +import com.google.gson.JsonObject; import com.google.gson.JsonParser; import java.io.ByteArrayInputStream; @@ -48,23 +49,8 @@ import org.onap.dcaegen2.collectors.datafile.integration.junit5.mockito.MockitoE class DatafileAppConfigTest { private static final String DATAFILE_ENDPOINTS = "datafile_endpoints.json"; - private static final String JSON_STRING = - "{\"configs\":{\"dmaap\":{\"dmaapConsumerConfiguration\":{\"consumerGroup\":\"other\",\"consumerId\":\"1\"," - + "\"dmaapContentType\":\"application/json\",\"dmaapHostName\":\"localhost\"," - + "\"dmaapPortNumber\":2222,\"dmaapProtocol\":\"http\",\"dmaapTopicName\":\"temp\"," - + "\"dmaapUserName\":\"admin\",\"dmaapUserPassword\":\"admin\",\"messageLimit\":1000," - + "\"timeoutMS\":1000},\"dmaapProducerConfiguration\":{\"dmaapContentType\":\"application/json\"," - + "\"dmaapHostName\":\"localhost\",\"dmaapPortNumber\":2223,\"dmaapProtocol\":\"http\"," - + "\"dmaapTopicName\":\"temp\",\"dmaapUserName\":\"admin\",\"dmaapUserPassword\":\"admin\"}}}}"; - private static final String INCORRECT_JSON_STRING = - "{\"configs\":{\"dmaap\":{\"dmaapConsumerConfiguration\":{\"consumerGroup\":\"other\",\"consumerId\":\"1\"," - + "\"dmaapContentType\":\"application/json\",\"dmaapHostName\":\"localhost\"," - + "\"dmaapPortNumber\":2222,\"dmaapProtocol\":\"http\",\"dmaapTopicName\":\"temp\"," - + "\"dmaapUserName\":\"admin\",\"dmaapUserPassword\":\"admin\",\"messageLimit\":1000," - + "\"timeoutMS\":1000},\"dmaapProducerConfiguration\":{\"dmaapContentType\":\"application/json\"," - + "\"dmaapHostName\":\"localhost\",\"dmaapPortNumber\":2223,\"dmaapProtocol\":\"http\"," - + "\"FAULTY_PARAMETER_NAME\":\"temp\"," - + "\"dmaapUserName\":\"admin\",\"dmaapUserPassword\":\"admin\"}}}}"; + private static final boolean CORRECT_JSON = true; + private static final boolean INCORRECT_JSON = false; private static DatafileAppConfig datafileAppConfig; private static AppConfig appConfig; @@ -90,9 +76,10 @@ class DatafileAppConfigTest { } @Test - public void whenTheConfigurationFits_GetAaiAndDmaapObjectRepresentationConfiguration() throws IOException { + public void whenTheConfigurationFits_GetFtpsAndDmaapObjectRepresentationConfiguration() throws IOException { // Given - InputStream inputStream = new ByteArrayInputStream((JSON_STRING.getBytes(StandardCharsets.UTF_8))); + InputStream inputStream = + new ByteArrayInputStream((getJsonConfig(CORRECT_JSON).getBytes(StandardCharsets.UTF_8))); // When datafileAppConfig.setFilepath(filePath); @@ -100,6 +87,7 @@ class DatafileAppConfigTest { datafileAppConfig.initFileStreamReader(); appConfig.dmaapConsumerConfiguration = datafileAppConfig.getDmaapConsumerConfiguration(); appConfig.dmaapPublisherConfiguration = datafileAppConfig.getDmaapPublisherConfiguration(); + appConfig.ftpesConfig = datafileAppConfig.getFtpesConfiguration(); // Then verify(datafileAppConfig, times(1)).setFilepath(anyString()); @@ -110,6 +98,7 @@ class DatafileAppConfigTest { datafileAppConfig.getDmaapPublisherConfiguration()); Assertions.assertEquals(appConfig.getDmaapConsumerConfiguration(), datafileAppConfig.getDmaapConsumerConfiguration()); + Assertions.assertEquals(appConfig.getFtpesConfiguration(), datafileAppConfig.getFtpesConfiguration()); } @@ -127,13 +116,15 @@ class DatafileAppConfigTest { verify(datafileAppConfig, times(1)).initFileStreamReader(); Assertions.assertNull(datafileAppConfig.getDmaapConsumerConfiguration()); Assertions.assertNull(datafileAppConfig.getDmaapPublisherConfiguration()); + Assertions.assertNull(datafileAppConfig.getFtpesConfiguration()); } @Test public void whenFileIsExistsButJsonIsIncorrect() throws IOException { // Given - InputStream inputStream = new ByteArrayInputStream((INCORRECT_JSON_STRING.getBytes(StandardCharsets.UTF_8))); + InputStream inputStream = + new ByteArrayInputStream((getJsonConfig(INCORRECT_JSON).getBytes(StandardCharsets.UTF_8))); // When datafileAppConfig.setFilepath(filePath); @@ -145,6 +136,7 @@ class DatafileAppConfigTest { verify(datafileAppConfig, times(1)).initFileStreamReader(); Assertions.assertNotNull(datafileAppConfig.getDmaapConsumerConfiguration()); Assertions.assertNull(datafileAppConfig.getDmaapPublisherConfiguration()); + Assertions.assertNotNull(datafileAppConfig.getFtpesConfiguration()); } @@ -152,7 +144,8 @@ class DatafileAppConfigTest { @Test public void whenTheConfigurationFits_ButRootElementIsNotAJsonObject() throws IOException { // Given - InputStream inputStream = new ByteArrayInputStream((JSON_STRING.getBytes(StandardCharsets.UTF_8))); + InputStream inputStream = + new ByteArrayInputStream((getJsonConfig(CORRECT_JSON).getBytes(StandardCharsets.UTF_8))); // When datafileAppConfig.setFilepath(filePath); doReturn(inputStream).when(datafileAppConfig).getInputStream(any()); @@ -162,11 +155,61 @@ class DatafileAppConfigTest { datafileAppConfig.initFileStreamReader(); appConfig.dmaapConsumerConfiguration = datafileAppConfig.getDmaapConsumerConfiguration(); appConfig.dmaapPublisherConfiguration = datafileAppConfig.getDmaapPublisherConfiguration(); + appConfig.ftpesConfig = datafileAppConfig.getFtpesConfiguration(); // Then verify(datafileAppConfig, times(1)).setFilepath(anyString()); verify(datafileAppConfig, times(1)).initFileStreamReader(); Assertions.assertNull(datafileAppConfig.getDmaapConsumerConfiguration()); Assertions.assertNull(datafileAppConfig.getDmaapPublisherConfiguration()); + Assertions.assertNull(datafileAppConfig.getFtpesConfiguration()); + } + + private String getJsonConfig(boolean correct) { + JsonObject dmaapConsumerConfigData = new JsonObject(); + dmaapConsumerConfigData.addProperty("dmaapHostName", "localhost"); + dmaapConsumerConfigData.addProperty("dmaapPortNumber", 2222); + dmaapConsumerConfigData.addProperty("dmaapTopicName", "/events/unauthenticated.VES_NOTIFICATION_OUTPUT"); + dmaapConsumerConfigData.addProperty("dmaapProtocol", "http"); + dmaapConsumerConfigData.addProperty("dmaapUserName", "admin"); + dmaapConsumerConfigData.addProperty("dmaapUserPassword", "admin"); + dmaapConsumerConfigData.addProperty("dmaapContentType", "application/json"); + dmaapConsumerConfigData.addProperty("consumerId", "C12"); + dmaapConsumerConfigData.addProperty("consumerGroup", "OpenDcae-c12"); + dmaapConsumerConfigData.addProperty("timeoutMS", -1); + dmaapConsumerConfigData.addProperty("messageLimit", 1); + + JsonObject dmaapProducerConfigData = new JsonObject(); + dmaapProducerConfigData.addProperty("dmaapHostName", "localhost"); + dmaapProducerConfigData.addProperty("dmaapPortNumber", 3907); + dmaapProducerConfigData.addProperty("dmaapTopicName", "publish"); + dmaapProducerConfigData.addProperty("dmaapProtocol", "https"); + if (correct) { + dmaapProducerConfigData.addProperty("dmaapUserName", "dradmin"); + dmaapProducerConfigData.addProperty("dmaapUserPassword", "dradmin"); + dmaapProducerConfigData.addProperty("dmaapContentType", "application/octet-stream"); + } + + JsonObject dmaapConfigs = new JsonObject(); + dmaapConfigs.add("dmaapConsumerConfiguration", dmaapConsumerConfigData); + dmaapConfigs.add("dmaapProducerConfiguration", dmaapProducerConfigData); + + JsonObject ftpesConfigData = new JsonObject(); + ftpesConfigData.addProperty("keyCert", "config/ftpKey.jks"); + ftpesConfigData.addProperty("keyPassword", "secret"); + ftpesConfigData.addProperty("trustedCA", "config/cacerts"); + ftpesConfigData.addProperty("trustedCAPassword", "secret"); + + JsonObject ftpesConfiguration = new JsonObject(); + ftpesConfiguration.add("ftpesConfiguration", ftpesConfigData); + + JsonObject configs = new JsonObject(); + configs.add("dmaap", dmaapConfigs); + configs.add("ftp", ftpesConfiguration); + + JsonObject completeJson = new JsonObject(); + completeJson.add("configs", configs); + + return completeJson.toString(); } } diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpClientTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpClientTest.java deleted file mode 100644 index a95b80ed..00000000 --- a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpClientTest.java +++ /dev/null @@ -1,57 +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.ftp; - -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.mockftpserver.fake.FakeFtpServer; -import org.mockftpserver.fake.UserAccount; -import org.mockftpserver.fake.filesystem.FileEntry; -import org.mockftpserver.fake.filesystem.FileSystem; -import org.mockftpserver.fake.filesystem.UnixFakeFileSystem; - -public class FtpClientTest { - - private static final String HOME_DIR = "/"; - private static final String FILE = "/dir/sample.txt"; - private static final String CONTENTS = "abcdef 1234567890"; - private static final int PORT = 8021; - - private static final String USERNAME = "bob"; - private static final String PASSWORD = "123"; - - private FakeFtpServer fakeFtpServer; - - @BeforeAll - protected void setUp() throws Exception { - fakeFtpServer = new FakeFtpServer(); - fakeFtpServer.setServerControlPort(PORT); - - FileSystem fileSystem = new UnixFakeFileSystem(); - fileSystem.add(new FileEntry(FILE, CONTENTS)); - fakeFtpServer.setFileSystem(fileSystem); - UserAccount userAccount = new UserAccount(USERNAME, PASSWORD, HOME_DIR); - fakeFtpServer.addUserAccount(userAccount); - - fakeFtpServer.start(); - } - - @AfterAll - protected void tearDown() throws Exception { - fakeFtpServer.stop(); - } -} diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClientTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClientTest.java deleted file mode 100644 index 13f1fbb9..00000000 --- a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClientTest.java +++ /dev/null @@ -1,103 +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.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 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.File; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; - -import org.junit.Rule; -import org.junit.Test; - -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 String LOCAL_DUMMY_FILE = "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 IOException, JSchException, SftpException { - SftpClient sftpClient = new SftpClient(); - sftpServer.putFile(REMOTE_DUMMY_FILE, DUMMY_CONTENT, UTF_8); - byte[] file = downloadFile(sftpServer, REMOTE_DUMMY_FILE); - FileServerData expectedFileServerData = ImmutableFileServerData.builder().serverAddress("127.0.0.1") - .userId(USERNAME).password(PASSWORD).port(sftpServer.getPort()).build(); - sftpClient.collectFile(expectedFileServerData, REMOTE_DUMMY_FILE, - LOCAL_DUMMY_FILE); - byte[] localFile = Files.readAllBytes(new File(LOCAL_DUMMY_FILE).toPath()); - assertThat(new String(file, UTF_8)).isEqualTo(DUMMY_CONTENT); - assertThat(new String(localFile, UTF_8)).isEqualTo(DUMMY_CONTENT); - } - - 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-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/XnfCollectorTaskImplTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/XnfCollectorTaskImplTest.java index 528a481c..3a3f16c0 100644 --- a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/XnfCollectorTaskImplTest.java +++ b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/XnfCollectorTaskImplTest.java @@ -24,7 +24,10 @@ import static org.mockito.Mockito.when; import java.io.File; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig; +import org.onap.dcaegen2.collectors.datafile.configuration.FtpesConfig; import org.onap.dcaegen2.collectors.datafile.ftp.FileServerData; import org.onap.dcaegen2.collectors.datafile.ftp.FtpsClient; import org.onap.dcaegen2.collectors.datafile.ftp.ImmutableFileServerData; @@ -60,14 +63,31 @@ public class XnfCollectorTaskImplTest { private static final String MEAS_COLLECT_FILE_FORMAT_TYPE = "org.3GPP.32.435#measCollec"; private static final String FILE_FORMAT_VERSION = "V10"; + private static final String FTP_KEY_PATH = "ftpKeyPath"; + private static final String FTP_KEY_PASSWORD = "ftpKeyPassword"; + private static final String TRUSTED_CA_PATH = "trustedCAPath"; + private static final String TRUSTED_CA_PASSWORD = "trustedCAPassword"; + + private static AppConfig appConfigMock = mock(AppConfig.class); + private static FtpesConfig ftpesConfigMock = mock(FtpesConfig.class); + private FtpsClient ftpsClientMock = mock(FtpsClient.class); private SftpClient sftpClientMock = mock(SftpClient.class); - private XnfCollectorTask collectorUndetTest = new XnfCollectorTaskImpl(ftpsClientMock, sftpClientMock); + private XnfCollectorTask collectorUndetTest = new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock); + + @BeforeAll + public static void setUpConfiguration() { + when(appConfigMock.getFtpesConfiguration()).thenReturn(ftpesConfigMock); + when(ftpesConfigMock.keyCert()).thenReturn(FTP_KEY_PATH); + when(ftpesConfigMock.keyPassword()).thenReturn(FTP_KEY_PASSWORD); + when(ftpesConfigMock.trustedCA()).thenReturn(TRUSTED_CA_PATH); + when(ftpesConfigMock.trustedCAPassword()).thenReturn(TRUSTED_CA_PASSWORD); + } @Test - public void whenSingleFtpesFile_returnCorrectResponse() { + public void whenFtpesFile_returnCorrectResponse() { FileData fileData = ImmutableFileData.builder().changeIdentifier(PM_MEAS_CHANGE_IDINTIFIER) .changeType(FILE_READY_CHANGE_TYPE).name(PM_FILE_NAME).location(FTPES_LOCATION) .compression(GZIP_COMPRESSION).fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE) @@ -86,11 +106,15 @@ public class XnfCollectorTaskImplTest { .verifyComplete(); verify(ftpsClientMock, times(1)).collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION); + verify(ftpsClientMock).setKeyCertPath(FTP_KEY_PATH); + verify(ftpsClientMock).setKeyCertPassword(FTP_KEY_PASSWORD); + verify(ftpsClientMock).setTrustedCAPath(TRUSTED_CA_PATH); + verify(ftpsClientMock).setTrustedCAPassword(TRUSTED_CA_PASSWORD); verifyNoMoreInteractions(ftpsClientMock); } @Test - public void whenSingleSftpFile_returnCorrectResponse() { + public void whenSftpFile_returnCorrectResponse() { FileData fileData = ImmutableFileData.builder().changeIdentifier(PM_MEAS_CHANGE_IDINTIFIER) .changeType(FILE_READY_CHANGE_TYPE).name(PM_FILE_NAME).location(SFTP_LOCATION) .compression(GZIP_COMPRESSION).fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE) @@ -109,6 +133,10 @@ public class XnfCollectorTaskImplTest { .verifyComplete(); verify(sftpClientMock, times(1)).collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION); + verify(ftpsClientMock).setKeyCertPath(FTP_KEY_PATH); + verify(ftpsClientMock).setKeyCertPassword(FTP_KEY_PASSWORD); + verify(ftpsClientMock).setTrustedCAPath(TRUSTED_CA_PATH); + verify(ftpsClientMock).setTrustedCAPassword(TRUSTED_CA_PASSWORD); verifyNoMoreInteractions(ftpsClientMock); } @@ -126,6 +154,10 @@ public class XnfCollectorTaskImplTest { StepVerifier.create(collectorUndetTest.execute(fileData)).expectNextCount(0).verifyComplete(); + verify(ftpsClientMock).setKeyCertPath(FTP_KEY_PATH); + verify(ftpsClientMock).setKeyCertPassword(FTP_KEY_PASSWORD); + verify(ftpsClientMock).setTrustedCAPath(TRUSTED_CA_PATH); + verify(ftpsClientMock).setTrustedCAPassword(TRUSTED_CA_PASSWORD); verifyNoMoreInteractions(ftpsClientMock); } } |