diff options
Diffstat (limited to 'certService/src/test/java/org/onap/aaf/certservice/certification/adapter/CsrMetaBuilderTest.java')
-rw-r--r-- | certService/src/test/java/org/onap/aaf/certservice/certification/adapter/CsrMetaBuilderTest.java | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/certService/src/test/java/org/onap/aaf/certservice/certification/adapter/CsrMetaBuilderTest.java b/certService/src/test/java/org/onap/aaf/certservice/certification/adapter/CsrMetaBuilderTest.java new file mode 100644 index 00000000..feb4bdba --- /dev/null +++ b/certService/src/test/java/org/onap/aaf/certservice/certification/adapter/CsrMetaBuilderTest.java @@ -0,0 +1,99 @@ +/* + * ============LICENSE_START======================================================= + * AAF Certification Service + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aaf.certservice.certification.adapter; + +import org.bouncycastle.asn1.x500.X500Name; +import org.bouncycastle.pkcs.PKCS10CertificationRequest; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onap.aaf.certservice.certification.configuration.model.Authentication; +import org.onap.aaf.certservice.certification.configuration.model.CaMode; +import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server; +import org.onap.aaf.certservice.certification.model.CsrModel; +import org.onap.aaf.certservice.cmpv2client.external.CsrMeta; + +import java.security.PrivateKey; +import java.security.PublicKey; +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class CsrMetaBuilderTest { + + private CsrMetaBuilder csrMetaBuilder; + + private static final String TEST_CA = "testCA"; + private static final X500Name TEST_SUBJECT_DATA = new X500Name("CN=testIssuer"); + + @BeforeEach + void setUp() { + csrMetaBuilder = new CsrMetaBuilder(); + } + + @Test + void shouldBuildCsrMetaWhenGivenCsrModelAndCmpv2ServerAreCorrect() { + // Given + CsrModel testCsrModel = mock(CsrModel.class); + Cmpv2Server testServer = createTestServer(); + + PKCS10CertificationRequest certificationRequest = mock(PKCS10CertificationRequest.class); + when(testCsrModel.getCsr()).thenReturn(certificationRequest); + PrivateKey mockPrivateKey = mock(PrivateKey.class); + when(testCsrModel.getPrivateKey()).thenReturn(mockPrivateKey); + PublicKey mockPublicKey = mock(PublicKey.class); + when(testCsrModel.getPublicKey()).thenReturn(mockPublicKey); + List<String> testSans = Arrays.asList("SAN01", "SAN02"); + when(testCsrModel.getSans()).thenReturn(testSans); + + when(testCsrModel.getSubjectData()).thenReturn(TEST_SUBJECT_DATA); + + // When + CsrMeta createdCsrMeta = csrMetaBuilder.build(testCsrModel, testServer); + + // Then + assertThat(createdCsrMeta.getPassword()).isEqualTo(testServer.getAuthentication().getIak()); + assertThat(createdCsrMeta.getSenderKid()).isEqualTo(testServer.getAuthentication().getRv()); + assertThat(createdCsrMeta.getCaUrl()).isEqualTo(testServer.getUrl()); + assertThat(createdCsrMeta.getSans()).containsAll(testSans); + assertThat(createdCsrMeta.getKeyPair().getPrivate()).isEqualTo(mockPrivateKey); + assertThat(createdCsrMeta.getKeyPair().getPublic()).isEqualTo(mockPublicKey); + assertThat(createdCsrMeta.getX500Name()).isEqualTo(TEST_SUBJECT_DATA); + assertThat(createdCsrMeta.getIssuerX500Name()).isEqualTo(TEST_SUBJECT_DATA); + } + + private Cmpv2Server createTestServer() { + Cmpv2Server testServer = new Cmpv2Server(); + testServer.setCaName(TEST_CA); + testServer.setIssuerDN(TEST_SUBJECT_DATA); + testServer.setUrl("http://test.ca.server"); + Authentication testAuthentication = new Authentication(); + testAuthentication.setIak("testIak"); + testAuthentication.setRv("testRv"); + testServer.setAuthentication(testAuthentication); + testServer.setCaMode(CaMode.RA); + + return testServer; + } + +} |