Merge "[OOM-CERT-SERVICE] Add Certification Request functionality"
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / oom / certservice / certification / 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;
22
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.exception.CsrDecryptionException;
27 import org.onap.oom.certservice.certification.exception.DecryptionException;
28 import org.onap.oom.certservice.certification.exception.KeyDecryptionException;
29 import org.onap.oom.certservice.certification.model.CsrModel;
30
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_CSR;
34 import static org.onap.oom.certservice.certification.TestData.TEST_PK;
35 import static org.onap.oom.certservice.certification.TestData.TEST_WRONG_CSR;
36 import static org.onap.oom.certservice.certification.TestData.TEST_WRONG_PEM;
37
38
39 class CsrModelFactoryTest {
40
41     private CsrModelFactory csrModelFactory;
42
43     @BeforeEach
44     void setUp() {
45         csrModelFactory = new CsrModelFactory();
46     }
47
48     @Test
49     void shouldDecryptCsrAndReturnStringWithDataAboutIt() throws DecryptionException {
50         // given
51         String encoderCsr = new String(Base64.encode(TEST_CSR.getBytes()));
52         String encoderPK = new String(Base64.encode(TEST_PK.getBytes()));
53
54         // when
55         CsrModel decryptedCsr = csrModelFactory
56             .createCsrModel(new StringBase64(encoderCsr), new StringBase64(encoderPK));
57
58         assertTrue(decryptedCsr.toString()
59             .contains(TestData.EXPECTED_CERT_SUBJECT));
60         System.out.println(decryptedCsr.toString());
61         assertTrue(decryptedCsr.toString()
62             .contains(TestData.EXPECTED_CERT_SANS));
63     }
64
65     @Test
66     void shouldThrowCsrDecryptionExceptionWhenCsrIsIncorrect() {
67         // given
68         String encoderPK = new String(Base64.encode(TEST_PK.getBytes()));
69         String wrongCsr = new String(Base64.encode(TEST_WRONG_CSR.getBytes()));
70
71         // when
72         Exception exception = assertThrows(
73             CsrDecryptionException.class, () -> csrModelFactory
74                 .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
75         );
76
77         String expectedMessage = "Incorrect CSR, decryption failed";
78         String actualMessage = exception.getMessage();
79
80         // then
81         assertTrue(actualMessage.contains(expectedMessage));
82     }
83
84     @Test
85     void shouldThrowKeyDecryptionExceptionWhenKeyIsIncorrect() {
86         // given
87         String encoderPK = new String(Base64.encode(TEST_WRONG_PEM.getBytes()));
88         String wrongCsr = new String(Base64.encode(TEST_CSR.getBytes()));
89
90         // when
91         Exception exception = assertThrows(
92             KeyDecryptionException.class, () -> csrModelFactory
93                 .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
94         );
95
96         String expectedMessage = "Incorrect Key, decryption failed";
97         String actualMessage = exception.getMessage();
98
99         // then
100         assertTrue(actualMessage.contains(expectedMessage));
101     }
102
103
104     @Test
105     void shouldThrowCsrDecryptionExceptionWhenCsrIsNotInBase64Encoding() {
106         // given
107         String encoderPK = new String(Base64.encode(TEST_PK.getBytes()));
108         String wrongCsr = "Not Base 64 Csr";
109
110         // when
111         Exception exception = assertThrows(
112             CsrDecryptionException.class, () -> csrModelFactory
113                 .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
114         );
115
116         String expectedMessage = "Incorrect CSR, decryption failed";
117         String actualMessage = exception.getMessage();
118
119         // then
120         assertTrue(actualMessage.contains(expectedMessage));
121     }
122
123     @Test
124     void shouldThrowKeyDecryptionExceptionWhenPkIsNotInBase64Encoding() {
125         // given
126         String encoderPK = "Not Base64 Key";
127         String wrongCsr = new String(Base64.encode(TEST_CSR.getBytes()));
128
129         // when
130         Exception exception = assertThrows(
131             KeyDecryptionException.class, () -> csrModelFactory
132                 .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
133         );
134
135         String expectedMessage = "Incorrect Key, decryption failed";
136         String actualMessage = exception.getMessage();
137
138         // then
139         assertTrue(actualMessage.contains(expectedMessage));
140     }
141 }