2 * ============LICENSE_START=======================================================
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;
23 import org.bouncycastle.util.encoders.Base64;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.onap.aaf.certservice.certification.CsrModelFactory.StringBase64;
27 import org.onap.aaf.certservice.certification.exception.CsrDecryptionException;
28 import org.onap.aaf.certservice.certification.exception.DecryptionException;
29 import org.onap.aaf.certservice.certification.exception.KeyDecryptionException;
30 import org.onap.aaf.certservice.certification.model.CsrModel;
32 import static org.junit.jupiter.api.Assertions.assertThrows;
33 import static org.junit.jupiter.api.Assertions.assertTrue;
34 import static org.onap.aaf.certservice.certification.TestData.TEST_CSR;
35 import static org.onap.aaf.certservice.certification.TestData.TEST_PK;
36 import static org.onap.aaf.certservice.certification.TestData.TEST_WRONG_CSR;
37 import static org.onap.aaf.certservice.certification.TestData.TEST_WRONG_PEM;
40 class CsrModelFactoryTest {
42 private CsrModelFactory csrModelFactory;
46 csrModelFactory = new CsrModelFactory();
50 void shouldDecryptCsrAndReturnStringWithDataAboutIt() throws DecryptionException {
52 String encoderCsr = new String(Base64.encode(TEST_CSR.getBytes()));
53 String encoderPK = new String(Base64.encode(TEST_PK.getBytes()));
56 CsrModel decryptedCsr = csrModelFactory
57 .createCsrModel(new StringBase64(encoderCsr), new StringBase64(encoderPK));
61 decryptedCsr.toString()
63 "C=US,ST=California,L=San-Francisco,O=Linux-Foundation,"
64 + "OU=ONAP,CN=onap.org,E=tester@onap.org")
66 decryptedCsr.toString()
67 .contains("SANs: [gerrit.onap.org, test.onap.org, onap.com]")
72 void shouldThrowCsrDecryptionExceptionWhenCsrIsIncorrect() {
74 String encoderPK = new String(Base64.encode(TEST_PK.getBytes()));
75 String wrongCsr = new String(Base64.encode(TEST_WRONG_CSR.getBytes()));
78 Exception exception = assertThrows(
79 CsrDecryptionException.class, () -> csrModelFactory
80 .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
83 String expectedMessage = "Incorrect CSR, decryption failed";
84 String actualMessage = exception.getMessage();
87 assertTrue(actualMessage.contains(expectedMessage));
91 void shouldThrowKeyDecryptionExceptionWhenKeyIsIncorrect() {
93 String encoderPK = new String(Base64.encode(TEST_WRONG_PEM.getBytes()));
94 String wrongCsr = new String(Base64.encode(TEST_CSR.getBytes()));
97 Exception exception = assertThrows(
98 KeyDecryptionException.class, () -> csrModelFactory
99 .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
102 String expectedMessage = "Incorrect Key, decryption failed";
103 String actualMessage = exception.getMessage();
106 assertTrue(actualMessage.contains(expectedMessage));
111 void shouldThrowCsrDecryptionExceptionWhenCsrIsNotInBase64Encoding() {
113 String encoderPK = new String(Base64.encode(TEST_PK.getBytes()));
114 String wrongCsr = "Not Base 64 Csr";
117 Exception exception = assertThrows(
118 CsrDecryptionException.class, () -> csrModelFactory
119 .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
122 String expectedMessage = "Incorrect CSR, decryption failed";
123 String actualMessage = exception.getMessage();
126 assertTrue(actualMessage.contains(expectedMessage));
130 void shouldThrowKeyDecryptionExceptionWhenPkIsNotInBase64Encoding() {
132 String encoderPK = "Not Base64 Key";
133 String wrongCsr = new String(Base64.encode(TEST_CSR.getBytes()));
136 Exception exception = assertThrows(
137 KeyDecryptionException.class, () -> csrModelFactory
138 .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
141 String expectedMessage = "Incorrect Key, decryption failed";
142 String actualMessage = exception.getMessage();
145 assertTrue(actualMessage.contains(expectedMessage));