[OOM-CERT-SERVICE] Refactor CertService API code
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / oom / certservice / certification / conversion / StringBase64ToPrivateKeyConverterTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nokia.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.oom.certservice.certification.conversion;
22
23 import org.bouncycastle.util.encoders.Base64;
24 import org.junit.jupiter.api.Test;
25 import org.onap.oom.certservice.certification.exception.KeyDecryptionException;
26
27 import java.security.PrivateKey;
28
29 import static org.junit.jupiter.api.Assertions.assertEquals;
30 import static org.junit.jupiter.api.Assertions.assertNotNull;
31 import static org.junit.jupiter.api.Assertions.assertThrows;
32 import static org.junit.jupiter.api.Assertions.assertTrue;
33 import static org.onap.oom.certservice.certification.TestData.TEST_PEM;
34 import static org.onap.oom.certservice.certification.TestData.TEST_PK;
35
36 class StringBase64ToPrivateKeyConverterTest {
37
38     private static final String RSA = "RSA";
39     public static final String PKCS_8 = "PKCS#8";
40
41     @Test
42     void shouldUseProperAlgorithmWhenConverting() throws KeyDecryptionException {
43         // Given
44         StringBase64ToPrivateKeyConverter stringBase64ToPrivateKeyConverter = new StringBase64ToPrivateKeyConverter();
45         String encodedPK = new String(Base64.encode(TEST_PK.getBytes()));
46         // When
47         PrivateKey privateKey = stringBase64ToPrivateKeyConverter.convert(new StringBase64(encodedPK));
48         // Then
49         assertEquals(RSA, privateKey.getAlgorithm());
50     }
51
52     @Test
53     void shouldUsePkcs8FormatWhenConverting() throws KeyDecryptionException {
54         // Given
55         StringBase64ToPrivateKeyConverter stringBase64ToPrivateKeyConverter = new StringBase64ToPrivateKeyConverter();
56         String encodedPK = new String(Base64.encode(TEST_PK.getBytes()));
57         // When
58         PrivateKey privateKey = stringBase64ToPrivateKeyConverter.convert(new StringBase64(encodedPK));
59         // Then
60         assertEquals(PKCS_8, privateKey.getFormat());
61     }
62
63     @Test
64     void shouldCorrectlyConvertWhenPrivateKeyPemIsProper() throws KeyDecryptionException {
65         // Given
66         StringBase64ToPrivateKeyConverter stringBase64ToPrivateKeyConverter = new StringBase64ToPrivateKeyConverter();
67         String encodedPK = new String(Base64.encode(TEST_PK.getBytes()));
68         // When
69         PrivateKey privateKey = stringBase64ToPrivateKeyConverter.convert(new StringBase64(encodedPK));
70         // Then
71         assertNotNull(privateKey.getEncoded());
72     }
73
74     @Test
75     void shouldThrowExceptionWhenPrivateKeyPemIsNotProperPrivateKey() {
76         // Given
77         StringBase64ToPrivateKeyConverter stringBase64ToPrivateKeyConverter = new StringBase64ToPrivateKeyConverter();
78         StringBase64 privateKey = new StringBase64(TEST_PEM);
79         // When
80         Exception exception = assertThrows(
81                 KeyDecryptionException.class, () -> stringBase64ToPrivateKeyConverter.convert(privateKey));
82
83         String expectedMessage = "Incorrect Key, decryption failed";
84         String actualMessage = exception.getMessage();
85         // Then
86         assertTrue(actualMessage.contains(expectedMessage));
87     }
88
89 }