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);
72 @Test(expected = AuthenticationException.class)
73 public void shoudNotGetHttpRequest_whenBasicAuthNotSet() throws AuthenticationException {
77 commonHttpClient.getReq(URI, TIMEOUT);
81 public void shouldPostHttpRequest_whenSetBasicAuth() throws AuthenticationException {
85 HttpPost httpPost = commonHttpClient.postReq(URL);
87 assertNotNull(httpPost);
88 assertNotNull(httpPost.getFirstHeader("Authorization"));
89 assertNotNull(httpPost.getConfig());
90 assertEquals(httpPost.getConfig().getSocketTimeout(), TIMEOUT_OFFSET);
93 @Test(expected = AuthenticationException.class)
94 public void shoudNotPostHttpRequest_whenBasicAuthNotSet() throws AuthenticationException {
98 commonHttpClient.postReq(URL);
102 public void shouldGetClient() {
103 assertNotNull(commonHttpClient.getClient());
107 public void shouldFormatHostString() {
108 String httpUrl = HTTP + URL + ":" + HTTP_PORT;
109 String httpsUrl = HTTPS + URL + ":" + HTTPS_PORT;
112 outputUrl = commonHttpClient.formatHostString(httpUrl);
113 assertTrue(assertMessage(httpUrl, outputUrl), httpUrl.equals(outputUrl));
115 outputUrl = commonHttpClient.formatHostString(httpsUrl);
116 assertTrue(assertMessage(httpsUrl, outputUrl), httpsUrl.equals(outputUrl));
118 outputUrl = commonHttpClient.formatHostString(httpsUrl + "/");
119 assertTrue(assertMessage(httpsUrl, outputUrl), httpsUrl.equals(outputUrl));
121 outputUrl = commonHttpClient.formatHostString(URL + ":" + HTTP_PORT);
122 assertTrue(assertMessage(httpUrl, outputUrl), httpUrl.equals(outputUrl));
124 outputUrl = commonHttpClient.formatHostString(URL + ":" + HTTPS_PORT);
125 assertTrue(assertMessage(httpsUrl, outputUrl), httpsUrl.equals(outputUrl));
127 outputUrl = commonHttpClient.formatHostString(URL);
128 assertTrue(assertMessage(httpUrl, outputUrl), httpUrl.equals(outputUrl));
131 private String assertMessage(String expected, String actual) {
132 return "Expected: " + expected + " Actual: " + actual;