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.onap.dcae.apod.analytics.cdap.tca.flowlet;
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;
39 import java.util.Date;
40 import java.util.List;
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;
51 * @author Rajiv Singla . Creation Date: 9/12/2017.
53 @SuppressWarnings("unchecked")
54 public class TCAVESAlertsAbatementFlowletTest extends BaseAnalyticsCDAPTCAUnitTest {
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);
61 private class TestTCAVESAlertsAbatementFlowlet extends TCAVESAlertsAbatementFlowlet {
63 public TestTCAVESAlertsAbatementFlowlet(String tcaAlertsAbatementTableName) {
64 super(tcaAlertsAbatementTableName);
65 this.alertsAbatementOutputEmitter = mockOutputEmitter;
66 doNothing().when(mockOutputEmitter).emit(any(String.class));
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);
79 @Test(expected = CDAPSettingsException.class)
80 public void testDetermineAbatementAlertsWhenViolatedMetricsEventNameIsBlank() throws Exception {
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("");
89 tcaAlertsAbatementFlowlet.determineAbatementAlerts(mockThresholdCalculatorOutput);
93 public void testDetermineAbatementAlertsWhenControlLoopTypeIsONSET() throws Exception {
95 final String testTCAAlertsAbatementTableName = "testTCAAlertsAbatementTableName";
96 final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
97 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
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);
104 doNothing().when(mockObjectMappedTable).write(any(String.class), any(TCAAlertsAbatementEntity.class));
106 final Threshold violatedThreshold = getViolatedThreshold(ClosedLoopEventStatus.ONSET);
107 final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
108 getMockThresholdCalculatorOutput(violatedThreshold);
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));
119 public void testDetermineAbatementAlertsWhenControlLoopTypeIsABATEDAndNoPreviousAlertWasSent() throws Exception {
121 final String testTCAAlertsAbatementTableName = "testTCAAlertsAbatementTableName";
122 final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
123 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
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);
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);
135 final Threshold violatedThreshold = getViolatedThreshold(ClosedLoopEventStatus.ABATED);
136 final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
137 getMockThresholdCalculatorOutput(violatedThreshold);
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));
147 public void testDetermineAbatementAlertsWhenControlLoopTypeIsABATEDAndPreviousAlertWasAlreadySent() throws
150 final String testTCAAlertsAbatementTableName = "testTCAAlertsAbatementTableName";
151 final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
152 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
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);
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));
165 final Threshold violatedThreshold = getViolatedThreshold(ClosedLoopEventStatus.ABATED);
166 final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
167 getMockThresholdCalculatorOutput(violatedThreshold);
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));
178 public void testDetermineAbatementAlertsWhenControlLoopTypeIsABATEDAndNoPreviousONSETEventFound() throws
181 final String testTCAAlertsAbatementTableName = "testTCAAlertsAbatementTableName";
182 final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
183 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
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);
190 doNothing().when(mockObjectMappedTable).write(any(String.class), any(TCAAlertsAbatementEntity.class));
191 when(mockObjectMappedTable.read(any(String.class))).thenReturn(null);
193 final Threshold violatedThreshold = getViolatedThreshold(ClosedLoopEventStatus.ABATED);
194 final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
195 getMockThresholdCalculatorOutput(violatedThreshold);
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));
204 @Test(expected = CDAPSettingsException.class)
205 public void testDetermineAbatementAlertsWhenControlLoopTypeIsNotOnsetOrAbated() throws
207 final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
208 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
209 final Threshold violatedThreshold = getViolatedThreshold(ClosedLoopEventStatus.CONTINUE);
210 final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
211 getMockThresholdCalculatorOutput(violatedThreshold);
213 tcaAlertsAbatementFlowlet.determineAbatementAlerts(mockThresholdCalculatorOutput);
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;
224 private static ThresholdCalculatorOutput getMockThresholdCalculatorOutput(final Threshold violatedThreshold) throws
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)
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;