1d6fcfd4375c96c938b44b6326b179c9e4107fdd
[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(expected = AuthenticationException.class)
73     public void shoudNotGetHttpRequest_whenBasicAuthNotSet() throws AuthenticationException {
74
75         noBasicAuth();
76
77         commonHttpClient.getReq(URI, TIMEOUT);
78     }
79
80     @Test
81     public void shouldPostHttpRequest_whenSetBasicAuth() throws AuthenticationException {
82
83         setBasicAuth();
84
85         HttpPost httpPost = commonHttpClient.postReq(URL);
86
87         assertNotNull(httpPost);
88         assertNotNull(httpPost.getFirstHeader("Authorization"));
89         assertNotNull(httpPost.getConfig());
90         assertEquals(httpPost.getConfig().getSocketTimeout(), TIMEOUT_OFFSET);
91     }
92
93     @Test(expected = AuthenticationException.class)
94     public void shoudNotPostHttpRequest_whenBasicAuthNotSet() throws AuthenticationException {
95
96         noBasicAuth();
97
98         commonHttpClient.postReq(URL);
99     }
100
101     @Test
102     public void shouldGetClient() {
103         assertNotNull(commonHttpClient.getClient());
104     }
105
106     @Test
107     public void shouldFormatHostString() {
108         String httpUrl = HTTP + URL + ":" + HTTP_PORT;
109         String httpsUrl = HTTPS + URL + ":" + HTTPS_PORT;
110         String outputUrl;
111
112         outputUrl = commonHttpClient.formatHostString(httpUrl);
113         assertTrue(assertMessage(httpUrl, outputUrl), httpUrl.equals(outputUrl));
114
115         outputUrl = commonHttpClient.formatHostString(httpsUrl);
116         assertTrue(assertMessage(httpsUrl, outputUrl), httpsUrl.equals(outputUrl));
117
118         outputUrl = commonHttpClient.formatHostString(httpsUrl + "/");
119         assertTrue(assertMessage(httpsUrl, outputUrl), httpsUrl.equals(outputUrl));
120
121         outputUrl = commonHttpClient.formatHostString(URL + ":" + HTTP_PORT);
122         assertTrue(assertMessage(httpUrl, outputUrl), httpUrl.equals(outputUrl));
123
124         outputUrl = commonHttpClient.formatHostString(URL + ":" + HTTPS_PORT);
125         assertTrue(assertMessage(httpsUrl, outputUrl), httpsUrl.equals(outputUrl));
126
127         outputUrl = commonHttpClient.formatHostString(URL);
128         assertTrue(assertMessage(httpUrl, outputUrl), httpUrl.equals(outputUrl));
129     }
130
131     private String assertMessage(String expected, String actual) {
132         return "Expected: " + expected + " Actual: " + actual;
133     }
134 }