2 * ============LICENSE_START=======================================================
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.appc.adapter.messaging.dmaap.http;
23 import static org.junit.Assert.*;
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;
31 public class TestCommonHttpClient {
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;
44 private CommonHttpClient commonHttpClient;
48 commonHttpClient = new CommonHttpClient() {};
51 private void setBasicAuth() {
52 commonHttpClient.setBasicAuth(USERNAME, PASSWORD);
55 private void noBasicAuth() {
56 commonHttpClient.setBasicAuth(null, null);
60 public void shouldGetHttpRequest_whenSetBasicAuth() throws AuthenticationException {
64 HttpGet httpGet = commonHttpClient.getReq(URI, TIMEOUT);
66 assertNotNull(httpGet);
67 assertNotNull(httpGet.getFirstHeader("Authorization"));
68 assertNotNull(httpGet.getConfig());
69 assertEquals(httpGet.getConfig().getSocketTimeout(), TIMEOUT + TIMEOUT_OFFSET);
73 public void shouldPostHttpRequest_whenSetBasicAuth() throws AuthenticationException {
77 HttpPost httpPost = commonHttpClient.postReq(URL);
79 assertNotNull(httpPost);
80 assertNotNull(httpPost.getFirstHeader("Authorization"));
81 assertNotNull(httpPost.getConfig());
82 assertEquals(httpPost.getConfig().getSocketTimeout(), TIMEOUT_OFFSET);
86 public void shouldGetClient() {
87 assertNotNull(commonHttpClient.getClient());
91 public void shouldFormatHostString() {
92 String httpUrl = HTTP + URL + ":" + HTTP_PORT;
93 String httpsUrl = HTTPS + URL + ":" + HTTPS_PORT;
96 outputUrl = commonHttpClient.formatHostString(httpUrl);
97 assertTrue(assertMessage(httpUrl, outputUrl), httpUrl.equals(outputUrl));
99 outputUrl = commonHttpClient.formatHostString(httpsUrl);
100 assertTrue(assertMessage(httpsUrl, outputUrl), httpsUrl.equals(outputUrl));
102 outputUrl = commonHttpClient.formatHostString(httpsUrl + "/");
103 assertTrue(assertMessage(httpsUrl, outputUrl), httpsUrl.equals(outputUrl));
105 outputUrl = commonHttpClient.formatHostString(URL + ":" + HTTP_PORT);
106 assertTrue(assertMessage(httpUrl, outputUrl), httpUrl.equals(outputUrl));
108 outputUrl = commonHttpClient.formatHostString(URL + ":" + HTTPS_PORT);
109 assertTrue(assertMessage(httpsUrl, outputUrl), httpsUrl.equals(outputUrl));
111 outputUrl = commonHttpClient.formatHostString(URL);
112 assertTrue(assertMessage(httpUrl, outputUrl), httpUrl.equals(outputUrl));
115 private String assertMessage(String expected, String actual) {
116 return "Expected: " + expected + " Actual: " + actual;