modify unit test and aai bug
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / utils / HttpsUtilsTest.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.holmes.common.utils;
18
19 import static org.hamcrest.CoreMatchers.equalTo;
20 import static org.hamcrest.MatcherAssert.assertThat;
21
22 import java.util.HashMap;
23 import java.util.Map;
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;
35
36 @PrepareForTest({HttpsUtils.class, CloseableHttpClient.class})
37 @RunWith(PowerMockRunner.class)
38 public class HttpsUtilsTest {
39
40     @Rule
41     public ExpectedException thrown = ExpectedException.none();
42     private HttpsUtils httpsUtils;
43
44     @Before
45     public void setUp() {
46         httpsUtils = new HttpsUtils();
47     }
48
49
50     @Test
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");
54         String url = "host";
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(""));
61     }
62
63     @Test
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");
67         String url = "host";
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(""));
74     }
75
76     @Test
77     public void testHttpsUtil_getResponseEntity_input_null() throws Exception {
78         PowerMock.resetAll();
79         httpsUtils = PowerMock.createMock(HttpsUtils.class);
80         PowerMock.replayAll();
81         String actual = Whitebox.invokeMethod(httpsUtils, "getResponseEntity", null);
82         PowerMock.verifyAll();
83         assertThat(actual, equalTo(""));
84     }
85
86
87     @Test
88     public void testHttpsUtil_getHttpClient_exception() throws Exception {
89         PowerMock.resetAll();
90         thrown.expect(Exception.class);
91         Whitebox.invokeMethod(HttpsUtils.class, "getHttpClient");
92         PowerMock.verifyAll();
93     }
94
95 }