2 * ===============================LICENSE_START======================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============================LICENSE_END===========================================
21 package org.openecomp.dcae.apod.analytics.dmaap.service.subscriber;
23 import com.jayway.jsonassert.impl.matcher.IsCollectionWithSize;
24 import org.apache.commons.lang3.tuple.ImmutablePair;
25 import org.apache.http.client.ResponseHandler;
26 import org.apache.http.client.methods.HttpUriRequest;
27 import org.apache.http.impl.client.CloseableHttpClient;
28 import org.junit.After;
29 import org.junit.Before;
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.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;
38 import org.openecomp.dcae.apod.analytics.dmaap.BaseAnalyticsDMaaPUnitTest;
39 import org.openecomp.dcae.apod.analytics.dmaap.domain.config.DMaaPMRSubscriberConfig;
40 import org.openecomp.dcae.apod.analytics.dmaap.domain.response.DMaaPMRSubscriberResponse;
42 import java.io.IOException;
43 import java.util.Random;
44 import java.util.UUID;
46 import static org.hamcrest.CoreMatchers.isA;
47 import static org.hamcrest.MatcherAssert.assertThat;
48 import static org.hamcrest.core.Is.is;
49 import static org.mockito.BDDMockito.given;
52 * @author Rajiv Singla . Creation Date: 10/21/2016.
54 @RunWith(MockitoJUnitRunner.class)
55 public class DMaaPMRSubscriberImplTest extends BaseAnalyticsDMaaPUnitTest {
58 private CloseableHttpClient closeableHttpClient;
60 private String consumerGroup, consumerId;
63 public void setUp() throws Exception {
64 Random random = new Random(10000L);
65 consumerGroup = "Test-Consumer-Group" + Long.toString(random.nextLong());
66 consumerId = UUID.randomUUID().toString();
70 public void tearDown() throws Exception {
75 public void testSubscriberSuccessfullyReceiveDmaapMessage() throws Exception {
77 String testMessages = "[{\"message\":\"I'm Object 1 Message\"}," +
78 "{\"message\":\"I'm Object 2 Message\"}]";
80 closeableHttpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class)))
81 .thenReturn(new ImmutablePair<>(200, testMessages));
83 DMaaPMRSubscriberImpl dmaapMRSubscriberImpl = new DMaaPMRSubscriberImpl(
84 getSubscriberConfig(consumerId, consumerGroup), closeableHttpClient);
85 DMaaPMRSubscriberResponse dmaapMRSubscriberResponse = dmaapMRSubscriberImpl.fetchMessages();
86 assertThat(dmaapMRSubscriberResponse.getResponseCode(), is(200));
87 assertThat(dmaapMRSubscriberResponse.getFetchedMessages(), IsCollectionWithSize.hasSize(2));
91 public void testSubscriberSuccessfullyReceiveDmaapMessageWithNoUsername() throws Exception {
93 DMaaPMRSubscriberConfig dmaapMRSubscriberConfig = new DMaaPMRSubscriberConfig.Builder(HOST_NAME, TOPIC_NAME)
94 .setPortNumber(PORT_NUMBER)
95 .setProtocol(HTTP_PROTOCOL)
96 .setContentType(CONTENT_TYPE)
97 .setConsumerGroup(consumerGroup != null ? consumerGroup : SUBSCRIBER_CONSUMER_GROUP_NAME)
98 .setConsumerId(consumerId != null ? consumerId : SUBSCRIBER_CONSUMER_ID)
99 .setTimeoutMS(SUBSCRIBER_TIMEOUT_MS)
100 .setMessageLimit(SUBSCRIBER_MESSAGE_LIMIT).build();
102 String testMessages = "[{\"message\":\"I'm Object 1 Message\"}," +
103 "{\"message\":\"I'm Object 2 Message\"}]";
105 closeableHttpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class)))
106 .thenReturn(new ImmutablePair<>(200, testMessages));
108 DMaaPMRSubscriberImpl dmaapMRSubscriberImpl = new DMaaPMRSubscriberImpl(
109 dmaapMRSubscriberConfig, closeableHttpClient);
110 DMaaPMRSubscriberResponse dmaapMRSubscriberResponse = dmaapMRSubscriberImpl.fetchMessages();
111 assertThat(dmaapMRSubscriberResponse.getResponseCode(), is(200));
112 assertThat(dmaapMRSubscriberResponse.getFetchedMessages(), IsCollectionWithSize.hasSize(2));
116 public void testSubscriberSuccessfullyReceiveNoDmaapMessage() throws Exception {
118 closeableHttpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class)))
119 .thenReturn(new ImmutablePair<>(200, null));
121 DMaaPMRSubscriberImpl dmaapMRSubscriberImpl = new DMaaPMRSubscriberImpl(
122 getSubscriberConfig(consumerId, consumerGroup), closeableHttpClient);
123 DMaaPMRSubscriberResponse dmaapMRSubscriberResponse = dmaapMRSubscriberImpl.fetchMessages();
124 assertThat(dmaapMRSubscriberResponse.getResponseCode(), is(200));
125 assertThat(dmaapMRSubscriberResponse.getFetchedMessages(), IsCollectionWithSize.hasSize(0));
129 public void testSubscriberSuccessfullyReceiveErrorMessage() throws Exception {
131 closeableHttpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class)))
132 .thenReturn(new ImmutablePair<>(400, "Bad Request"));
134 DMaaPMRSubscriberImpl dmaapMRSubscriberImpl = new DMaaPMRSubscriberImpl(
135 getSubscriberConfig(consumerId, consumerGroup), closeableHttpClient);
136 DMaaPMRSubscriberResponse dmaapMRSubscriberResponse = dmaapMRSubscriberImpl.fetchMessages();
137 assertThat(dmaapMRSubscriberResponse.getResponseCode(), is(400));
138 assertThat(dmaapMRSubscriberResponse.getFetchedMessages(), IsCollectionWithSize.hasSize(0));
142 public ExpectedException httpIOException = ExpectedException.none();
145 public void testSubscriberSuccessfullyReceiveException() throws Exception {
147 httpIOException.expect(DCAEAnalyticsRuntimeException.class);
148 httpIOException.expectCause(isA(IOException.class));
150 given(closeableHttpClient.execute(
151 Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))).willThrow(IOException.class);
153 DMaaPMRSubscriberImpl dmaapMRSubscriberImpl = new DMaaPMRSubscriberImpl(
154 getSubscriberConfig(consumerId, consumerGroup), closeableHttpClient);
155 dmaapMRSubscriberImpl.fetchMessages();