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.oom.certservice.certification.conversion;
 
  23 import static org.junit.jupiter.api.Assertions.assertThrows;
 
  24 import static org.junit.jupiter.api.Assertions.assertTrue;
 
  25 import static org.onap.oom.certservice.certification.TestData.TEST_CSR;
 
  26 import static org.onap.oom.certservice.certification.TestData.TEST_PK;
 
  27 import static org.onap.oom.certservice.certification.TestData.TEST_WRONG_CSR;
 
  28 import static org.onap.oom.certservice.certification.TestData.TEST_WRONG_PEM;
 
  30 import org.bouncycastle.util.encoders.Base64;
 
  31 import org.junit.jupiter.api.BeforeEach;
 
  32 import org.junit.jupiter.api.Test;
 
  33 import org.onap.oom.certservice.certification.TestData;
 
  34 import org.onap.oom.certservice.certification.exception.CsrDecryptionException;
 
  35 import org.onap.oom.certservice.certification.exception.DecryptionException;
 
  36 import org.onap.oom.certservice.certification.exception.KeyDecryptionException;
 
  37 import org.onap.oom.certservice.certification.model.CsrModel;
 
  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         System.out.println(decryptedCsr.toString());
 
  62         assertTrue(decryptedCsr.toString()
 
  63             .contains(TestData.EXPECTED_CERT_SANS));
 
  67     void shouldThrowCsrDecryptionExceptionWhenCsrIsIncorrect() {
 
  69         String encoderPK = new String(Base64.encode(TEST_PK.getBytes()));
 
  70         String wrongCsr = new String(Base64.encode(TEST_WRONG_CSR.getBytes()));
 
  73         Exception exception = assertThrows(
 
  74             CsrDecryptionException.class, () -> csrModelFactory
 
  75                 .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
 
  78         String expectedMessage = "Incorrect CSR, decryption failed";
 
  79         String actualMessage = exception.getMessage();
 
  82         assertTrue(actualMessage.contains(expectedMessage));
 
  86     void shouldThrowKeyDecryptionExceptionWhenKeyIsIncorrect() {
 
  88         String encoderPK = new String(Base64.encode(TEST_WRONG_PEM.getBytes()));
 
  89         String wrongCsr = new String(Base64.encode(TEST_CSR.getBytes()));
 
  92         Exception exception = assertThrows(
 
  93             KeyDecryptionException.class, () -> csrModelFactory
 
  94                 .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
 
  97         String expectedMessage = "Incorrect Key, decryption failed";
 
  98         String actualMessage = exception.getMessage();
 
 101         assertTrue(actualMessage.contains(expectedMessage));
 
 106     void shouldThrowCsrDecryptionExceptionWhenCsrIsNotInBase64Encoding() {
 
 108         String encoderPK = new String(Base64.encode(TEST_PK.getBytes()));
 
 109         String wrongCsr = "Not Base 64 Csr";
 
 112         Exception exception = assertThrows(
 
 113             CsrDecryptionException.class, () -> csrModelFactory
 
 114                 .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
 
 117         String expectedMessage = "Incorrect CSR, decryption failed";
 
 118         String actualMessage = exception.getMessage();
 
 121         assertTrue(actualMessage.contains(expectedMessage));
 
 125     void shouldThrowKeyDecryptionExceptionWhenPkIsNotInBase64Encoding() {
 
 127         String encoderPK = "Not Base64 Key";
 
 128         String wrongCsr = new String(Base64.encode(TEST_CSR.getBytes()));
 
 131         Exception exception = assertThrows(
 
 132             KeyDecryptionException.class, () -> csrModelFactory
 
 133                 .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
 
 136         String expectedMessage = "Incorrect Key, decryption failed";
 
 137         String actualMessage = exception.getMessage();
 
 140         assertTrue(actualMessage.contains(expectedMessage));