Add support for ABATED alerts within CDAP TCA
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-tca / src / test / java / org / openecomp / dcae / apod / analytics / cdap / tca / flowlet / TCAVESThresholdViolationCalculatorFlowletTest.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.openecomp.dcae.apod.analytics.cdap.tca.flowlet;
22
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 co.cask.cdap.api.metrics.Metrics;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mockito;
31 import org.openecomp.dcae.apod.analytics.cdap.common.CDAPComponentsConstants;
32 import org.openecomp.dcae.apod.analytics.cdap.common.domain.tca.ThresholdCalculatorOutput;
33 import org.openecomp.dcae.apod.analytics.cdap.common.persistance.tca.TCAMessageStatusEntity;
34 import org.openecomp.dcae.apod.analytics.cdap.tca.BaseAnalyticsCDAPTCAUnitTest;
35 import org.openecomp.dcae.apod.analytics.cdap.tca.utils.CDAPTCAUtils;
36 import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener;
37 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy;
38 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Threshold;
39
40 import static org.mockito.ArgumentMatchers.any;
41 import static org.mockito.ArgumentMatchers.anyInt;
42 import static org.mockito.ArgumentMatchers.anyString;
43 import static org.mockito.Mockito.doNothing;
44 import static org.mockito.Mockito.times;
45 import static org.mockito.Mockito.verify;
46 import static org.mockito.Mockito.when;
47
48 /**
49  * @author Rajiv Singla . Creation Date: 12/19/2016.
50  */
51 @SuppressWarnings("unchecked")
52 public class TCAVESThresholdViolationCalculatorFlowletTest extends BaseAnalyticsCDAPTCAUnitTest {
53
54     private static final String messageStatusTableName = "TEST_MESSAGE_STATUS_TABLE";
55
56     private TCAVESThresholdViolationCalculatorFlowlet violationCalculatorFlowlet;
57     private Metrics metrics;
58     private OutputEmitter outputEmitter;
59     private ObjectMappedTable<TCAMessageStatusEntity> vesMessageStatusTable;
60
61     private static class TCATestVESThresholdViolationCalculatorFlowlet extends
62             TCAVESThresholdViolationCalculatorFlowlet {
63         public TCATestVESThresholdViolationCalculatorFlowlet(
64                 final String messageStatusTableName,
65                 final OutputEmitter tcaAlertOutputEmitter,
66                 ObjectMappedTable<TCAMessageStatusEntity> vesMessageStatusTable,
67                 Metrics metrics) {
68             super(messageStatusTableName);
69             this.tcaAlertOutputEmitter = tcaAlertOutputEmitter;
70             this.metrics = metrics;
71         }
72     }
73
74     @Before
75     public void before() {
76         violationCalculatorFlowlet = new TCAVESThresholdViolationCalculatorFlowlet(messageStatusTableName);
77         vesMessageStatusTable = Mockito.mock(ObjectMappedTable.class);
78         outputEmitter = Mockito.mock(OutputEmitter.class);
79         metrics = Mockito.mock(Metrics.class);
80     }
81
82     @Test
83     public void testConfigure() throws Exception {
84         assertFlowletNameAndDescription(
85                 CDAPComponentsConstants.TCA_FIXED_VES_THRESHOLD_VIOLATION_CALCULATOR_NAME_FLOWLET,
86                 CDAPComponentsConstants.TCA_FIXED_VES_THRESHOLD_VIOLATION_CALCULATOR_DESCRIPTION_FLOWLET,
87                 violationCalculatorFlowlet);
88     }
89
90     @Test
91     public void testInitialize() throws Exception {
92         final FlowletContext mockFlowletContext = initializeFlowlet(violationCalculatorFlowlet, vesMessageStatusTable);
93         verify(mockFlowletContext, times(1)).getDataset(anyString());
94     }
95
96     @Test
97     public void testFilterVESMessagesWhenVESMessageIsInApplicable() throws Exception {
98         final TCATestVESThresholdViolationCalculatorFlowlet thresholdViolationCalculatorFlowlet =
99                 createTestViolationCalculator(vesMessageStatusTable, outputEmitter, metrics);
100         initializeFlowlet(thresholdViolationCalculatorFlowlet, vesMessageStatusTable);
101         thresholdViolationCalculatorFlowlet.filterVESMessages("inapplicable");
102         verify(vesMessageStatusTable, times(1)).write(anyString(),
103                 any(TCAMessageStatusEntity.class));
104     }
105
106     @Test
107     public void testFilterVESMessagesWhenVESMessageIsCompliant() throws Exception {
108         final TCATestVESThresholdViolationCalculatorFlowlet thresholdViolationCalculatorFlowlet =
109                 createTestViolationCalculator(vesMessageStatusTable, outputEmitter, metrics);
110         initializeFlowlet(thresholdViolationCalculatorFlowlet, vesMessageStatusTable);
111         thresholdViolationCalculatorFlowlet.filterVESMessages(getValidCEFMessage());
112         verify(vesMessageStatusTable, times(1)).write(anyString(),
113                 any(TCAMessageStatusEntity.class));
114     }
115
116     @Test
117     public void testFilterVESMessagesWhenVESMessageNonCompliant() throws Exception {
118         final TCATestVESThresholdViolationCalculatorFlowlet thresholdViolationCalculatorFlowlet =
119                 createTestViolationCalculator(vesMessageStatusTable, outputEmitter, metrics);
120         final FlowletContext flowletContext =
121                 initializeFlowlet(thresholdViolationCalculatorFlowlet, vesMessageStatusTable);
122         final TCAPolicy policy = CDAPTCAUtils.getValidatedTCAPolicyPreferences(flowletContext);
123         final Threshold threshold = policy.getMetricsPerEventName().get(0).getThresholds().get(0);
124         final Long thresholdValue = threshold.getThresholdValue();
125         final EventListener thresholdViolatingMessage = getCEFEventListener();
126         thresholdViolatingMessage.getEvent().getMeasurementsForVfScalingFields().getVNicPerformanceArray().
127                 get(0).setReceivedBroadcastPacketsAccumulated(thresholdValue - 1);
128         thresholdViolationCalculatorFlowlet.filterVESMessages(
129                 ANALYTICS_MODEL_OBJECT_MAPPER.writeValueAsString(thresholdViolatingMessage));
130         verify(vesMessageStatusTable, times(1)).write(anyString(),
131                 any(TCAMessageStatusEntity.class));
132         verify(outputEmitter, times(1)).emit(any(ThresholdCalculatorOutput.class));
133     }
134
135     private static TCATestVESThresholdViolationCalculatorFlowlet createTestViolationCalculator(
136             final ObjectMappedTable<TCAMessageStatusEntity> vesMessageStatusTable,
137             final OutputEmitter outputEmitter, final Metrics metrics) {
138         doNothing().when(outputEmitter).emit(anyString());
139         doNothing().when(metrics).count(anyString(), anyInt());
140         doNothing().when(vesMessageStatusTable).write(anyString(), any(TCAMessageStatusEntity.class));
141         return new TCATestVESThresholdViolationCalculatorFlowlet(messageStatusTableName, outputEmitter,
142                 vesMessageStatusTable, metrics);
143     }
144
145     private static <T extends TCAVESThresholdViolationCalculatorFlowlet> FlowletContext initializeFlowlet(
146             T calculatorFlowlet, ObjectMappedTable<TCAMessageStatusEntity> vesMessageStatusTable) throws Exception {
147         final FlowletContext mockFlowletContext = getTestFlowletContextWithValidPolicy();
148         when(mockFlowletContext.getDataset(anyString())).thenReturn(vesMessageStatusTable);
149         when(mockFlowletContext.getInstanceId()).thenReturn(1);
150         ApplicationSpecification mockApplicationSpecification = Mockito.mock(ApplicationSpecification.class);
151         when(mockApplicationSpecification.getConfiguration()).thenReturn(fromStream(TCA_APP_CONFIG_FILE_LOCATION));
152         when(mockFlowletContext.getApplicationSpecification()).thenReturn(mockApplicationSpecification);
153         when(mockApplicationSpecification.getName()).thenReturn("TestTCAAppName");
154         try {
155             calculatorFlowlet.initialize(mockFlowletContext);
156             return mockFlowletContext;
157         } catch (Exception e) {
158             LOG.error("error while flowlet initialization");
159             throw new RuntimeException(e);
160         }
161     }
162
163 }