7184384c91fd90d9a97a7b14dbf8810a7b443141
[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.bouncycastle.asn1.x500.X500Name;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.extension.ExtendWith;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.junit.jupiter.MockitoExtension;
30 import org.onap.aaf.certservice.certification.configuration.model.Authentication;
31 import org.onap.aaf.certservice.certification.configuration.model.CaMode;
32 import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server;
33 import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent;
34
35 import java.util.List;
36
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.mockito.ArgumentMatchers.any;
39 import static org.mockito.ArgumentMatchers.startsWith;
40
41 @ExtendWith(MockitoExtension.class)
42 class CmpServersConfigTest {
43
44     private static final String APP_CONFIG_PATH = "/fake/path/to/config";
45
46     private static final List<Cmpv2Server> SAMPLE_CMP_SERVERS = generateTestConfiguration();
47
48     @Mock
49     private CmpServersConfigLoader cmpServersConfigLoader;
50
51     private CmpServersConfig cmpServersConfig;
52
53     @BeforeEach
54     void setUp() {
55         cmpServersConfig = new CmpServersConfig(APP_CONFIG_PATH, cmpServersConfigLoader);
56     }
57
58     @Test
59     void shouldCallLoaderWithPathFromPropertiesWhenCreated() {
60         this.cmpServersConfig.loadConfiguration();      // Manual PostConstruct call
61         Mockito.verify(cmpServersConfigLoader).load(startsWith(APP_CONFIG_PATH));
62     }
63
64     @Test
65     void shouldReturnLoadedServersWhenGetCalled() {
66         // Given
67         Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS);
68         this.cmpServersConfig.loadConfiguration();      // Manual PostConstruct call
69
70         // When
71         List<Cmpv2Server> receivedCmpServers = this.cmpServersConfig.getCmpServers();
72
73         // Then
74         assertThat(receivedCmpServers).containsAll(SAMPLE_CMP_SERVERS);
75     }
76
77     @Test
78     void shouldReturnLoadedServersAfterRefreshWhenGetCalled() {
79         // Given
80         Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS);
81
82         List<Cmpv2Server> receivedCmpServers = this.cmpServersConfig.getCmpServers();
83         assertThat(receivedCmpServers).isNull();
84
85         this.cmpServersConfig.onRefreshScope(new RefreshScopeRefreshedEvent());
86
87         // When
88         receivedCmpServers = this.cmpServersConfig.getCmpServers();
89
90         // Then
91         assertThat(receivedCmpServers).containsAll(SAMPLE_CMP_SERVERS);
92     }
93
94     @Test
95     void shouldNotReturnIakAndRvWhenToStringMethodIsUsed() {
96         // Given
97         Mockito.when(cmpServersConfigLoader.load(any())).thenReturn(SAMPLE_CMP_SERVERS);
98         this.cmpServersConfig.loadConfiguration();      // Manual PostConstruct call
99
100         // When
101         List<Cmpv2Server> receivedCmpServers = this.cmpServersConfig.getCmpServers();
102
103         // Then
104         receivedCmpServers.forEach((server)-> assertThat(server.toString())
105                 .doesNotContain(
106                         server.getAuthentication().getIak(),
107                         server.getAuthentication().getRv()
108                 ));
109     }
110
111     private static List<Cmpv2Server> generateTestConfiguration() {
112         Cmpv2Server testServer1 = new Cmpv2Server();
113         testServer1.setCaName("TEST_CA1");
114         testServer1.setIssuerDN(new X500Name("CN=testIssuer"));
115         testServer1.setUrl("http://test.ca.server");
116         Authentication testAuthentication1 = new Authentication();
117         testAuthentication1.setIak("testIak");
118         testAuthentication1.setRv("testRv");
119         testServer1.setAuthentication(testAuthentication1);
120         testServer1.setCaMode(CaMode.RA);
121
122         Cmpv2Server testServer2 = new Cmpv2Server();
123         testServer2.setCaName("TEST_CA2");
124         testServer2.setIssuerDN(new X500Name("CN=testIssuer2"));
125         testServer2.setUrl("http://test.ca.server");
126         Authentication testAuthentication2 = new Authentication();
127         testAuthentication2.setIak("test2Iak");
128         testAuthentication2.setRv("test2Rv");
129         testServer2.setAuthentication(testAuthentication2);
130         testServer2.setCaMode(CaMode.CLIENT);
131
132         return List.of(testServer1, testServer2);
133     }
134
135 }