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.easymock.EasyMock;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37 import org.junit.runner.RunWith;
38 import org.mockito.Matchers;
39 import org.onap.holmes.common.dmaap.entity.PolicyMsg;
40 import org.onap.holmes.common.exception.CorrelationException;
41 import org.onap.holmes.common.utils.HttpsUtils;
42 import org.powermock.api.easymock.PowerMock;
43 import org.powermock.api.mockito.PowerMockito;
44 import org.powermock.core.classloader.annotations.PrepareForTest;
45 import org.powermock.modules.junit4.PowerMockRunner;
47 @PrepareForTest({HttpsUtils.class, HttpResponse.class})
48 @RunWith(PowerMockRunner.class)
49 public class PublisherTest {
52 public ExpectedException thrown = ExpectedException.none();
54 private static final String URL = "http://localhost/dmaapTopic";
57 public void publish_exception() throws Exception {
59 Publisher publisher = new Publisher();
60 publisher.setUrl(URL);
62 thrown.expect(CorrelationException.class);
63 thrown.expectMessage("Failed to connect to DCAE");
65 publisher.publish(new PolicyMsg());
69 public void publish_normal() throws Exception {
71 Publisher publisher = new Publisher();
72 publisher.setUrl(URL);
74 PowerMockito.mockStatic(HttpsUtils.class);
75 HttpResponse httpResponse = PowerMockito.mock(HttpResponse.class);
76 PowerMockito.when(HttpsUtils.post(Matchers.eq("http://localhost/dmaapTopic"),
77 Matchers.any(HashMap.class), Matchers.any(HashMap.class), Matchers.any(StringEntity.class))).thenReturn(httpResponse);
78 StatusLine statusLine = PowerMockito.mock(StatusLine.class);
79 PowerMockito.when(httpResponse.getStatusLine()).thenReturn(statusLine);
80 PowerMockito.when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_OK);
82 PowerMock.replayAll();
84 assertThat(publisher.publish(new PolicyMsg()), is(true));
86 PowerMock.verifyAll();