Add tests to HttpClient
[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.api.ExitCode;
31 import org.onap.aaf.certservice.client.httpclient.exception.CertServiceApiResponseException;
32 import org.onap.aaf.certservice.client.httpclient.exception.HttpClientException;
33 import org.onap.aaf.certservice.client.httpclient.model.CertServiceResponse;
34
35 import java.io.ByteArrayInputStream;
36 import java.io.IOException;
37 import java.util.List;
38
39 import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
40 import static java.net.HttpURLConnection.HTTP_OK;
41 import static org.junit.jupiter.api.Assertions.assertEquals;
42 import static org.junit.jupiter.api.Assertions.assertNotNull;
43 import static org.junit.jupiter.api.Assertions.assertThrows;
44 import static org.mockito.Mockito.any;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.when;
47 import static org.onap.aaf.certservice.client.CerServiceRequestTestData.CA_NAME;
48 import static org.onap.aaf.certservice.client.CerServiceRequestTestData.CORRECT_RESPONSE;
49 import static org.onap.aaf.certservice.client.CerServiceRequestTestData.CSR;
50 import static org.onap.aaf.certservice.client.CerServiceRequestTestData.EXPECTED_FIRST_ELEMENT_OF_CERTIFICATE_CHAIN;
51 import static org.onap.aaf.certservice.client.CerServiceRequestTestData.EXPECTED_FIRST_ELEMENT_OF_TRUSTED_CERTIFICATES;
52 import static org.onap.aaf.certservice.client.CerServiceRequestTestData.MISSING_PK_RESPONSE;
53 import static org.onap.aaf.certservice.client.CerServiceRequestTestData.PK;
54
55 class HttpClientTest {
56
57     private HttpClient httpClient;
58     private CloseableHttpClient closeableHttpClient;
59     private HttpEntity httpEntity;
60     private StatusLine statusLine;
61     private CloseableHttpResponse httpResponse;
62
63     @BeforeEach
64     void setUp() {
65
66         closeableHttpClient = mock(CloseableHttpClient.class);
67         httpEntity = mock(HttpEntity.class);
68         statusLine = mock(StatusLine.class);
69         httpResponse = mock(CloseableHttpResponse.class);
70
71         CloseableHttpClientProvider httpClientProvider = mock(CloseableHttpClientProvider.class);
72
73         when(httpClientProvider.getClient()).thenReturn(closeableHttpClient);
74         String testCertServiceAddress = "";
75         httpClient = new HttpClient(httpClientProvider, testCertServiceAddress);
76     }
77
78     @Test
79     void shouldReturnCorrectListsOfCertificatedChainsAndTrustedCertificates_WhenRequestDataIsCorrect()
80             throws Exception {
81
82         // given
83         mockServerResponse(HTTP_OK, CORRECT_RESPONSE);
84
85         // when
86         CertServiceResponse certServiceResponse =
87                 httpClient.retrieveCertServiceData(CA_NAME, CSR, PK);
88         List<String> certificateChain = certServiceResponse.getCertificateChain();
89         List<String> trustedCertificate = certServiceResponse.getTrustedCertificates();
90
91         // then
92         assertNotNull(certServiceResponse);
93
94         final int expectedTwoElements = 2;
95         assertEquals(expectedTwoElements, certificateChain.size());
96         assertEquals(expectedTwoElements, trustedCertificate.size());
97
98         assertEquals(EXPECTED_FIRST_ELEMENT_OF_CERTIFICATE_CHAIN, certificateChain.get(0));
99         assertEquals(EXPECTED_FIRST_ELEMENT_OF_TRUSTED_CERTIFICATES, trustedCertificate.get(0));
100     }
101
102     @Test
103     void shouldThrowCertServiceApiResponseException_WhenPkHeaderIsMissing() throws Exception {
104
105         // given
106         mockServerResponse(HTTP_BAD_REQUEST, MISSING_PK_RESPONSE);
107
108         // when
109         CertServiceApiResponseException exception =
110                 assertThrows(CertServiceApiResponseException.class,
111                         () -> httpClient.retrieveCertServiceData(CA_NAME, CSR, ""));
112
113         // then
114         assertEquals(ExitCode.CERT_SERVICE_API_CONNECTION_EXCEPTION.getValue(), exception.applicationExitCode());
115     }
116
117     @Test
118     void shouldThrowHttpClientException_WhenCannotExecuteRequestToAPI() throws Exception{
119
120         //given
121         when(closeableHttpClient.execute(any(HttpGet.class))).thenThrow(IOException.class);
122
123         //when
124         HttpClientException exception =
125                 assertThrows(HttpClientException.class,
126                         () -> httpClient.retrieveCertServiceData(CA_NAME, CSR, ""));
127
128         //then
129         assertEquals(ExitCode.HTTP_CLIENT_EXCEPTION.getValue(), exception.applicationExitCode());
130     }
131
132     @Test
133     void shouldThrowHttpClientException_WhenCannotParseResponseToString() throws Exception{
134
135         //given
136         mockServerResponse(HTTP_OK, CORRECT_RESPONSE);
137         when(httpEntity.getContent()).thenThrow(IOException.class);
138
139         //when
140         HttpClientException exception =
141                 assertThrows(HttpClientException.class,
142                         () -> httpClient.retrieveCertServiceData(CA_NAME, CSR, ""));
143
144         //then
145         assertEquals(ExitCode.HTTP_CLIENT_EXCEPTION.getValue(), exception.applicationExitCode());
146     }
147
148     private void mockServerResponse(int serverCodeResponse, String stringResponse)
149             throws IOException {
150         when(statusLine.getStatusCode()).thenReturn(serverCodeResponse);
151         when(httpResponse.getStatusLine()).thenReturn(statusLine);
152         when(httpResponse.getEntity()).thenReturn(httpEntity);
153         when(closeableHttpClient.execute(any(HttpGet.class))).thenReturn(httpResponse);
154
155         when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream(stringResponse.getBytes()));
156     }
157 }