Improve exceptions logging in certservice client
[oom/platform/cert-service.git] / certServiceClient / src / test / java / org / onap / aaf / certservice / client / httpclient / HttpClientTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * aaf-certservice-client
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.client.httpclient;
22
23 import org.apache.http.HttpEntity;
24 import org.apache.http.StatusLine;
25 import org.apache.http.client.methods.CloseableHttpResponse;
26 import org.apache.http.client.methods.HttpGet;
27 import org.apache.http.impl.client.CloseableHttpClient;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.onap.aaf.certservice.client.httpclient.exception.CertServiceApiResponseException;
31 import org.onap.aaf.certservice.client.httpclient.exception.HttpClientException;
32 import org.onap.aaf.certservice.client.httpclient.model.CertServiceResponse;
33
34 import java.io.ByteArrayInputStream;
35 import java.io.IOException;
36 import java.util.List;
37
38 import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
39 import static java.net.HttpURLConnection.HTTP_OK;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
42 import static org.mockito.Mockito.any;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
45 import static org.onap.aaf.certservice.client.CerServiceRequestTestData.CA_NAME;
46 import static org.onap.aaf.certservice.client.CerServiceRequestTestData.CORRECT_RESPONSE;
47 import static org.onap.aaf.certservice.client.CerServiceRequestTestData.CSR;
48 import static org.onap.aaf.certservice.client.CerServiceRequestTestData.EXPECTED_FIRST_ELEMENT_OF_CERTIFICATE_CHAIN;
49 import static org.onap.aaf.certservice.client.CerServiceRequestTestData.EXPECTED_FIRST_ELEMENT_OF_TRUSTED_CERTIFICATES;
50 import static org.onap.aaf.certservice.client.CerServiceRequestTestData.MISSING_PK_RESPONSE;
51 import static org.onap.aaf.certservice.client.CerServiceRequestTestData.PK;
52
53 class HttpClientTest {
54
55     private HttpClient httpClient;
56     private CloseableHttpClient closeableHttpClient;
57     private HttpEntity httpEntity;
58     private StatusLine statusLine;
59     private CloseableHttpResponse httpResponse;
60
61     @BeforeEach
62     void setUp() {
63
64         closeableHttpClient = mock(CloseableHttpClient.class);
65         httpEntity = mock(HttpEntity.class);
66         statusLine = mock(StatusLine.class);
67         httpResponse = mock(CloseableHttpResponse.class);
68
69         CloseableHttpClientProvider httpClientProvider = mock(CloseableHttpClientProvider.class);
70
71         when(httpClientProvider.getClient()).thenReturn(closeableHttpClient);
72         String testCertServiceAddress = "";
73         httpClient = new HttpClient(httpClientProvider, testCertServiceAddress);
74     }
75
76     @Test
77     void shouldReturnCorrectListsOfCertificatedChainsAndTrustedCertificates_WhenRequestDataIsCorrect()
78             throws Exception {
79
80         // given
81         mockServerResponse(HTTP_OK, CORRECT_RESPONSE);
82
83         // when
84         CertServiceResponse certServiceResponse =
85                 httpClient.retrieveCertServiceData(CA_NAME, CSR, PK);
86         List<String> certificateChain = certServiceResponse.getCertificateChain();
87         List<String> trustedCertificate = certServiceResponse.getTrustedCertificates();
88
89         // then
90         assertThat(certServiceResponse).isNotNull();
91
92         final int expectedTwoElements = 2;
93
94         assertThat(certificateChain).hasSize(expectedTwoElements);
95         assertThat(trustedCertificate).hasSize(expectedTwoElements);
96
97         assertThat(certificateChain.get(0)).isEqualTo(EXPECTED_FIRST_ELEMENT_OF_CERTIFICATE_CHAIN);
98         assertThat(trustedCertificate.get(0)).isEqualTo(EXPECTED_FIRST_ELEMENT_OF_TRUSTED_CERTIFICATES);
99     }
100
101     @Test
102     void shouldThrowCertServiceApiResponseException_WhenPkHeaderIsMissing() throws Exception {
103
104         //given
105         mockServerResponse(HTTP_BAD_REQUEST, MISSING_PK_RESPONSE);
106
107         //when //then
108         assertThatExceptionOfType(CertServiceApiResponseException.class)
109                 .isThrownBy(()->httpClient.retrieveCertServiceData(CA_NAME, CSR, ""));
110     }
111
112     @Test
113     void shouldThrowHttpClientException_WhenCannotExecuteRequestToAPI() throws Exception {
114
115         //given
116         when(closeableHttpClient.execute(any(HttpGet.class))).thenThrow(IOException.class);
117
118         //when //then
119         assertThatExceptionOfType(HttpClientException.class)
120                 .isThrownBy(()->httpClient.retrieveCertServiceData(CA_NAME, CSR, ""));
121     }
122
123     @Test
124     void shouldThrowHttpClientException_WhenCannotParseResponseToString() throws Exception {
125
126         //given
127         mockServerResponse(HTTP_OK, CORRECT_RESPONSE);
128         when(httpEntity.getContent()).thenThrow(IOException.class);
129
130         //when //then
131         assertThatExceptionOfType(HttpClientException.class)
132                 .isThrownBy(()->httpClient.retrieveCertServiceData(CA_NAME, CSR, ""));
133     }
134
135     private void mockServerResponse(int serverCodeResponse, String stringResponse)
136             throws IOException {
137         when(statusLine.getStatusCode()).thenReturn(serverCodeResponse);
138         when(httpResponse.getStatusLine()).thenReturn(statusLine);
139         when(httpResponse.getEntity()).thenReturn(httpEntity);
140         when(closeableHttpClient.execute(any(HttpGet.class))).thenReturn(httpResponse);
141
142         when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream(stringResponse.getBytes()));
143     }
144 }