1 package org.openecomp.dcae.apod.analytics.cdap.tca.flowlet;
3 import co.cask.cdap.api.dataset.lib.ObjectMappedTable;
4 import co.cask.cdap.api.flow.flowlet.FlowletContext;
5 import co.cask.cdap.api.flow.flowlet.OutputEmitter;
6 import com.google.common.collect.ImmutableList;
7 import org.apache.commons.lang3.tuple.Pair;
9 import org.mockito.Mockito;
10 import org.openecomp.dcae.apod.analytics.cdap.common.CDAPComponentsConstants;
11 import org.openecomp.dcae.apod.analytics.cdap.common.domain.tca.ThresholdCalculatorOutput;
12 import org.openecomp.dcae.apod.analytics.cdap.common.exception.CDAPSettingsException;
13 import org.openecomp.dcae.apod.analytics.cdap.common.persistance.tca.TCAAlertsAbatementEntity;
14 import org.openecomp.dcae.apod.analytics.cdap.common.persistance.tca.TCAVESAlertEntity;
15 import org.openecomp.dcae.apod.analytics.cdap.tca.BaseAnalyticsCDAPTCAUnitTest;
16 import org.openecomp.dcae.apod.analytics.cdap.tca.settings.TCAPolicyPreferences;
17 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.ControlLoopEventStatus;
18 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.MetricsPerEventName;
19 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Threshold;
20 import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils;
22 import java.util.Date;
23 import java.util.List;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.doNothing;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
35 * Author: rs153v (Rajiv Singla) . Creation Date: 9/12/2017.
37 public class TCAVESAlertsAbatementFlowletTest extends BaseAnalyticsCDAPTCAUnitTest {
39 private static final TCAPolicyPreferences sampleTCAPolicyPreferences = getSampleTCAPolicyPreferences();
40 private static final List<MetricsPerEventName> metricsPerEventNames = sampleTCAPolicyPreferences
41 .getMetricsPerEventName();
42 private final OutputEmitter<String> mockOutputEmitter = mock(OutputEmitter.class);
44 private class TestTCAVESAlertsAbatementFlowlet extends TCAVESAlertsAbatementFlowlet {
46 public TestTCAVESAlertsAbatementFlowlet(String tcaAlertsAbatementTableName) {
47 super(tcaAlertsAbatementTableName);
48 this.alertsAbatementOutputEmitter = mockOutputEmitter;
49 doNothing().when(mockOutputEmitter).emit(any(String.class));
54 public void testConfigure() throws Exception {
55 final TCAVESAlertsAbatementFlowlet tcavesAlertsAbatementFlowlet =
56 new TCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
57 assertFlowletNameAndDescription(CDAPComponentsConstants.TCA_FIXED_VES_ALERTS_ABATEMENT_NAME_FLOWLET,
58 CDAPComponentsConstants.TCA_FIXED_VES_ALERTS_ABATEMENT_DESCRIPTION_FLOWLET,
59 tcavesAlertsAbatementFlowlet);
62 @Test(expected = CDAPSettingsException.class)
63 public void testDetermineAbatementAlertsWhenViolatedMetricsEventNameIsBlank() throws Exception {
65 final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
66 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
67 final Threshold violatedThreshold = getViolatedThreshold(ControlLoopEventStatus.ONSET);
68 final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
69 getMockThresholdCalculatorOutput(violatedThreshold);
70 when(mockThresholdCalculatorOutput.getViolatedMetricsPerEventName()).thenReturn("");
72 tcaAlertsAbatementFlowlet.determineAbatementAlerts(mockThresholdCalculatorOutput);
76 public void testDetermineAbatementAlertsWhenControlLoopTypeIsONSET() throws Exception {
78 final String testTCAAlertsAbatementTableName = "testTCAAlertsAbatementTableName";
79 final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
80 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
82 final FlowletContext mockFlowletContext = mock(FlowletContext.class);
83 final ObjectMappedTable<TCAAlertsAbatementEntity> mockObjectMappedTable = mock(ObjectMappedTable.class);
84 when(mockFlowletContext.getDataset(eq(testTCAAlertsAbatementTableName))).thenReturn(mockObjectMappedTable);
85 tcaAlertsAbatementFlowlet.initialize(mockFlowletContext);
87 doNothing().when(mockObjectMappedTable).write(any(String.class), any(TCAAlertsAbatementEntity.class));
89 final Threshold violatedThreshold = getViolatedThreshold(ControlLoopEventStatus.ONSET);
90 final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
91 getMockThresholdCalculatorOutput(violatedThreshold);
93 tcaAlertsAbatementFlowlet.determineAbatementAlerts(mockThresholdCalculatorOutput);
94 verify(mockObjectMappedTable,
95 times(1)).write(any(String.class), any(TCAAlertsAbatementEntity.class));
96 verify(mockOutputEmitter, times(1)).emit(any(String.class));
102 public void testDetermineAbatementAlertsWhenControlLoopTypeIsABATEDAndNoPreviousAlertWasSent() throws Exception {
104 final String testTCAAlertsAbatementTableName = "testTCAAlertsAbatementTableName";
105 final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
106 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
108 final FlowletContext mockFlowletContext = mock(FlowletContext.class);
109 final ObjectMappedTable<TCAAlertsAbatementEntity> mockObjectMappedTable = mock(ObjectMappedTable.class);
110 when(mockFlowletContext.getDataset(eq(testTCAAlertsAbatementTableName))).thenReturn(mockObjectMappedTable);
111 tcaAlertsAbatementFlowlet.initialize(mockFlowletContext);
113 doNothing().when(mockObjectMappedTable).write(any(String.class), any(TCAAlertsAbatementEntity.class));
114 final TCAAlertsAbatementEntity tcaAlertsAbatementEntity = mock(TCAAlertsAbatementEntity.class);
115 when(mockObjectMappedTable.read(any(String.class))).thenReturn(tcaAlertsAbatementEntity);
116 when(tcaAlertsAbatementEntity.getAbatementSentTS()).thenReturn(null);
118 final Threshold violatedThreshold = getViolatedThreshold(ControlLoopEventStatus.ABATED);
119 final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
120 getMockThresholdCalculatorOutput(violatedThreshold);
122 tcaAlertsAbatementFlowlet.determineAbatementAlerts(mockThresholdCalculatorOutput);
123 verify(mockObjectMappedTable,
124 times(1)).write(any(String.class), any(TCAAlertsAbatementEntity.class));
125 verify(mockOutputEmitter, times(1)).emit(any(String.class));
130 public void testDetermineAbatementAlertsWhenControlLoopTypeIsABATEDAndPreviousAlertWasAlreadySent() throws
133 final String testTCAAlertsAbatementTableName = "testTCAAlertsAbatementTableName";
134 final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
135 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
137 final FlowletContext mockFlowletContext = mock(FlowletContext.class);
138 final ObjectMappedTable<TCAAlertsAbatementEntity> mockObjectMappedTable = mock(ObjectMappedTable.class);
139 when(mockFlowletContext.getDataset(eq(testTCAAlertsAbatementTableName))).thenReturn(mockObjectMappedTable);
140 tcaAlertsAbatementFlowlet.initialize(mockFlowletContext);
142 doNothing().when(mockObjectMappedTable).write(any(String.class), any(TCAAlertsAbatementEntity.class));
143 final TCAAlertsAbatementEntity tcaAlertsAbatementEntity = mock(TCAAlertsAbatementEntity.class);
144 when(mockObjectMappedTable.read(any(String.class))).thenReturn(tcaAlertsAbatementEntity);
145 final long time = new Date().getTime();
146 when(tcaAlertsAbatementEntity.getAbatementSentTS()).thenReturn(Long.toString(time));
148 final Threshold violatedThreshold = getViolatedThreshold(ControlLoopEventStatus.ABATED);
149 final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
150 getMockThresholdCalculatorOutput(violatedThreshold);
152 tcaAlertsAbatementFlowlet.determineAbatementAlerts(mockThresholdCalculatorOutput);
153 verify(mockObjectMappedTable,
154 times(0)).write(any(String.class), any(TCAAlertsAbatementEntity.class));
155 verify(mockOutputEmitter, times(0)).emit(any(String.class));
161 public void testDetermineAbatementAlertsWhenControlLoopTypeIsABATEDAndNoPreviousONSETEventFound() throws
164 final String testTCAAlertsAbatementTableName = "testTCAAlertsAbatementTableName";
165 final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
166 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
168 final FlowletContext mockFlowletContext = mock(FlowletContext.class);
169 final ObjectMappedTable<TCAAlertsAbatementEntity> mockObjectMappedTable = mock(ObjectMappedTable.class);
170 when(mockFlowletContext.getDataset(eq(testTCAAlertsAbatementTableName))).thenReturn(mockObjectMappedTable);
171 tcaAlertsAbatementFlowlet.initialize(mockFlowletContext);
173 doNothing().when(mockObjectMappedTable).write(any(String.class), any(TCAAlertsAbatementEntity.class));
174 when(mockObjectMappedTable.read(any(String.class))).thenReturn(null);
176 final Threshold violatedThreshold = getViolatedThreshold(ControlLoopEventStatus.ABATED);
177 final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
178 getMockThresholdCalculatorOutput(violatedThreshold);
180 tcaAlertsAbatementFlowlet.determineAbatementAlerts(mockThresholdCalculatorOutput);
181 verify(mockObjectMappedTable,
182 times(0)).write(any(String.class), any(TCAAlertsAbatementEntity.class));
183 verify(mockOutputEmitter, times(0)).emit(any(String.class));
187 @Test(expected = CDAPSettingsException.class)
188 public void testDetermineAbatementAlertsWhenControlLoopTypeIsNotOnsetOrAbated() throws
190 final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
191 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
192 final Threshold violatedThreshold = getViolatedThreshold(ControlLoopEventStatus.CONTINUE);
193 final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
194 getMockThresholdCalculatorOutput(violatedThreshold);
196 tcaAlertsAbatementFlowlet.determineAbatementAlerts(mockThresholdCalculatorOutput);
200 private static Threshold getViolatedThreshold(final ControlLoopEventStatus controlLoopEventStatus) {
201 final Threshold violatedThreshold = Threshold.copy(metricsPerEventNames.get(0).getThresholds().get(0));
202 violatedThreshold.setClosedLoopEventStatus(controlLoopEventStatus);
203 return violatedThreshold;
207 private static ThresholdCalculatorOutput getMockThresholdCalculatorOutput(final Threshold violatedThreshold) throws
210 final MetricsPerEventName violatedMetricsPerEventName =
211 MetricsPerEventName.copy(metricsPerEventNames.get(0));
212 violatedMetricsPerEventName.setThresholds(ImmutableList.of(violatedThreshold));
213 return getMockThresholdCalculatorOutput(
214 fromStream(CEF_MESSAGE_JSON_FILE_LOCATION),
215 fromStream(TCA_POLICY_JSON_FILE_LOCATION),
216 TCAUtils.writeValueAsString(violatedMetricsPerEventName),
217 fromStream(TCA_ALERT_JSON_FILE_LOCATION)
222 private static ThresholdCalculatorOutput getMockThresholdCalculatorOutput(final String cefMessage,
223 final String tcaPolicy,
224 final String violatedMetricsPerEventName,
225 final String alertMessage) {
226 final ThresholdCalculatorOutput thresholdCalculatorOutput = mock(ThresholdCalculatorOutput.class);
227 when(thresholdCalculatorOutput.getCefMessage()).thenReturn(cefMessage);
228 when(thresholdCalculatorOutput.getTcaPolicy()).thenReturn(tcaPolicy);
229 when(thresholdCalculatorOutput.getViolatedMetricsPerEventName()).thenReturn(violatedMetricsPerEventName);
230 when(thresholdCalculatorOutput.getAlertMessage()).thenReturn(alertMessage);
231 return thresholdCalculatorOutput;