Possible fix for event listener
[appc.git] / appc-adapters / appc-dmaap-adapter / appc-dmaap-adapter-bundle / src / test / java / org / onap / appc / adapter / messaging / dmaap / http / TestCommonHttpClient.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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.appc.adapter.messaging.dmaap.http;
22
23 import static org.junit.Assert.*;
24
25 import java.net.URI;
26 import org.apache.http.client.methods.HttpGet;
27 import org.apache.http.client.methods.HttpPost;
28 import org.junit.Before;
29 import org.junit.Test;
30
31 public class TestCommonHttpClient {
32
33     private static final String HTTP = "http://";
34     private static final String HTTPS = "https://";
35     private static final String URL = "example.org/location";
36     private static final URI URI = java.net.URI.create(HTTP + URL);
37     private static final String USERNAME = "username";
38     private static final String PASSWORD = "password";
39     private static final int TIMEOUT = 15000;
40     private static final int TIMEOUT_OFFSET = 5000;
41     private static final int HTTP_PORT = 3904;
42     private static final int HTTPS_PORT = 3905;
43
44     private CommonHttpClient commonHttpClient;
45
46     @Before
47     public void setUp() {
48         commonHttpClient = new CommonHttpClient() {};
49     }
50
51     private void setBasicAuth() {
52         commonHttpClient.setBasicAuth(USERNAME, PASSWORD);
53     }
54
55     private void noBasicAuth() {
56         commonHttpClient.setBasicAuth(null, null);
57     }
58
59     @Test
60     public void shouldGetHttpRequest_whenSetBasicAuth() throws AuthenticationException {
61
62         setBasicAuth();
63
64         HttpGet httpGet = commonHttpClient.getReq(URI, TIMEOUT);
65
66         assertNotNull(httpGet);
67         assertNotNull(httpGet.getFirstHeader("Authorization"));
68         assertNotNull(httpGet.getConfig());
69         assertEquals(httpGet.getConfig().getSocketTimeout(), TIMEOUT + TIMEOUT_OFFSET);
70     }
71
72     @Test
73     public void shouldPostHttpRequest_whenSetBasicAuth() throws AuthenticationException {
74
75         setBasicAuth();
76
77         HttpPost httpPost = commonHttpClient.postReq(URL);
78
79         assertNotNull(httpPost);
80         assertNotNull(httpPost.getFirstHeader("Authorization"));
81         assertNotNull(httpPost.getConfig());
82         assertEquals(httpPost.getConfig().getSocketTimeout(), TIMEOUT_OFFSET);
83     }
84
85     @Test
86     public void shouldGetClient() {
87         assertNotNull(commonHttpClient.getClient());
88     }
89
90     @Test
91     public void shouldFormatHostString() {
92         String httpUrl = HTTP + URL + ":" + HTTP_PORT;
93         String httpsUrl = HTTPS + URL + ":" + HTTPS_PORT;
94         String outputUrl;
95
96         outputUrl = commonHttpClient.formatHostString(httpUrl);
97         assertTrue(assertMessage(httpUrl, outputUrl), httpUrl.equals(outputUrl));
98
99         outputUrl = commonHttpClient.formatHostString(httpsUrl);
100         assertTrue(assertMessage(httpsUrl, outputUrl), httpsUrl.equals(outputUrl));
101
102         outputUrl = commonHttpClient.formatHostString(httpsUrl + "/");
103         assertTrue(assertMessage(httpsUrl, outputUrl), httpsUrl.equals(outputUrl));
104
105         outputUrl = commonHttpClient.formatHostString(URL + ":" + HTTP_PORT);
106         assertTrue(assertMessage(httpUrl, outputUrl), httpUrl.equals(outputUrl));
107
108         outputUrl = commonHttpClient.formatHostString(URL + ":" + HTTPS_PORT);
109         assertTrue(assertMessage(httpsUrl, outputUrl), httpsUrl.equals(outputUrl));
110
111         outputUrl = commonHttpClient.formatHostString(URL);
112         assertTrue(assertMessage(httpUrl, outputUrl), httpUrl.equals(outputUrl));
113     }
114
115     private String assertMessage(String expected, String actual) {
116         return "Expected: " + expected + " Actual: " + actual;
117     }
118 }