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