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 --- .../api/CertificationControllerTest.java | 133 +++++++++++++++++++++ .../certservice/api/CertificationServiceTest.java | 133 --------------------- .../api/ReloadConfigControllerTest.java | 89 ++++++++++++++ .../configuration/CmpServersConfigLoaderTest.java | 47 +++++--- .../configuration/CmpServersConfigTest.java | 97 +++++++++++---- .../Cmpv2ServerConfigurationValidatorTest.java | 124 ++++++++++++++----- .../constraints/Cmpv2URLValidatorTest.java | 2 +- .../violations/PortNumberViolationTest.java | 2 +- .../violations/RequestTypeViolationTest.java | 2 +- .../src/test/resources/invalidCmpServers.json | 19 +++ 10 files changed, 448 insertions(+), 200 deletions(-) create mode 100644 certService/src/test/java/org/onap/aaf/certservice/api/CertificationControllerTest.java delete mode 100644 certService/src/test/java/org/onap/aaf/certservice/api/CertificationServiceTest.java create mode 100644 certService/src/test/java/org/onap/aaf/certservice/api/ReloadConfigControllerTest.java create mode 100644 certService/src/test/resources/invalidCmpServers.json (limited to 'certService/src/test') diff --git a/certService/src/test/java/org/onap/aaf/certservice/api/CertificationControllerTest.java b/certService/src/test/java/org/onap/aaf/certservice/api/CertificationControllerTest.java new file mode 100644 index 00000000..2fc6d665 --- /dev/null +++ b/certService/src/test/java/org/onap/aaf/certservice/api/CertificationControllerTest.java @@ -0,0 +1,133 @@ +/* + * ============LICENSE_START======================================================= + * PROJECT + * ================================================================================ + * Copyright (C) 2020 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.aaf.certservice.api; + +import static org.assertj.core.api.Assertions.assertThat; +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.mock; +import static org.mockito.Mockito.when; + +import com.google.gson.Gson; +import java.io.IOException; +import java.util.Arrays; +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.junit.jupiter.MockitoExtension; +import org.onap.aaf.certservice.certification.CertificationModelFactory; +import org.onap.aaf.certservice.certification.CsrModelFactory; +import org.onap.aaf.certservice.certification.CsrModelFactory.StringBase64; +import org.onap.aaf.certservice.certification.exception.CsrDecryptionException; +import org.onap.aaf.certservice.certification.exception.DecryptionException; +import org.onap.aaf.certservice.certification.exception.KeyDecryptionException; +import org.onap.aaf.certservice.certification.model.CertificationModel; +import org.onap.aaf.certservice.certification.model.CsrModel; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + + +@ExtendWith(MockitoExtension.class) +class CertificationControllerTest { + + private CertificationController certificationController; + + @Mock + private CsrModelFactory csrModelFactory; + + @Mock + private CertificationModelFactory certificationModelFactory; + + @BeforeEach + void setUp() { + certificationController = new CertificationController(csrModelFactory, certificationModelFactory); + } + + @Test + void shouldReturnDataAboutCsrBaseOnEncodedParameters() throws DecryptionException { + // given + final String testStringCsr = "testData"; + final String testCaName = "TestCa"; + CsrModel mockedCsrModel = mock(CsrModel.class); + CertificationModel testCertificationModel = new CertificationModel( + Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"), + Arrays.asList("CA_CERT", "EXTRA_CA_CERT") + ); + when(mockedCsrModel.toString()).thenReturn(testStringCsr); + when(csrModelFactory.createCsrModel(any(StringBase64.class), any(StringBase64.class))) + .thenReturn(mockedCsrModel); + when(certificationModelFactory.createCertificationModel(mockedCsrModel, testCaName)) + .thenReturn(testCertificationModel); + + // when + ResponseEntity testResponse = + certificationController.signCertificate(testCaName, "encryptedCSR", "encryptedPK"); + + CertificationModel responseCertificationModel = new Gson().fromJson(testResponse.getBody(), CertificationModel.class); + + // then + assertEquals(HttpStatus.OK, testResponse.getStatusCode()); + assertThat(responseCertificationModel + ).isEqualToComparingFieldByField(testCertificationModel); + + } + + @Test + void shouldThrowCsrDecryptionExceptionWhenCreatingCsrModelFails() throws DecryptionException { + // given + String expectedMessage = "Incorrect CSR, decryption failed"; + when(csrModelFactory.createCsrModel(any(StringBase64.class), any(StringBase64.class))) + .thenThrow(new CsrDecryptionException(expectedMessage,new IOException())); + + // when + Exception exception = assertThrows( + CsrDecryptionException.class, () -> certificationController. + signCertificate("TestCa", "encryptedCSR", "encryptedPK") + ); + + String actualMessage = exception.getMessage(); + + // then + assertEquals(expectedMessage, actualMessage); + } + + @Test + void shouldThrowPemDecryptionExceptionWhenCreatingPemModelFails() throws DecryptionException { + // given + String expectedMessage = "Incorrect PEM, decryption failed"; + when(csrModelFactory.createCsrModel(any(StringBase64.class), any(StringBase64.class))) + .thenThrow(new KeyDecryptionException(expectedMessage,new IOException())); + + // when + Exception exception = assertThrows( + KeyDecryptionException.class, () -> certificationController. + signCertificate("TestCa", "encryptedCSR", "encryptedPK") + ); + + String actualMessage = exception.getMessage(); + + // then + assertEquals(expectedMessage, actualMessage); + } + +} diff --git a/certService/src/test/java/org/onap/aaf/certservice/api/CertificationServiceTest.java b/certService/src/test/java/org/onap/aaf/certservice/api/CertificationServiceTest.java deleted file mode 100644 index ee1ce1ef..00000000 --- a/certService/src/test/java/org/onap/aaf/certservice/api/CertificationServiceTest.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * PROJECT - * ================================================================================ - * Copyright (C) 2020 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.aaf.certservice.api; - -import com.google.gson.Gson; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.onap.aaf.certservice.certification.CertificationModelFactory; -import org.onap.aaf.certservice.certification.CsrModelFactory; -import org.onap.aaf.certservice.certification.CsrModelFactory.StringBase64; -import org.onap.aaf.certservice.certification.exception.CsrDecryptionException; -import org.onap.aaf.certservice.certification.exception.DecryptionException; -import org.onap.aaf.certservice.certification.exception.KeyDecryptionException; -import org.onap.aaf.certservice.certification.model.CertificationModel; -import org.onap.aaf.certservice.certification.model.CsrModel; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; - -import java.io.IOException; -import java.util.Arrays; - -import static org.assertj.core.api.Assertions.assertThat; -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.mock; -import static org.mockito.Mockito.when; - - -class CertificationServiceTest { - - private CertificationService certificationService; - - @Mock - private CsrModelFactory csrModelFactory; - - @Mock - private CertificationModelFactory certificationModelFactory; - - @BeforeEach - void serUp() { - MockitoAnnotations.initMocks(this); - certificationService = new CertificationService(csrModelFactory, certificationModelFactory); - } - - @Test - void shouldReturnDataAboutCsrBaseOnEncodedParameters() throws DecryptionException { - // given - final String testStringCsr = "testData"; - final String testCaName = "TestCa"; - CsrModel mockedCsrModel = mock(CsrModel.class); - CertificationModel testCertificationModel = new CertificationModel( - Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"), - Arrays.asList("CA_CERT", "EXTRA_CA_CERT") - ); - when(mockedCsrModel.toString()).thenReturn(testStringCsr); - when(csrModelFactory.createCsrModel(any(StringBase64.class), any(StringBase64.class))) - .thenReturn(mockedCsrModel); - when(certificationModelFactory.createCertificationModel(mockedCsrModel, testCaName)) - .thenReturn(testCertificationModel); - - // when - ResponseEntity testResponse = - certificationService.signCertificate(testCaName, "encryptedCSR", "encryptedPK"); - - CertificationModel responseCertificationModel = new Gson().fromJson(testResponse.getBody(), CertificationModel.class); - - // then - assertEquals(HttpStatus.OK, testResponse.getStatusCode()); - assertThat(responseCertificationModel - ).isEqualToComparingFieldByField(testCertificationModel); - - } - - @Test - void shouldThrowCsrDecryptionExceptionWhenCreatingCsrModelFails() throws DecryptionException { - // given - String expectedMessage = "Incorrect CSR, decryption failed"; - when(csrModelFactory.createCsrModel(any(StringBase64.class), any(StringBase64.class))) - .thenThrow(new CsrDecryptionException(expectedMessage,new IOException())); - - // when - Exception exception = assertThrows( - CsrDecryptionException.class, () -> certificationService. - signCertificate("TestCa", "encryptedCSR", "encryptedPK") - ); - - String actualMessage = exception.getMessage(); - - // then - assertEquals(expectedMessage, actualMessage); - } - - @Test - void shouldThrowPemDecryptionExceptionWhenCreatingPemModelFails() throws DecryptionException { - // given - String expectedMessage = "Incorrect PEM, decryption failed"; - when(csrModelFactory.createCsrModel(any(StringBase64.class), any(StringBase64.class))) - .thenThrow(new KeyDecryptionException(expectedMessage,new IOException())); - - // when - Exception exception = assertThrows( - KeyDecryptionException.class, () -> certificationService. - signCertificate("TestCa", "encryptedCSR", "encryptedPK") - ); - - String actualMessage = exception.getMessage(); - - // then - assertEquals(expectedMessage, actualMessage); - } - -} diff --git a/certService/src/test/java/org/onap/aaf/certservice/api/ReloadConfigControllerTest.java b/certService/src/test/java/org/onap/aaf/certservice/api/ReloadConfigControllerTest.java new file mode 100644 index 00000000..17db24b8 --- /dev/null +++ b/certService/src/test/java/org/onap/aaf/certservice/api/ReloadConfigControllerTest.java @@ -0,0 +1,89 @@ +/* + * ============LICENSE_START======================================================= + * PROJECT + * ================================================================================ + * Copyright (C) 2020 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.aaf.certservice.api; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.doThrow; + +import org.assertj.core.api.Assertions; +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.junit.jupiter.MockitoExtension; +import org.onap.aaf.certservice.certification.configuration.CmpServersConfig; +import org.onap.aaf.certservice.certification.configuration.CmpServersConfigLoadingException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +@ExtendWith(MockitoExtension.class) +public class ReloadConfigControllerTest { + + private static final String ERROR_MESSAGE = "Exception occurred during CMP Servers configuration loading"; + + private ReloadConfigController reloadConfigController; + + @Mock + public CmpServersConfig cmpServersConfig; + + @BeforeEach + public void setUp() { + this.reloadConfigController = new ReloadConfigController(cmpServersConfig); + } + + @Test + public void shouldReturnStatusOkWhenSuccessfullyReloaded() throws CmpServersConfigLoadingException { + // When + ResponseEntity response = reloadConfigController.reloadConfiguration(); + + // Then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + } + + @Test + public void shouldRethrowSameErrorWhenFailedToReload() throws CmpServersConfigLoadingException { + // Given + doThrow(new CmpServersConfigLoadingException(ERROR_MESSAGE)).when(cmpServersConfig).reloadConfiguration(); + + // When + Exception exception = assertThrows( + CmpServersConfigLoadingException.class, + () -> reloadConfigController.reloadConfiguration()); + + // Then + Assertions.assertThat(exception.getMessage()).isEqualTo(ERROR_MESSAGE); + } + + @Test + void shouldReturnErrorStatusAndMessageWhenExceptionOccurred() { + // Given + CmpServersConfigLoadingException exception = new CmpServersConfigLoadingException(ERROR_MESSAGE); + + // When + ResponseEntity response = reloadConfigController.handle(exception); + + // Then + assertEquals(ERROR_MESSAGE, response.getBody()); + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + } +} 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 d14dc7b7..c78f6ceb 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 @@ -20,26 +20,26 @@ package org.onap.aaf.certservice.certification.configuration; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.List; +import java.util.Map; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.onap.aaf.certservice.CertServiceApplication; import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.PropertySource; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.jupiter.SpringExtension; -import java.io.IOException; -import java.util.List; -import java.util.Map; - -import static org.assertj.core.api.Assertions.assertThat; - @ExtendWith(SpringExtension.class) @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 INVALID_CONFIG_FILENAME = "invalidCmpServers.json"; + private static final String NONEXISTENT_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,9 +61,9 @@ class CmpServersConfigLoaderTest { private CmpServersConfigLoader configLoader; @Test - void shouldLoadCmpServersConfigWhenFileAvailable() { + void shouldLoadCmpServersConfigWhenFileAvailable() throws CmpServersConfigLoadingException { // Given - String path = getClass().getClassLoader().getResource(EXISTING_CONFIG_FILENAME).getFile(); + String path = getResourcePath(EXISTING_CONFIG_FILENAME); // When List cmpServers = configLoader.load(path); @@ -76,13 +76,32 @@ class CmpServersConfigLoaderTest { } @Test - void shouldReturnEmptyListWhenFileMissing() { + void shouldThrowExceptionWhenFileMissing() { // When - List cmpServers = configLoader.load(NONEXISTING_CONFIG_FILENAME); + Exception exception = assertThrows( + CmpServersConfigLoadingException.class, + () -> configLoader.load(NONEXISTENT_CONFIG_FILENAME)); // Then - assertThat(cmpServers).isNotNull(); - assertThat(cmpServers).isEmpty(); + assertThat(exception.getMessage()).contains("Exception occurred during CMP Servers configuration loading"); + } + + @Test + void shouldThrowExceptionWhenConfigurationIsInvalid() { + // Given + String path = getResourcePath(INVALID_CONFIG_FILENAME); + + // When + Exception exception = assertThrows( + CmpServersConfigLoadingException.class, + () -> configLoader.load(path)); + + // Then + assertThat(exception.getMessage()).contains("Validation of CMPv2 servers configuration failed"); + } + + private String getResourcePath(String invalidConfigFilename) { + return getClass().getClassLoader().getResource(invalidConfigFilename).getFile(); } private void verifyThatCmpServerEquals(Cmpv2Server cmpv2Server, Map expected) { 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() { diff --git a/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/validation/Cmpv2ServerConfigurationValidatorTest.java b/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/validation/Cmpv2ServerConfigurationValidatorTest.java index 18097608..10a7ba46 100644 --- a/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/validation/Cmpv2ServerConfigurationValidatorTest.java +++ b/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/validation/Cmpv2ServerConfigurationValidatorTest.java @@ -20,6 +20,9 @@ package org.onap.aaf.certservice.certification.configuration.validation; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; + import org.bouncycastle.asn1.x500.X500Name; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -32,13 +35,12 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.jupiter.SpringExtension; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertThrows; - @ExtendWith(SpringExtension.class) @ContextConfiguration(classes = CertServiceApplication.class) class Cmpv2ServerConfigurationValidatorTest { + private static final String EMPTY_STRING = ""; + @Autowired private Cmpv2ServerConfigurationValidator validator; @@ -52,53 +54,121 @@ class Cmpv2ServerConfigurationValidatorTest { } @Test - public void givenValidServerDetailsWhenValidatingShouldNotThrowAnyException() { - //then + public void shouldNotThrowExceptionWhenServerConfigurationIsValid() { + // Then assertDoesNotThrow(() -> validator.validate(server)); } @Test - public void givenWrongProtocolInURLServerDetailsWhenValidatingShouldThrowException() { - //given + public void shouldThrowExceptionWhenWrongProtocolInURL() { + // Given server.setUrl("https://test.test.test:60000/"); - //then - assertThrows(IllegalArgumentException.class, () -> {validator.validate(server);}); + // Then + assertExceptionIsThrown(); } @Test - public void givenWrongPortInURLServerDetailsWhenValidatingShouldThrowException() { - //given + public void shouldThrowExceptionWhenWrongPortInURL() { + // Given server.setUrl("http://test.test.test:70000/"); - //then - assertThrows(IllegalArgumentException.class, () -> validator.validate(server)); + // Then + assertExceptionIsThrown(); } @Test - public void givenWrongCANameLengthInURLServerDetailsWhenValidatingShouldThrowException() { - //given - server.setCaName(""); + public void shouldThrowExceptionWhenWrongCANameLength() { + // Given + server.setCaName(EMPTY_STRING); - //then - assertThrows(IllegalArgumentException.class, () -> validator.validate(server)); + // Then + assertExceptionIsThrown(); } @Test - public void givenWrongRVLengthInURLServerDetailsWhenValidatingShouldThrowException() { - //given - authentication.setRv(""); + public void shouldThrowExceptionWhenWrongRVLength() { + // Given + authentication.setRv(EMPTY_STRING); - //then - assertThrows(IllegalArgumentException.class, () -> validator.validate(server)); + // Then + assertExceptionIsThrown(); } + @Test - public void givenWrongIAKLengthInURLServerDetailsWhenValidatingShouldThrowException() { - //given - authentication.setIak(""); + public void shouldThrowExceptionWhenWrongIAKLength() { + // Given + authentication.setIak(EMPTY_STRING); + + // Then + assertExceptionIsThrown(); + } + + @Test + public void shouldThrowExceptionWhenCaNameIsNull() { + // Given + server.setCaName(null); + + // Then + assertExceptionIsThrown(); + } + + @Test + public void shouldThrowExceptionWhenIssuerDnIsNull() { + // Given + server.setIssuerDN(null); + + // Then + assertExceptionIsThrown(); + } + + @Test + public void shouldThrowExceptionWhenCaModeIsNull() { + // Given + server.setCaMode(null); + + // Then + assertExceptionIsThrown(); + } + + @Test + public void shouldThrowExceptionWhenUrlIsNull() { + // Given + server.setUrl(null); + + // Then + assertExceptionIsThrown(); + } + + @Test + public void shouldThrowExceptionWhenAuthenticationIsNull() { + // Given + server.setAuthentication(null); + + // Then + assertExceptionIsThrown(); + } + + @Test + public void shouldThrowExceptionWhenIakIsNull() { + // Given + authentication.setIak(null); + + // Then + assertExceptionIsThrown(); + } + + @Test + public void shouldThrowExceptionWhenRvIsNull() { + // Given + authentication.setRv(null); + + // Then + assertExceptionIsThrown(); + } - //then + private void assertExceptionIsThrown() { assertThrows(IllegalArgumentException.class, () -> validator.validate(server)); } diff --git a/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/validation/constraints/Cmpv2URLValidatorTest.java b/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/validation/constraints/Cmpv2URLValidatorTest.java index a2a03860..78368d8a 100644 --- a/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/validation/constraints/Cmpv2URLValidatorTest.java +++ b/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/validation/constraints/Cmpv2URLValidatorTest.java @@ -27,7 +27,7 @@ import static org.junit.jupiter.api.Assertions.*; class Cmpv2URLValidatorTest { - private Cmpv2URLValidator validator = new Cmpv2URLValidator(); + private final Cmpv2URLValidator validator = new Cmpv2URLValidator(); @Test public void givenCorrectURLWhenValidatingShouldReturnTrue() { diff --git a/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/validation/constraints/violations/PortNumberViolationTest.java b/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/validation/constraints/violations/PortNumberViolationTest.java index 8ae4cd88..d1850473 100644 --- a/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/validation/constraints/violations/PortNumberViolationTest.java +++ b/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/validation/constraints/violations/PortNumberViolationTest.java @@ -27,7 +27,7 @@ import static org.junit.jupiter.api.Assertions.*; class PortNumberViolationTest { - private PortNumberViolation violation = new PortNumberViolation(); + private final PortNumberViolation violation = new PortNumberViolation(); @Test public void givenValidPortShouldReturnTrue() { diff --git a/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/validation/constraints/violations/RequestTypeViolationTest.java b/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/validation/constraints/violations/RequestTypeViolationTest.java index 9e220560..cba2f340 100644 --- a/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/validation/constraints/violations/RequestTypeViolationTest.java +++ b/certService/src/test/java/org/onap/aaf/certservice/certification/configuration/validation/constraints/violations/RequestTypeViolationTest.java @@ -26,7 +26,7 @@ import static org.junit.jupiter.api.Assertions.*; class RequestTypeViolationTest { - private RequestTypeViolation violation = new RequestTypeViolation(); + private final RequestTypeViolation violation = new RequestTypeViolation(); @Test public void givenValidRequestTypeShouldReturnTrue() { diff --git a/certService/src/test/resources/invalidCmpServers.json b/certService/src/test/resources/invalidCmpServers.json new file mode 100644 index 00000000..ac4b34af --- /dev/null +++ b/certService/src/test/resources/invalidCmpServers.json @@ -0,0 +1,19 @@ +{ + "cmpv2Servers": [ + { + "caName": " ", + "url": "http://127.0.0.1/ejbca/publicweb/cmp/cmp", + "issuerDN": "CN=ManagementCA", + "caMode": "CLIENT" + }, + { + "caName": "TEST2", + "url": "http://127.0.0.1/ejbca/publicweb/cmp/cmpRA", + "caMode": "RA", + "authentication": { + "iak": "xxx", + "rv": "yyy" + } + } + ] +} \ No newline at end of file -- cgit 1.2.3-korg