Sonar Critical Fix
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-common / src / test / java / org / openecomp / dcae / apod / analytics / cdap / common / utils / DMaaPMRUtilsTest.java
1 /*\r
2  * ===============================LICENSE_START======================================\r
3  *  dcae-analytics\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
10  *\r
11  *          http://www.apache.org/licenses/LICENSE-2.0\r
12  *\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
19  */\r
20 \r
21 package org.openecomp.dcae.apod.analytics.cdap.common.utils;\r
22 \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
34 \r
35 import java.util.Collections;\r
36 \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
44 \r
45 /**\r
46  * @author Rajiv Singla . Creation Date: 2/6/2017.\r
47  */\r
48 public class DMaaPMRUtilsTest extends BaseAnalyticsCDAPCommonUnitTest {\r
49 \r
50     private DMaaPMRSubscriber subscriber;\r
51     private Metrics metrics;\r
52 \r
53 \r
54     @Before\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
59     }\r
60 \r
61     @Test\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
71     }\r
72 \r
73     @Test\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
79     }\r
80 \r
81     @Test\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
91     }\r
92 \r
93 \r
94     @Test\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
102     }\r
103 \r
104     @Test\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
110     }\r
111 \r
112 }\r