From 09128196b16368651e8fa2a20140216e086700f2 Mon Sep 17 00:00:00 2001 From: awudzins Date: Tue, 3 Mar 2020 14:51:21 +0100 Subject: Fix reload endpoint to return status Signed-off-by: Adam Wudzinski Issue-ID: AAF-997 wChange-Id: I4563428ef407b4dc8c84c3efc8ec213145806b32 Change-Id: Iee33a26feb5cc1acd39d91ee9f34a49183040e06 --- .../configuration/CmpServersConfigTest.java | 97 +++++++++++++++++----- 1 file changed, 74 insertions(+), 23 deletions(-) (limited to 'certService/src/test/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigTest.java') 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 7184384c..5d850fe9 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,6 +20,12 @@ package org.onap.aaf.certservice.certification.configuration; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.startsWith; + +import java.util.List; import org.bouncycastle.asn1.x500.X500Name; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -30,19 +36,12 @@ 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.cloud.context.scope.refresh.RefreshScopeRefreshedEvent; - -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.startsWith; @ExtendWith(MockitoExtension.class) class CmpServersConfigTest { + private static final String ERROR_MESSAGE = "Exception occurred during CMP Servers configuration loading"; private static final String APP_CONFIG_PATH = "/fake/path/to/config"; - private static final List SAMPLE_CMP_SERVERS = generateTestConfiguration(); @Mock @@ -56,16 +55,19 @@ class CmpServersConfigTest { } @Test - void shouldCallLoaderWithPathFromPropertiesWhenCreated() { - this.cmpServersConfig.loadConfiguration(); // Manual PostConstruct call + void shouldCallLoaderWithPathFromPropertiesWhenCreated() throws CmpServersConfigLoadingException { + // When + this.cmpServersConfig.init(); // Manual PostConstruct call + + // Then Mockito.verify(cmpServersConfigLoader).load(startsWith(APP_CONFIG_PATH)); } @Test - void shouldReturnLoadedServersWhenGetCalled() { + void shouldReturnLoadedServersWhenGetCalled() throws CmpServersConfigLoadingException { // Given Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS); - this.cmpServersConfig.loadConfiguration(); // Manual PostConstruct call + this.cmpServersConfig.init(); // Manual PostConstruct call // When List receivedCmpServers = this.cmpServersConfig.getCmpServers(); @@ -75,16 +77,14 @@ class CmpServersConfigTest { } @Test - void shouldReturnLoadedServersAfterRefreshWhenGetCalled() { + void shouldReturnLoadedServersAfterReloadWhenGetCalled() throws CmpServersConfigLoadingException { // Given Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS); - List receivedCmpServers = this.cmpServersConfig.getCmpServers(); assertThat(receivedCmpServers).isNull(); - this.cmpServersConfig.onRefreshScope(new RefreshScopeRefreshedEvent()); - // When + this.cmpServersConfig.reloadConfiguration(); receivedCmpServers = this.cmpServersConfig.getCmpServers(); // Then @@ -92,20 +92,71 @@ class CmpServersConfigTest { } @Test - void shouldNotReturnIakAndRvWhenToStringMethodIsUsed() { + void shouldRethrowExceptionWhenReloaded() throws CmpServersConfigLoadingException { + // Given + Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException( + ERROR_MESSAGE)); + + // Then + assertThrows( + CmpServersConfigLoadingException.class, + () -> cmpServersConfig.reloadConfiguration()); + } + + @Test + void shouldPassMessageToRethrownErrorWhenReloadingFails() throws CmpServersConfigLoadingException { + // Given + Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE)); + + // When + Exception exception = assertThrows( + CmpServersConfigLoadingException.class, + () -> cmpServersConfig.reloadConfiguration()); + + // Then + assertThat(exception.getMessage()).isEqualTo(ERROR_MESSAGE); + } + + @Test + void shouldNotReturnIakAndRvWhenToStringMethodIsUsed() throws CmpServersConfigLoadingException { // Given Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS); - this.cmpServersConfig.loadConfiguration(); // Manual PostConstruct call + this.cmpServersConfig.init(); // Manual PostConstruct call // When List receivedCmpServers = this.cmpServersConfig.getCmpServers(); // Then - receivedCmpServers.forEach((server)-> assertThat(server.toString()) - .doesNotContain( - server.getAuthentication().getIak(), - server.getAuthentication().getRv() - )); + receivedCmpServers.forEach((server) -> assertThat(server.toString()) + .doesNotContain( + server.getAuthentication().getIak(), + server.getAuthentication().getRv() + )); + } + + @Test + void shouldRethrowErrorWhenLoadingFails() throws CmpServersConfigLoadingException { + // Given + Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE)); + + // Then + assertThrows( + CmpServersConfigLoadingException.class, + () -> cmpServersConfig.loadConfiguration()); + } + + @Test + void shouldPassMessageToRethrownErrorWhenLoadingFails() throws CmpServersConfigLoadingException { + // Given + Mockito.when(cmpServersConfigLoader.load(any())).thenThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE)); + + // When + Exception exception = assertThrows( + CmpServersConfigLoadingException.class, + () -> cmpServersConfig.loadConfiguration()); + + // Then + assertThat(exception.getMessage()).isEqualTo(ERROR_MESSAGE); } private static List generateTestConfiguration() { -- cgit 1.2.3-korg