Sonar Critical Fix
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-tca / src / test / java / org / openecomp / dcae / apod / analytics / cdap / tca / flowlet / TCAVESThresholdViolationCalculatorFlowletTest.java
1 /*\r
2  * ===============================LICENSE_START======================================\r
3  *  dcae-analytics\r
4  * ================================================================================\r
5  *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  *  Licensed under the Apache License, Version 2.0 (the "License");\r
8  *  you may not use this file except in compliance with the License.\r
9  *   You may obtain a copy of the License at\r
10  *\r
11  *          http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  *  Unless required by applicable law or agreed to in writing, software\r
14  *  distributed under the License is distributed on an "AS IS" BASIS,\r
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  *  See the License for the specific language governing permissions and\r
17  *  limitations under the License.\r
18  *  ============================LICENSE_END===========================================\r
19  */\r
20 \r
21 package org.openecomp.dcae.apod.analytics.cdap.tca.flowlet;\r
22 \r
23 import co.cask.cdap.api.app.ApplicationSpecification;\r
24 import co.cask.cdap.api.dataset.lib.ObjectMappedTable;\r
25 import co.cask.cdap.api.flow.flowlet.FlowletContext;\r
26 import co.cask.cdap.api.flow.flowlet.OutputEmitter;\r
27 import co.cask.cdap.api.metrics.Metrics;\r
28 import org.junit.Before;\r
29 import org.junit.Test;\r
30 import org.mockito.Mockito;\r
31 import org.openecomp.dcae.apod.analytics.cdap.common.CDAPComponentsConstants;\r
32 import org.openecomp.dcae.apod.analytics.cdap.common.domain.tca.ThresholdCalculatorOutput;\r
33 import org.openecomp.dcae.apod.analytics.cdap.common.persistance.tca.TCAMessageStatusEntity;\r
34 import org.openecomp.dcae.apod.analytics.cdap.tca.BaseAnalyticsCDAPTCAUnitTest;\r
35 import org.openecomp.dcae.apod.analytics.cdap.tca.utils.CDAPTCAUtils;\r
36 import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener;\r
37 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy;\r
38 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Threshold;\r
39 \r
40 import static org.mockito.ArgumentMatchers.any;\r
41 import static org.mockito.ArgumentMatchers.anyInt;\r
42 import static org.mockito.ArgumentMatchers.anyString;\r
43 import static org.mockito.Mockito.doNothing;\r
44 import static org.mockito.Mockito.times;\r
45 import static org.mockito.Mockito.verify;\r
46 import static org.mockito.Mockito.when;\r
47 \r
48 /**\r
49  * @author Rajiv Singla . Creation Date: 12/19/2016.\r
50  */\r
51 @SuppressWarnings("unchecked")\r
52 public class TCAVESThresholdViolationCalculatorFlowletTest extends BaseAnalyticsCDAPTCAUnitTest {\r
53 \r
54     private static final String messageStatusTableName = "TEST_MESSAGE_STATUS_TABLE";\r
55 \r
56     private TCAVESThresholdViolationCalculatorFlowlet violationCalculatorFlowlet;\r
57     private Metrics metrics;\r
58     private OutputEmitter outputEmitter;\r
59     private ObjectMappedTable<TCAMessageStatusEntity> vesMessageStatusTable;\r
60 \r
61     private static class TCATestVESThresholdViolationCalculatorFlowlet extends\r
62             TCAVESThresholdViolationCalculatorFlowlet {\r
63         public TCATestVESThresholdViolationCalculatorFlowlet(\r
64                 final String messageStatusTableName,\r
65                 final OutputEmitter tcaAlertOutputEmitter,\r
66                 ObjectMappedTable<TCAMessageStatusEntity> vesMessageStatusTable,\r
67                 Metrics metrics) {\r
68             super(messageStatusTableName);\r
69             this.tcaAlertOutputEmitter = tcaAlertOutputEmitter;\r
70             this.metrics = metrics;\r
71         }\r
72     }\r
73 \r
74     @Before\r
75     public void before() {\r
76         violationCalculatorFlowlet = new TCAVESThresholdViolationCalculatorFlowlet(messageStatusTableName);\r
77         vesMessageStatusTable = Mockito.mock(ObjectMappedTable.class);\r
78         outputEmitter = Mockito.mock(OutputEmitter.class);\r
79         metrics = Mockito.mock(Metrics.class);\r
80     }\r
81 \r
82     @Test\r
83     public void testConfigure() throws Exception {\r
84         assertFlowletNameAndDescription(\r
85                 CDAPComponentsConstants.TCA_FIXED_VES_THRESHOLD_VIOLATION_CALCULATOR_NAME_FLOWLET,\r
86                 CDAPComponentsConstants.TCA_FIXED_VES_THRESHOLD_VIOLATION_CALCULATOR_DESCRIPTION_FLOWLET,\r
87                 violationCalculatorFlowlet);\r
88     }\r
89 \r
90     @Test\r
91     public void testInitialize() throws Exception {\r
92         final FlowletContext mockFlowletContext = initializeFlowlet(violationCalculatorFlowlet, vesMessageStatusTable);\r
93         verify(mockFlowletContext, times(1)).getDataset(anyString());\r
94     }\r
95 \r
96     @Test\r
97     public void testFilterVESMessagesWhenVESMessageIsInApplicable() throws Exception {\r
98         final TCATestVESThresholdViolationCalculatorFlowlet thresholdViolationCalculatorFlowlet =\r
99                 createTestViolationCalculator(vesMessageStatusTable, outputEmitter, metrics);\r
100         initializeFlowlet(thresholdViolationCalculatorFlowlet, vesMessageStatusTable);\r
101         thresholdViolationCalculatorFlowlet.filterVESMessages("inapplicable");\r
102         verify(vesMessageStatusTable, times(1)).write(anyString(),\r
103                 any(TCAMessageStatusEntity.class));\r
104     }\r
105 \r
106     @Test\r
107     public void testFilterVESMessagesWhenVESMessageIsCompliant() throws Exception {\r
108         final TCATestVESThresholdViolationCalculatorFlowlet thresholdViolationCalculatorFlowlet =\r
109                 createTestViolationCalculator(vesMessageStatusTable, outputEmitter, metrics);\r
110         initializeFlowlet(thresholdViolationCalculatorFlowlet, vesMessageStatusTable);\r
111         thresholdViolationCalculatorFlowlet.filterVESMessages(getValidCEFMessage());\r
112         verify(vesMessageStatusTable, times(1)).write(anyString(),\r
113                 any(TCAMessageStatusEntity.class));\r
114     }\r
115 \r
116     @Test\r
117     public void testFilterVESMessagesWhenVESMessageNonCompliant() throws Exception {\r
118         final TCATestVESThresholdViolationCalculatorFlowlet thresholdViolationCalculatorFlowlet =\r
119                 createTestViolationCalculator(vesMessageStatusTable, outputEmitter, metrics);\r
120         final FlowletContext flowletContext =\r
121                 initializeFlowlet(thresholdViolationCalculatorFlowlet, vesMessageStatusTable);\r
122         final TCAPolicy policy = CDAPTCAUtils.getValidatedTCAPolicyPreferences(flowletContext);\r
123         final Threshold threshold = policy.getMetricsPerEventName().get(0).getThresholds().get(0);\r
124         final Long thresholdValue = threshold.getThresholdValue();\r
125         final EventListener thresholdViolatingMessage = getCEFEventListener();\r
126         thresholdViolatingMessage.getEvent().getMeasurementsForVfScalingFields().getVNicPerformanceArray().\r
127                 get(0).setReceivedBroadcastPacketsAccumulated(thresholdValue - 1);\r
128         thresholdViolationCalculatorFlowlet.filterVESMessages(\r
129                 ANALYTICS_MODEL_OBJECT_MAPPER.writeValueAsString(thresholdViolatingMessage));\r
130         verify(vesMessageStatusTable, times(1)).write(anyString(),\r
131                 any(TCAMessageStatusEntity.class));\r
132         verify(outputEmitter, times(1)).emit(any(ThresholdCalculatorOutput.class));\r
133     }\r
134 \r
135     private static TCATestVESThresholdViolationCalculatorFlowlet createTestViolationCalculator(\r
136             final ObjectMappedTable<TCAMessageStatusEntity> vesMessageStatusTable,\r
137             final OutputEmitter outputEmitter, final Metrics metrics) {\r
138         doNothing().when(outputEmitter).emit(anyString());\r
139         doNothing().when(metrics).count(anyString(), anyInt());\r
140         doNothing().when(vesMessageStatusTable).write(anyString(), any(TCAMessageStatusEntity.class));\r
141         return new TCATestVESThresholdViolationCalculatorFlowlet(messageStatusTableName, outputEmitter,\r
142                 vesMessageStatusTable, metrics);\r
143     }\r
144 \r
145     private static <T extends TCAVESThresholdViolationCalculatorFlowlet> FlowletContext initializeFlowlet(\r
146             T calculatorFlowlet, ObjectMappedTable<TCAMessageStatusEntity> vesMessageStatusTable) throws Exception {\r
147         final FlowletContext mockFlowletContext = getTestFlowletContextWithValidPolicy();\r
148         when(mockFlowletContext.getDataset(anyString())).thenReturn(vesMessageStatusTable);\r
149         when(mockFlowletContext.getInstanceId()).thenReturn(1);\r
150         ApplicationSpecification mockApplicationSpecification = Mockito.mock(ApplicationSpecification.class);\r
151         when(mockApplicationSpecification.getConfiguration()).thenReturn(fromStream(TCA_APP_CONFIG_FILE_LOCATION));\r
152         when(mockFlowletContext.getApplicationSpecification()).thenReturn(mockApplicationSpecification);\r
153         when(mockApplicationSpecification.getName()).thenReturn("TestTCAAppName");\r
154         try {\r
155             calculatorFlowlet.initialize(mockFlowletContext);\r
156             return mockFlowletContext;\r
157         } catch (Exception e) {\r
158             LOG.error("error while flowlet initialization");\r
159             throw new RuntimeException(e);\r
160         }\r
161     }\r
162 \r
163 }\r