0e7fe3dde7df578bd6ca4fbe9d4ada2d64853921
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / dmaap / PublisherTest.java
1 /*
2  * Copyright 2017-2020 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.HttpResponse;
19 import org.apache.http.HttpStatus;
20 import org.apache.http.StatusLine;
21 import org.apache.http.client.methods.HttpPost;
22 import org.apache.http.entity.StringEntity;
23 import org.apache.http.impl.client.CloseableHttpClient;
24 import org.easymock.EasyMock;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.junit.runner.RunWith;
29 import org.onap.holmes.common.dmaap.entity.PolicyMsg;
30 import org.onap.holmes.common.exception.CorrelationException;
31 import org.onap.holmes.common.utils.HttpsUtils;
32 import org.powermock.api.easymock.PowerMock;
33 import org.powermock.core.classloader.annotations.PrepareForTest;
34 import org.powermock.modules.junit4.PowerMockRunner;
35
36 import java.util.HashMap;
37
38 import static org.hamcrest.CoreMatchers.is;
39 import static org.junit.Assert.assertThat;
40
41 @PrepareForTest({HttpsUtils.class, HttpResponse.class, Publisher.class})
42 @RunWith(PowerMockRunner.class)
43 public class PublisherTest {
44
45     @Rule
46     public ExpectedException thrown = ExpectedException.none();
47
48     private static final String URL = "http://localhost/dmaapTopic";
49
50     @Test
51     public void publish_exception() throws Exception {
52
53         Publisher publisher = new Publisher();
54         publisher.setUrl(URL);
55
56         thrown.expect(CorrelationException.class);
57         thrown.expectMessage("Failed to connect to DCAE");
58
59         publisher.publish(new PolicyMsg());
60     }
61
62     @Test
63     public void publish_normal() throws Exception {
64
65         Publisher publisher = new Publisher();
66         publisher.setUrl(URL);
67
68         PowerMock.mockStatic(HttpsUtils.class);
69         CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
70         EasyMock.expect(HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT)).andReturn(httpClient);
71         HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
72         EasyMock.expect(HttpsUtils
73                 .post(EasyMock.anyObject(HttpPost.class), EasyMock.anyObject(HashMap.class),
74                         EasyMock.anyObject(HashMap.class), EasyMock.anyObject(StringEntity.class),
75                         EasyMock.anyObject(CloseableHttpClient.class))).andReturn(httpResponse);
76         StatusLine statusLine = PowerMock.createMock(StatusLine.class);
77         EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLine);
78         EasyMock.expect(statusLine.getStatusCode()).andReturn(HttpStatus.SC_OK);
79         httpClient.close();
80         EasyMock.expectLastCall();
81
82         PowerMock.replayAll();
83
84         assertThat(publisher.publish(new PolicyMsg()), is(true));
85
86         PowerMock.verifyAll();
87     }
88
89 }