1a92c0c81dc2bf44fe5419009b7c9e14ec977700
[oom/platform/cert-service.git] /
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 com.google.gson.Gson;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.onap.aaf.certservice.certification.exception.Cmpv2ServerNotFoundException;
27 import org.onap.aaf.certservice.certification.exception.CsrDecryptionException;
28 import org.onap.aaf.certservice.certification.exception.ErrorResponseModel;
29 import org.onap.aaf.certservice.certification.exception.KeyDecryptionException;
30 import org.springframework.http.ResponseEntity;
31
32 import static org.junit.jupiter.api.Assertions.assertEquals;
33
34 class CertificationExceptionControllerTest {
35
36     private CertificationExceptionController certificationExceptionController;
37
38     @BeforeEach
39     void setUp() {
40         certificationExceptionController =
41                 new CertificationExceptionController();
42     }
43
44     @Test
45     void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenCsrDecryptionException() {
46         // given
47         String expectedMessage = "Wrong certificate signing request (CSR) format";
48         CsrDecryptionException csrDecryptionException = new CsrDecryptionException("test csr exception");
49
50         // when
51         ResponseEntity<String> responseEntity = certificationExceptionController.handle(csrDecryptionException);
52
53         ErrorResponseModel response = new Gson().fromJson(responseEntity.getBody(), ErrorResponseModel.class);
54
55         // then
56         assertEquals(expectedMessage, response.getErrorMessage());
57     }
58
59     @Test
60     void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenKeyDecryptionException() {
61         // given
62         String expectedMessage = "Wrong key (PK) format";
63         KeyDecryptionException csrDecryptionException = new KeyDecryptionException("test pk exception");
64
65         // when
66         ResponseEntity<String> responseEntity = certificationExceptionController.handle(csrDecryptionException);
67
68         ErrorResponseModel response = new Gson().fromJson(responseEntity.getBody(), ErrorResponseModel.class);
69
70         // then
71         assertEquals(expectedMessage, response.getErrorMessage());
72     }
73
74     @Test
75     void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenCaNameIsNotPresentInConfig() {
76         // given
77         String expectedMessage = "Certification authority not found for given CAName";
78         Cmpv2ServerNotFoundException csrDecryptionException = new Cmpv2ServerNotFoundException("test Ca exception");
79
80         // when
81         ResponseEntity<String> responseEntity = certificationExceptionController.handle(csrDecryptionException);
82
83         ErrorResponseModel response = new Gson().fromJson(responseEntity.getBody(), ErrorResponseModel.class);
84
85         // then
86         assertEquals(expectedMessage, response.getErrorMessage());
87     }
88 }