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 javax.ws.rs.client.Client;
22 import javax.ws.rs.client.ClientBuilder;
23 import javax.ws.rs.client.Entity;
24 import javax.ws.rs.client.Invocation.Builder;
25 import javax.ws.rs.client.WebTarget;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
28 import org.apache.http.HttpStatus;
29 import org.easymock.EasyMock;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.ExpectedException;
33 import org.junit.runner.RunWith;
34 import org.onap.holmes.common.dmaap.entity.PolicyMsg;
35 import org.onap.holmes.common.exception.CorrelationException;
36 import org.powermock.api.easymock.PowerMock;
37 import org.powermock.core.classloader.annotations.PrepareForTest;
38 import org.powermock.modules.junit4.PowerMockRunner;
40 @PrepareForTest({Client.class, WebTarget.class, ClientBuilder.class, Response.class, Builder.class})
41 @RunWith(PowerMockRunner.class)
42 public class PublisherTest {
45 public ExpectedException thrown = ExpectedException.none();
47 private static final String URL = "http://localhost/dmaapTopic";
50 public void publish_exception() throws Exception {
52 Publisher publisher = new Publisher();
53 publisher.setUrl(URL);
55 thrown.expect(CorrelationException.class);
56 thrown.expectMessage("Failed to connect to DCAE");
58 publisher.publish(new PolicyMsg());
62 public void publish_normal() throws Exception {
64 Publisher publisher = new Publisher();
65 publisher.setUrl(URL);
67 WebTarget target = PowerMock.createMock(WebTarget.class);
68 Client client = PowerMock.createMock(Client.class);
69 Builder builder = PowerMock.createMock(Builder.class);
70 Response response = PowerMock.createMock(Response.class);
71 PowerMock.mockStatic(ClientBuilder.class);
73 EasyMock.expect(ClientBuilder.newClient()).andReturn(client);
74 EasyMock.expect(client.target(publisher.getUrl())).andReturn(target);
75 EasyMock.expect(target.request(MediaType.APPLICATION_JSON)).andReturn(builder);
76 EasyMock.expect(builder.post(EasyMock.anyObject(Entity.class))).andReturn(response);
77 EasyMock.expect(response.getStatus()).andReturn(HttpStatus.SC_OK);
79 PowerMock.replayAll();
81 assertThat(publisher.publish(new PolicyMsg()), is(true));
83 PowerMock.verifyAll();