[OOM-CERT-SERVICE] Add update endpoint
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / oom / certservice / api / CertificationControllerTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
4  * ================================================================================
5  * Copyright (C) 2020-2021 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.api;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.mockito.Mockito.when;
27
28 import java.util.Arrays;
29
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.api.extension.ExtendWith;
33 import org.mockito.Mock;
34 import org.mockito.junit.jupiter.MockitoExtension;
35 import org.onap.oom.certservice.certification.model.CertificateUpdateModel;
36 import org.onap.oom.certservice.certification.CertificationModelFactory;
37 import org.onap.oom.certservice.certification.exception.Cmpv2ServerNotFoundException;
38 import org.onap.oom.certservice.certification.exception.CsrDecryptionException;
39 import org.onap.oom.certservice.certification.exception.DecryptionException;
40 import org.onap.oom.certservice.certification.exception.KeyDecryptionException;
41 import org.onap.oom.certservice.certification.model.CertificationModel;
42 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
43 import org.springframework.http.HttpStatus;
44 import org.springframework.http.ResponseEntity;
45
46 @ExtendWith(MockitoExtension.class)
47 class CertificationControllerTest {
48
49     private static final String TEST_CA_NAME = "TestCa";
50     private static final String TEST_ENCODED_CSR = "encodedCSR";
51     private static final String TEST_ENCODED_PK = "encodedPK";
52     private static final String TEST_WRONG_ENCODED_CSR = "wrongEncodedCSR";
53     private static final String TEST_WRONG_ENCODED_PK = "wrongEncodedPK";
54     private static final String TEST_WRONG_CA_NAME = "wrongTestCa";
55     private static final String TEST_ENCODED_OLD_PK = "encodedOldPK";
56     private static final String TEST_ENCODED_OLD_CERT = "encodedOldCert";
57
58     private CertificationController certificationController;
59
60     @Mock
61     private CertificationModelFactory certificationModelFactory;
62
63     @BeforeEach
64     void serUp() {
65         certificationController = new CertificationController(certificationModelFactory);
66     }
67
68     @Test
69     void shouldReturnDataAboutCsrBaseOnEncodedParameters()
70             throws DecryptionException, CmpClientException {
71         // Given
72         CertificationModel testCertificationModel = new CertificationModel(
73                 Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"),
74                 Arrays.asList("CA_CERT", "EXTRA_CA_CERT")
75         );
76         when(certificationModelFactory.createCertificationModel(TEST_ENCODED_CSR, TEST_ENCODED_PK, TEST_CA_NAME))
77                 .thenReturn(testCertificationModel);
78
79         // When
80         ResponseEntity<CertificationModel> responseCertificationModel =
81                 certificationController.signCertificate(TEST_CA_NAME, TEST_ENCODED_CSR, TEST_ENCODED_PK);
82
83         // Then
84         assertEquals(HttpStatus.OK, responseCertificationModel.getStatusCode());
85         assertThat(responseCertificationModel.getBody()
86         ).isEqualToComparingFieldByField(testCertificationModel);
87
88     }
89
90     @Test
91     void shouldThrowCsrDecryptionExceptionWhenCreatingCsrModelFails()
92             throws DecryptionException, CmpClientException {
93         // Given
94         String expectedMessage = "Incorrect CSR, decryption failed";
95         when(certificationModelFactory.createCertificationModel(TEST_WRONG_ENCODED_CSR, TEST_ENCODED_PK, TEST_CA_NAME))
96                 .thenThrow(new CsrDecryptionException(expectedMessage));
97
98         // When
99         Exception exception = assertThrows(
100                 CsrDecryptionException.class, () ->
101                         certificationController.signCertificate(TEST_CA_NAME, TEST_WRONG_ENCODED_CSR, TEST_ENCODED_PK)
102         );
103
104         String actualMessage = exception.getMessage();
105
106         // Then
107         assertEquals(expectedMessage, actualMessage);
108     }
109
110     @Test
111     void shouldThrowPemDecryptionExceptionWhenCreatingPemModelFails()
112             throws DecryptionException, CmpClientException {
113         // Given
114         String expectedMessage = "Incorrect PEM, decryption failed";
115         when(certificationModelFactory.createCertificationModel(TEST_ENCODED_CSR, TEST_WRONG_ENCODED_PK, TEST_CA_NAME))
116                 .thenThrow(new KeyDecryptionException(expectedMessage));
117
118         // When
119         Exception exception = assertThrows(
120                 KeyDecryptionException.class, () ->
121                         certificationController.signCertificate(TEST_CA_NAME, TEST_ENCODED_CSR, TEST_WRONG_ENCODED_PK)
122         );
123
124         String actualMessage = exception.getMessage();
125
126         // Then
127         assertEquals(expectedMessage, actualMessage);
128     }
129
130     @Test
131     void shouldThrowCmpv2ServerNotFoundWhenGivenWrongCaName()
132             throws DecryptionException, CmpClientException {
133         // Given
134         String expectedMessage = "No server found for given CA name";
135         when(certificationModelFactory.createCertificationModel(TEST_ENCODED_CSR, TEST_ENCODED_PK, TEST_WRONG_CA_NAME))
136                 .thenThrow(new Cmpv2ServerNotFoundException(expectedMessage));
137
138         // When
139         Exception exception = assertThrows(
140                 Cmpv2ServerNotFoundException.class, () ->
141                         certificationController.signCertificate(TEST_WRONG_CA_NAME, TEST_ENCODED_CSR, TEST_ENCODED_PK)
142         );
143
144         String actualMessage = exception.getMessage();
145
146         // Then
147         assertEquals(expectedMessage, actualMessage);
148     }
149
150     @Test
151     void shouldUpdateEndpointReturnDataAboutCsrBaseOnEncodedParameters() {
152         // Given
153         CertificationModel testCertificationModel = new CertificationModel(
154                 Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"),
155                 Arrays.asList("CA_CERT", "EXTRA_CA_CERT")
156         );
157         CertificateUpdateModel certificateUpdateModel = new CertificateUpdateModel.CertificateUpdateModelBuilder()
158                 .setEncodedCsr(TEST_ENCODED_CSR)
159                 .setEncodedPrivateKey(TEST_ENCODED_PK)
160                 .setEncodedOldCert(TEST_ENCODED_OLD_CERT)
161                 .setEncodedOldPrivateKey(TEST_ENCODED_OLD_PK)
162                 .setCaName(TEST_CA_NAME)
163                 .build();
164         when(certificationModelFactory.createCertificationModel(certificateUpdateModel)).thenReturn(testCertificationModel);
165
166         // When
167         ResponseEntity<CertificationModel> responseCertificationModel =
168                 certificationController.updateCertificate(TEST_CA_NAME, TEST_ENCODED_CSR,
169                         TEST_ENCODED_PK, TEST_ENCODED_OLD_CERT, TEST_ENCODED_OLD_PK);
170
171         // Then
172         assertEquals(HttpStatus.OK, responseCertificationModel.getStatusCode());
173         assertThat(responseCertificationModel.getBody()).isEqualToComparingFieldByField(testCertificationModel);
174     }
175
176 }