fix https timeout get connection
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / dmaap / PublisherTest.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 package org.onap.holmes.common.dmaap;
17
18 import static org.hamcrest.CoreMatchers.is;
19 import static org.junit.Assert.assertThat;
20
21 import java.util.HashMap;
22 import javax.ws.rs.client.Client;
23 import javax.ws.rs.client.ClientBuilder;
24 import javax.ws.rs.client.Entity;
25 import javax.ws.rs.client.Invocation.Builder;
26 import javax.ws.rs.client.WebTarget;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29 import org.apache.http.HttpResponse;
30 import org.apache.http.HttpStatus;
31 import org.apache.http.StatusLine;
32 import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
33 import org.apache.http.client.methods.HttpPost;
34 import org.apache.http.entity.StringEntity;
35 import org.apache.http.impl.client.CloseableHttpClient;
36 import org.easymock.EasyMock;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.ExpectedException;
40 import org.junit.runner.RunWith;
41 import org.mockito.Matchers;
42 import org.onap.holmes.common.dmaap.entity.PolicyMsg;
43 import org.onap.holmes.common.exception.CorrelationException;
44 import org.onap.holmes.common.utils.HttpsUtils;
45 import org.powermock.api.easymock.PowerMock;
46 import org.powermock.api.mockito.PowerMockito;
47 import org.powermock.core.classloader.annotations.PrepareForTest;
48 import org.powermock.modules.junit4.PowerMockRunner;
49
50 @PrepareForTest({HttpsUtils.class, HttpResponse.class})
51 @RunWith(PowerMockRunner.class)
52 public class PublisherTest {
53
54     @Rule
55     public ExpectedException thrown = ExpectedException.none();
56
57     private static final String URL = "http://localhost/dmaapTopic";
58
59     @Test
60     public void publish_exception() throws Exception {
61
62         Publisher publisher = new Publisher();
63         publisher.setUrl(URL);
64
65         thrown.expect(CorrelationException.class);
66         thrown.expectMessage("Failed to connect to DCAE");
67
68         publisher.publish(new PolicyMsg());
69     }
70
71     @Test
72     public void publish_normal() throws Exception {
73
74         Publisher publisher = new Publisher();
75         publisher.setUrl(URL);
76
77         PowerMockito.mockStatic(HttpsUtils.class);
78         HttpResponse httpResponse = PowerMockito.mock(HttpResponse.class);
79         PowerMockito.when(HttpsUtils
80                 .post(Matchers.any(HttpPost.class), Matchers.any(HashMap.class),
81                         Matchers.any(HashMap.class), Matchers.any(StringEntity.class),
82                         Matchers.any(CloseableHttpClient.class))).thenReturn(httpResponse);
83         StatusLine statusLine = PowerMockito.mock(StatusLine.class);
84         PowerMockito.when(httpResponse.getStatusLine()).thenReturn(statusLine);
85         PowerMockito.when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_OK);
86
87         PowerMock.replayAll();
88
89         assertThat(publisher.publish(new PolicyMsg()), is(true));
90
91         PowerMock.verifyAll();
92     }
93
94 }