diff options
author | 2020-02-20 16:45:12 +0100 | |
---|---|---|
committer | 2020-02-28 11:41:26 +0100 | |
commit | e78bfe13bb52196ad84e68f4d4aa476aad1b6c52 (patch) | |
tree | d913c9049a18f6cca8446319bd43b4455319d122 /certServiceClient/src/test/java/org/onap/aaf/certservice | |
parent | 9e692d47d60d47c3668ba3281fdca61921a92fcf (diff) |
Add HttpClient
Issue-ID: AAF-996
Signed-off-by: Tomasz Wrobel <tomasz.wrobel@nokia.com>
Change-Id: I3ebb0ea88ef1a72c16064fd9a1931943b57410d8
Diffstat (limited to 'certServiceClient/src/test/java/org/onap/aaf/certservice')
2 files changed, 214 insertions, 0 deletions
diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/CerServiceRequestTestData.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/CerServiceRequestTestData.java new file mode 100644 index 00000000..8f252c31 --- /dev/null +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/CerServiceRequestTestData.java @@ -0,0 +1,89 @@ +/* + * ============LICENSE_START======================================================= + * aaf-certservice-client + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aaf.certservice.client; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; + +public final class CerServiceRequestTestData { + + private static final String RESOURCE_PATH = "src/test/resources/"; + + // Request parameters + public static final String CA_NAME = "TestCA"; + public static final String CSR = getCsrValue(); + public static final String PK = getPkValue(); + + // Correct response data + public static final String CORRECT_RESPONSE = getCorrectResponse(); + public static final String EXPECTED_FIRST_ELEMENT_OF_CERTIFICATE_CHAIN = + getExpectedFirstElementOfCertificateChain(); + public static final String EXPECTED_FIRST_ELEMENT_OF_TRUSTED_CERTIFICATES = + getExpectedFirstElementOfTrustedCertificates(); + + // Error response data + public static final String MISSING_PK_RESPONSE = getMissingPkResponse(); + + private CerServiceRequestTestData() { + } + + private static String getMissingPkResponse() { + String fileName = "missingPkResponse"; + return readFromFile(RESOURCE_PATH + fileName); + } + + private static String getExpectedFirstElementOfTrustedCertificates() { + + String fileName = "expectedFirstElementOfTrustedCertificates"; + return readFromFile(RESOURCE_PATH + fileName); + } + + private static String getExpectedFirstElementOfCertificateChain() { + String fileName = "expectedFirstElementOfCertificateChain"; + return readFromFile(RESOURCE_PATH + fileName); + } + + private static String getCorrectResponse() { + String fileName = "correctResponse"; + return readFromFile(RESOURCE_PATH + fileName); + } + + private static String getPkValue() { + String fileName = "testPk"; + return readFromFile(RESOURCE_PATH + fileName); + } + + private static String getCsrValue() { + String fileName = "testCsr"; + return readFromFile(RESOURCE_PATH + fileName); + } + + private static String readFromFile(String path) { + try { + return Files.readString(Paths.get(path), StandardCharsets.UTF_8); + } catch (IOException e) { + e.printStackTrace(); + return "File not found"; + } + } +} diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/httpclient/HttpClientTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/httpclient/HttpClientTest.java new file mode 100644 index 00000000..f65aefdf --- /dev/null +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/httpclient/HttpClientTest.java @@ -0,0 +1,125 @@ +/* + * ============LICENSE_START======================================================= + * aaf-certservice-client + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aaf.certservice.client.httpclient; + +import org.apache.http.HttpEntity; +import org.apache.http.StatusLine; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onap.aaf.certservice.client.api.ExitCode; +import org.onap.aaf.certservice.client.httpclient.exception.CertServiceApiResponseException; +import org.onap.aaf.certservice.client.httpclient.model.CertServiceResponse; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.List; + +import static java.net.HttpURLConnection.HTTP_BAD_REQUEST; +import static java.net.HttpURLConnection.HTTP_OK; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.onap.aaf.certservice.client.CerServiceRequestTestData.CA_NAME; +import static org.onap.aaf.certservice.client.CerServiceRequestTestData.CORRECT_RESPONSE; +import static org.onap.aaf.certservice.client.CerServiceRequestTestData.CSR; +import static org.onap.aaf.certservice.client.CerServiceRequestTestData.EXPECTED_FIRST_ELEMENT_OF_CERTIFICATE_CHAIN; +import static org.onap.aaf.certservice.client.CerServiceRequestTestData.EXPECTED_FIRST_ELEMENT_OF_TRUSTED_CERTIFICATES; +import static org.onap.aaf.certservice.client.CerServiceRequestTestData.MISSING_PK_RESPONSE; +import static org.onap.aaf.certservice.client.CerServiceRequestTestData.PK; + +class HttpClientTest { + + private HttpClient httpClient; + private CloseableHttpClient closeableHttpClient; + private HttpEntity httpEntity; + private StatusLine statusLine; + private CloseableHttpResponse httpResponse; + + @BeforeEach + void setUp() { + + closeableHttpClient = mock(CloseableHttpClient.class); + httpEntity = mock(HttpEntity.class); + statusLine = mock(StatusLine.class); + httpResponse = mock(CloseableHttpResponse.class); + + CloseableHttpClientProvider httpClientProvider = mock(CloseableHttpClientProvider.class); + + when(httpClientProvider.getClient()).thenReturn(closeableHttpClient); + String testCertServiceAddress = ""; + httpClient = new HttpClient(httpClientProvider, testCertServiceAddress); + } + + @Test + void shouldReturnCorrectListsOfCertificatedChainsAndTrustedCertificates_WhenRequestDataIsCorrect() + throws Exception { + + // given + mockServerResponse(HTTP_OK, CORRECT_RESPONSE); + + // when + CertServiceResponse certServiceResponse = + httpClient.retrieveCertServiceData(CA_NAME, CSR, PK); + List<String> certificateChain = certServiceResponse.getCertificateChain(); + List<String> trustedCertificate = certServiceResponse.getTrustedCertificates(); + + // then + assertNotNull(certServiceResponse); + + final int expectedTwoElements = 2; + assertEquals(expectedTwoElements, certificateChain.size()); + assertEquals(expectedTwoElements, trustedCertificate.size()); + + assertEquals(EXPECTED_FIRST_ELEMENT_OF_CERTIFICATE_CHAIN, certificateChain.get(0)); + assertEquals(EXPECTED_FIRST_ELEMENT_OF_TRUSTED_CERTIFICATES, trustedCertificate.get(0)); + } + + @Test + void shouldThrowCertServiceApiResponseException_WhenPkHeaderIsMissing() throws Exception { + + // given + mockServerResponse(HTTP_BAD_REQUEST, MISSING_PK_RESPONSE); + + // when + CertServiceApiResponseException exception = + assertThrows(CertServiceApiResponseException.class, + () -> httpClient.retrieveCertServiceData(CA_NAME, CSR, "")); + + // then + assertEquals(ExitCode.CERT_SERVICE_API_CONNECTION_EXCEPTION.getValue(), exception.applicationExitCode()); + } + + private void mockServerResponse(int serverCodeResponse, String stringResponse) + throws IOException { + when(statusLine.getStatusCode()).thenReturn(serverCodeResponse); + when(httpResponse.getStatusLine()).thenReturn(statusLine); + when(httpResponse.getEntity()).thenReturn(httpEntity); + when(closeableHttpClient.execute(any(HttpGet.class))).thenReturn(httpResponse); + + when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream(stringResponse.getBytes())); + } +} |