aboutsummaryrefslogtreecommitdiffstats
path: root/certService/src/test/java/org/onap/aaf/certservice/certification/configuration
diff options
context:
space:
mode:
Diffstat (limited to 'certService/src/test/java/org/onap/aaf/certservice/certification/configuration')
-rw-r--r--certService/src/test/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigLoaderTest.java49
-rw-r--r--certService/src/test/java/org/onap/aaf/certservice/certification/configuration/CmpServersConfigTest.java167
-rw-r--r--certService/src/test/java/org/onap/aaf/certservice/certification/configuration/validation/Cmpv2ServerConfigurationValidatorTest.java124
-rw-r--r--certService/src/test/java/org/onap/aaf/certservice/certification/configuration/validation/constraints/Cmpv2URLValidatorTest.java2
-rw-r--r--certService/src/test/java/org/onap/aaf/certservice/certification/configuration/validation/constraints/violations/PortNumberViolationTest.java2
-rw-r--r--certService/src/test/java/org/onap/aaf/certservice/certification/configuration/validation/constraints/violations/RequestTypeViolationTest.java2
6 files changed, 274 insertions, 72 deletions
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..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<String, String> 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
- public void shouldLoadCmpServersConfigWhenFileAvailable() {
+ void shouldLoadCmpServersConfigWhenFileAvailable() throws CmpServersConfigLoadingException {
// Given
- String path = getClass().getClassLoader().getResource(EXISTING_CONFIG_FILENAME).getFile();
+ String path = getResourcePath(EXISTING_CONFIG_FILENAME);
// When
List<Cmpv2Server> cmpServers = configLoader.load(path);
@@ -75,14 +75,33 @@ class CmpServersConfigLoaderTest {
verifyThatCmpServerEquals(cmpServers.get(1), EXPECTED_SECOND_CMP_SERVER);
}
- @Test()
- public void shouldReturnEmptyListWhenFileMissing() {
+ @Test
+ void shouldThrowExceptionWhenFileMissing() {
+ // When
+ Exception exception = assertThrows(
+ CmpServersConfigLoadingException.class,
+ () -> configLoader.load(NONEXISTENT_CONFIG_FILENAME));
+
+ // Then
+ assertThat(exception.getMessage()).contains("Exception occurred during CMP Servers configuration loading");
+ }
+
+ @Test
+ void shouldThrowExceptionWhenConfigurationIsInvalid() {
+ // Given
+ String path = getResourcePath(INVALID_CONFIG_FILENAME);
+
// When
- List<Cmpv2Server> cmpServers = configLoader.load(NONEXISTING_CONFIG_FILENAME);
+ Exception exception = assertThrows(
+ CmpServersConfigLoadingException.class,
+ () -> configLoader.load(path));
// Then
- assertThat(cmpServers).isNotNull();
- assertThat(cmpServers).isEmpty();
+ 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<String, String> 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 43094f09..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,54 +20,167 @@
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;
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 java.util.List;
-
-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<Cmpv2Server> SAMPLE_CMP_SERVERS = List.of(
- new Cmpv2Server(),
- new Cmpv2Server()
- );
+ 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<Cmpv2Server> SAMPLE_CMP_SERVERS = generateTestConfiguration();
- @MockBean
+ @Mock
private CmpServersConfigLoader cmpServersConfigLoader;
- @Autowired
private CmpServersConfig cmpServersConfig;
+ @BeforeEach
+ void setUp() {
+ cmpServersConfig = new CmpServersConfig(APP_CONFIG_PATH, cmpServersConfigLoader);
+ }
+
@Test
- public void shouldCallLoaderWithPathFromPropertiesWhenCreated() {
- Mockito.verify(cmpServersConfigLoader).load(startsWith("/fake/path/to/config"));
+ void shouldCallLoaderWithPathFromPropertiesWhenCreated() throws CmpServersConfigLoadingException {
+ // When
+ this.cmpServersConfig.init(); // Manual PostConstruct call
+
+ // Then
+ Mockito.verify(cmpServersConfigLoader).load(startsWith(APP_CONFIG_PATH));
}
@Test
- public 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<Cmpv2Server> receivedCmpServers = this.cmpServersConfig.getCmpServers();
// Then
- assertThat(receivedCmpServers).hasSize(SAMPLE_CMP_SERVERS.size());
+ assertThat(receivedCmpServers).containsAll(SAMPLE_CMP_SERVERS);
}
-} \ No newline at end of file
+
+ @Test
+ void shouldReturnLoadedServersAfterReloadWhenGetCalled() throws CmpServersConfigLoadingException {
+ // Given
+ Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS);
+ List<Cmpv2Server> receivedCmpServers = this.cmpServersConfig.getCmpServers();
+ assertThat(receivedCmpServers).isNull();
+
+ // When
+ this.cmpServersConfig.reloadConfiguration();
+ receivedCmpServers = this.cmpServersConfig.getCmpServers();
+
+ // Then
+ assertThat(receivedCmpServers).containsAll(SAMPLE_CMP_SERVERS);
+ }
+
+ @Test
+ 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.init(); // Manual PostConstruct call
+
+ // When
+ List<Cmpv2Server> receivedCmpServers = this.cmpServersConfig.getCmpServers();
+
+ // Then
+ 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<Cmpv2Server> 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);
+ }
+
+}
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() {