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.
16 package org.onap.holmes.common.dmaap;
18 import static org.hamcrest.CoreMatchers.is;
19 import static org.junit.Assert.assertThat;
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.entity.StringEntity;
33 import org.apache.http.impl.client.CloseableHttpClient;
34 import org.easymock.EasyMock;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.rules.ExpectedException;
38 import org.junit.runner.RunWith;
39 import org.mockito.Matchers;
40 import org.onap.holmes.common.dmaap.entity.PolicyMsg;
41 import org.onap.holmes.common.exception.CorrelationException;
42 import org.onap.holmes.common.utils.HttpsUtils;
43 import org.powermock.api.easymock.PowerMock;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.core.classloader.annotations.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
48 @PrepareForTest({HttpsUtils.class, HttpResponse.class})
49 @RunWith(PowerMockRunner.class)
50 public class PublisherTest {
53 public ExpectedException thrown = ExpectedException.none();
55 private static final String URL = "http://localhost/dmaapTopic";
58 public void publish_exception() throws Exception {
60 Publisher publisher = new Publisher();
61 publisher.setUrl(URL);
63 thrown.expect(CorrelationException.class);
64 thrown.expectMessage("Failed to connect to DCAE");
66 publisher.publish(new PolicyMsg());
70 public void publish_normal() throws Exception {
72 Publisher publisher = new Publisher();
73 publisher.setUrl(URL);
75 PowerMockito.mockStatic(HttpsUtils.class);
76 HttpResponse httpResponse = PowerMockito.mock(HttpResponse.class);
77 PowerMockito.when(HttpsUtils
78 .post(Matchers.eq("http://localhost/dmaapTopic"), Matchers.any(HashMap.class),
79 Matchers.any(HashMap.class), Matchers.any(StringEntity.class),
80 Matchers.any(CloseableHttpClient.class))).thenReturn(httpResponse);
81 StatusLine statusLine = PowerMockito.mock(StatusLine.class);
82 PowerMockito.when(httpResponse.getStatusLine()).thenReturn(statusLine);
83 PowerMockito.when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_OK);
85 PowerMock.replayAll();
87 assertThat(publisher.publish(new PolicyMsg()), is(true));
89 PowerMock.verifyAll();