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