Add validation for uniqueness of CA names
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / aaf / certservice / certification / configuration / CmpServersConfigLoaderTest.java
index f4421ff..8796429 100644 (file)
 
 package org.onap.aaf.certservice.certification.configuration;
 
-import org.junit.jupiter.api.Test;
-import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
-import java.io.IOException;
 import java.util.List;
 import java.util.Map;
 
-import static org.assertj.core.api.Assertions.assertThat;
+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.test.context.ContextConfiguration;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
 
+@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 = "nonExistingCmpServers.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",
@@ -49,13 +58,16 @@ class CmpServersConfigLoaderTest {
             "RV", "yyy"
     );
 
+    @Autowired
+    private CmpServersConfigLoader configLoader;
+
     @Test
-    public void shouldLoadCmpServersConfigWhenFileAvailable() throws IOException {
+    void shouldLoadCmpServersConfigWhenFileAvailable() throws CmpServersConfigLoadingException {
         // Given
-        String path = getClass().getClassLoader().getResource(EXISTING_CONFIG_FILENAME).getFile();
+        String path = getResourcePath(EXISTING_CONFIG_FILENAME);
 
         // When
-        List<Cmpv2Server> cmpServers = new CmpServersConfigLoader().load(path);
+        List<Cmpv2Server> cmpServers = configLoader.load(path);
 
         // Then
         assertThat(cmpServers).isNotNull();
@@ -64,22 +76,42 @@ class CmpServersConfigLoaderTest {
         verifyThatCmpServerEquals(cmpServers.get(1), EXPECTED_SECOND_CMP_SERVER);
     }
 
-    @Test()
-    public void shouldReturnEmptyListWhenFileMissing() {
+    @Test
+    void shouldThrowExceptionWhenFileMissing() {
         // When
-        List<Cmpv2Server> cmpServers = new CmpServersConfigLoader().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");
+        assertThat(exception.getCause().getMessage()).contains("authentication");
+    }
+
+    private String getResourcePath(String configFilename) {
+        return getClass().getClassLoader().getResource(configFilename).getFile();
     }
 
     private void verifyThatCmpServerEquals(Cmpv2Server cmpv2Server, Map<String, String> expected) {
         assertThat(cmpv2Server.getCaName()).isEqualTo(expected.get("CA_NAME"));
         assertThat(cmpv2Server.getUrl()).isEqualTo(expected.get("URL"));
-        assertThat(cmpv2Server.getIssuerDN()).isEqualTo(expected.get("ISSUER_DN"));
+        assertThat(cmpv2Server.getIssuerDN().toString()).isEqualTo(expected.get("ISSUER_DN"));
         assertThat(cmpv2Server.getCaMode().name()).isEqualTo(expected.get("CA_MODE"));
         assertThat(cmpv2Server.getAuthentication().getIak()).isEqualTo(expected.get("IAK"));
         assertThat(cmpv2Server.getAuthentication().getRv()).isEqualTo(expected.get("RV"));
     }
-}
\ No newline at end of file
+}