2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 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.oom.certservice.certification.conversion;
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.oom.certservice.certification.TestData;
27 import org.onap.oom.certservice.certification.exception.CsrDecryptionException;
28 import org.onap.oom.certservice.certification.exception.DecryptionException;
29 import org.onap.oom.certservice.certification.exception.KeyDecryptionException;
30 import org.onap.oom.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.oom.certservice.certification.TestData.TEST_CSR;
35 import static org.onap.oom.certservice.certification.TestData.TEST_PK;
36 import static org.onap.oom.certservice.certification.TestData.TEST_WRONG_CSR;
37 import static org.onap.oom.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));
59 assertTrue(decryptedCsr.toString()
60 .contains(TestData.EXPECTED_CERT_SUBJECT));
61 assertTrue(decryptedCsr.toString()
62 .contains(TestData.EXPECTED_CERT_SANS));
66 void shouldThrowCsrDecryptionExceptionWhenCsrIsIncorrect() {
68 String encoderPK = new String(Base64.encode(TEST_PK.getBytes()));
69 String wrongCsr = new String(Base64.encode(TEST_WRONG_CSR.getBytes()));
72 Exception exception = assertThrows(
73 CsrDecryptionException.class, () -> csrModelFactory
74 .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
77 String expectedMessage = "Incorrect CSR, decryption failed";
78 String actualMessage = exception.getMessage();
81 assertTrue(actualMessage.contains(expectedMessage));
85 void shouldThrowKeyDecryptionExceptionWhenKeyIsIncorrect() {
87 String encoderPK = new String(Base64.encode(TEST_WRONG_PEM.getBytes()));
88 String wrongCsr = new String(Base64.encode(TEST_CSR.getBytes()));
91 Exception exception = assertThrows(
92 KeyDecryptionException.class, () -> csrModelFactory
93 .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
96 String expectedMessage = "Incorrect Key, decryption failed";
97 String actualMessage = exception.getMessage();
100 assertTrue(actualMessage.contains(expectedMessage));
105 void shouldThrowCsrDecryptionExceptionWhenCsrIsNotInBase64Encoding() {
107 String encoderPK = new String(Base64.encode(TEST_PK.getBytes()));
108 String wrongCsr = "Not Base 64 Csr";
111 Exception exception = assertThrows(
112 CsrDecryptionException.class, () -> csrModelFactory
113 .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
116 String expectedMessage = "Incorrect CSR, decryption failed";
117 String actualMessage = exception.getMessage();
120 assertTrue(actualMessage.contains(expectedMessage));
124 void shouldThrowKeyDecryptionExceptionWhenPkIsNotInBase64Encoding() {
126 String encoderPK = "Not Base64 Key";
127 String wrongCsr = new String(Base64.encode(TEST_CSR.getBytes()));
130 Exception exception = assertThrows(
131 KeyDecryptionException.class, () -> csrModelFactory
132 .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
135 String expectedMessage = "Incorrect Key, decryption failed";
136 String actualMessage = exception.getMessage();
139 assertTrue(actualMessage.contains(expectedMessage));