991455d5c1658d920f6272b953d200d6d3653920
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / dmaap / PublisherTest.java
1 /*
2  * Copyright 2017 ZTE Corporation.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.holmes.common.dmaap;
17
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;
30
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;
38
39 import static org.hamcrest.CoreMatchers.is;
40 import static org.junit.Assert.assertThat;
41
42 @PrepareForTest({Client.class, WebTarget.class, ClientBuilder.class, Response.class, Builder.class})
43 @RunWith(PowerMockRunner.class)
44 public class PublisherTest {
45
46     @Rule
47     public ExpectedException thrown = ExpectedException.none();
48
49     private static final String URL = "http://localhost/dmaapTopic";
50
51     @Test
52     public void publish_exception() throws Exception {
53
54         Publisher publisher = new Publisher();
55         publisher.setUrl(URL);
56
57         thrown.expect(CorrelationException.class);
58         thrown.expectMessage("Failed to connect to DCAE");
59
60         publisher.publish(new PolicyMsg());
61     }
62
63     @Test
64     public void publish_normal() throws Exception {
65
66         Publisher publisher = new Publisher();
67         publisher.setUrl(URL);
68
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);
74
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);
80
81         PowerMock.replayAll();
82
83         assertThat(publisher.publish(new PolicyMsg()), is(true));
84
85         PowerMock.verifyAll();
86     }
87
88 }