aboutsummaryrefslogtreecommitdiffstats
path: root/dcae-analytics-cdap-common/src/test/java/org/openecomp/dcae/apod/analytics/cdap/common/utils/DMaaPMRUtilsTest.java
blob: 9a52e412fce777f8f3fb89a3a6e2fc27d8fced2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * ===============================LICENSE_START======================================
 *  dcae-analytics
 * ================================================================================
 *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  ============================LICENSE_END===========================================
 */

package org.openecomp.dcae.apod.analytics.cdap.common.utils;

import co.cask.cdap.api.metrics.Metrics;
import com.google.common.collect.ImmutableList;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.openecomp.dcae.apod.analytics.cdap.common.BaseAnalyticsCDAPCommonUnitTest;
import org.openecomp.dcae.apod.analytics.cdap.common.CDAPMetricsConstants;
import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;
import org.openecomp.dcae.apod.analytics.dmaap.domain.response.DMaaPMRSubscriberResponse;
import org.openecomp.dcae.apod.analytics.dmaap.service.subscriber.DMaaPMRSubscriber;

import java.util.Collections;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * @author Rajiv Singla . Creation Date: 2/6/2017.
 */
public class DMaaPMRUtilsTest extends BaseAnalyticsCDAPCommonUnitTest {

    private DMaaPMRSubscriber subscriber;
    private Metrics metrics;


    @Before
    public void before() throws Exception {
        metrics = mock(Metrics.class);
        doNothing().when(metrics).count(anyString(), anyInt());
        subscriber = mock(DMaaPMRSubscriber.class);
    }

    @Test
    public void testGetSubscriberMessagesWhenMessagesAreFound() throws Exception {
        final DMaaPMRSubscriberResponse subscriberResponse = mock(DMaaPMRSubscriberResponse.class);
        when(subscriberResponse.getResponseCode()).thenReturn(200);
        when(subscriberResponse.getResponseMessage()).thenReturn("testMessage");
        when(subscriberResponse.getFetchedMessages()).thenReturn(ImmutableList.of("testMessage1", "testMessage1"));
        when(subscriber.fetchMessages()).thenReturn(subscriberResponse);
        DMaaPMRUtils.getSubscriberMessages(subscriber, metrics);
        verify(metrics, Mockito.times(1)).count(ArgumentMatchers.eq(CDAPMetricsConstants
                .DMAAP_MR_SUBSCRIBER_TOTAL_MESSAGES_PROCESSED_METRIC), eq(2));
    }

    @Test
    public void testSubscriberMessagesWhenSubscriberResponseCodeIsNull() throws Exception {
        final DMaaPMRSubscriberResponse subscriberResponse = mock(DMaaPMRSubscriberResponse.class);
        when(subscriberResponse.getResponseCode()).thenReturn(null);
        when(subscriber.fetchMessages()).thenReturn(subscriberResponse);
        DMaaPMRUtils.getSubscriberMessages(subscriber, metrics);
    }

    @Test
    public void testSubscriberMessagesWhenNoMessagesFound() throws Exception {
        final DMaaPMRSubscriberResponse subscriberResponse = mock(DMaaPMRSubscriberResponse.class);
        when(subscriberResponse.getResponseCode()).thenReturn(200);
        when(subscriberResponse.getResponseMessage()).thenReturn("no messages");
        when(subscriberResponse.getFetchedMessages()).thenReturn(Collections.<String>emptyList());
        when(subscriber.fetchMessages()).thenReturn(subscriberResponse);
        DMaaPMRUtils.getSubscriberMessages(subscriber, metrics);
        verify(metrics, Mockito.times(1)).count(eq(CDAPMetricsConstants
                .DMAAP_MR_SUBSCRIBER_RESPONSES_WITH_NO_MESSAGES_METRIC), eq(1));
    }


    @Test
    public void testWhenSubscriberReturnNonSuccessfulReturnCode() throws Exception {
        final DMaaPMRSubscriberResponse subscriberResponse = mock(DMaaPMRSubscriberResponse.class);
        when(subscriberResponse.getResponseCode()).thenReturn(500);
        when(subscriber.fetchMessages()).thenReturn(subscriberResponse);
        DMaaPMRUtils.getSubscriberMessages(subscriber, metrics);
        verify(metrics, Mockito.times(1)).count(eq(CDAPMetricsConstants
                .DMAAP_MR_SUBSCRIBER_UNSUCCESSFUL_RESPONSES_METRIC), eq(1));
    }

    @Test
    public void testWhenSubscriberThrowsException() throws Exception {
        final DMaaPMRSubscriberResponse subscriberResponse = mock(DMaaPMRSubscriberResponse.class);
        when(subscriberResponse.getResponseCode()).thenReturn(500);
        when(subscriber.fetchMessages()).thenThrow(DCAEAnalyticsRuntimeException.class);
        DMaaPMRUtils.getSubscriberMessages(subscriber, metrics);
    }

}