2 * Copyright 2017 ZTE Corporation.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.holmes.common.utils;
19 import static org.hamcrest.CoreMatchers.equalTo;
20 import static org.hamcrest.MatcherAssert.assertThat;
22 import java.util.HashMap;
24 import org.apache.http.impl.client.CloseableHttpClient;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.junit.runner.RunWith;
30 import org.onap.holmes.common.exception.CorrelationException;
31 import org.powermock.api.easymock.PowerMock;
32 import org.powermock.core.classloader.annotations.PrepareForTest;
33 import org.powermock.modules.junit4.PowerMockRunner;
34 import org.powermock.reflect.Whitebox;
36 @PrepareForTest({HttpsUtils.class, CloseableHttpClient.class})
37 @RunWith(PowerMockRunner.class)
38 public class HttpsUtilsTest {
41 public ExpectedException thrown = ExpectedException.none();
42 private HttpsUtils httpsUtils;
46 httpsUtils = new HttpsUtils();
51 public void testHttpsUtil_get_excepiton() throws Exception {
52 thrown.expect(CorrelationException.class);
53 thrown.expectMessage("Failed to use get method query data from server");
55 Map<String, String> header = new HashMap<>();
56 header.put("accept", "application/json");
57 Map<String, String> para = new HashMap<>();
58 para.put("tset", "1111");
59 String response = HttpsUtils.get(url, header);
60 assertThat(response, equalTo(""));
64 public void testHttpsUtil_post_excepiton() throws Exception {
65 thrown.expect(CorrelationException.class);
66 thrown.expectMessage("Failed to use post method query data from server");
68 Map<String, String> header = new HashMap<>();
69 header.put("accept", "application/json");
70 Map<String, String> para = new HashMap<>();
71 para.put("tset", "1111");
72 String response = HttpsUtils.post(url, header, para, null);
73 assertThat(response, equalTo(""));
77 public void testHttpsUtil_getResponseEntity_input_null() throws Exception {
79 httpsUtils = PowerMock.createMock(HttpsUtils.class);
80 PowerMock.replayAll();
81 String actual = Whitebox.invokeMethod(httpsUtils, "getResponseEntity", null);
82 PowerMock.verifyAll();
83 assertThat(actual, equalTo(""));
88 public void testHttpsUtil_getHttpClient_exception() throws Exception {
90 thrown.expect(Exception.class);
91 Whitebox.invokeMethod(HttpsUtils.class, "getHttpClient");
92 PowerMock.verifyAll();