[OOM-CERT-SERVICE] Code refactor
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / oom / certservice / certification / conversion / CsrModelFactoryTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Cert Service
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.oom.certservice.certification.conversion;
22
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;
29
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;
38
39
40 class CsrModelFactoryTest {
41
42     private CsrModelFactory csrModelFactory;
43
44     @BeforeEach
45     void setUp() {
46         csrModelFactory = new CsrModelFactory();
47     }
48
49     @Test
50     void shouldDecryptCsrAndReturnStringWithDataAboutIt() throws DecryptionException {
51         // given
52         String encoderCsr = new String(Base64.encode(TEST_CSR.getBytes()));
53         String encoderPK = new String(Base64.encode(TEST_PK.getBytes()));
54
55         // when
56         CsrModel decryptedCsr = csrModelFactory
57             .createCsrModel(new StringBase64(encoderCsr), new StringBase64(encoderPK));
58
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));
64     }
65
66     @Test
67     void shouldThrowCsrDecryptionExceptionWhenCsrIsIncorrect() {
68         // given
69         String encoderPK = new String(Base64.encode(TEST_PK.getBytes()));
70         String wrongCsr = new String(Base64.encode(TEST_WRONG_CSR.getBytes()));
71
72         // when
73         Exception exception = assertThrows(
74             CsrDecryptionException.class, () -> csrModelFactory
75                 .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
76         );
77
78         String expectedMessage = "Incorrect CSR, decryption failed";
79         String actualMessage = exception.getMessage();
80
81         // then
82         assertTrue(actualMessage.contains(expectedMessage));
83     }
84
85     @Test
86     void shouldThrowKeyDecryptionExceptionWhenKeyIsIncorrect() {
87         // given
88         String encoderPK = new String(Base64.encode(TEST_WRONG_PEM.getBytes()));
89         String wrongCsr = new String(Base64.encode(TEST_CSR.getBytes()));
90
91         // when
92         Exception exception = assertThrows(
93             KeyDecryptionException.class, () -> csrModelFactory
94                 .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
95         );
96
97         String expectedMessage = "Incorrect Key, decryption failed";
98         String actualMessage = exception.getMessage();
99
100         // then
101         assertTrue(actualMessage.contains(expectedMessage));
102     }
103
104
105     @Test
106     void shouldThrowCsrDecryptionExceptionWhenCsrIsNotInBase64Encoding() {
107         // given
108         String encoderPK = new String(Base64.encode(TEST_PK.getBytes()));
109         String wrongCsr = "Not Base 64 Csr";
110
111         // when
112         Exception exception = assertThrows(
113             CsrDecryptionException.class, () -> csrModelFactory
114                 .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
115         );
116
117         String expectedMessage = "Incorrect CSR, decryption failed";
118         String actualMessage = exception.getMessage();
119
120         // then
121         assertTrue(actualMessage.contains(expectedMessage));
122     }
123
124     @Test
125     void shouldThrowKeyDecryptionExceptionWhenPkIsNotInBase64Encoding() {
126         // given
127         String encoderPK = "Not Base64 Key";
128         String wrongCsr = new String(Base64.encode(TEST_CSR.getBytes()));
129
130         // when
131         Exception exception = assertThrows(
132             KeyDecryptionException.class, () -> csrModelFactory
133                 .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
134         );
135
136         String expectedMessage = "Incorrect Key, decryption failed";
137         String actualMessage = exception.getMessage();
138
139         // then
140         assertTrue(actualMessage.contains(expectedMessage));
141     }
142 }