20a8578348ffef77bcefbcaacdd3e46db0593082
[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.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
33 import java.util.Collections;
34
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.mockito.Mockito.when;
37
38 @ExtendWith(MockitoExtension.class)
39 class Cmpv2ServerProviderTest {
40
41     private static final String TEST_CA = "testCA";
42
43     private Cmpv2ServerProvider cmpv2ServerProvider;
44
45     @Mock
46     private CmpServersConfig cmpServersConfig;
47
48     @BeforeEach
49     void setUp() {
50         cmpv2ServerProvider =
51                 new  Cmpv2ServerProvider(cmpServersConfig);
52     }
53
54     @Test
55     void shouldReturnOptionalWithServerWhenServerWithGivenCaNameIsPresentInConfig() {
56         // given
57         Cmpv2Server testServer = createTestServer();
58         when(cmpServersConfig.getCmpServers()).thenReturn(Collections.singletonList(testServer));
59
60         // when
61         Cmpv2Server receivedServer = cmpv2ServerProvider
62                 .getCmpv2Server(TEST_CA)
63                 .get();
64
65         // then
66         assertThat(receivedServer).isEqualToComparingFieldByField(testServer);
67     }
68
69
70     @Test
71     void shouldReturnEmptyOptionalWhenServerWithGivenCaNameIsNotPresentInConfig() {
72         // given
73         when(cmpServersConfig.getCmpServers()).thenReturn(Collections.emptyList());
74
75         // when
76         Boolean isEmpty = cmpv2ServerProvider
77                 .getCmpv2Server(TEST_CA)
78                 .isEmpty();
79
80         // then
81         assertThat(isEmpty).isTrue();
82     }
83
84     private Cmpv2Server createTestServer() {
85         Cmpv2Server testServer = new Cmpv2Server();
86         testServer.setCaName(TEST_CA);
87         testServer.setIssuerDN(new X500Name("CN=testIssuer"));
88         testServer.setUrl("http://test.ca.server");
89         Authentication testAuthentication = new Authentication();
90         testAuthentication.setIak("testIak");
91         testAuthentication.setRv("testRv");
92         testServer.setAuthentication(testAuthentication);
93         testServer.setCaMode(CaMode.RA);
94
95         return testServer;
96     }
97 }