2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Ericsson
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.
19 * ============LICENSE_END=========================================================
22 package org.onap.appc.util;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNull;
26 import java.io.IOException;
27 import java.util.Properties;
28 import org.apache.http.HttpEntity;
29 import org.apache.http.StatusLine;
30 import org.apache.http.client.ClientProtocolException;
31 import org.apache.http.client.methods.CloseableHttpResponse;
32 import org.apache.http.impl.client.CloseableHttpClient;
33 import org.apache.http.impl.client.HttpClientBuilder;
34 import org.apache.http.impl.client.HttpClients;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mockito;
39 import org.onap.appc.configuration.Configuration;
40 import org.onap.appc.configuration.ConfigurationFactory;
41 import org.onap.appc.exceptions.APPCException;
42 import org.powermock.api.mockito.PowerMockito;
43 import org.powermock.core.classloader.annotations.PrepareForTest;
44 import org.powermock.modules.junit4.PowerMockRunner;
47 @RunWith(PowerMockRunner.class)
48 @PrepareForTest({ConfigurationFactory.class, HttpClients.class})
49 public class HttpClientTest {
51 private CloseableHttpClient client;
52 private CloseableHttpResponse httpResponse;
53 private StatusLine statusLine;
54 private HttpEntity httpEntity;
58 public void setup() throws ClientProtocolException, IOException {
59 Configuration configuration = Mockito.mock(Configuration.class);
60 Properties properties = new Properties();
61 properties.put("username", "username");
62 properties.put("password", "password");
63 Mockito.when(configuration.getProperty("username")).thenReturn("username");
64 Mockito.when(configuration.getProperty("password")).thenReturn("password");
65 PowerMockito.mockStatic(ConfigurationFactory.class);
66 PowerMockito.when(ConfigurationFactory.getConfiguration()).thenReturn(configuration);
67 client = Mockito.mock(CloseableHttpClient.class);
68 PowerMockito.mockStatic(HttpClients.class);
69 HttpClientBuilder httpClientBuilder = Mockito.mock(HttpClientBuilder.class);
70 PowerMockito.when(HttpClients.custom()).thenReturn(httpClientBuilder);
71 PowerMockito.when(httpClientBuilder.build()).thenReturn(client);
72 httpResponse = Mockito.mock(CloseableHttpResponse.class);
73 statusLine = Mockito.mock(StatusLine.class);
74 Mockito.when(statusLine.getStatusCode()).thenReturn(300);
75 Mockito.when(httpResponse.getStatusLine()).thenReturn(statusLine);
76 httpEntity = Mockito.mock(HttpEntity.class);
77 Mockito.when(httpResponse.getEntity()).thenReturn(httpEntity);
78 Mockito.when(client.execute(Mockito.any())).thenReturn(httpResponse);
82 public void testPostMethod() throws APPCException {
83 assertEquals(300, httpClient.postMethod("http", "127.0.0.1", 22, "/path", "{}", "application/json"));
87 public void testPutMethod() throws APPCException {
88 assertEquals(300, httpClient.putMethod("http", "127.0.0.1", 22, "/path", "{}", "application/json"));
92 public void testGetMethod() throws APPCException {
93 assertNull(httpClient.getMethod("http", "127.0.0.1", 22, "/path", "application/json"));
97 public void testDeleteMethod() throws APPCException {
98 assertEquals(300, httpClient.deleteMethod("http", "127.0.0.1", 22, "/path", "application/json"));