7755a1380aa07e871d47b593bde154b4fb9edbb3
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-tca / src / test / java / org / onap / dcae / apod / analytics / cdap / tca / flowlet / TCAVESAlertsAbatementFlowletTest.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.onap.dcae.apod.analytics.cdap.tca.flowlet;
22
23 import co.cask.cdap.api.dataset.lib.ObjectMappedTable;
24 import co.cask.cdap.api.flow.flowlet.FlowletContext;
25 import co.cask.cdap.api.flow.flowlet.OutputEmitter;
26 import com.google.common.collect.ImmutableList;
27 import org.junit.Test;
28 import org.onap.dcae.apod.analytics.cdap.common.CDAPComponentsConstants;
29 import org.onap.dcae.apod.analytics.cdap.common.domain.tca.ThresholdCalculatorOutput;
30 import org.onap.dcae.apod.analytics.cdap.common.exception.CDAPSettingsException;
31 import org.onap.dcae.apod.analytics.cdap.common.persistance.tca.TCAAlertsAbatementEntity;
32 import org.onap.dcae.apod.analytics.cdap.tca.BaseAnalyticsCDAPTCAUnitTest;
33 import org.onap.dcae.apod.analytics.cdap.tca.settings.TCAPolicyPreferences;
34 import org.onap.dcae.apod.analytics.model.domain.policy.tca.ClosedLoopEventStatus;
35 import org.onap.dcae.apod.analytics.model.domain.policy.tca.MetricsPerEventName;
36 import org.onap.dcae.apod.analytics.model.domain.policy.tca.Threshold;
37 import org.onap.dcae.apod.analytics.tca.utils.TCAUtils;
38
39 import java.util.Date;
40 import java.util.List;
41
42 import static org.mockito.ArgumentMatchers.any;
43 import static org.mockito.ArgumentMatchers.eq;
44 import static org.mockito.Mockito.doNothing;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.times;
47 import static org.mockito.Mockito.verify;
48 import static org.mockito.Mockito.when;
49
50 /**
51  * @author Rajiv Singla . Creation Date: 9/12/2017.
52  */
53 @SuppressWarnings("unchecked")
54 public class TCAVESAlertsAbatementFlowletTest extends BaseAnalyticsCDAPTCAUnitTest {
55
56     private static final TCAPolicyPreferences sampleTCAPolicyPreferences = getSampleTCAPolicyPreferences();
57     private static final List<MetricsPerEventName> metricsPerEventNames = sampleTCAPolicyPreferences
58             .getMetricsPerEventName();
59     private final OutputEmitter<String> mockOutputEmitter = mock(OutputEmitter.class);
60
61     private class TestTCAVESAlertsAbatementFlowlet extends TCAVESAlertsAbatementFlowlet {
62
63         public TestTCAVESAlertsAbatementFlowlet(String tcaAlertsAbatementTableName) {
64             super(tcaAlertsAbatementTableName);
65             this.alertsAbatementOutputEmitter = mockOutputEmitter;
66             doNothing().when(mockOutputEmitter).emit(any(String.class));
67         }
68     }
69
70     @Test
71     public void testConfigure() throws Exception {
72         final TCAVESAlertsAbatementFlowlet tcavesAlertsAbatementFlowlet =
73                 new TCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
74         assertFlowletNameAndDescription(CDAPComponentsConstants.TCA_FIXED_VES_ALERTS_ABATEMENT_NAME_FLOWLET,
75                 CDAPComponentsConstants.TCA_FIXED_VES_ALERTS_ABATEMENT_DESCRIPTION_FLOWLET,
76                 tcavesAlertsAbatementFlowlet);
77     }
78
79     @Test(expected = CDAPSettingsException.class)
80     public void testDetermineAbatementAlertsWhenViolatedMetricsEventNameIsBlank() throws Exception {
81
82         final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
83                 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
84         final Threshold violatedThreshold = getViolatedThreshold(ClosedLoopEventStatus.ONSET);
85         final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
86                 getMockThresholdCalculatorOutput(violatedThreshold);
87         when(mockThresholdCalculatorOutput.getViolatedMetricsPerEventName()).thenReturn("");
88
89         tcaAlertsAbatementFlowlet.determineAbatementAlerts(mockThresholdCalculatorOutput);
90     }
91
92     @Test
93     public void testDetermineAbatementAlertsWhenControlLoopTypeIsONSET() throws Exception {
94
95         final String testTCAAlertsAbatementTableName = "testTCAAlertsAbatementTableName";
96         final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
97                 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
98
99         final FlowletContext mockFlowletContext = mock(FlowletContext.class);
100         final ObjectMappedTable<TCAAlertsAbatementEntity> mockObjectMappedTable = mock(ObjectMappedTable.class);
101         when(mockFlowletContext.getDataset(eq(testTCAAlertsAbatementTableName))).thenReturn(mockObjectMappedTable);
102         tcaAlertsAbatementFlowlet.initialize(mockFlowletContext);
103
104         doNothing().when(mockObjectMappedTable).write(any(String.class), any(TCAAlertsAbatementEntity.class));
105
106         final Threshold violatedThreshold = getViolatedThreshold(ClosedLoopEventStatus.ONSET);
107         final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
108                 getMockThresholdCalculatorOutput(violatedThreshold);
109
110         tcaAlertsAbatementFlowlet.determineAbatementAlerts(mockThresholdCalculatorOutput);
111         verify(mockObjectMappedTable,
112                 times(1)).write(any(String.class), any(TCAAlertsAbatementEntity.class));
113         verify(mockOutputEmitter, times(1)).emit(any(String.class));
114
115     }
116
117
118     @Test
119     public void testDetermineAbatementAlertsWhenControlLoopTypeIsABATEDAndNoPreviousAlertWasSent() throws Exception {
120
121         final String testTCAAlertsAbatementTableName = "testTCAAlertsAbatementTableName";
122         final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
123                 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
124
125         final FlowletContext mockFlowletContext = mock(FlowletContext.class);
126         final ObjectMappedTable<TCAAlertsAbatementEntity> mockObjectMappedTable = mock(ObjectMappedTable.class);
127         when(mockFlowletContext.getDataset(eq(testTCAAlertsAbatementTableName))).thenReturn(mockObjectMappedTable);
128         tcaAlertsAbatementFlowlet.initialize(mockFlowletContext);
129
130         doNothing().when(mockObjectMappedTable).write(any(String.class), any(TCAAlertsAbatementEntity.class));
131         final TCAAlertsAbatementEntity tcaAlertsAbatementEntity = mock(TCAAlertsAbatementEntity.class);
132         when(mockObjectMappedTable.read(any(String.class))).thenReturn(tcaAlertsAbatementEntity);
133         when(tcaAlertsAbatementEntity.getAbatementSentTS()).thenReturn(null);
134
135         final Threshold violatedThreshold = getViolatedThreshold(ClosedLoopEventStatus.ABATED);
136         final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
137                 getMockThresholdCalculatorOutput(violatedThreshold);
138
139         tcaAlertsAbatementFlowlet.determineAbatementAlerts(mockThresholdCalculatorOutput);
140         verify(mockObjectMappedTable,
141                 times(1)).write(any(String.class), any(TCAAlertsAbatementEntity.class));
142         verify(mockOutputEmitter, times(1)).emit(any(String.class));
143
144     }
145
146     @Test
147     public void testDetermineAbatementAlertsWhenControlLoopTypeIsABATEDAndPreviousAlertWasAlreadySent() throws
148             Exception {
149
150         final String testTCAAlertsAbatementTableName = "testTCAAlertsAbatementTableName";
151         final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
152                 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
153
154         final FlowletContext mockFlowletContext = mock(FlowletContext.class);
155         final ObjectMappedTable<TCAAlertsAbatementEntity> mockObjectMappedTable = mock(ObjectMappedTable.class);
156         when(mockFlowletContext.getDataset(eq(testTCAAlertsAbatementTableName))).thenReturn(mockObjectMappedTable);
157         tcaAlertsAbatementFlowlet.initialize(mockFlowletContext);
158
159         doNothing().when(mockObjectMappedTable).write(any(String.class), any(TCAAlertsAbatementEntity.class));
160         final TCAAlertsAbatementEntity tcaAlertsAbatementEntity = mock(TCAAlertsAbatementEntity.class);
161         when(mockObjectMappedTable.read(any(String.class))).thenReturn(tcaAlertsAbatementEntity);
162         final long time = new Date().getTime();
163         when(tcaAlertsAbatementEntity.getAbatementSentTS()).thenReturn(Long.toString(time));
164
165         final Threshold violatedThreshold = getViolatedThreshold(ClosedLoopEventStatus.ABATED);
166         final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
167                 getMockThresholdCalculatorOutput(violatedThreshold);
168
169         tcaAlertsAbatementFlowlet.determineAbatementAlerts(mockThresholdCalculatorOutput);
170         verify(mockObjectMappedTable,
171                 times(0)).write(any(String.class), any(TCAAlertsAbatementEntity.class));
172         verify(mockOutputEmitter, times(0)).emit(any(String.class));
173
174     }
175
176
177     @Test
178     public void testDetermineAbatementAlertsWhenControlLoopTypeIsABATEDAndNoPreviousONSETEventFound() throws
179             Exception {
180
181         final String testTCAAlertsAbatementTableName = "testTCAAlertsAbatementTableName";
182         final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
183                 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
184
185         final FlowletContext mockFlowletContext = mock(FlowletContext.class);
186         final ObjectMappedTable<TCAAlertsAbatementEntity> mockObjectMappedTable = mock(ObjectMappedTable.class);
187         when(mockFlowletContext.getDataset(eq(testTCAAlertsAbatementTableName))).thenReturn(mockObjectMappedTable);
188         tcaAlertsAbatementFlowlet.initialize(mockFlowletContext);
189
190         doNothing().when(mockObjectMappedTable).write(any(String.class), any(TCAAlertsAbatementEntity.class));
191         when(mockObjectMappedTable.read(any(String.class))).thenReturn(null);
192
193         final Threshold violatedThreshold = getViolatedThreshold(ClosedLoopEventStatus.ABATED);
194         final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
195                 getMockThresholdCalculatorOutput(violatedThreshold);
196
197         tcaAlertsAbatementFlowlet.determineAbatementAlerts(mockThresholdCalculatorOutput);
198         verify(mockObjectMappedTable,
199                 times(0)).write(any(String.class), any(TCAAlertsAbatementEntity.class));
200         verify(mockOutputEmitter, times(0)).emit(any(String.class));
201
202     }
203
204     @Test(expected = CDAPSettingsException.class)
205     public void testDetermineAbatementAlertsWhenControlLoopTypeIsNotOnsetOrAbated() throws
206             Exception {
207         final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
208                 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
209         final Threshold violatedThreshold = getViolatedThreshold(ClosedLoopEventStatus.CONTINUE);
210         final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
211                 getMockThresholdCalculatorOutput(violatedThreshold);
212
213         tcaAlertsAbatementFlowlet.determineAbatementAlerts(mockThresholdCalculatorOutput);
214
215     }
216
217     private static Threshold getViolatedThreshold(final ClosedLoopEventStatus closedLoopEventStatus) {
218         final Threshold violatedThreshold = Threshold.copy(metricsPerEventNames.get(0).getThresholds().get(0));
219         violatedThreshold.setClosedLoopEventStatus(closedLoopEventStatus);
220         return violatedThreshold;
221     }
222
223
224     private static ThresholdCalculatorOutput getMockThresholdCalculatorOutput(final Threshold violatedThreshold) throws
225             Exception {
226
227         final MetricsPerEventName violatedMetricsPerEventName =
228                 MetricsPerEventName.copy(metricsPerEventNames.get(0));
229         violatedMetricsPerEventName.setThresholds(ImmutableList.of(violatedThreshold));
230         return getMockThresholdCalculatorOutput(
231                 fromStream(CEF_MESSAGE_JSON_FILE_LOCATION),
232                 fromStream(TCA_POLICY_JSON_FILE_LOCATION),
233                 TCAUtils.writeValueAsString(violatedMetricsPerEventName),
234                 fromStream(TCA_ALERT_JSON_FILE_LOCATION)
235         );
236     }
237
238
239     private static ThresholdCalculatorOutput getMockThresholdCalculatorOutput(final String cefMessage,
240                                                                               final String tcaPolicy,
241                                                                               final String violatedMetricsPerEventName,
242                                                                               final String alertMessage) {
243         final ThresholdCalculatorOutput thresholdCalculatorOutput = mock(ThresholdCalculatorOutput.class);
244         when(thresholdCalculatorOutput.getCefMessage()).thenReturn(cefMessage);
245         when(thresholdCalculatorOutput.getTcaPolicy()).thenReturn(tcaPolicy);
246         when(thresholdCalculatorOutput.getViolatedMetricsPerEventName()).thenReturn(violatedMetricsPerEventName);
247         when(thresholdCalculatorOutput.getAlertMessage()).thenReturn(alertMessage);
248         return thresholdCalculatorOutput;
249     }
250
251 }