Removing AAF references from Cert-Service in OOM repo.
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / oom / certservice / api / advice / CertificationExceptionAdviceTest.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.advice;
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.oom.certservice.certification.exception.Cmpv2ClientAdapterException;
27 import org.onap.oom.certservice.certification.exception.Cmpv2ServerNotFoundException;
28 import org.onap.oom.certservice.certification.exception.CsrDecryptionException;
29 import org.onap.oom.certservice.certification.exception.ErrorResponseModel;
30 import org.onap.oom.certservice.certification.exception.KeyDecryptionException;
31 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.ResponseEntity;
34
35 import static org.junit.jupiter.api.Assertions.assertEquals;
36 import static org.junit.jupiter.api.Assertions.assertThrows;
37
38 class CertificationExceptionAdviceTest {
39
40     private CertificationExceptionAdvice certificationExceptionAdvice;
41
42     @BeforeEach
43     void setUp() {
44         certificationExceptionAdvice =
45                 new CertificationExceptionAdvice();
46     }
47
48     @Test
49     void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenCsrDecryptionException() {
50         // Given
51         String expectedMessage = "Wrong certificate signing request (CSR) format";
52         CsrDecryptionException csrDecryptionException = new CsrDecryptionException("test csr exception");
53
54         // When
55         ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(csrDecryptionException);
56
57         // Then
58         assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
59         assertEquals(expectedMessage, response.getBody().getErrorMessage());
60     }
61
62     @Test
63     void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenKeyDecryptionException() {
64         // Given
65         String expectedMessage = "Wrong key (PK) format";
66         KeyDecryptionException csrDecryptionException = new KeyDecryptionException("test pk exception");
67
68         // When
69         ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(csrDecryptionException);
70
71         // Then
72         assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
73         assertEquals(expectedMessage, response.getBody().getErrorMessage());
74     }
75
76     @Test
77     void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenCaNameIsNotPresentInConfig() {
78         // Given
79         String expectedMessage = "Certification authority not found for given CAName";
80         Cmpv2ServerNotFoundException csrDecryptionException = new Cmpv2ServerNotFoundException("test Ca exception");
81
82         // When
83         ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(csrDecryptionException);
84
85         // Then
86         assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
87         assertEquals(expectedMessage, response.getBody().getErrorMessage());
88     }
89
90     @Test
91     void shouldReturnResponseEntityWithAppropriateErrorMessageWhenCallingCmpClientFail() {
92         // Given
93         String expectedMessage = "Exception occurred during call to cmp client";
94         CmpClientException cmpClientException = new CmpClientException("Calling CMPv2 client failed");
95
96         // When
97         ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(cmpClientException);
98
99         // Then
100         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
101         assertEquals(expectedMessage, response.getBody().getErrorMessage());
102     }
103
104     @Test
105     void shouldReturnResponseEntityWithAppropriateErrorMessageWhenModelTransformationInAdapterFail() {
106         // Given
107         String expectedMessage = "Exception occurred parsing cmp client response";
108         Cmpv2ClientAdapterException cmpv2ClientAdapterException = new Cmpv2ClientAdapterException(new Throwable());
109
110         // When
111         ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(cmpv2ClientAdapterException);
112
113         // Then
114         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
115         assertEquals(expectedMessage, response.getBody().getErrorMessage());
116     }
117
118     @Test
119     void shouldThrowCmpClientExceptionWhenNotHandledRunTimeExceptionOccur() {
120         // Given
121         String expectedMessage = "Runtime exception occurred calling cmp client business logic";
122         RuntimeException runtimeException = new RuntimeException("Unknown runtime exception");
123
124         // When
125         Exception exception = assertThrows(
126                 CmpClientException.class, () ->
127                         certificationExceptionAdvice.handle(runtimeException)
128         );
129
130         // Then
131         assertEquals(expectedMessage, exception.getMessage());
132     }
133
134 }