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.openecomp.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 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;
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;
48 * @author Rajiv Singla . Creation Date: 12/19/2016.
50 @SuppressWarnings("unchecked")
51 public class TCAVESThresholdViolationCalculatorFlowletTest extends BaseAnalyticsCDAPTCAUnitTest {
53 private static final String messageStatusTableName = "TEST_MESSAGE_STATUS_TABLE";
55 private TCAVESThresholdViolationCalculatorFlowlet violationCalculatorFlowlet;
56 private Metrics metrics;
57 private OutputEmitter outputEmitter;
58 private ObjectMappedTable<TCAMessageStatusEntity> vesMessageStatusTable;
60 private static class TCATestVESThresholdViolationCalculatorFlowlet extends
61 TCAVESThresholdViolationCalculatorFlowlet {
62 public TCATestVESThresholdViolationCalculatorFlowlet(
63 final String messageStatusTableName,
64 final OutputEmitter tcaAlertOutputEmitter,
65 ObjectMappedTable<TCAMessageStatusEntity> vesMessageStatusTable,
67 super(messageStatusTableName);
68 this.tcaAlertOutputEmitter = tcaAlertOutputEmitter;
69 this.metrics = metrics;
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);
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);
90 public void testInitialize() throws Exception {
91 final FlowletContext mockFlowletContext = initializeFlowlet(violationCalculatorFlowlet, vesMessageStatusTable);
92 verify(mockFlowletContext, times(1)).getDataset(anyString());
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));
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));
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());
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);
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");
153 calculatorFlowlet.initialize(mockFlowletContext);
154 return mockFlowletContext;
155 } catch (Exception e) {
156 LOG.error("error while flowlet initialization");
157 throw new RuntimeException(e);