Automation adds INFO.yaml
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / aaf / certservice / certification / CsrModelFactoryTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
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.aaf.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.aaf.certservice.certification.CsrModelFactory.StringBase64;
27 import org.onap.aaf.certservice.certification.exception.CsrDecryptionException;
28 import org.onap.aaf.certservice.certification.exception.DecryptionException;
29 import org.onap.aaf.certservice.certification.exception.KeyDecryptionException;
30 import org.onap.aaf.certservice.certification.model.CsrModel;
31
32 import static org.junit.jupiter.api.Assertions.assertThrows;
33 import static org.junit.jupiter.api.Assertions.assertTrue;
34 import static org.onap.aaf.certservice.certification.TestData.TEST_CSR;
35 import static org.onap.aaf.certservice.certification.TestData.TEST_PK;
36 import static org.onap.aaf.certservice.certification.TestData.TEST_WRONG_CSR;
37 import static org.onap.aaf.certservice.certification.TestData.TEST_WRONG_PEM;
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         // then
60         assertTrue(
61                 decryptedCsr.toString()
62                         .contains(
63                                 "C=US,ST=California,L=San-Francisco,O=Linux-Foundation,"
64                                         + "OU=ONAP,CN=onap.org,E=tester@onap.org")
65                         &&
66                         decryptedCsr.toString()
67                                 .contains("SANs: [gerrit.onap.org, test.onap.org, onap.com]")
68         );
69     }
70
71     @Test
72     void shouldThrowCsrDecryptionExceptionWhenCsrIsIncorrect() {
73         // given
74         String encoderPK = new String(Base64.encode(TEST_PK.getBytes()));
75         String wrongCsr = new String(Base64.encode(TEST_WRONG_CSR.getBytes()));
76
77         // when
78         Exception exception = assertThrows(
79                 CsrDecryptionException.class, () -> csrModelFactory
80                         .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
81         );
82
83         String expectedMessage = "Incorrect CSR, decryption failed";
84         String actualMessage = exception.getMessage();
85
86         // then
87         assertTrue(actualMessage.contains(expectedMessage));
88     }
89
90     @Test
91     void shouldThrowKeyDecryptionExceptionWhenKeyIsIncorrect() {
92         // given
93         String encoderPK = new String(Base64.encode(TEST_WRONG_PEM.getBytes()));
94         String wrongCsr = new String(Base64.encode(TEST_CSR.getBytes()));
95
96         // when
97         Exception exception = assertThrows(
98                 KeyDecryptionException.class, () -> csrModelFactory
99                         .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
100         );
101
102         String expectedMessage = "Incorrect Key, decryption failed";
103         String actualMessage = exception.getMessage();
104
105         // then
106         assertTrue(actualMessage.contains(expectedMessage));
107     }
108
109
110     @Test
111     void shouldThrowCsrDecryptionExceptionWhenCsrIsNotInBase64Encoding() {
112         // given
113         String encoderPK = new String(Base64.encode(TEST_PK.getBytes()));
114         String wrongCsr = "Not Base 64 Csr";
115
116         // when
117         Exception exception = assertThrows(
118                 CsrDecryptionException.class, () -> csrModelFactory
119                         .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
120         );
121
122         String expectedMessage = "Incorrect CSR, decryption failed";
123         String actualMessage = exception.getMessage();
124
125         // then
126         assertTrue(actualMessage.contains(expectedMessage));
127     }
128
129     @Test
130     void shouldThrowKeyDecryptionExceptionWhenPkIsNotInBase64Encoding() {
131         // given
132         String encoderPK = "Not Base64 Key";
133         String wrongCsr = new String(Base64.encode(TEST_CSR.getBytes()));
134
135         // when
136         Exception exception = assertThrows(
137                 KeyDecryptionException.class, () -> csrModelFactory
138                         .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
139         );
140
141         String expectedMessage = "Incorrect Key, decryption failed";
142         String actualMessage = exception.getMessage();
143
144         // then
145         assertTrue(actualMessage.contains(expectedMessage));
146     }
147 }