Initial TCA commit into DCAEGEN2
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-tca / src / test / java / org / openecomp / dcae / apod / analytics / cdap / tca / worker / TCADMaaPMRPublisherJobTest.java
1 /*
2  * ===============================LICENSE_START======================================
3  *  dcae-analytics
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
10  *
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *
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===========================================
19  */
20
21 package org.openecomp.dcae.apod.analytics.cdap.tca.worker;
22
23 import co.cask.cdap.api.TxRunnable;
24 import co.cask.cdap.api.metrics.Metrics;
25 import co.cask.cdap.api.worker.WorkerContext;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.ArgumentMatchers;
29 import org.openecomp.dcae.apod.analytics.cdap.common.CDAPMetricsConstants;
30 import org.openecomp.dcae.apod.analytics.cdap.common.persistance.tca.TCAVESAlertEntity;
31 import org.openecomp.dcae.apod.analytics.cdap.tca.BaseAnalyticsCDAPTCAUnitTest;
32 import org.openecomp.dcae.apod.analytics.common.AnalyticsConstants;
33 import org.openecomp.dcae.apod.analytics.dmaap.domain.response.DMaaPMRPublisherResponse;
34 import org.openecomp.dcae.apod.analytics.dmaap.service.publisher.DMaaPMRPublisher;
35 import org.quartz.JobDataMap;
36 import org.quartz.JobExecutionContext;
37
38 import java.util.HashMap;
39 import java.util.Map;
40 import java.util.Set;
41
42 import static org.mockito.ArgumentMatchers.any;
43 import static org.mockito.ArgumentMatchers.anyInt;
44 import static org.mockito.ArgumentMatchers.anyString;
45 import static org.mockito.ArgumentMatchers.eq;
46 import static org.mockito.Mockito.doNothing;
47 import static org.mockito.Mockito.mock;
48 import static org.mockito.Mockito.times;
49 import static org.mockito.Mockito.verify;
50 import static org.mockito.Mockito.when;
51
52 /**
53  * @author Rajiv Singla . Creation Date: 12/20/2016.
54  */
55 public class TCADMaaPMRPublisherJobTest extends BaseAnalyticsCDAPTCAUnitTest {
56
57     private JobExecutionContext jobExecutionContext;
58     private TCADMaaPMRPublisherJob publisherJob;
59     private JobDataMap jobDataMap;
60     private WorkerContext workerContext;
61     private DMaaPMRPublisher publisher;
62     private Metrics metrics;
63
64     private class TCATestDMaaPMRPublisherJob extends TCADMaaPMRPublisherJob {
65
66         private Map<String, TCAVESAlertEntity> alertEntityMap;
67
68         public TCATestDMaaPMRPublisherJob(Map<String, TCAVESAlertEntity> alertEntityMap) {
69             this.alertEntityMap = alertEntityMap;
70         }
71
72         @Override
73         protected Map<String, TCAVESAlertEntity> getNewAlertsMap(
74                 String cdapAlertsTableName, WorkerContext workerContext) {
75             return alertEntityMap;
76         }
77
78         @Override
79         protected void deleteAlertsByKey(String cdapAlertsTableName, WorkerContext workerContext,
80                                          Set<String> rowKeys, Metrics metrics) {
81             // do nothing
82         }
83     }
84
85     @Before
86     public void before() throws Exception {
87
88         jobExecutionContext = mock(JobExecutionContext.class);
89         workerContext = mock(WorkerContext.class);
90
91         metrics = mock(Metrics.class);
92         doNothing().when(metrics).count(anyString(), anyInt());
93         publisher = mock(DMaaPMRPublisher.class);
94
95         jobDataMap = mock(JobDataMap.class);
96         when(jobDataMap.getString(eq(AnalyticsConstants.CDAP_ALERTS_TABLE_VARIABLE_NAME))).thenReturn
97                 ("testAlertTableName");
98         when(jobDataMap.get(eq(AnalyticsConstants.WORKER_CONTEXT_VARIABLE_NAME))).thenReturn(workerContext);
99         when(jobDataMap.get(eq(AnalyticsConstants.DMAAP_PUBLISHER_VARIABLE_NAME))).thenReturn(publisher);
100         when(jobDataMap.get(AnalyticsConstants.DMAAP_METRICS_VARIABLE_NAME)).thenReturn(metrics);
101         when(jobExecutionContext.getMergedJobDataMap()).thenReturn(jobDataMap);
102
103
104         publisherJob = new TCADMaaPMRPublisherJob();
105     }
106
107     @Test
108     public void testExecuteWhenNoAlertsFoundInAlertsTable() throws Exception {
109         doNothing().when(workerContext).execute(any(TxRunnable.class));
110         publisherJob.execute(jobExecutionContext);
111         verify(metrics, times(1))
112                 .count(eq(CDAPMetricsConstants.TCA_PUBLISHER_NO_NEW_ALERTS_LOOKUP_METRIC), eq(1));
113     }
114
115     @Test
116     public void testExecuteWhenAlertsWereFoundInAlertsTable() throws Exception {
117
118         final DMaaPMRPublisherResponse publisherResponse = mock(DMaaPMRPublisherResponse.class);
119         when(publisherResponse.getResponseCode()).thenReturn(200);
120         when(publisherResponse.getResponseMessage()).thenReturn("success");
121         when(publisherResponse.getPendingMessagesCount()).thenReturn(0);
122         when(publisher.publish(ArgumentMatchers.<String>anyList())).thenReturn(publisherResponse);
123
124         final TCAVESAlertEntity tcavesAlertEntity = mock(TCAVESAlertEntity.class);
125         when(tcavesAlertEntity.getAlertMessage()).thenReturn("testAlertMessage");
126         Map<String, TCAVESAlertEntity> alertEntityMap = new HashMap<>();
127         alertEntityMap.put("key1", tcavesAlertEntity);
128         final TCATestDMaaPMRPublisherJob testPublisherJob = new TCATestDMaaPMRPublisherJob(alertEntityMap);
129         testPublisherJob.execute(jobExecutionContext);
130         verify(metrics, times(1))
131                 .count(eq(CDAPMetricsConstants.TCA_PUBLISHER_NEW_ALERTS_METRIC), eq(1));
132         verify(metrics, times(1))
133                 .count(eq(CDAPMetricsConstants.TCA_PUBLISHER_SUCCESSFUL_DMAAP_RESPONSE_METRIC), eq(1));
134     }
135
136     @Test
137     public void testExecuteWhenAlertsWereFoundButPublisherReturnedNon200ResponseCode() throws Exception {
138
139         final DMaaPMRPublisherResponse publisherResponse = mock(DMaaPMRPublisherResponse.class);
140         when(publisherResponse.getResponseCode()).thenReturn(500);
141         when(publisherResponse.getResponseMessage()).thenReturn("failed");
142         when(publisherResponse.getPendingMessagesCount()).thenReturn(0);
143         when(publisher.publish(ArgumentMatchers.<String>anyList())).thenReturn(publisherResponse);
144
145         final TCAVESAlertEntity tcavesAlertEntity = mock(TCAVESAlertEntity.class);
146         when(tcavesAlertEntity.getAlertMessage()).thenReturn("testAlertMessage");
147         Map<String, TCAVESAlertEntity> alertEntityMap = new HashMap<>();
148         alertEntityMap.put("key1", tcavesAlertEntity);
149         final TCATestDMaaPMRPublisherJob testPublisherJob = new TCATestDMaaPMRPublisherJob(alertEntityMap);
150         testPublisherJob.execute(jobExecutionContext);
151         verify(metrics, times(1))
152                 .count(eq(CDAPMetricsConstants.TCA_PUBLISHER_NEW_ALERTS_METRIC), eq(1));
153         verify(metrics, times(1))
154                 .count(eq(CDAPMetricsConstants.TCA_PUBLISHER_UNSUCCESSFUL_DMAAP_RESPONSE_METRIC), eq(1));
155     }
156
157
158 }