baf42ccbf1286a514786610f53e470924389d28d
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-tca / src / test / java / org / openecomp / dcae / apod / analytics / cdap / tca / flowlet / TCAVESAlertsAbatementFlowletTest.java
1 package org.openecomp.dcae.apod.analytics.cdap.tca.flowlet;
2
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;
8 import org.junit.Test;
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;
21
22 import java.util.Date;
23 import java.util.List;
24
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;
33
34 /**
35  * Author: rs153v (Rajiv Singla) . Creation Date: 9/12/2017.
36  */
37 public class TCAVESAlertsAbatementFlowletTest extends BaseAnalyticsCDAPTCAUnitTest {
38
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);
43
44     private class TestTCAVESAlertsAbatementFlowlet extends TCAVESAlertsAbatementFlowlet {
45
46         public TestTCAVESAlertsAbatementFlowlet(String tcaAlertsAbatementTableName) {
47             super(tcaAlertsAbatementTableName);
48             this.alertsAbatementOutputEmitter = mockOutputEmitter;
49             doNothing().when(mockOutputEmitter).emit(any(String.class));
50         }
51     }
52
53     @Test
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);
60     }
61
62     @Test(expected = CDAPSettingsException.class)
63     public void testDetermineAbatementAlertsWhenViolatedMetricsEventNameIsBlank() throws Exception {
64
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("");
71
72         tcaAlertsAbatementFlowlet.determineAbatementAlerts(mockThresholdCalculatorOutput);
73     }
74
75     @Test
76     public void testDetermineAbatementAlertsWhenControlLoopTypeIsONSET() throws Exception {
77
78         final String testTCAAlertsAbatementTableName = "testTCAAlertsAbatementTableName";
79         final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
80                 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
81
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);
86
87         doNothing().when(mockObjectMappedTable).write(any(String.class), any(TCAAlertsAbatementEntity.class));
88
89         final Threshold violatedThreshold = getViolatedThreshold(ControlLoopEventStatus.ONSET);
90         final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
91                 getMockThresholdCalculatorOutput(violatedThreshold);
92
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));
97
98     }
99
100
101     @Test
102     public void testDetermineAbatementAlertsWhenControlLoopTypeIsABATEDAndNoPreviousAlertWasSent() throws Exception {
103
104         final String testTCAAlertsAbatementTableName = "testTCAAlertsAbatementTableName";
105         final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
106                 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
107
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);
112
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);
117
118         final Threshold violatedThreshold = getViolatedThreshold(ControlLoopEventStatus.ABATED);
119         final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
120                 getMockThresholdCalculatorOutput(violatedThreshold);
121
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));
126
127     }
128
129     @Test
130     public void testDetermineAbatementAlertsWhenControlLoopTypeIsABATEDAndPreviousAlertWasAlreadySent() throws
131             Exception {
132
133         final String testTCAAlertsAbatementTableName = "testTCAAlertsAbatementTableName";
134         final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
135                 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
136
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);
141
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));
147
148         final Threshold violatedThreshold = getViolatedThreshold(ControlLoopEventStatus.ABATED);
149         final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
150                 getMockThresholdCalculatorOutput(violatedThreshold);
151
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));
156
157     }
158
159
160     @Test
161     public void testDetermineAbatementAlertsWhenControlLoopTypeIsABATEDAndNoPreviousONSETEventFound() throws
162             Exception {
163
164         final String testTCAAlertsAbatementTableName = "testTCAAlertsAbatementTableName";
165         final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
166                 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
167
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);
172
173         doNothing().when(mockObjectMappedTable).write(any(String.class), any(TCAAlertsAbatementEntity.class));
174         when(mockObjectMappedTable.read(any(String.class))).thenReturn(null);
175
176         final Threshold violatedThreshold = getViolatedThreshold(ControlLoopEventStatus.ABATED);
177         final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
178                 getMockThresholdCalculatorOutput(violatedThreshold);
179
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));
184
185     }
186
187     @Test(expected = CDAPSettingsException.class)
188     public void testDetermineAbatementAlertsWhenControlLoopTypeIsNotOnsetOrAbated() throws
189             Exception {
190         final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
191                 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
192         final Threshold violatedThreshold = getViolatedThreshold(ControlLoopEventStatus.CONTINUE);
193         final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
194                 getMockThresholdCalculatorOutput(violatedThreshold);
195
196         tcaAlertsAbatementFlowlet.determineAbatementAlerts(mockThresholdCalculatorOutput);
197
198     }
199
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;
204     }
205
206
207     private static ThresholdCalculatorOutput getMockThresholdCalculatorOutput(final Threshold violatedThreshold) throws
208             Exception {
209
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)
218         );
219     }
220
221
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;
232     }
233
234 }