Refactor tests for configuration
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / aaf / certservice / certification / configuration / CmpServersConfigLoaderTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
4  * ================================================================================
5  * Copyright (C) 2020 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.aaf.certservice.certification.configuration;
22
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.api.extension.ExtendWith;
25 import org.onap.aaf.certservice.CertServiceApplication;
26 import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.context.annotation.PropertySource;
29 import org.springframework.test.context.ContextConfiguration;
30 import org.springframework.test.context.junit.jupiter.SpringExtension;
31
32 import java.io.IOException;
33 import java.util.List;
34 import java.util.Map;
35
36 import static org.assertj.core.api.Assertions.assertThat;
37
38 @ExtendWith(SpringExtension.class)
39 @ContextConfiguration(classes = CertServiceApplication.class)
40 class CmpServersConfigLoaderTest {
41     private static final String EXISTING_CONFIG_FILENAME = "cmpServers.json";
42     private static final String NONEXISTING_CONFIG_FILENAME = "nonExisting_cmpServers.json";
43     private static final Map<String, String> EXPECTED_FIRST_CMP_SERVER = Map.of(
44             "CA_NAME", "TEST",
45             "URL", "http://127.0.0.1/ejbca/publicweb/cmp/cmp",
46             "ISSUER_DN", "CN=ManagementCA",
47             "CA_MODE", "CLIENT",
48             "IAK", "xxx",
49             "RV", "yyy"
50     );
51     private static final Map<String, String> EXPECTED_SECOND_CMP_SERVER = Map.of(
52             "CA_NAME", "TEST2",
53             "URL", "http://127.0.0.1/ejbca/publicweb/cmp/cmpRA",
54             "ISSUER_DN", "CN=ManagementCA2",
55             "CA_MODE", "RA",
56             "IAK", "xxx",
57             "RV", "yyy"
58     );
59
60     @Autowired
61     private CmpServersConfigLoader configLoader;
62
63     @Test
64     void shouldLoadCmpServersConfigWhenFileAvailable() {
65         // Given
66         String path = getClass().getClassLoader().getResource(EXISTING_CONFIG_FILENAME).getFile();
67
68         // When
69         List<Cmpv2Server> cmpServers = configLoader.load(path);
70
71         // Then
72         assertThat(cmpServers).isNotNull();
73         assertThat(cmpServers).hasSize(2);
74         verifyThatCmpServerEquals(cmpServers.get(0), EXPECTED_FIRST_CMP_SERVER);
75         verifyThatCmpServerEquals(cmpServers.get(1), EXPECTED_SECOND_CMP_SERVER);
76     }
77
78     @Test
79     void shouldReturnEmptyListWhenFileMissing() {
80         // When
81         List<Cmpv2Server> cmpServers = configLoader.load(NONEXISTING_CONFIG_FILENAME);
82
83         // Then
84         assertThat(cmpServers).isNotNull();
85         assertThat(cmpServers).isEmpty();
86     }
87
88     private void verifyThatCmpServerEquals(Cmpv2Server cmpv2Server, Map<String, String> expected) {
89         assertThat(cmpv2Server.getCaName()).isEqualTo(expected.get("CA_NAME"));
90         assertThat(cmpv2Server.getUrl()).isEqualTo(expected.get("URL"));
91         assertThat(cmpv2Server.getIssuerDN().toString()).isEqualTo(expected.get("ISSUER_DN"));
92         assertThat(cmpv2Server.getCaMode().name()).isEqualTo(expected.get("CA_MODE"));
93         assertThat(cmpv2Server.getAuthentication().getIak()).isEqualTo(expected.get("IAK"));
94         assertThat(cmpv2Server.getAuthentication().getRv()).isEqualTo(expected.get("RV"));
95     }
96 }