Removing AAF references from Cert-Service in OOM repo.
[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 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.CertificationModelFactory;
36 import org.onap.oom.certservice.certification.exception.Cmpv2ClientAdapterException;
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
56     private CertificationController certificationController;
57
58     @Mock
59     private CertificationModelFactory certificationModelFactory;
60
61     @BeforeEach
62     void serUp() {
63         certificationController = new CertificationController(certificationModelFactory);
64     }
65
66     @Test
67     void shouldReturnDataAboutCsrBaseOnEncodedParameters()
68             throws DecryptionException, CmpClientException, Cmpv2ClientAdapterException {
69         // Given
70         CertificationModel testCertificationModel = new CertificationModel(
71                 Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"),
72                 Arrays.asList("CA_CERT", "EXTRA_CA_CERT")
73         );
74         when(certificationModelFactory.createCertificationModel(TEST_ENCODED_CSR, TEST_ENCODED_PK, TEST_CA_NAME))
75                 .thenReturn(testCertificationModel);
76
77         // When
78         ResponseEntity<CertificationModel> responseCertificationModel =
79                 certificationController.signCertificate(TEST_CA_NAME, TEST_ENCODED_CSR, TEST_ENCODED_PK);
80
81         // Then
82         assertEquals(HttpStatus.OK, responseCertificationModel.getStatusCode());
83         assertThat(responseCertificationModel.getBody()
84         ).isEqualToComparingFieldByField(testCertificationModel);
85
86     }
87
88     @Test
89     void shouldThrowCsrDecryptionExceptionWhenCreatingCsrModelFails()
90             throws DecryptionException, CmpClientException, Cmpv2ClientAdapterException {
91         // Given
92         String expectedMessage = "Incorrect CSR, decryption failed";
93         when(certificationModelFactory.createCertificationModel(TEST_WRONG_ENCODED_CSR, TEST_ENCODED_PK, TEST_CA_NAME))
94                 .thenThrow(new CsrDecryptionException(expectedMessage));
95
96         // When
97         Exception exception = assertThrows(
98                 CsrDecryptionException.class, () ->
99                         certificationController.signCertificate(TEST_CA_NAME, TEST_WRONG_ENCODED_CSR, TEST_ENCODED_PK)
100         );
101
102         String actualMessage = exception.getMessage();
103
104         // Then
105         assertEquals(expectedMessage, actualMessage);
106     }
107
108     @Test
109     void shouldThrowPemDecryptionExceptionWhenCreatingPemModelFails()
110             throws DecryptionException, CmpClientException, Cmpv2ClientAdapterException {
111         // Given
112         String expectedMessage = "Incorrect PEM, decryption failed";
113         when(certificationModelFactory.createCertificationModel(TEST_ENCODED_CSR, TEST_WRONG_ENCODED_PK, TEST_CA_NAME))
114                 .thenThrow(new KeyDecryptionException(expectedMessage));
115
116         // When
117         Exception exception = assertThrows(
118                 KeyDecryptionException.class, () ->
119                         certificationController.signCertificate(TEST_CA_NAME, TEST_ENCODED_CSR, TEST_WRONG_ENCODED_PK)
120         );
121
122         String actualMessage = exception.getMessage();
123
124         // Then
125         assertEquals(expectedMessage, actualMessage);
126     }
127
128     @Test
129     void shouldThrowCmpv2ServerNotFoundWhenGivenWrongCaName()
130             throws DecryptionException, CmpClientException, Cmpv2ClientAdapterException {
131         // Given
132         String expectedMessage = "No server found for given CA name";
133         when(certificationModelFactory.createCertificationModel(TEST_ENCODED_CSR, TEST_ENCODED_PK, TEST_WRONG_CA_NAME))
134                 .thenThrow(new Cmpv2ServerNotFoundException(expectedMessage));
135
136         // When
137         Exception exception = assertThrows(
138                 Cmpv2ServerNotFoundException.class, () ->
139                         certificationController.signCertificate(TEST_WRONG_CA_NAME, TEST_ENCODED_CSR, TEST_ENCODED_PK)
140         );
141
142         String actualMessage = exception.getMessage();
143
144         // Then
145         assertEquals(expectedMessage, actualMessage);
146     }
147 }