diff options
author | Tomasz Wrobel <tomasz.wrobel@nokia.com> | 2022-01-25 11:00:42 +0100 |
---|---|---|
committer | Tomasz Wrobel <tomasz.wrobel@nokia.com> | 2022-02-11 12:27:06 +0100 |
commit | 9d02ea09cac22c177688622ed50a7eb3f6fd38ce (patch) | |
tree | 430c1dfc64d505a24a380d7eeef48d0f1c3d37db /src/test/java/org/onap | |
parent | 6fa1dfca682711066189bef2f946433b614239fe (diff) |
Fix loading SSL Context when certpaths not exist in configuration1.8.0
- Make cert paths field optional in configuration.
- Allow to skip ssl context load.
- Make PublisherConfig and SubscriberConfig fields optional.
- Remove Auth Header when AAF credentials are empty
Issue-ID: DCAEGEN2-3032
Issue-ID: DCAEGEN2-3038
Signed-off-by: Tomasz Wrobel <tomasz.wrobel@nokia.com>
Change-Id: I27d44cf8c2887b3a75c5ad16f833439b7b5757ee
Diffstat (limited to 'src/test/java/org/onap')
-rw-r--r-- | src/test/java/org/onap/dcaegen2/services/pmmapper/config/ConfigHandlerTests.java | 20 | ||||
-rw-r--r-- | src/test/java/org/onap/dcaegen2/services/pmmapper/ssl/SSLContextFactoryTest.java | 83 |
2 files changed, 98 insertions, 5 deletions
diff --git a/src/test/java/org/onap/dcaegen2/services/pmmapper/config/ConfigHandlerTests.java b/src/test/java/org/onap/dcaegen2/services/pmmapper/config/ConfigHandlerTests.java index a1538ee..d007123 100644 --- a/src/test/java/org/onap/dcaegen2/services/pmmapper/config/ConfigHandlerTests.java +++ b/src/test/java/org/onap/dcaegen2/services/pmmapper/config/ConfigHandlerTests.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. - * Copyright (C) 2022 Nokia. + * 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. @@ -20,6 +20,7 @@ */ package org.onap.dcaegen2.services.pmmapper.config; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; @@ -62,6 +63,8 @@ class ConfigHandlerTests { private static String validMapperConfigChanged; private static final Path INVALID_CONFIGS_DIRECTORY = Paths.get("src/test/resources/invalid_configs/"); + private static final Path MISSING_OPTIONAL_FIELDS_CONFIGS_DIRECTORY = + Paths.get("src/test/resources/missing_optional_fields/"); private static final String EXPECTED_ERROR_MESSAGE_IN_LOG = "Error parsing configuration"; private static final String EXPECTED_CHANGED_VALUE = "https://dmaap-dr-node:8443/delete_changed"; @@ -142,6 +145,17 @@ class ConfigHandlerTests { } @ParameterizedTest + @MethodSource("getConfigsWithMissingOptionalFields") + void should_parse_json_with_missing_optional_fields(String mapperConfig) { + Mono<JsonObject> just = createMonoJsonObject(mapperConfig); + + when(cbsClient.get(any())).thenReturn(just); + ConfigHandler configHandler = new ConfigHandler(cbsClient, cbsRequest); + + assertDoesNotThrow(configHandler::getInitialConfiguration); + } + + @ParameterizedTest @MethodSource("getInvalidConfigs") void parse_valid_json_bad_values_mapper_config(String mapperConfig) throws Exception { Mono<JsonObject> just = createMonoJsonObject(mapperConfig); @@ -169,4 +183,8 @@ class ConfigHandlerTests { private static List<String> getInvalidConfigs() throws IOException { return FileUtils.getFilesFromDirectory(INVALID_CONFIGS_DIRECTORY); } + + private static List<String> getConfigsWithMissingOptionalFields() throws IOException { + return FileUtils.getFilesFromDirectory(MISSING_OPTIONAL_FIELDS_CONFIGS_DIRECTORY); + } } diff --git a/src/test/java/org/onap/dcaegen2/services/pmmapper/ssl/SSLContextFactoryTest.java b/src/test/java/org/onap/dcaegen2/services/pmmapper/ssl/SSLContextFactoryTest.java index 6f5cee9..747715c 100644 --- a/src/test/java/org/onap/dcaegen2/services/pmmapper/ssl/SSLContextFactoryTest.java +++ b/src/test/java/org/onap/dcaegen2/services/pmmapper/ssl/SSLContextFactoryTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. + * 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. @@ -23,28 +24,39 @@ package org.onap.dcaegen2.services.pmmapper.ssl; import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import java.util.ArrayList; +import java.util.List; +import javax.net.ssl.SSLContext; import org.junit.Rule; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import org.junit.rules.ExpectedException; + +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import org.mockito.junit.jupiter.MockitoExtension; +import org.onap.dcaegen2.services.pmmapper.exceptions.MapperConfigException; import org.onap.dcaegen2.services.pmmapper.model.MapperConfig; -import javax.net.ssl.*; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) public class SSLContextFactoryTest { + private static final String TEST_PATH = ""; + @Rule public ExpectedException exception = ExpectedException.none(); @@ -52,7 +64,6 @@ public class SSLContextFactoryTest { private static MapperConfig validConfig; private static MapperConfig inValidConfig; - private static final Path validConfigPath = Paths.get("src/test/resources/valid_mapper_config.json"); private SSLContextFactory objUnderTest; @@ -81,4 +92,68 @@ public class SSLContextFactoryTest { assertThrows(IOException.class, () -> objUnderTest.createSSLContext(inValidConfig)); } -}
\ No newline at end of file + + @Test + void shouldThrowExceptionWhenCertPathAreMissingButHttpIsDisabled() { + MapperConfig mapperConfig = mock(MapperConfig.class); + when(mapperConfig.getKeyStorePath()).thenReturn(TEST_PATH); + when(mapperConfig.getEnableHttp()).thenReturn(false); + + SSLContextFactory sslContextFactory = new SSLContextFactory(mapperConfig); + + assertThrows(MapperConfigException.class, () -> sslContextFactory.createSSLContext(mapperConfig)); + } + + + @ParameterizedTest + @MethodSource("pathsAreNull") + void shouldReturnNullWhenOneCertPathIsNull(MapperConfig mapperConfig) throws IOException { + SSLContextFactory sslContextFactory = new SSLContextFactory(mapperConfig); + SSLContext sslContext = sslContextFactory.createSSLContext(mapperConfig); + + assertNull(sslContext); + } + + @ParameterizedTest + @MethodSource("pathsAreEmpty") + void shouldReturnNullWhenOneCertPathIsEmpty(MapperConfig mapperConfig) throws IOException { + SSLContextFactory sslContextFactory = new SSLContextFactory(mapperConfig); + SSLContext sslContext = sslContextFactory.createSSLContext(mapperConfig); + + assertNull(sslContext); + } + + private static List<MapperConfig> pathsAreNull() { + return mockMapperConfigList(null); + } + + private static List<MapperConfig> pathsAreEmpty() { + return mockMapperConfigList(""); + } + + private static List<MapperConfig> mockMapperConfigList(String returnValue) { + List<MapperConfig> mapperConfigList = new ArrayList<>(); + + MapperConfig mapperConfig1 = mock(MapperConfig.class); + when(mapperConfig1.getKeyStorePath()).thenReturn(returnValue); + when(mapperConfig1.getEnableHttp()).thenReturn(true); + mapperConfigList.add(mapperConfig1); + + MapperConfig mapperConfig2 = mock(MapperConfig.class); + when(mapperConfig2.getKeyStorePassPath()).thenReturn(returnValue); + when(mapperConfig2.getEnableHttp()).thenReturn(true); + mapperConfigList.add(mapperConfig2); + + MapperConfig mapperConfig3 = mock(MapperConfig.class); + when(mapperConfig3.getTrustStorePath()).thenReturn(returnValue); + when(mapperConfig3.getEnableHttp()).thenReturn(true); + mapperConfigList.add(mapperConfig3); + + MapperConfig mapperConfig4 = mock(MapperConfig.class); + when(mapperConfig4.getTrustStorePassPath()).thenReturn(returnValue); + when(mapperConfig4.getEnableHttp()).thenReturn(true); + mapperConfigList.add(mapperConfig4); + + return mapperConfigList; + } +} |