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