From 15e8c3bd2bbe5ee32f2ff041450cbc276863d0d0 Mon Sep 17 00:00:00 2001 From: Bartosz Gardziejewski Date: Thu, 27 Feb 2020 08:14:24 +0100 Subject: Refactor tests for configuration Issue-ID: AAF-997 Signed-off-by: Bartosz Gardziejewski Change-Id: I91129fdf70263bd5821feb4482ca116ae9a68747 --- .../configuration/CmpServersConfigLoader.java | 3 +- .../configuration/CmpServersConfigLoaderTest.java | 8 +- .../configuration/CmpServersConfigTest.java | 102 +++++++++++++++++---- 3 files changed, 87 insertions(+), 26 deletions(-) diff --git a/certService/src/main/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigLoader.java b/certService/src/main/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigLoader.java index a6dd5fcf..94530100 100644 --- a/certService/src/main/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigLoader.java +++ b/certService/src/main/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigLoader.java @@ -51,12 +51,11 @@ class CmpServersConfigLoader { try { servers = loadConfigFromFile(path).getCmpv2Servers(); servers.forEach(validator::validate); - LOGGER.info(String.format("CMP Servers configuration successfully loaded from file '%s'", path)); + LOGGER.info("CMP Servers configuration successfully loaded from file {}", path); } catch (IOException e) { LOGGER.error("Exception occurred during CMP Servers configuration loading: ", e); } catch (InvalidParameterException e) { LOGGER.error("Validation of CMPv2 servers configuration failed:", e); - throw e; } return servers; diff --git a/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigLoaderTest.java b/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigLoaderTest.java index cf8c07a1..d14dc7b7 100644 --- a/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigLoaderTest.java +++ b/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigLoaderTest.java @@ -39,7 +39,7 @@ import static org.assertj.core.api.Assertions.assertThat; @ContextConfiguration(classes = CertServiceApplication.class) class CmpServersConfigLoaderTest { private static final String EXISTING_CONFIG_FILENAME = "cmpServers.json"; - private static final String NONEXISTING_CONFIG_FILENAME = "nonexisting_cmpServers.json"; + private static final String NONEXISTING_CONFIG_FILENAME = "nonExisting_cmpServers.json"; private static final Map EXPECTED_FIRST_CMP_SERVER = Map.of( "CA_NAME", "TEST", "URL", "http://127.0.0.1/ejbca/publicweb/cmp/cmp", @@ -61,7 +61,7 @@ class CmpServersConfigLoaderTest { private CmpServersConfigLoader configLoader; @Test - public void shouldLoadCmpServersConfigWhenFileAvailable() { + void shouldLoadCmpServersConfigWhenFileAvailable() { // Given String path = getClass().getClassLoader().getResource(EXISTING_CONFIG_FILENAME).getFile(); @@ -75,8 +75,8 @@ class CmpServersConfigLoaderTest { verifyThatCmpServerEquals(cmpServers.get(1), EXPECTED_SECOND_CMP_SERVER); } - @Test() - public void shouldReturnEmptyListWhenFileMissing() { + @Test + void shouldReturnEmptyListWhenFileMissing() { // When List cmpServers = configLoader.load(NONEXISTING_CONFIG_FILENAME); diff --git a/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigTest.java b/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigTest.java index 43094f09..7184384c 100644 --- a/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigTest.java +++ b/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigTest.java @@ -20,16 +20,17 @@ package org.onap.aaf.certservice.certification.configuration; +import org.bouncycastle.asn1.x500.X500Name; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; import org.mockito.Mockito; -import org.onap.aaf.certservice.CertServiceApplication; +import org.mockito.junit.jupiter.MockitoExtension; +import org.onap.aaf.certservice.certification.configuration.model.Authentication; +import org.onap.aaf.certservice.certification.configuration.model.CaMode; import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent; import java.util.List; @@ -37,29 +38,61 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.startsWith; -@ExtendWith(SpringExtension.class) -@ContextConfiguration(classes = CertServiceApplication.class) -@TestPropertySource(properties = {"app.config.path=/fake/path/to/config"}) +@ExtendWith(MockitoExtension.class) class CmpServersConfigTest { - private static final List SAMPLE_CMP_SERVERS = List.of( - new Cmpv2Server(), - new Cmpv2Server() - ); + private static final String APP_CONFIG_PATH = "/fake/path/to/config"; - @MockBean + private static final List SAMPLE_CMP_SERVERS = generateTestConfiguration(); + + @Mock private CmpServersConfigLoader cmpServersConfigLoader; - @Autowired private CmpServersConfig cmpServersConfig; + @BeforeEach + void setUp() { + cmpServersConfig = new CmpServersConfig(APP_CONFIG_PATH, cmpServersConfigLoader); + } + + @Test + void shouldCallLoaderWithPathFromPropertiesWhenCreated() { + this.cmpServersConfig.loadConfiguration(); // Manual PostConstruct call + Mockito.verify(cmpServersConfigLoader).load(startsWith(APP_CONFIG_PATH)); + } + + @Test + void shouldReturnLoadedServersWhenGetCalled() { + // Given + Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS); + this.cmpServersConfig.loadConfiguration(); // Manual PostConstruct call + + // When + List receivedCmpServers = this.cmpServersConfig.getCmpServers(); + + // Then + assertThat(receivedCmpServers).containsAll(SAMPLE_CMP_SERVERS); + } + @Test - public void shouldCallLoaderWithPathFromPropertiesWhenCreated() { - Mockito.verify(cmpServersConfigLoader).load(startsWith("/fake/path/to/config")); + void shouldReturnLoadedServersAfterRefreshWhenGetCalled() { + // Given + Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS); + + List receivedCmpServers = this.cmpServersConfig.getCmpServers(); + assertThat(receivedCmpServers).isNull(); + + this.cmpServersConfig.onRefreshScope(new RefreshScopeRefreshedEvent()); + + // When + receivedCmpServers = this.cmpServersConfig.getCmpServers(); + + // Then + assertThat(receivedCmpServers).containsAll(SAMPLE_CMP_SERVERS); } @Test - public void shouldReturnLoadedServersWhenGetCalled() { + void shouldNotReturnIakAndRvWhenToStringMethodIsUsed() { // Given Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS); this.cmpServersConfig.loadConfiguration(); // Manual PostConstruct call @@ -68,6 +101,35 @@ class CmpServersConfigTest { List receivedCmpServers = this.cmpServersConfig.getCmpServers(); // Then - assertThat(receivedCmpServers).hasSize(SAMPLE_CMP_SERVERS.size()); + receivedCmpServers.forEach((server)-> assertThat(server.toString()) + .doesNotContain( + server.getAuthentication().getIak(), + server.getAuthentication().getRv() + )); + } + + private static List generateTestConfiguration() { + Cmpv2Server testServer1 = new Cmpv2Server(); + testServer1.setCaName("TEST_CA1"); + testServer1.setIssuerDN(new X500Name("CN=testIssuer")); + testServer1.setUrl("http://test.ca.server"); + Authentication testAuthentication1 = new Authentication(); + testAuthentication1.setIak("testIak"); + testAuthentication1.setRv("testRv"); + testServer1.setAuthentication(testAuthentication1); + testServer1.setCaMode(CaMode.RA); + + Cmpv2Server testServer2 = new Cmpv2Server(); + testServer2.setCaName("TEST_CA2"); + testServer2.setIssuerDN(new X500Name("CN=testIssuer2")); + testServer2.setUrl("http://test.ca.server"); + Authentication testAuthentication2 = new Authentication(); + testAuthentication2.setIak("test2Iak"); + testAuthentication2.setRv("test2Rv"); + testServer2.setAuthentication(testAuthentication2); + testServer2.setCaMode(CaMode.CLIENT); + + return List.of(testServer1, testServer2); } -} \ No newline at end of file + +} -- cgit 1.2.3-korg