diff options
author | Michal Banka <michal.banka@nokia.com> | 2020-03-17 16:46:56 +0100 |
---|---|---|
committer | Michal Banka <michal.banka@nokia.com> | 2020-03-18 11:30:45 +0100 |
commit | 8252e258b55d09a389a37d9880b97f16fd4d9275 (patch) | |
tree | 3cf2856ce20c3b4278e552090ba88584a6a4dfa7 /certService/src/test/java | |
parent | c85a8965e876fde2089582a6468eb02ce18bafd5 (diff) |
Remove CSRMeta class dependency
Signed-off-by: Michal Banka <michal.banka@nokia.com>
Change-Id: If3458612629dd84f95bf9ba1e0778e65696bb13d
Issue-ID: AAF-1107
Diffstat (limited to 'certService/src/test/java')
3 files changed, 41 insertions, 188 deletions
diff --git a/certService/src/test/java/org/onap/aaf/certservice/certification/adapter/Cmpv2ClientAdapterTest.java b/certService/src/test/java/org/onap/aaf/certservice/certification/adapter/Cmpv2ClientAdapterTest.java index 32fd2074..e18d1ffb 100644 --- a/certService/src/test/java/org/onap/aaf/certservice/certification/adapter/Cmpv2ClientAdapterTest.java +++ b/certService/src/test/java/org/onap/aaf/certservice/certification/adapter/Cmpv2ClientAdapterTest.java @@ -51,7 +51,6 @@ import org.onap.aaf.certservice.certification.model.CertificationModel; import org.onap.aaf.certservice.certification.model.CsrModel; import org.onap.aaf.certservice.cmpv2client.api.CmpClient; import org.onap.aaf.certservice.cmpv2client.exceptions.CmpClientException; -import org.onap.aaf.certservice.cmpv2client.external.CsrMeta; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest @@ -83,10 +82,6 @@ class Cmpv2ClientAdapterTest { private X509Certificate certificate; @Mock private CertificateFactoryProvider certificateFactoryProvider; - @Mock - private CsrMetaBuilder csrMetaBuilder; - @Mock - private CsrMeta csrMeta; @InjectMocks private Cmpv2ClientAdapter adapter; @@ -102,7 +97,7 @@ class Cmpv2ClientAdapterTest { stubInternalProperties(); // When - Mockito.when(cmpClient.createCertificate(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) + Mockito.when(cmpClient.createCertificate(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) .thenThrow(new CmpClientException(TEST_MSG)); // Then @@ -117,7 +112,7 @@ class Cmpv2ClientAdapterTest { stubInternalProperties(); // When - Mockito.when(cmpClient.createCertificate(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) + Mockito.when(cmpClient.createCertificate(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) .thenReturn(createCorrectClientResponse()); CertificationModel certificationModel = adapter.callCmpClient(csrModel, server); @@ -144,7 +139,7 @@ class Cmpv2ClientAdapterTest { stubInternalProperties(); // When - Mockito.when(cmpClient.createCertificate(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) + Mockito.when(cmpClient.createCertificate(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) .thenReturn(createCorrectClientResponse()); Mockito.when(certificateFactoryProvider.generateCertificate(Mockito.any())) .thenThrow(new CertificateException(TEST_MSG)); @@ -179,7 +174,6 @@ class Cmpv2ClientAdapterTest { Mockito.when(holder.toASN1Structure()).thenReturn(asn1Certificate); Mockito.when(certificateFactoryProvider.generateCertificate(Mockito.any())).thenReturn(certificate); Mockito.when(holder.toASN1Structure().getEncoded()).thenReturn("".getBytes()); - Mockito.when(csrMetaBuilder.build(csrModel, server)).thenReturn(csrMeta); } } 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 deleted file mode 100644 index feb4bdba..00000000 --- a/certService/src/test/java/org/onap/aaf/certservice/certification/adapter/CsrMetaBuilderTest.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * ============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; - } - -} diff --git a/certService/src/test/java/org/onap/aaf/certservice/cmpv2client/Cmpv2ClientTest.java b/certService/src/test/java/org/onap/aaf/certservice/cmpv2client/Cmpv2ClientTest.java index 3f5a2540..bea6b6a1 100644 --- a/certService/src/test/java/org/onap/aaf/certservice/cmpv2client/Cmpv2ClientTest.java +++ b/certService/src/test/java/org/onap/aaf/certservice/cmpv2client/Cmpv2ClientTest.java @@ -30,7 +30,6 @@ import java.io.InputStream; import java.io.OutputStream; import java.security.KeyFactory; import java.security.KeyPair; -import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.PrivateKey; @@ -42,7 +41,7 @@ import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.ArrayList; +import java.util.Collections; import java.util.Date; import java.util.List; @@ -50,15 +49,18 @@ import org.apache.commons.io.IOUtils; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.impl.client.CloseableHttpClient; -import org.bouncycastle.cert.CertException; +import org.bouncycastle.asn1.x500.X500Name; +import org.bouncycastle.asn1.x500.X500NameBuilder; +import org.bouncycastle.asn1.x500.style.BCStyle; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mock; +import org.onap.aaf.certservice.certification.configuration.model.Authentication; +import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server; +import org.onap.aaf.certservice.certification.model.CsrModel; import org.onap.aaf.certservice.cmpv2client.exceptions.CmpClientException; -import org.onap.aaf.certservice.cmpv2client.external.CsrMeta; -import org.onap.aaf.certservice.cmpv2client.external.Rdn; import org.onap.aaf.certservice.cmpv2client.impl.CmpClientImpl; class Cmpv2ClientTest { @@ -67,12 +69,11 @@ class Cmpv2ClientTest { Security.addProvider(new BouncyCastleProvider()); } - private CsrMeta csrMeta; + private CsrModel csrModel; + private Cmpv2Server server; private Date notBefore; private Date notAfter; - - @Mock - KeyPairGenerator kpg; + private X500Name dn; @Mock X509Certificate cert; @@ -87,22 +88,15 @@ class Cmpv2ClientTest { HttpEntity httpEntity; private static KeyPair keyPair; - private static ArrayList<Rdn> rdns; @BeforeEach void setUp() throws NoSuchProviderException, NoSuchAlgorithmException, IOException, InvalidKeySpecException { - KeyPairGenerator keyGenerator; - keyGenerator = KeyPairGenerator.getInstance("RSA", BouncyCastleProvider.PROVIDER_NAME); - keyGenerator.initialize(2048); keyPair = loadKeyPair(); - rdns = new ArrayList<>(); - try { - rdns.add(new Rdn("O=CommonCompany")); - } catch (CertException e) { - e.printStackTrace(); - } + dn = new X500NameBuilder() + .addRDN(BCStyle.O, "TestOrganization") + .build(); initMocks(this); } @@ -133,15 +127,10 @@ class Cmpv2ClientTest { // given Date beforeDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2019/11/11 12:00:00"); Date afterDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2020/11/11 12:00:00"); - setCsrMetaValuesAndDateValues( - rdns, - "CN=CommonName", - "CN=ManagementCA", - "CommonName.com", - "CommonName@cn.com", + setCsrModelAndServerValues( "mypassword", - "http://127.0.0.1/ejbca/publicweb/cmp/cmp", "senderKID", + "http://127.0.0.1/ejbca/publicweb/cmp/cmp", beforeDate, afterDate); when(httpClient.execute(any())).thenReturn(httpResponse); @@ -164,7 +153,7 @@ class Cmpv2ClientTest { CmpClientImpl cmpClient = spy(new CmpClientImpl(httpClient)); // when List<List<X509Certificate>> cmpClientResult = - cmpClient.createCertificate("data", "RA", csrMeta, cert, notBefore, notAfter); + cmpClient.createCertificate("data", "RA", csrModel, server, cert, notBefore, notAfter); // then assertNotNull(cmpClientResult); } @@ -176,15 +165,10 @@ class Cmpv2ClientTest { // given Date beforeDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2019/11/11 12:00:00"); Date afterDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2020/11/11 12:00:00"); - setCsrMetaValuesAndDateValues( - rdns, - "CN=CommonName", - "CN=ManagementCA", - "CommonName.com", - "CommonName@cn.com", + setCsrModelAndServerValues( "password", - "http://127.0.0.1/ejbca/publicweb/cmp/cmp", "senderKID", + "http://127.0.0.1/ejbca/publicweb/cmp/cmp", beforeDate, afterDate); when(httpClient.execute(any())).thenReturn(httpResponse); @@ -208,7 +192,7 @@ class Cmpv2ClientTest { // then Assertions.assertThrows( CmpClientException.class, - () -> cmpClient.createCertificate("data", "RA", csrMeta, cert, notBefore, notAfter)); + () -> cmpClient.createCertificate("data", "RA", csrModel, server, cert, notBefore, notAfter)); } @Test @@ -217,15 +201,10 @@ class Cmpv2ClientTest { // given Date beforeDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2019/11/11 12:00:00"); Date afterDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2020/11/11 12:00:00"); - setCsrMetaValuesAndDateValues( - rdns, - "CN=CommonName", - "CN=ManagementCA", - "CommonName.com", - "CommonName@cn.com", + setCsrModelAndServerValues( "password", - "http://127.0.0.1/ejbca/publicweb/cmp/cmp", "senderKID", + "http://127.0.0.1/ejbca/publicweb/cmp/cmp", beforeDate, afterDate); when(httpClient.execute(any())).thenReturn(httpResponse); @@ -250,7 +229,7 @@ class Cmpv2ClientTest { // then Assertions.assertThrows( CmpClientException.class, - () -> cmpClient.createCertificate("data", "RA", csrMeta, cert, notBefore, notAfter)); + () -> cmpClient.createCertificate("data", "RA", csrModel, server, cert, notBefore, notAfter)); } @Test @@ -259,22 +238,17 @@ class Cmpv2ClientTest { // given Date beforeDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2020/11/11 12:00:00"); Date afterDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2019/11/11 12:00:00"); - setCsrMetaValuesAndDateValues( - rdns, - "CN=CommonName", - "CN=ManagementCA", - "CommonName.com", - "CommonName@cn.com", + setCsrModelAndServerValues( "password", - "http://127.0.0.1/ejbca/publicweb/cmp/cmp", "senderKID", + "http://127.0.0.1/ejbca/publicweb/cmp/cmp", beforeDate, afterDate); CmpClientImpl cmpClient = new CmpClientImpl(httpClient); // then Assertions.assertThrows( IllegalArgumentException.class, - () -> cmpClient.createCertificate("data", "RA", csrMeta, cert, notBefore, notAfter)); + () -> cmpClient.createCertificate("data", "RA", csrModel, server, cert, notBefore, notAfter)); } @Test @@ -283,15 +257,10 @@ class Cmpv2ClientTest { // given Date beforeDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2019/11/11 12:00:00"); Date afterDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2020/11/11 12:00:00"); - setCsrMetaValuesAndDateValues( - rdns, - "CN=Common", - "CN=CommonCA", - "Common.com", - "Common@cn.com", + setCsrModelAndServerValues( "myPassword", - "http://127.0.0.1/ejbca/publicweb/cmp/cmpTest", "sender", + "http://127.0.0.1/ejbca/publicweb/cmp/cmpTest", beforeDate, afterDate); when(httpClient.execute(any())).thenThrow(IOException.class); @@ -299,30 +268,19 @@ class Cmpv2ClientTest { // then Assertions.assertThrows( CmpClientException.class, - () -> cmpClient.createCertificate("data", "RA", csrMeta, cert, notBefore, notAfter)); + () -> cmpClient.createCertificate("data", "RA", csrModel, server, cert, notBefore, notAfter)); } - private void setCsrMetaValuesAndDateValues( - List<Rdn> rdns, - String cn, - String issuerCn, - String san, - String email, - String password, - String externalCaUrl, - String senderKid, - Date notBefore, - Date notAfter) { - csrMeta = new CsrMeta(rdns); - csrMeta.setCn(cn); - csrMeta.addSan(san); - csrMeta.setPassword(password); - csrMeta.setEmail(email); - csrMeta.setIssuerCn(issuerCn); - when(kpg.generateKeyPair()).thenReturn(keyPair); - csrMeta.getKeyPairOrGenerateIfNull(); - csrMeta.setCaUrl(externalCaUrl); - csrMeta.setSenderKid(senderKid); + private void setCsrModelAndServerValues(String iak, String rv, String externalCaUrl, Date notBefore, Date notAfter) { + csrModel = new CsrModel(null, dn, keyPair.getPrivate(), keyPair.getPublic(), Collections.emptyList()); + + Authentication authentication = new Authentication(); + authentication.setIak(iak); + authentication.setRv(rv); + server = new Cmpv2Server(); + server.setAuthentication(authentication); + server.setUrl(externalCaUrl); + server.setIssuerDN(dn); this.notBefore = notBefore; this.notAfter = notAfter; } |