Test Replace Jackson with GSON
[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 static org.hamcrest.CoreMatchers.is;
19 import static org.junit.Assert.assertThat;
20
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;
39
40 @PrepareForTest({Client.class, WebTarget.class, ClientBuilder.class, Response.class, Builder.class})
41 @RunWith(PowerMockRunner.class)
42 public class PublisherTest {
43
44     @Rule
45     public ExpectedException thrown = ExpectedException.none();
46
47     private static final String URL = "http://localhost/dmaapTopic";
48
49     @Test
50     public void publish_exception() throws Exception {
51
52         Publisher publisher = new Publisher();
53         publisher.setUrl(URL);
54
55         thrown.expect(CorrelationException.class);
56         thrown.expectMessage("Failed to connect to DCAE");
57
58         publisher.publish(new PolicyMsg());
59     }
60
61     @Test
62     public void publish_normal() throws Exception {
63
64         Publisher publisher = new Publisher();
65         publisher.setUrl(URL);
66
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);
72
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);
78
79         PowerMock.replayAll();
80
81         assertThat(publisher.publish(new PolicyMsg()), is(true));
82
83         PowerMock.verifyAll();
84     }
85
86 }