d38d59ccb5c4a7516b49a31a3b3176b9ca8a69d2
[dcaegen2/analytics/tca.git] / dcae-analytics-dmaap / src / test / java / org / openecomp / dcae / apod / analytics / dmaap / service / publisher / DMaaPMRPublisherImplTest.java
1 /*\r
2  * ===============================LICENSE_START======================================\r
3  *  dcae-analytics\r
4  * ================================================================================\r
5  *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  *  Licensed under the Apache License, Version 2.0 (the "License");\r
8  *  you may not use this file except in compliance with the License.\r
9  *   You may obtain a copy of the License at\r
10  *\r
11  *          http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  *  Unless required by applicable law or agreed to in writing, software\r
14  *  distributed under the License is distributed on an "AS IS" BASIS,\r
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  *  See the License for the specific language governing permissions and\r
17  *  limitations under the License.\r
18  *  ============================LICENSE_END===========================================\r
19  */\r
20 \r
21 package org.openecomp.dcae.apod.analytics.dmaap.service.publisher;\r
22 \r
23 import org.apache.commons.lang3.tuple.ImmutablePair;\r
24 import org.apache.http.client.ResponseHandler;\r
25 import org.apache.http.client.methods.HttpUriRequest;\r
26 import org.apache.http.impl.client.CloseableHttpClient;\r
27 import org.junit.After;\r
28 import org.junit.Before;\r
29 import org.junit.Rule;\r
30 import org.junit.Test;\r
31 import org.junit.rules.ExpectedException;\r
32 import org.junit.runner.RunWith;\r
33 import org.mockito.Mock;\r
34 import org.mockito.Mockito;\r
35 import org.mockito.junit.MockitoJUnitRunner;\r
36 import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;\r
37 import org.openecomp.dcae.apod.analytics.dmaap.BaseAnalyticsDMaaPUnitTest;\r
38 import org.openecomp.dcae.apod.analytics.dmaap.domain.config.DMaaPMRPublisherConfig;\r
39 import org.openecomp.dcae.apod.analytics.dmaap.domain.response.DMaaPMRPublisherResponse;\r
40 \r
41 import java.io.IOException;\r
42 import java.util.ArrayList;\r
43 \r
44 import static org.hamcrest.CoreMatchers.isA;\r
45 import static org.hamcrest.core.Is.is;\r
46 import static org.junit.Assert.assertThat;\r
47 import static org.mockito.BDDMockito.given;\r
48 import static org.mockito.Mockito.times;\r
49 import static org.mockito.Mockito.verify;\r
50 \r
51 /**\r
52  * @author Rajiv Singla . Creation Date: 10/21/2016.\r
53  */\r
54 @RunWith(MockitoJUnitRunner.class)\r
55 public class DMaaPMRPublisherImplTest extends BaseAnalyticsDMaaPUnitTest {\r
56 \r
57     @Mock\r
58     private DMaaPMRPublisherQueueFactory dmaapMRPublisherQueueFactory;\r
59     @Mock\r
60     private CloseableHttpClient closeableHttpClient;\r
61     @Mock\r
62     private DMaaPMRPublisherQueue dmaapMRPublisherQueue;\r
63 \r
64     @Before\r
65     public void setUp() throws Exception {\r
66         given(dmaapMRPublisherQueueFactory.create(Mockito.anyInt(), Mockito.anyInt()))\r
67                 .willReturn(dmaapMRPublisherQueue);\r
68     }\r
69 \r
70     @After\r
71     public void tearDown() throws Exception {\r
72     }\r
73 \r
74     @Test\r
75     public void testPublishSmallMessageList() throws Exception {\r
76         given(dmaapMRPublisherQueue.getBatchQueueRemainingSize()).willReturn(10);\r
77         given(dmaapMRPublisherQueue.addBatchMessages(Mockito.<String>anyList())).willReturn(2);\r
78 \r
79         DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl(\r
80                 getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient);\r
81 \r
82         DMaaPMRPublisherResponse dmaapMRPublisherResponse = dmaapMRPublisherImpl.publish(getTwoSampleMessages());\r
83 \r
84         assertThat(dmaapMRPublisherResponse.getResponseCode(), is(202));\r
85         assertThat(dmaapMRPublisherResponse.getPendingMessagesCount(), is(2));\r
86         assertThat(dmaapMRPublisherResponse.getResponseMessage(),\r
87                 is("Accepted - Messages queued for batch publishing to MR Topic"));\r
88     }\r
89 \r
90     @Test\r
91     public void testPublishBigMessageList() throws Exception {\r
92 \r
93         given(dmaapMRPublisherQueue.getBatchQueueRemainingSize()).willReturn(0);\r
94         given(dmaapMRPublisherQueue.getMessageForPublishing()).willReturn(getTwoSampleMessages());\r
95         Mockito.when(\r
96                 closeableHttpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class)))\r
97                 .thenReturn(new ImmutablePair<>(200, "Message successfully posted"));\r
98 \r
99         DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl(\r
100                 getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient);\r
101 \r
102         DMaaPMRPublisherResponse dmaapMRPublisherResponse = dmaapMRPublisherImpl.publish(getTwoSampleMessages());\r
103 \r
104         assertThat(dmaapMRPublisherResponse.getResponseCode(), is(200));\r
105         assertThat(dmaapMRPublisherResponse.getPendingMessagesCount(), is(200));\r
106         assertThat(dmaapMRPublisherResponse.getResponseMessage(), is("Message successfully posted"));\r
107     }\r
108 \r
109     @Test\r
110     public void testForcePublishSuccessful() throws Exception {\r
111         DMaaPMRPublisherConfig dmaapMRPublisherConfig = new\r
112                 DMaaPMRPublisherConfig.Builder(HOST_NAME, TOPIC_NAME)\r
113                 .setPortNumber(PORT_NUMBER)\r
114                 .setProtocol(HTTP_PROTOCOL)\r
115                 .setContentType(CONTENT_TYPE)\r
116                 .setMaxRecoveryQueueSize(PUBLISHER_MAX_RECOVERY_QUEUE_SIZE)\r
117                 .setMaxBatchSize(PUBLISHER_MAX_BATCH_QUEUE_SIZE).build();\r
118 \r
119         Mockito.when(closeableHttpClient.execute(\r
120                 Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class)))\r
121                 .thenReturn(new ImmutablePair<>(200, "Message successfully posted"));\r
122 \r
123         DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl(\r
124                 dmaapMRPublisherConfig, dmaapMRPublisherQueueFactory, closeableHttpClient);\r
125         DMaaPMRPublisherResponse response = dmaapMRPublisherImpl.forcePublish(getTwoSampleMessages());\r
126         assertThat(response.getResponseCode(), is(200));\r
127     }\r
128 \r
129     @Test\r
130     public void testForcePublishFailure() throws Exception {\r
131         Mockito.when(closeableHttpClient.execute(\r
132                 Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class)))\r
133                 .thenReturn(new ImmutablePair<>(503, "Message successfully posted"));\r
134 \r
135         DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl(\r
136                 getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient);\r
137         DMaaPMRPublisherResponse response = dmaapMRPublisherImpl.forcePublish(getTwoSampleMessages());\r
138         assertThat(response.getResponseCode(), is(503));\r
139     }\r
140 \r
141     @Rule\r
142     public ExpectedException httpIOException = ExpectedException.none();\r
143 \r
144     @Test\r
145     public void testForcePublishHttpFailure() throws Exception {\r
146 \r
147         httpIOException.expect(DCAEAnalyticsRuntimeException.class);\r
148         httpIOException.expectCause(isA(IOException.class));\r
149 \r
150         given(closeableHttpClient.execute(\r
151                 Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))).willThrow(IOException.class);\r
152 \r
153         DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl(\r
154                 getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient);\r
155         dmaapMRPublisherImpl.forcePublish(getTwoSampleMessages());\r
156     }\r
157 \r
158     @Test\r
159     public void testFlushSuccessful() throws Exception {\r
160         Mockito.when(closeableHttpClient.execute(\r
161                 Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class)))\r
162                 .thenReturn(new ImmutablePair<>(200, "Message successfully posted"));\r
163 \r
164         Mockito.when(dmaapMRPublisherQueue.getMessageForPublishing()).thenReturn(getTwoSampleMessages());\r
165 \r
166         DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl(\r
167                 getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient);\r
168         DMaaPMRPublisherResponse response = dmaapMRPublisherImpl.flush();\r
169         assertThat(response.getResponseCode(), is(200));\r
170     }\r
171 \r
172     @Test\r
173     public void testFlushEmptyList() throws Exception {\r
174         Mockito.when(dmaapMRPublisherQueue.getMessageForPublishing()).thenReturn(new ArrayList<String>());\r
175 \r
176         DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl(\r
177                 getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient);\r
178         DMaaPMRPublisherResponse response = dmaapMRPublisherImpl.flush();\r
179         assertThat(response.getResponseCode(), is(204));\r
180     }\r
181 \r
182     @Test\r
183     public void testClose() throws Exception {\r
184         Mockito.when(dmaapMRPublisherQueue.getMessageForPublishing()).thenReturn(new ArrayList<String>());\r
185         Mockito.when(closeableHttpClient.execute(\r
186                 Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class)))\r
187                 .thenReturn(new ImmutablePair<>(200, "Message successfully posted"));\r
188         Mockito.when(dmaapMRPublisherQueue.getMessageForPublishing()).thenReturn(getTwoSampleMessages());\r
189 \r
190         DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl(\r
191                 getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient);\r
192         dmaapMRPublisherImpl.close();\r
193         verify(closeableHttpClient).execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class));\r
194     }\r
195 \r
196     @Test\r
197     public void testCloseUnsuccessful() throws Exception {\r
198         Mockito.when(dmaapMRPublisherQueue.getMessageForPublishing()).thenReturn(new ArrayList<String>());\r
199         Mockito.when(closeableHttpClient.execute(\r
200                 Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class)))\r
201                 .thenReturn(new ImmutablePair<>(400, "Message successfully posted"));\r
202         Mockito.when(dmaapMRPublisherQueue.getMessageForPublishing()).thenReturn(getTwoSampleMessages());\r
203 \r
204         DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl(\r
205                 getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient);\r
206         dmaapMRPublisherImpl.close();\r
207         verify(closeableHttpClient, times(6)).execute(Mockito.any(HttpUriRequest.class),\r
208                 Mockito.any(ResponseHandler.class));\r
209     }\r
210 }\r