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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aaf.certservice.certification.adapter;
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;
33 import java.security.PrivateKey;
34 import java.security.PublicKey;
35 import java.util.Arrays;
36 import java.util.List;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
42 public class CSRMetaBuilderTest {
44 private CSRMetaBuilder csrMetaBuilder;
46 private static final String TEST_CA = "testCA";
47 private static final X500Name TEST_SUBJECT_DATA = new X500Name("CN=testIssuer");
51 csrMetaBuilder = new CSRMetaBuilder();
55 void shouldBuildCsrMetaWhenGivenCsrModelAndCmpv2ServerAreCorrect() {
57 CsrModel testCsrModel = mock(CsrModel.class);
58 Cmpv2Server testServer = createTestServer();
60 PKCS10CertificationRequest certificationRequest = mock(PKCS10CertificationRequest.class);
61 when(testCsrModel.getCsr()).thenReturn(certificationRequest);
62 PrivateKey mockPrivateKey = mock(PrivateKey.class);
63 when(testCsrModel.getPrivateKey()).thenReturn(mockPrivateKey);
64 PublicKey mockPublicKey = mock(PublicKey.class);
65 when(testCsrModel.getPublicKey()).thenReturn(mockPublicKey);
66 List<String> testSans = Arrays.asList("SAN01","SAN02");
67 when(testCsrModel.getSans()).thenReturn(testSans);
69 when(testCsrModel.getSubjectData()).thenReturn(TEST_SUBJECT_DATA);
72 CSRMeta createdCSRMeta = csrMetaBuilder.build(testCsrModel, testServer);
75 assertThat(createdCSRMeta.password()).isEqualTo(testServer.getAuthentication().getIak());
76 assertThat(createdCSRMeta.senderKid()).isEqualTo(testServer.getAuthentication().getRv());
77 assertThat(createdCSRMeta.caUrl()).isEqualTo(testServer.getUrl());
78 assertThat(createdCSRMeta.sans()).containsAll(testSans);
79 assertThat(createdCSRMeta.keyPair().getPrivate()).isEqualTo(mockPrivateKey);
80 assertThat(createdCSRMeta.keyPair().getPublic()).isEqualTo(mockPublicKey);
81 assertThat(createdCSRMeta.x500Name()).isEqualTo(TEST_SUBJECT_DATA);
82 assertThat(createdCSRMeta.issuerx500Name()).isEqualTo(TEST_SUBJECT_DATA);
85 private Cmpv2Server createTestServer() {
86 Cmpv2Server testServer = new Cmpv2Server();
87 testServer.setCaName(TEST_CA);
88 testServer.setIssuerDN(TEST_SUBJECT_DATA);
89 testServer.setUrl("http://test.ca.server");
90 Authentication testAuthentication = new Authentication();
91 testAuthentication.setIak("testIak");
92 testAuthentication.setRv("testRv");
93 testServer.setAuthentication(testAuthentication);
94 testServer.setCaMode(CaMode.RA);