2 * ============LICENSE_START=======================================================
3 * PNF-REGISTRATION-HANDLER
4 * ================================================================================
5 * Copyright (C) 2018 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.pnfsimulator.simulator.client;
23 import org.apache.http.HttpResponse;
24 import org.apache.http.client.HttpClient;
25 import org.apache.http.conn.socket.PlainConnectionSocketFactory;
26 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
30 import java.io.IOException;
31 import java.net.MalformedURLException;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.junit.jupiter.api.Assertions.assertThrows;
35 import static org.mockito.ArgumentMatchers.any;
36 import static org.mockito.Mockito.doReturn;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.verify;
40 class HttpClientAdapterImplTest {
42 private static final String HTTPS_URL = "https://0.0.0.0:8443/";
43 private static final String HTTP_URL = "http://0.0.0.0:8000/";
45 private HttpClient httpClient;
46 private HttpResponse httpResponse;
50 httpClient = mock(HttpClient.class);
51 httpResponse = mock(HttpResponse.class);
55 void sendShouldSuccessfullySendRequestGivenValidUrl() throws IOException {
56 assertAdapterSentRequest("http://valid-url:8080");
60 void sendShouldSuccessfullySendRequestGivenValidUrlUsingHTTPS() throws IOException {
61 assertAdapterSentRequest("https://valid-url:8443");
65 void shouldThrowExceptionWhenMalformedVesUrlPassed(){
66 assertThrows(MalformedURLException.class, () -> new HttpClientAdapterImpl("http://blablabla:VES-PORT"));
69 void shouldCreateAdapterWithClientNotSupportingSSLConnection() throws MalformedURLException {
70 HttpClientAdapter adapterWithHttps = new HttpClientAdapterImpl(HTTPS_URL);
72 adapterWithHttps.send("sample");
73 } catch (Exception actualException) {
74 assertThat(actualException).hasStackTraceContaining(SSLConnectionSocketFactory.class.toString());
79 void shouldCreateAdapterWithClientSupportingPlainConnectionOnly() throws MalformedURLException {
80 HttpClientAdapter adapterWithHttps = new HttpClientAdapterImpl(HTTP_URL);
82 adapterWithHttps.send("sample");
83 } catch (Exception actualException) {
84 assertThat(actualException).hasStackTraceContaining(PlainConnectionSocketFactory.class.toString());
88 private void assertAdapterSentRequest(String targetUrl) throws IOException {
89 HttpClientAdapter adapter = new HttpClientAdapterImpl(httpClient, targetUrl);
90 doReturn(httpResponse).when(httpClient).execute(any());
92 adapter.send("test-msg");
94 verify(httpClient).execute(any());
95 verify(httpResponse).getStatusLine();