41bd7b1e63ed602ef452325cc6fd6df6185d69a9
[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
30 import java.io.IOException;
31 import java.net.MalformedURLException;
32
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;
39
40 class HttpClientAdapterImplTest {
41
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/";
44
45     private HttpClient httpClient;
46     private HttpResponse httpResponse;
47
48     @BeforeEach
49     void setup() {
50         httpClient = mock(HttpClient.class);
51         httpResponse = mock(HttpResponse.class);
52     }
53
54     @Test
55     void sendShouldSuccessfullySendRequestGivenValidUrl() throws IOException {
56         assertAdapterSentRequest("http://valid-url:8080");
57     }
58
59     @Test
60     void sendShouldSuccessfullySendRequestGivenValidUrlUsingHTTPS() throws IOException {
61         assertAdapterSentRequest("https://valid-url:8443");
62     }
63
64     @Test
65     void shouldThrowExceptionWhenMalformedVesUrlPassed(){
66         assertThrows(MalformedURLException.class, () -> new HttpClientAdapterImpl("http://blablabla:VES-PORT"));
67     }
68     @Test
69     void shouldCreateAdapterWithClientNotSupportingSSLConnection() throws MalformedURLException {
70         HttpClientAdapter adapterWithHttps = new HttpClientAdapterImpl(HTTPS_URL);
71         try {
72             adapterWithHttps.send("sample");
73         } catch (Exception actualException) {
74             assertThat(actualException).hasStackTraceContaining(SSLConnectionSocketFactory.class.toString());
75         }
76     }
77
78     @Test
79     void shouldCreateAdapterWithClientSupportingPlainConnectionOnly() throws MalformedURLException {
80         HttpClientAdapter adapterWithHttps = new HttpClientAdapterImpl(HTTP_URL);
81         try {
82             adapterWithHttps.send("sample");
83         } catch (Exception actualException) {
84             assertThat(actualException).hasStackTraceContaining(PlainConnectionSocketFactory.class.toString());
85         }
86     }
87
88     private void assertAdapterSentRequest(String targetUrl) throws IOException {
89         HttpClientAdapter adapter = new HttpClientAdapterImpl(httpClient, targetUrl);
90         doReturn(httpResponse).when(httpClient).execute(any());
91
92         adapter.send("test-msg");
93
94         verify(httpClient).execute(any());
95         verify(httpResponse).getStatusLine();
96     }
97 }