b83fb9aaa711b512c06e725dd14e982d195c3262
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / aaf / certservice / certification / configuration / Cmpv2ServerProviderTest.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.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.junit.jupiter.MockitoExtension;
29 import org.onap.aaf.certservice.certification.configuration.model.Authentication;
30 import org.onap.aaf.certservice.certification.configuration.model.CaMode;
31 import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server;
32 import org.onap.aaf.certservice.certification.exception.Cmpv2ServerNotFoundException;
33
34 import java.util.Collections;
35
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.junit.jupiter.api.Assertions.assertThrows;
38 import static org.junit.jupiter.api.Assertions.assertTrue;
39 import static org.mockito.Mockito.when;
40
41 @ExtendWith(MockitoExtension.class)
42 class Cmpv2ServerProviderTest {
43
44     private static final String TEST_CA = "testCA";
45
46     private Cmpv2ServerProvider cmpv2ServerProvider;
47
48     @Mock
49     private CmpServersConfig cmpServersConfig;
50
51     @BeforeEach
52     void setUp() {
53         cmpv2ServerProvider =
54                 new Cmpv2ServerProvider(cmpServersConfig);
55     }
56
57     @Test
58     void shouldReturnOptionalWithServerWhenServerWithGivenCaNameIsPresentInConfig() {
59         // Given
60         Cmpv2Server testServer = createTestServer();
61         when(cmpServersConfig.getCmpServers()).thenReturn(Collections.singletonList(testServer));
62
63         // When
64         Cmpv2Server receivedServer = cmpv2ServerProvider
65                 .getCmpv2Server(TEST_CA);
66
67         // Then
68         assertThat(receivedServer).isEqualToComparingFieldByField(testServer);
69     }
70
71     @Test
72     void shouldReturnEmptyOptionalWhenServerWithGivenCaNameIsNotPresentInConfig() {
73         // Given
74         String expectedMessage = "No server found for given CA name";
75         when(cmpServersConfig.getCmpServers()).thenReturn(Collections.emptyList());
76
77         // When
78         Exception exception = assertThrows(
79                 Cmpv2ServerNotFoundException.class, () ->
80                         cmpv2ServerProvider.getCmpv2Server(TEST_CA)
81         );
82
83         // Then
84         assertTrue(exception.getMessage().contains(expectedMessage));
85     }
86
87     private Cmpv2Server createTestServer() {
88         Cmpv2Server testServer = new Cmpv2Server();
89         testServer.setCaName(TEST_CA);
90         testServer.setIssuerDN(new X500Name("CN=testIssuer"));
91         testServer.setUrl("http://test.ca.server");
92         Authentication testAuthentication = new Authentication();
93         testAuthentication.setIak("testIak");
94         testAuthentication.setRv("testRv");
95         testServer.setAuthentication(testAuthentication);
96         testServer.setCaMode(CaMode.RA);
97
98         return testServer;
99     }
100 }