165c9ec13c0fce4e867e7d9b82d78b541ccd33be
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / aaf / certservice / certification / adapter / CSRMetaBuilderTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * AAF Certification Service
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.adapter;
22
23 import org.bouncycastle.asn1.x500.X500Name;
24 import org.bouncycastle.pkcs.PKCS10CertificationRequest;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.onap.aaf.certservice.certification.configuration.model.Authentication;
28 import org.onap.aaf.certservice.certification.configuration.model.CaMode;
29 import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server;
30 import org.onap.aaf.certservice.certification.model.CsrModel;
31 import org.onap.aaf.certservice.cmpv2client.external.CSRMeta;
32
33 import java.security.PrivateKey;
34 import java.security.PublicKey;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.List;
38
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.when;
42
43 public class CSRMetaBuilderTest {
44
45     private CSRMetaBuilder csrMetaBuilder;
46
47     private static final String TEST_CA = "testCA";
48     private static final X500Name TEST_SUBJECT_DATA = new X500Name("CN=testIssuer");
49
50     @BeforeEach
51     void setUp() {
52         csrMetaBuilder = new CSRMetaBuilder();
53     }
54
55     @Test
56     void shouldBuildCsrMetaWhenGivenCsrModelAndCmpv2ServerAreCorrect() {
57         // Given
58         CsrModel testCsrModel = mock(CsrModel.class);
59         Cmpv2Server testServer = createTestServer();
60
61         PKCS10CertificationRequest certificationRequest = mock(PKCS10CertificationRequest.class);
62         when(testCsrModel.getCsr()).thenReturn(certificationRequest);
63         PrivateKey mockPrivateKey = mock(PrivateKey.class);
64         when(testCsrModel.getPrivateKey()).thenReturn(mockPrivateKey);
65         PublicKey mockPublicKey = mock(PublicKey.class);
66         when(testCsrModel.getPublicKey()).thenReturn(mockPublicKey);
67         List<String> testSans = Arrays.asList("SAN01","SAN02");
68         when(testCsrModel.getSans()).thenReturn(testSans);
69
70         when(testCsrModel.getSubjectData()).thenReturn(TEST_SUBJECT_DATA);
71
72         // When
73         CSRMeta createdCSRMeta = csrMetaBuilder.build(testCsrModel, testServer);
74
75         // Then
76         assertThat(createdCSRMeta.password()).isEqualTo(testServer.getAuthentication().getIak());
77         assertThat(createdCSRMeta.senderKid()).isEqualTo(testServer.getAuthentication().getRv());
78         assertThat(createdCSRMeta.caUrl()).isEqualTo(testServer.getUrl());
79         assertThat(createdCSRMeta.sans()).containsAll(testSans);
80         assertThat(createdCSRMeta.keyPair().getPrivate()).isEqualTo(mockPrivateKey);
81         assertThat(createdCSRMeta.keyPair().getPublic()).isEqualTo(mockPublicKey);
82         assertThat(createdCSRMeta.x500Name()).isEqualTo(TEST_SUBJECT_DATA);
83         assertThat(createdCSRMeta.issuerx500Name()).isEqualTo(TEST_SUBJECT_DATA);
84     }
85
86     private Cmpv2Server createTestServer() {
87         Cmpv2Server testServer = new Cmpv2Server();
88         testServer.setCaName(TEST_CA);
89         testServer.setIssuerDN(TEST_SUBJECT_DATA);
90         testServer.setUrl("http://test.ca.server");
91         Authentication testAuthentication = new Authentication();
92         testAuthentication.setIak("testIak");
93         testAuthentication.setRv("testRv");
94         testServer.setAuthentication(testAuthentication);
95         testServer.setCaMode(CaMode.RA);
96
97         return testServer;
98     }
99
100 }