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