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.app.ApplicationSpecification;
24 import co.cask.cdap.api.dataset.lib.ObjectMappedTable;
25 import co.cask.cdap.api.flow.flowlet.FlowletContext;
26 import co.cask.cdap.api.flow.flowlet.OutputEmitter;
27 import com.google.common.collect.ImmutableList;
28 import org.junit.Test;
29 import org.mockito.Mockito;
30 import org.onap.dcae.apod.analytics.cdap.common.CDAPComponentsConstants;
31 import org.onap.dcae.apod.analytics.cdap.common.domain.tca.ThresholdCalculatorOutput;
32 import org.onap.dcae.apod.analytics.cdap.common.exception.CDAPSettingsException;
33 import org.onap.dcae.apod.analytics.cdap.common.persistance.tca.TCAAlertsAbatementEntity;
34 import org.onap.dcae.apod.analytics.cdap.tca.BaseAnalyticsCDAPTCAUnitTest;
35 import org.onap.dcae.apod.analytics.cdap.tca.settings.TCAPolicyPreferences;
36 import org.onap.dcae.apod.analytics.model.domain.policy.tca.ClosedLoopEventStatus;
37 import org.onap.dcae.apod.analytics.model.domain.policy.tca.MetricsPerEventName;
38 import org.onap.dcae.apod.analytics.model.domain.policy.tca.Threshold;
39 import org.onap.dcae.apod.analytics.tca.utils.TCAUtils;
41 import java.io.IOException;
42 import java.util.Date;
43 import java.util.List;
45 import static org.mockito.ArgumentMatchers.any;
46 import static org.mockito.ArgumentMatchers.eq;
47 import static org.mockito.Mockito.doNothing;
48 import static org.mockito.Mockito.mock;
49 import static org.mockito.Mockito.times;
50 import static org.mockito.Mockito.verify;
51 import static org.mockito.Mockito.when;
54 * @author Rajiv Singla . Creation Date: 9/12/2017.
56 @SuppressWarnings("unchecked")
57 public class TCAVESAlertsAbatementFlowletTest extends BaseAnalyticsCDAPTCAUnitTest {
59 private static final TCAPolicyPreferences sampleTCAPolicyPreferences = getSampleTCAPolicyPreferences();
60 private static final List<MetricsPerEventName> metricsPerEventNames = sampleTCAPolicyPreferences
61 .getMetricsPerEventName();
62 private final OutputEmitter<String> mockOutputEmitter = mock(OutputEmitter.class);
64 private class TestTCAVESAlertsAbatementFlowlet extends TCAVESAlertsAbatementFlowlet {
66 public TestTCAVESAlertsAbatementFlowlet(String tcaAlertsAbatementTableName) {
67 super(tcaAlertsAbatementTableName);
68 this.alertsAbatementOutputEmitter = mockOutputEmitter;
69 doNothing().when(mockOutputEmitter).emit(any(String.class));
74 public void testConfigure() throws Exception {
75 final TCAVESAlertsAbatementFlowlet tcavesAlertsAbatementFlowlet =
76 new TCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
77 assertFlowletNameAndDescription(CDAPComponentsConstants.TCA_FIXED_VES_ALERTS_ABATEMENT_NAME_FLOWLET,
78 CDAPComponentsConstants.TCA_FIXED_VES_ALERTS_ABATEMENT_DESCRIPTION_FLOWLET,
79 tcavesAlertsAbatementFlowlet);
82 @Test(expected = CDAPSettingsException.class)
83 public void testDetermineAbatementAlertsWhenViolatedMetricsEventNameIsBlank() throws Exception {
85 final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
86 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
87 final Threshold violatedThreshold = getViolatedThreshold(ClosedLoopEventStatus.ONSET);
88 final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
89 getMockThresholdCalculatorOutput(violatedThreshold);
90 when(mockThresholdCalculatorOutput.getViolatedMetricsPerEventName()).thenReturn("");
92 tcaAlertsAbatementFlowlet.determineAbatementAlerts(mockThresholdCalculatorOutput);
96 public void testDetermineAbatementAlertsWhenControlLoopTypeIsONSET() throws Exception {
98 final String testTCAAlertsAbatementTableName = "testTCAAlertsAbatementTableName";
99 final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
100 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
102 final FlowletContext mockFlowletContext = mock(FlowletContext.class);
103 final ObjectMappedTable<TCAAlertsAbatementEntity> mockObjectMappedTable = mock(ObjectMappedTable.class);
104 when(mockFlowletContext.getDataset(eq(testTCAAlertsAbatementTableName))).thenReturn(mockObjectMappedTable);
105 configureFlowletContext(mockFlowletContext);
106 tcaAlertsAbatementFlowlet.initialize(mockFlowletContext);
108 doNothing().when(mockObjectMappedTable).write(any(String.class), any(TCAAlertsAbatementEntity.class));
110 final Threshold violatedThreshold = getViolatedThreshold(ClosedLoopEventStatus.ONSET);
111 final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
112 getMockThresholdCalculatorOutput(violatedThreshold);
114 tcaAlertsAbatementFlowlet.determineAbatementAlerts(mockThresholdCalculatorOutput);
115 verify(mockObjectMappedTable,
116 times(1)).write(any(String.class), any(TCAAlertsAbatementEntity.class));
117 verify(mockOutputEmitter, times(1)).emit(any(String.class));
123 public void testDetermineAbatementAlertsWhenControlLoopTypeIsABATEDAndNoPreviousAlertWasSent() throws Exception {
125 final String testTCAAlertsAbatementTableName = "testTCAAlertsAbatementTableName";
126 final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
127 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
129 final FlowletContext mockFlowletContext = mock(FlowletContext.class);
130 final ObjectMappedTable<TCAAlertsAbatementEntity> mockObjectMappedTable = mock(ObjectMappedTable.class);
131 when(mockFlowletContext.getDataset(eq(testTCAAlertsAbatementTableName))).thenReturn(mockObjectMappedTable);
132 configureFlowletContext(mockFlowletContext);
133 tcaAlertsAbatementFlowlet.initialize(mockFlowletContext);
135 doNothing().when(mockObjectMappedTable).write(any(String.class), any(TCAAlertsAbatementEntity.class));
136 final TCAAlertsAbatementEntity tcaAlertsAbatementEntity = mock(TCAAlertsAbatementEntity.class);
137 when(mockObjectMappedTable.read(any(String.class))).thenReturn(tcaAlertsAbatementEntity);
138 when(tcaAlertsAbatementEntity.getAbatementSentTS()).thenReturn(null);
140 final Threshold violatedThreshold = getViolatedThreshold(ClosedLoopEventStatus.ABATED);
141 final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
142 getMockThresholdCalculatorOutput(violatedThreshold);
144 tcaAlertsAbatementFlowlet.determineAbatementAlerts(mockThresholdCalculatorOutput);
145 verify(mockObjectMappedTable,
146 times(1)).write(any(String.class), any(TCAAlertsAbatementEntity.class));
147 verify(mockOutputEmitter, times(1)).emit(any(String.class));
152 public void testDetermineAbatementAlertsWhenControlLoopTypeIsABATEDAndPreviousAlertWasAlreadySent() throws
155 final String testTCAAlertsAbatementTableName = "testTCAAlertsAbatementTableName";
156 final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
157 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
159 final FlowletContext mockFlowletContext = mock(FlowletContext.class);
160 final ObjectMappedTable<TCAAlertsAbatementEntity> mockObjectMappedTable = mock(ObjectMappedTable.class);
161 when(mockFlowletContext.getDataset(eq(testTCAAlertsAbatementTableName))).thenReturn(mockObjectMappedTable);
162 configureFlowletContext(mockFlowletContext);
163 tcaAlertsAbatementFlowlet.initialize(mockFlowletContext);
165 doNothing().when(mockObjectMappedTable).write(any(String.class), any(TCAAlertsAbatementEntity.class));
166 final TCAAlertsAbatementEntity tcaAlertsAbatementEntity = mock(TCAAlertsAbatementEntity.class);
167 when(mockObjectMappedTable.read(any(String.class))).thenReturn(tcaAlertsAbatementEntity);
168 final long time = new Date().getTime();
169 when(tcaAlertsAbatementEntity.getAbatementSentTS()).thenReturn(Long.toString(time));
171 final Threshold violatedThreshold = getViolatedThreshold(ClosedLoopEventStatus.ABATED);
172 final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
173 getMockThresholdCalculatorOutput(violatedThreshold);
175 tcaAlertsAbatementFlowlet.determineAbatementAlerts(mockThresholdCalculatorOutput);
176 verify(mockObjectMappedTable,
177 times(0)).write(any(String.class), any(TCAAlertsAbatementEntity.class));
178 verify(mockOutputEmitter, times(0)).emit(any(String.class));
184 public void testDetermineAbatementAlertsWhenControlLoopTypeIsABATEDAndNoPreviousONSETEventFound() throws
187 final String testTCAAlertsAbatementTableName = "testTCAAlertsAbatementTableName";
188 final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
189 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
191 final FlowletContext mockFlowletContext = mock(FlowletContext.class);
192 final ObjectMappedTable<TCAAlertsAbatementEntity> mockObjectMappedTable = mock(ObjectMappedTable.class);
193 when(mockFlowletContext.getDataset(eq(testTCAAlertsAbatementTableName))).thenReturn(mockObjectMappedTable);
194 configureFlowletContext(mockFlowletContext);
195 tcaAlertsAbatementFlowlet.initialize(mockFlowletContext);
196 doNothing().when(mockObjectMappedTable).write(any(String.class), any(TCAAlertsAbatementEntity.class));
197 when(mockObjectMappedTable.read(any(String.class))).thenReturn(null);
199 final Threshold violatedThreshold = getViolatedThreshold(ClosedLoopEventStatus.ABATED);
200 final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
201 getMockThresholdCalculatorOutput(violatedThreshold);
203 tcaAlertsAbatementFlowlet.determineAbatementAlerts(mockThresholdCalculatorOutput);
204 verify(mockObjectMappedTable,
205 times(0)).write(any(String.class), any(TCAAlertsAbatementEntity.class));
206 verify(mockOutputEmitter, times(0)).emit(any(String.class));
210 @Test(expected = CDAPSettingsException.class)
211 public void testDetermineAbatementAlertsWhenControlLoopTypeIsNotOnsetOrAbated() throws
213 final TestTCAVESAlertsAbatementFlowlet tcaAlertsAbatementFlowlet =
214 new TestTCAVESAlertsAbatementFlowlet("testTCAAlertsAbatementTableName");
215 final Threshold violatedThreshold = getViolatedThreshold(ClosedLoopEventStatus.CONTINUE);
216 final ThresholdCalculatorOutput mockThresholdCalculatorOutput =
217 getMockThresholdCalculatorOutput(violatedThreshold);
219 tcaAlertsAbatementFlowlet.determineAbatementAlerts(mockThresholdCalculatorOutput);
223 private static Threshold getViolatedThreshold(final ClosedLoopEventStatus closedLoopEventStatus) {
224 final Threshold violatedThreshold = Threshold.copy(metricsPerEventNames.get(0).getThresholds().get(0));
225 violatedThreshold.setClosedLoopEventStatus(closedLoopEventStatus);
226 return violatedThreshold;
230 private static ThresholdCalculatorOutput getMockThresholdCalculatorOutput(final Threshold violatedThreshold) throws
233 final MetricsPerEventName violatedMetricsPerEventName =
234 MetricsPerEventName.copy(metricsPerEventNames.get(0));
235 violatedMetricsPerEventName.setThresholds(ImmutableList.of(violatedThreshold));
236 return getMockThresholdCalculatorOutput(
237 fromStream(CEF_MESSAGE_JSON_FILE_LOCATION),
238 fromStream(TCA_POLICY_JSON_FILE_LOCATION),
239 TCAUtils.writeValueAsString(violatedMetricsPerEventName),
240 fromStream(TCA_ALERT_JSON_FILE_LOCATION)
245 private static ThresholdCalculatorOutput getMockThresholdCalculatorOutput(final String cefMessage,
246 final String tcaPolicy,
247 final String violatedMetricsPerEventName,
248 final String alertMessage) {
249 final ThresholdCalculatorOutput thresholdCalculatorOutput = mock(ThresholdCalculatorOutput.class);
250 when(thresholdCalculatorOutput.getCefMessage()).thenReturn(cefMessage);
251 when(thresholdCalculatorOutput.getTcaPolicy()).thenReturn(tcaPolicy);
252 when(thresholdCalculatorOutput.getViolatedMetricsPerEventName()).thenReturn(violatedMetricsPerEventName);
253 when(thresholdCalculatorOutput.getAlertMessage()).thenReturn(alertMessage);
254 return thresholdCalculatorOutput;
257 private void configureFlowletContext(final FlowletContext mockFlowletContext) throws IOException {
258 when(mockFlowletContext.getRuntimeArguments()).thenReturn(getPreferenceMap());
259 ApplicationSpecification mockApplicationSpecification = Mockito.mock(ApplicationSpecification.class);
260 when(mockApplicationSpecification.getConfiguration()).thenReturn(fromStream(TCA_APP_CONFIG_FILE_LOCATION));
261 when(mockFlowletContext.getApplicationSpecification()).thenReturn(mockApplicationSpecification);