aboutsummaryrefslogtreecommitdiffstats
path: root/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors
diff options
context:
space:
mode:
Diffstat (limited to 'datafile-app-server/src/test/java/org/onap/dcaegen2/collectors')
-rw-r--r--datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/configuration/AppConfigTest.java24
-rw-r--r--datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/configuration/CloudConfigParserTest.java78
-rw-r--r--datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/FileCollectorTest.java37
3 files changed, 136 insertions, 3 deletions
diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/configuration/AppConfigTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/configuration/AppConfigTest.java
index 839a9a18..3c940297 100644
--- a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/configuration/AppConfigTest.java
+++ b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/configuration/AppConfigTest.java
@@ -1,7 +1,7 @@
/*-
* ============LICENSE_START======================================================================
- * Copyright (C) 2018, 2020-2021 NOKIA Intellectual Property, 2018-2019 Nordix Foundation.
- * All rights reserved.
+ * Copyright (C) 2018, 2020-2022 Nokia. All rights reserved.
+ * 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
@@ -91,6 +91,7 @@ class AppConfigTest {
.trustedCa("/src/test/resources/cert.jks") //
.trustedCaPasswordPath("/src/test/resources/cert.jks.pass") //
.httpsHostnameVerify(true)
+ .enableCertAuth(true)
.build();
private AppConfig appConfigUnderTest;
@@ -129,6 +130,19 @@ class AppConfigTest {
}
@Test
+ void shouldInitializeApplicationWithoutCertificates() throws IOException {
+ // When
+ doReturn(getCorrectConfigWithoutTLS()).when(appConfigUnderTest).createInputStream(any());
+ appConfigUnderTest.initialize();
+
+ // Then
+ verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
+
+ CertificateConfig certificateConfig = appConfigUnderTest.getCertificateConfiguration();
+ assertThat(certificateConfig).isNotNull();
+ }
+
+ @Test
void whenTheConfigurationFits_twoProducers() throws IOException, DatafileTaskException {
// When
doReturn(getCorrectJsonTwoProducers()).when(appConfigUnderTest).createInputStream(any());
@@ -293,6 +307,12 @@ class AppConfigTest {
return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
}
+ private static InputStream getCorrectConfigWithoutTLS() throws IOException {
+ URL url = CloudConfigParser.class.getClassLoader().getResource("datafile_test_config_no_tls.json");
+ String string = Resources.toString(url, Charsets.UTF_8);
+ return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
+ }
+
private static InputStream getCorrectJsonTwoProducers() throws IOException {
URL url = CloudConfigParser.class.getClassLoader().getResource("datafile_endpoints_test_2producers.json");
String string = Resources.toString(url, Charsets.UTF_8);
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
new file mode 100644
index 00000000..e7ef7d79
--- /dev/null
+++ b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/configuration/CloudConfigParserTest.java
@@ -0,0 +1,78 @@
+/*-
+ * ============LICENSE_START======================================================================
+ * Copyright (C) 2022 Nokia. All rights reserved.
+ * ===============================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ * ============LICENSE_END========================================================================
+ */
+
+package org.onap.dcaegen2.collectors.datafile.configuration;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import org.junit.jupiter.api.Test;
+import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
+
+class CloudConfigParserTest {
+
+ public static final String CONFIG_TLS_JSON = "src/test/resources/datafile_test_config_tls.json";
+ public static final String CONFIG_NO_TLS_JSON = "src/test/resources/datafile_test_config_no_tls.json";
+ public static final String INCORRECT_CERT_CONFIG_JSON = "src/test/resources/datafile_test_config_incorrect_cert_config.json";
+ public static final String EXPECTED_EXCEPTION_MESSAGE = "Wrong configuration. External certificate enabled but configs are missing: Could not find member: dmaap.certificateConfig.keyCert";
+
+ @Test
+ public void shouldCorrectReadCertificateConfigWithTLS () throws IOException, DatafileTaskException {
+
+ CloudConfigParser parser = getCloudConfigParser(CONFIG_TLS_JSON);
+ CertificateConfig certificateConfig = parser.getCertificateConfig();
+
+ assertEquals(true, certificateConfig.enableCertAuth());
+ assertEquals("/src/test/resources/dfc.jks", certificateConfig.keyCert());
+ assertEquals("/src/test/resources/dfc.jks.pass", certificateConfig.keyPasswordPath());
+ assertEquals("/src/test/resources/cert.jks", certificateConfig.trustedCa());
+ assertEquals("/src/test/resources/cert.jks.pass", certificateConfig.trustedCaPasswordPath());
+ assertEquals(true, certificateConfig.httpsHostnameVerify());
+ }
+
+ @Test
+ public void shouldCorrectReadCertificateConfigWithoutTLS () throws IOException, DatafileTaskException {
+ CloudConfigParser parser = getCloudConfigParser(CONFIG_NO_TLS_JSON);
+ CertificateConfig certificateConfig = parser.getCertificateConfig();
+
+ assertEquals(false, certificateConfig.enableCertAuth());
+ assertEquals("", certificateConfig.keyCert());
+ assertEquals("", certificateConfig.keyPasswordPath());
+ assertEquals("", certificateConfig.trustedCa());
+ assertEquals("", certificateConfig.trustedCaPasswordPath());
+ }
+
+ @Test
+ public void shouldThrowExceptionWhenCertAuthIsEnabledButPathsPropertyIsMissing () throws IOException {
+ CloudConfigParser parser = getCloudConfigParser(INCORRECT_CERT_CONFIG_JSON);
+
+ DatafileTaskException exception = assertThrows(DatafileTaskException.class, parser::getCertificateConfig);
+ assertTrue(exception.getMessage().contains(EXPECTED_EXCEPTION_MESSAGE));
+ }
+
+ private CloudConfigParser getCloudConfigParser(String configPath) throws IOException {
+ String jsonStr = Files.readString(Path.of(configPath));
+ JsonObject jsonObject = JsonParser.parseString(jsonStr).getAsJsonObject();
+
+ return new CloudConfigParser(jsonObject,null);
+ }
+}
diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/FileCollectorTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/FileCollectorTest.java
index ceb8a989..917055cf 100644
--- a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/FileCollectorTest.java
+++ b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/FileCollectorTest.java
@@ -1,7 +1,7 @@
/*-
* ============LICENSE_START======================================================================
* Copyright (C) 2018-2019 Nordix Foundation. All rights reserved.
- * Copyright (C) 2020 Nokia. All rights reserved.
+ * Copyright (C) 2020-2022 Nokia. All rights reserved.
* ===============================================================================================
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
@@ -18,6 +18,7 @@
package org.onap.dcaegen2.collectors.datafile.tasks;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
@@ -197,6 +198,8 @@ public class FileCollectorTest {
assertEquals(0, counters.getNoOfFailedHttpAttempts(),"failedHttpAttempts should have been 0");
}
+
+
@Test
public void whenSftpFile_returnCorrectResponse() throws Exception {
FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
@@ -291,6 +294,38 @@ public class FileCollectorTest {
}
@Test
+ public void whenTlsDisabled_ThrowExceptionForHttpsFile() {
+ when(appConfigMock.getCertificateConfiguration().enableCertAuth()).thenReturn(false);
+ FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
+ FileData fileData = createFileData(HTTPS_LOCATION, Scheme.HTTPS);
+
+ StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
+ .expectErrorMessage("Retries exhausted: 3/3")
+ .verify();
+
+ StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
+ .consumeErrorWith(throwable ->
+ assertEquals("HTTPS error: TLS connection is disabled", throwable.getCause().getMessage()))
+ .verify();
+ }
+
+ @Test
+ public void whenTlsDisabled_ThrowExceptionForFtpesFile() {
+ when(appConfigMock.getCertificateConfiguration().enableCertAuth()).thenReturn(false);
+ FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
+ FileData fileData = createFileData(FTPES_LOCATION, Scheme.FTPES);
+
+ StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
+ .expectErrorMessage("Retries exhausted: 3/3")
+ .verify();
+
+ StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
+ .consumeErrorWith(throwable ->
+ assertEquals("FTPES error: TLS connection is disabled", throwable.getCause().getMessage()))
+ .verify();
+ }
+
+ @Test
public void whenFtpesFileAlwaysFail_retryAndFail() throws Exception {
FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
doReturn(ftpesClientMock).when(collectorUndetTest).createFtpesClient(any());