2 * ===============================LICENSE_START======================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
21 package org.openecomp.dcae.apod.analytics.dmaap.service.publisher;
\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
41 import java.io.IOException;
\r
42 import java.util.ArrayList;
\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
52 * @author Rajiv Singla . Creation Date: 10/21/2016.
\r
54 @RunWith(MockitoJUnitRunner.class)
\r
55 public class DMaaPMRPublisherImplTest extends BaseAnalyticsDMaaPUnitTest {
\r
58 private DMaaPMRPublisherQueueFactory dmaapMRPublisherQueueFactory;
\r
60 private CloseableHttpClient closeableHttpClient;
\r
62 private DMaaPMRPublisherQueue dmaapMRPublisherQueue;
\r
65 public void setUp() throws Exception {
\r
66 given(dmaapMRPublisherQueueFactory.create(Mockito.anyInt(), Mockito.anyInt()))
\r
67 .willReturn(dmaapMRPublisherQueue);
\r
71 public void tearDown() throws Exception {
\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
79 DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl(
\r
80 getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient);
\r
82 DMaaPMRPublisherResponse dmaapMRPublisherResponse = dmaapMRPublisherImpl.publish(getTwoSampleMessages());
\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
91 public void testPublishBigMessageList() throws Exception {
\r
93 given(dmaapMRPublisherQueue.getBatchQueueRemainingSize()).willReturn(0);
\r
94 given(dmaapMRPublisherQueue.getMessageForPublishing()).willReturn(getTwoSampleMessages());
\r
96 closeableHttpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class)))
\r
97 .thenReturn(new ImmutablePair<>(200, "Message successfully posted"));
\r
99 DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl(
\r
100 getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient);
\r
102 DMaaPMRPublisherResponse dmaapMRPublisherResponse = dmaapMRPublisherImpl.publish(getTwoSampleMessages());
\r
104 assertThat(dmaapMRPublisherResponse.getResponseCode(), is(200));
\r
105 assertThat(dmaapMRPublisherResponse.getPendingMessagesCount(), is(200));
\r
106 assertThat(dmaapMRPublisherResponse.getResponseMessage(), is("Message successfully posted"));
\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
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
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
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
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
142 public ExpectedException httpIOException = ExpectedException.none();
\r
145 public void testForcePublishHttpFailure() throws Exception {
\r
147 httpIOException.expect(DCAEAnalyticsRuntimeException.class);
\r
148 httpIOException.expectCause(isA(IOException.class));
\r
150 given(closeableHttpClient.execute(
\r
151 Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))).willThrow(IOException.class);
\r
153 DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl(
\r
154 getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient);
\r
155 dmaapMRPublisherImpl.forcePublish(getTwoSampleMessages());
\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
164 Mockito.when(dmaapMRPublisherQueue.getMessageForPublishing()).thenReturn(getTwoSampleMessages());
\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
173 public void testFlushEmptyList() throws Exception {
\r
174 Mockito.when(dmaapMRPublisherQueue.getMessageForPublishing()).thenReturn(new ArrayList<String>());
\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
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
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
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
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