f65aefdfdea4151398114be78a2c2564d211577e
[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.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.junit.jupiter.api.Assertions.assertEquals;
41 import static org.junit.jupiter.api.Assertions.assertNotNull;
42 import static org.junit.jupiter.api.Assertions.assertThrows;
43 import static org.mockito.Mockito.any;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.when;
46 import static org.onap.aaf.certservice.client.CerServiceRequestTestData.CA_NAME;
47 import static org.onap.aaf.certservice.client.CerServiceRequestTestData.CORRECT_RESPONSE;
48 import static org.onap.aaf.certservice.client.CerServiceRequestTestData.CSR;
49 import static org.onap.aaf.certservice.client.CerServiceRequestTestData.EXPECTED_FIRST_ELEMENT_OF_CERTIFICATE_CHAIN;
50 import static org.onap.aaf.certservice.client.CerServiceRequestTestData.EXPECTED_FIRST_ELEMENT_OF_TRUSTED_CERTIFICATES;
51 import static org.onap.aaf.certservice.client.CerServiceRequestTestData.MISSING_PK_RESPONSE;
52 import static org.onap.aaf.certservice.client.CerServiceRequestTestData.PK;
53
54 class HttpClientTest {
55
56     private HttpClient httpClient;
57     private CloseableHttpClient closeableHttpClient;
58     private HttpEntity httpEntity;
59     private StatusLine statusLine;
60     private CloseableHttpResponse httpResponse;
61
62     @BeforeEach
63     void setUp() {
64
65         closeableHttpClient = mock(CloseableHttpClient.class);
66         httpEntity = mock(HttpEntity.class);
67         statusLine = mock(StatusLine.class);
68         httpResponse = mock(CloseableHttpResponse.class);
69
70         CloseableHttpClientProvider httpClientProvider = mock(CloseableHttpClientProvider.class);
71
72         when(httpClientProvider.getClient()).thenReturn(closeableHttpClient);
73         String testCertServiceAddress = "";
74         httpClient = new HttpClient(httpClientProvider, testCertServiceAddress);
75     }
76
77     @Test
78     void shouldReturnCorrectListsOfCertificatedChainsAndTrustedCertificates_WhenRequestDataIsCorrect()
79             throws Exception {
80
81         // given
82         mockServerResponse(HTTP_OK, CORRECT_RESPONSE);
83
84         // when
85         CertServiceResponse certServiceResponse =
86                 httpClient.retrieveCertServiceData(CA_NAME, CSR, PK);
87         List<String> certificateChain = certServiceResponse.getCertificateChain();
88         List<String> trustedCertificate = certServiceResponse.getTrustedCertificates();
89
90         // then
91         assertNotNull(certServiceResponse);
92
93         final int expectedTwoElements = 2;
94         assertEquals(expectedTwoElements, certificateChain.size());
95         assertEquals(expectedTwoElements, trustedCertificate.size());
96
97         assertEquals(EXPECTED_FIRST_ELEMENT_OF_CERTIFICATE_CHAIN, certificateChain.get(0));
98         assertEquals(EXPECTED_FIRST_ELEMENT_OF_TRUSTED_CERTIFICATES, trustedCertificate.get(0));
99     }
100
101     @Test
102     void shouldThrowCertServiceApiResponseException_WhenPkHeaderIsMissing() throws Exception {
103
104         // given
105         mockServerResponse(HTTP_BAD_REQUEST, MISSING_PK_RESPONSE);
106
107         // when
108         CertServiceApiResponseException exception =
109                 assertThrows(CertServiceApiResponseException.class,
110                         () -> httpClient.retrieveCertServiceData(CA_NAME, CSR, ""));
111
112         // then
113         assertEquals(ExitCode.CERT_SERVICE_API_CONNECTION_EXCEPTION.getValue(), exception.applicationExitCode());
114     }
115
116     private void mockServerResponse(int serverCodeResponse, String stringResponse)
117             throws IOException {
118         when(statusLine.getStatusCode()).thenReturn(serverCodeResponse);
119         when(httpResponse.getStatusLine()).thenReturn(statusLine);
120         when(httpResponse.getEntity()).thenReturn(httpEntity);
121         when(closeableHttpClient.execute(any(HttpGet.class))).thenReturn(httpResponse);
122
123         when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream(stringResponse.getBytes()));
124     }
125 }