Fix sonar issues
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / oom / certservice / certification / configuration / CmpServersConfigLoaderTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
4  * ================================================================================
5  * Copyright (C) 2020-2021 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.oom.certservice.certification.configuration;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25
26 import java.util.List;
27 import java.util.Map;
28
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.api.extension.ExtendWith;
31 import org.onap.oom.certservice.CertServiceApplication;
32 import org.onap.oom.certservice.certification.configuration.model.Cmpv2Server;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.test.context.ContextConfiguration;
35 import org.springframework.test.context.junit.jupiter.SpringExtension;
36
37 @ExtendWith(SpringExtension.class)
38 @ContextConfiguration(classes = CertServiceApplication.class)
39 class CmpServersConfigLoaderTest {
40     private static final String EXISTING_CONFIG_FILENAME = "cmpServers.json";
41     private static final String INVALID_CONFIG_FILENAME = "invalidCmpServers.json";
42     private static final String NONEXISTENT_CONFIG_FILENAME = "nonExistingCmpServers.json";
43
44     private static final Map<String, String> EXPECTED_FIRST_CMP_SERVER = Map.of(
45             "CA_NAME", "TEST",
46             "URL", "http://127.0.0.1/ejbca/publicweb/cmp/cmp",
47             "ISSUER_DN", "CN=ManagementCA",
48             "CA_MODE", "CLIENT",
49             "IAK", "xxx",
50             "RV", "yyy"
51     );
52     private static final Map<String, String> EXPECTED_SECOND_CMP_SERVER = Map.of(
53             "CA_NAME", "TEST2",
54             "URL", "http://127.0.0.1/ejbca/publicweb/cmp/cmpRA",
55             "ISSUER_DN", "CN=ManagementCA2",
56             "CA_MODE", "RA",
57             "IAK", "xxx",
58             "RV", "yyy"
59     );
60
61     @Autowired
62     private CmpServersConfigLoader configLoader;
63
64     @Test
65     void shouldLoadCmpServersConfigWhenFileAvailable() throws CmpServersConfigLoadingException {
66         // Given
67         String path = getResourcePath(EXISTING_CONFIG_FILENAME);
68
69         // When
70         List<Cmpv2Server> cmpServers = configLoader.load(path);
71
72         // Then
73         assertThat(cmpServers)
74             .isNotNull()
75             .hasSize(2);
76         verifyThatCmpServerEquals(cmpServers.get(0), EXPECTED_FIRST_CMP_SERVER);
77         verifyThatCmpServerEquals(cmpServers.get(1), EXPECTED_SECOND_CMP_SERVER);
78     }
79
80     @Test
81     void shouldThrowExceptionWhenFileMissing() {
82         // When
83         Exception exception = assertThrows(
84                 CmpServersConfigLoadingException.class,
85                 () -> configLoader.load(NONEXISTENT_CONFIG_FILENAME));
86
87         // Then
88         assertThat(exception.getMessage()).contains("Exception occurred during CMP Servers configuration loading");
89     }
90
91     @Test
92     void shouldThrowExceptionWhenConfigurationIsInvalid() {
93         // Given
94         String path = getResourcePath(INVALID_CONFIG_FILENAME);
95
96         // When
97         Exception exception = assertThrows(
98                 CmpServersConfigLoadingException.class,
99                 () -> configLoader.load(path));
100
101         // Then
102         assertThat(exception.getMessage()).contains("Validation of CMPv2 servers configuration failed");
103         assertThat(exception.getCause().getMessage()).contains("authentication");
104     }
105
106     private String getResourcePath(String configFilename) {
107         return getClass().getClassLoader().getResource(configFilename).getFile();
108     }
109
110     private void verifyThatCmpServerEquals(Cmpv2Server cmpv2Server, Map<String, String> expected) {
111         assertThat(cmpv2Server.getCaName()).isEqualTo(expected.get("CA_NAME"));
112         assertThat(cmpv2Server.getUrl()).isEqualTo(expected.get("URL"));
113         assertThat(cmpv2Server.getIssuerDN()).hasToString(expected.get("ISSUER_DN"));
114         assertThat(cmpv2Server.getCaMode().name()).isEqualTo(expected.get("CA_MODE"));
115         assertThat(cmpv2Server.getAuthentication().getIak()).isEqualTo(expected.get("IAK"));
116         assertThat(cmpv2Server.getAuthentication().getRv()).isEqualTo(expected.get("RV"));
117     }
118 }