63c1b72214a8950d8018d227446cbb7365be2a77
[integration.git] /
1 /*
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
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.pnfsimulator.simulator.client;
22
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;
29 import org.onap.pnfsimulator.simulator.client.utils.ssl.SSLAuthenticationHelper;
30
31 import java.io.IOException;
32 import java.net.MalformedURLException;
33 import java.security.GeneralSecurityException;
34
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.junit.jupiter.api.Assertions.assertThrows;
37 import static org.mockito.ArgumentMatchers.any;
38 import static org.mockito.Mockito.doReturn;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.verify;
41
42 class HttpClientAdapterImplTest {
43
44     private static final String HTTPS_URL = "https://0.0.0.0:8443/";
45     private static final String HTTP_URL = "http://0.0.0.0:8000/";
46
47     private HttpClient httpClient;
48     private HttpResponse httpResponse;
49
50     @BeforeEach
51     void setup() {
52         httpClient = mock(HttpClient.class);
53         httpResponse = mock(HttpResponse.class);
54     }
55
56     @Test
57     void sendShouldSuccessfullySendRequestGivenValidUrl() throws IOException {
58         assertAdapterSentRequest("http://valid-url:8080");
59     }
60
61     @Test
62     void sendShouldSuccessfullySendRequestGivenValidUrlUsingHTTPS() throws IOException {
63         assertAdapterSentRequest("https://valid-url:8443");
64     }
65
66     @Test
67     void shouldThrowExceptionWhenMalformedVesUrlPassed(){
68         assertThrows(MalformedURLException.class, () -> new HttpClientAdapterImpl("http://blablabla:VES-PORT", new SSLAuthenticationHelper()));
69     }
70     @Test
71     void shouldCreateAdapterWithClientNotSupportingSSLConnection() throws IOException, GeneralSecurityException {
72         HttpClientAdapter adapterWithHttps = new HttpClientAdapterImpl(HTTPS_URL, new SSLAuthenticationHelper());
73         try {
74             adapterWithHttps.send("sample");
75         } catch (Exception actualException) {
76             assertThat(actualException).hasStackTraceContaining(SSLConnectionSocketFactory.class.toString());
77         }
78     }
79
80     @Test
81     void shouldCreateAdapterWithClientSupportingPlainConnectionOnly() throws IOException, GeneralSecurityException {
82         HttpClientAdapter adapterWithHttps = new HttpClientAdapterImpl(HTTP_URL, new SSLAuthenticationHelper());
83         try {
84             adapterWithHttps.send("sample");
85         } catch (Exception actualException) {
86             assertThat(actualException).hasStackTraceContaining(PlainConnectionSocketFactory.class.toString());
87         }
88     }
89
90     private void assertAdapterSentRequest(String targetUrl) throws IOException {
91         HttpClientAdapter adapter = new HttpClientAdapterImpl(httpClient, targetUrl);
92         doReturn(httpResponse).when(httpClient).execute(any());
93
94         adapter.send("test-msg");
95
96         verify(httpClient).execute(any());
97         verify(httpResponse).getStatusLine();
98     }
99 }