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.openecomp.dcae.apod.analytics.cdap.tca.worker;
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;
38 import java.util.HashMap;
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;
53 * @author Rajiv Singla . Creation Date: 12/20/2016.
55 public class TCADMaaPMRPublisherJobTest extends BaseAnalyticsCDAPTCAUnitTest {
57 private JobExecutionContext jobExecutionContext;
58 private TCADMaaPMRPublisherJob publisherJob;
59 private JobDataMap jobDataMap;
60 private WorkerContext workerContext;
61 private DMaaPMRPublisher publisher;
62 private Metrics metrics;
64 private class TCATestDMaaPMRPublisherJob extends TCADMaaPMRPublisherJob {
66 private Map<String, TCAVESAlertEntity> alertEntityMap;
68 public TCATestDMaaPMRPublisherJob(Map<String, TCAVESAlertEntity> alertEntityMap) {
69 this.alertEntityMap = alertEntityMap;
73 protected Map<String, TCAVESAlertEntity> getNewAlertsMap(
74 String cdapAlertsTableName, WorkerContext workerContext) {
75 return alertEntityMap;
79 protected void deleteAlertsByKey(String cdapAlertsTableName, WorkerContext workerContext,
80 Set<String> rowKeys, Metrics metrics) {
86 public void before() throws Exception {
88 jobExecutionContext = mock(JobExecutionContext.class);
89 workerContext = mock(WorkerContext.class);
91 metrics = mock(Metrics.class);
92 doNothing().when(metrics).count(anyString(), anyInt());
93 publisher = mock(DMaaPMRPublisher.class);
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);
104 publisherJob = new TCADMaaPMRPublisherJob();
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));
116 public void testExecuteWhenAlertsWereFoundInAlertsTable() throws Exception {
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);
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));
137 public void testExecuteWhenAlertsWereFoundButPublisherReturnedNon200ResponseCode() throws Exception {
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);
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));