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.cdap.common.utils;
\r
23 import co.cask.cdap.api.metrics.Metrics;
\r
24 import com.google.common.collect.ImmutableList;
\r
25 import org.junit.Before;
\r
26 import org.junit.Test;
\r
27 import org.mockito.ArgumentMatchers;
\r
28 import org.mockito.Mockito;
\r
29 import org.openecomp.dcae.apod.analytics.cdap.common.BaseAnalyticsCDAPCommonUnitTest;
\r
30 import org.openecomp.dcae.apod.analytics.cdap.common.CDAPMetricsConstants;
\r
31 import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;
\r
32 import org.openecomp.dcae.apod.analytics.dmaap.domain.response.DMaaPMRSubscriberResponse;
\r
33 import org.openecomp.dcae.apod.analytics.dmaap.service.subscriber.DMaaPMRSubscriber;
\r
35 import java.util.Collections;
\r
37 import static org.mockito.ArgumentMatchers.anyInt;
\r
38 import static org.mockito.ArgumentMatchers.anyString;
\r
39 import static org.mockito.ArgumentMatchers.eq;
\r
40 import static org.mockito.Mockito.doNothing;
\r
41 import static org.mockito.Mockito.mock;
\r
42 import static org.mockito.Mockito.verify;
\r
43 import static org.mockito.Mockito.when;
\r
46 * @author Rajiv Singla . Creation Date: 2/6/2017.
\r
48 public class DMaaPMRUtilsTest extends BaseAnalyticsCDAPCommonUnitTest {
\r
50 private DMaaPMRSubscriber subscriber;
\r
51 private Metrics metrics;
\r
55 public void before() throws Exception {
\r
56 metrics = mock(Metrics.class);
\r
57 doNothing().when(metrics).count(anyString(), anyInt());
\r
58 subscriber = mock(DMaaPMRSubscriber.class);
\r
62 public void testGetSubscriberMessagesWhenMessagesAreFound() throws Exception {
\r
63 final DMaaPMRSubscriberResponse subscriberResponse = mock(DMaaPMRSubscriberResponse.class);
\r
64 when(subscriberResponse.getResponseCode()).thenReturn(200);
\r
65 when(subscriberResponse.getResponseMessage()).thenReturn("testMessage");
\r
66 when(subscriberResponse.getFetchedMessages()).thenReturn(ImmutableList.of("testMessage1", "testMessage1"));
\r
67 when(subscriber.fetchMessages()).thenReturn(subscriberResponse);
\r
68 DMaaPMRUtils.getSubscriberMessages(subscriber, metrics);
\r
69 verify(metrics, Mockito.times(1)).count(ArgumentMatchers.eq(CDAPMetricsConstants
\r
70 .DMAAP_MR_SUBSCRIBER_TOTAL_MESSAGES_PROCESSED_METRIC), eq(2));
\r
74 public void testSubscriberMessagesWhenSubscriberResponseCodeIsNull() throws Exception {
\r
75 final DMaaPMRSubscriberResponse subscriberResponse = mock(DMaaPMRSubscriberResponse.class);
\r
76 when(subscriberResponse.getResponseCode()).thenReturn(null);
\r
77 when(subscriber.fetchMessages()).thenReturn(subscriberResponse);
\r
78 DMaaPMRUtils.getSubscriberMessages(subscriber, metrics);
\r
82 public void testSubscriberMessagesWhenNoMessagesFound() throws Exception {
\r
83 final DMaaPMRSubscriberResponse subscriberResponse = mock(DMaaPMRSubscriberResponse.class);
\r
84 when(subscriberResponse.getResponseCode()).thenReturn(200);
\r
85 when(subscriberResponse.getResponseMessage()).thenReturn("no messages");
\r
86 when(subscriberResponse.getFetchedMessages()).thenReturn(Collections.<String>emptyList());
\r
87 when(subscriber.fetchMessages()).thenReturn(subscriberResponse);
\r
88 DMaaPMRUtils.getSubscriberMessages(subscriber, metrics);
\r
89 verify(metrics, Mockito.times(1)).count(eq(CDAPMetricsConstants
\r
90 .DMAAP_MR_SUBSCRIBER_RESPONSES_WITH_NO_MESSAGES_METRIC), eq(1));
\r
95 public void testWhenSubscriberReturnNonSuccessfulReturnCode() throws Exception {
\r
96 final DMaaPMRSubscriberResponse subscriberResponse = mock(DMaaPMRSubscriberResponse.class);
\r
97 when(subscriberResponse.getResponseCode()).thenReturn(500);
\r
98 when(subscriber.fetchMessages()).thenReturn(subscriberResponse);
\r
99 DMaaPMRUtils.getSubscriberMessages(subscriber, metrics);
\r
100 verify(metrics, Mockito.times(1)).count(eq(CDAPMetricsConstants
\r
101 .DMAAP_MR_SUBSCRIBER_UNSUCCESSFUL_RESPONSES_METRIC), eq(1));
\r
105 public void testWhenSubscriberThrowsException() throws Exception {
\r
106 final DMaaPMRSubscriberResponse subscriberResponse = mock(DMaaPMRSubscriberResponse.class);
\r
107 when(subscriberResponse.getResponseCode()).thenReturn(500);
\r
108 when(subscriber.fetchMessages()).thenThrow(DCAEAnalyticsRuntimeException.class);
\r
109 DMaaPMRUtils.getSubscriberMessages(subscriber, metrics);
\r