2 * Copyright 2017-2020 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.
16 package org.onap.holmes.common.dmaap;
18 import org.apache.http.HttpResponse;
19 import org.apache.http.HttpStatus;
20 import org.apache.http.StatusLine;
21 import org.apache.http.client.methods.HttpPost;
22 import org.apache.http.entity.StringEntity;
23 import org.apache.http.impl.client.CloseableHttpClient;
24 import org.easymock.EasyMock;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.junit.runner.RunWith;
29 import org.onap.holmes.common.dmaap.entity.PolicyMsg;
30 import org.onap.holmes.common.exception.CorrelationException;
31 import org.onap.holmes.common.utils.HttpsUtils;
32 import org.powermock.api.easymock.PowerMock;
33 import org.powermock.core.classloader.annotations.PrepareForTest;
34 import org.powermock.modules.junit4.PowerMockRunner;
36 import java.util.HashMap;
38 import static org.hamcrest.CoreMatchers.is;
39 import static org.junit.Assert.assertThat;
41 @PrepareForTest({HttpsUtils.class, HttpResponse.class, Publisher.class})
42 @RunWith(PowerMockRunner.class)
43 public class PublisherTest {
46 public ExpectedException thrown = ExpectedException.none();
48 private static final String URL = "http://localhost/dmaapTopic";
51 public void publish_exception() throws Exception {
53 Publisher publisher = new Publisher();
54 publisher.setUrl(URL);
56 thrown.expect(CorrelationException.class);
57 thrown.expectMessage("Failed to connect to DCAE");
59 publisher.publish(new PolicyMsg());
63 public void publish_normal() throws Exception {
65 Publisher publisher = new Publisher();
66 publisher.setUrl(URL);
68 PowerMock.mockStatic(HttpsUtils.class);
69 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
70 EasyMock.expect(HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT)).andReturn(httpClient);
71 HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
72 EasyMock.expect(HttpsUtils
73 .post(EasyMock.anyObject(HttpPost.class), EasyMock.anyObject(HashMap.class),
74 EasyMock.anyObject(HashMap.class), EasyMock.anyObject(StringEntity.class),
75 EasyMock.anyObject(CloseableHttpClient.class))).andReturn(httpResponse);
76 StatusLine statusLine = PowerMock.createMock(StatusLine.class);
77 EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLine);
78 EasyMock.expect(statusLine.getStatusCode()).andReturn(HttpStatus.SC_OK);
80 EasyMock.expectLastCall();
82 PowerMock.replayAll();
84 assertThat(publisher.publish(new PolicyMsg()), is(true));
86 PowerMock.verifyAll();