Resolve all checkstyle warnings
[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.List;
37
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
41
42 public class CsrMetaBuilderTest {
43
44     private CsrMetaBuilder csrMetaBuilder;
45
46     private static final String TEST_CA = "testCA";
47     private static final X500Name TEST_SUBJECT_DATA = new X500Name("CN=testIssuer");
48
49     @BeforeEach
50     void setUp() {
51         csrMetaBuilder = new CsrMetaBuilder();
52     }
53
54     @Test
55     void shouldBuildCsrMetaWhenGivenCsrModelAndCmpv2ServerAreCorrect() {
56         // Given
57         CsrModel testCsrModel = mock(CsrModel.class);
58         Cmpv2Server testServer = createTestServer();
59
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);
68
69         when(testCsrModel.getSubjectData()).thenReturn(TEST_SUBJECT_DATA);
70
71         // When
72         CsrMeta createdCsrMeta = csrMetaBuilder.build(testCsrModel, testServer);
73
74         // Then
75         assertThat(createdCsrMeta.getPassword()).isEqualTo(testServer.getAuthentication().getIak());
76         assertThat(createdCsrMeta.getSenderKid()).isEqualTo(testServer.getAuthentication().getRv());
77         assertThat(createdCsrMeta.getCaUrl()).isEqualTo(testServer.getUrl());
78         assertThat(createdCsrMeta.getSans()).containsAll(testSans);
79         assertThat(createdCsrMeta.getKeyPair().getPrivate()).isEqualTo(mockPrivateKey);
80         assertThat(createdCsrMeta.getKeyPair().getPublic()).isEqualTo(mockPublicKey);
81         assertThat(createdCsrMeta.getX500Name()).isEqualTo(TEST_SUBJECT_DATA);
82         assertThat(createdCsrMeta.getIssuerX500Name()).isEqualTo(TEST_SUBJECT_DATA);
83     }
84
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);
95
96         return testServer;
97     }
98
99 }