f4421ffecd2a6583c834870150a13a4c852f85ed
[oom/platform/cert-service.git] /
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.onap.aaf.certservice.certification.configuration.model.Cmpv2Server;
25
26 import java.io.IOException;
27 import java.util.List;
28 import java.util.Map;
29
30 import static org.assertj.core.api.Assertions.assertThat;
31
32 class CmpServersConfigLoaderTest {
33     private static final String EXISTING_CONFIG_FILENAME = "cmpServers.json";
34     private static final String NONEXISTING_CONFIG_FILENAME = "nonexisting_cmpServers.json";
35     private static final Map<String, String> EXPECTED_FIRST_CMP_SERVER = Map.of(
36             "CA_NAME", "TEST",
37             "URL", "http://127.0.0.1/ejbca/publicweb/cmp/cmp",
38             "ISSUER_DN", "CN=ManagementCA",
39             "CA_MODE", "CLIENT",
40             "IAK", "xxx",
41             "RV", "yyy"
42     );
43     private static final Map<String, String> EXPECTED_SECOND_CMP_SERVER = Map.of(
44             "CA_NAME", "TEST2",
45             "URL", "http://127.0.0.1/ejbca/publicweb/cmp/cmpRA",
46             "ISSUER_DN", "CN=ManagementCA2",
47             "CA_MODE", "RA",
48             "IAK", "xxx",
49             "RV", "yyy"
50     );
51
52     @Test
53     public void shouldLoadCmpServersConfigWhenFileAvailable() throws IOException {
54         // Given
55         String path = getClass().getClassLoader().getResource(EXISTING_CONFIG_FILENAME).getFile();
56
57         // When
58         List<Cmpv2Server> cmpServers = new CmpServersConfigLoader().load(path);
59
60         // Then
61         assertThat(cmpServers).isNotNull();
62         assertThat(cmpServers).hasSize(2);
63         verifyThatCmpServerEquals(cmpServers.get(0), EXPECTED_FIRST_CMP_SERVER);
64         verifyThatCmpServerEquals(cmpServers.get(1), EXPECTED_SECOND_CMP_SERVER);
65     }
66
67     @Test()
68     public void shouldReturnEmptyListWhenFileMissing() {
69         // When
70         List<Cmpv2Server> cmpServers = new CmpServersConfigLoader().load(NONEXISTING_CONFIG_FILENAME);
71
72         // Then
73         assertThat(cmpServers).isNotNull();
74         assertThat(cmpServers).isEmpty();
75     }
76
77     private void verifyThatCmpServerEquals(Cmpv2Server cmpv2Server, Map<String, String> expected) {
78         assertThat(cmpv2Server.getCaName()).isEqualTo(expected.get("CA_NAME"));
79         assertThat(cmpv2Server.getUrl()).isEqualTo(expected.get("URL"));
80         assertThat(cmpv2Server.getIssuerDN()).isEqualTo(expected.get("ISSUER_DN"));
81         assertThat(cmpv2Server.getCaMode().name()).isEqualTo(expected.get("CA_MODE"));
82         assertThat(cmpv2Server.getAuthentication().getIak()).isEqualTo(expected.get("IAK"));
83         assertThat(cmpv2Server.getAuthentication().getRv()).isEqualTo(expected.get("RV"));
84     }
85 }