dc1becb3bbf3a81c1e24378d52bb924344adf16f
[dcaegen2/services.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 China Mobile.
4  *  Copyright (C) 2022 Deutsche Telekom AG. All rights reserved.
5  *  Copyright (C) 2022 Wipro Limited. 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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.dcaegen2.kpi.computation;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Map;
32
33 import org.junit.Test;
34 import org.junit.jupiter.api.Assertions;
35 import org.mockito.Spy;
36 import org.onap.dcaegen2.kpi.computation.KpiComputation;
37 import org.onap.dcaegen2.kpi.exception.KpiComputationException;
38 import org.onap.dcaegen2.kpi.models.Configuration;
39 import org.onap.dcaegen2.kpi.models.KpiOperand;
40 import org.onap.dcaegen2.kpi.models.MeasDataCollection;
41 import org.onap.dcaegen2.kpi.models.VesEvent;
42 import org.powermock.core.classloader.annotations.PrepareForTest;
43 import org.powermock.reflect.Whitebox;
44
45 import java.math.BigDecimal;
46 import java.math.RoundingMode;
47
48 public class KpiComputationTest {
49
50     private static final String KPI_CONFIG_FILE = "kpi/kpi_config.json";
51     private static final String VES_MESSAGE_FILE = "kpi/ves_message.json";
52     private static final String KPI_CONFIG_RATIO_FILE = "kpi/kpi_config_ratio.json";
53     private static final String KPI_CONFIG_SLICING_RATIO_FILE = "kpi/kpi_config_slicing.json";
54     private static final String KPI_CONFIG_SUMRATIO_FILE = "kpi/kpi_config_sumratio.json";
55     private static final String VES_MESSAGE_EMPTY_FILE = "kpi/ves_message_empty.json";
56     private static final String VES_MESSAGE_NULL_FILE = "kpi/ves_message_null.json";
57     private static final String VES_MESSAGE_SLICING_FILE = "kpi/ves_message_slicing.json";
58     private static final String VES_MESSAGE_EVENTNAME_FILE = "kpi/ves_message_eventname.json";
59     private static final String VES_MESSAGE_FIELD_MISSING ="kpi/ves_message_missing_field.json";
60     
61     
62     @Test
63     public void testKpiComputation() {
64
65
66         String strKpiConfig = FileUtils.getFileContents(KPI_CONFIG_FILE);
67
68         String vesMessage = FileUtils.getFileContents(VES_MESSAGE_FILE);
69
70         Configuration config = mock(Configuration.class);
71         when(config.getKpiConfig()).thenReturn(strKpiConfig);
72         List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
73
74         VesEvent vesEvent = vesList.get(0);
75         assertEquals(vesEvent.getEvent().getPerf3gppFields().getMeasDataCollection().getMeasInfoList().get(0)
76                 .getMeasValuesList().get(0).getMeasResults().get(0).getSvalue(), "40");
77     }
78
79     @Test
80     public void testKpiComputationRatio() {
81         String strKpiConfigRatio = FileUtils.getFileContents(KPI_CONFIG_RATIO_FILE);
82         String vesMessage = FileUtils.getFileContents(VES_MESSAGE_FILE);
83         Configuration config = mock(Configuration.class);
84         when(config.getKpiConfig()).thenReturn(strKpiConfigRatio);
85         List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
86         VesEvent vesEvent = vesList.get(0);
87         assertEquals(vesEvent.getEvent().getPerf3gppFields().getMeasDataCollection().getMeasInfoList().get(0)
88                  .getMeasValuesList().get(0).getMeasResults().get(0).getSvalue(), "50");
89     }
90
91     @Test
92     public void testKpiComputationSlicingRatio() {
93         String strKpiConfigRatio = FileUtils.getFileContents(KPI_CONFIG_SLICING_RATIO_FILE);
94         String vesMessage = FileUtils.getFileContents(VES_MESSAGE_SLICING_FILE);
95         Configuration config = mock(Configuration.class);
96         when(config.getKpiConfig()).thenReturn(strKpiConfigRatio);
97         List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
98         VesEvent vesEvent = vesList.get(0);
99         assertEquals(vesEvent.getEvent().getPerf3gppFields().getMeasDataCollection().getMeasInfoList().get(0)
100                      .getMeasValuesList().get(0).getMeasResults().get(0).getSvalue(), "158");
101     }
102     
103     @Test
104     public void testKpiComputationSumRatio() {
105
106         String strKpiConfigSumRatio  = FileUtils.getFileContents(KPI_CONFIG_SUMRATIO_FILE);
107         String vesMessage = FileUtils.getFileContents(VES_MESSAGE_FILE);
108         Configuration config = mock(Configuration.class);
109         when(config.getKpiConfig()).thenReturn(strKpiConfigSumRatio);
110         List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
111         VesEvent vesEvent = vesList.get(0);
112         assertEquals(vesEvent.getEvent().getPerf3gppFields().getMeasDataCollection().getMeasInfoList().get(0)
113                 .getMeasValuesList().get(0).getMeasResults().get(0).getSvalue(), "67");
114     }
115
116     @Test
117     public void testKpiComputationSumRatioEmptyCheck() {
118         String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_SUMRATIO_FILE);
119
120         String vesMessage = FileUtils.getFileContents(VES_MESSAGE_EMPTY_FILE);
121         Configuration config = mock(Configuration.class);
122         when(config.getKpiConfig()).thenReturn(strKpiConfigSumRatio);
123         List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
124         assertEquals(0, vesList.size());
125     }
126
127     @Test
128     public void testKpiComputationSumRatioOperandsCheck() {
129         String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_SUMRATIO_FILE);
130
131         String vesMessage = FileUtils.getFileContents(VES_MESSAGE_NULL_FILE);
132         Configuration config = mock(Configuration.class);
133         when(config.getKpiConfig()).thenReturn(strKpiConfigSumRatio);
134         List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
135         assertEquals(0, vesList.size());
136     }
137
138     @Test
139     public void testKpiComputationSumRatioEventNameCheck() {
140         String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_SUMRATIO_FILE);
141
142         String vesMessage = FileUtils.getFileContents(VES_MESSAGE_EVENTNAME_FILE);
143         Configuration config = mock(Configuration.class);
144         when(config.getKpiConfig()).thenReturn(strKpiConfigSumRatio);
145         List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
146         assertEquals(null, vesList);
147     }
148     
149     @Test
150     public void testKpiComputationException() {
151         String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_SUMRATIO_FILE);
152
153         String vesMessage = FileUtils.getFileContents(VES_MESSAGE_FIELD_MISSING);
154         Configuration config = mock(Configuration.class);
155         when(config.getKpiConfig()).thenReturn(strKpiConfigSumRatio);
156         KpiComputationException kpiException = Assertions.assertThrows(KpiComputationException.class, () -> {
157                 List<VesEvent>  vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
158         });
159         Assertions.assertEquals("Required Field: EventName not present", kpiException.getMessage());
160     }
161     @Test
162     public void testNullOperands() throws Exception {
163         Map<String, List<KpiOperand>> operands = null;
164         operands = Whitebox.invokeMethod(new KpiComputation(), "getOperands", new MeasDataCollection(), null);
165         Assertions.assertNull(operands);
166     }
167     @Test
168     public void testEmptyOperands() throws Exception {
169          Map<String, List<KpiOperand>> operands = null;
170          List<String> inputOperands = new ArrayList<String>();
171          operands = Whitebox.invokeMethod(new KpiComputation(), "getOperands", new MeasDataCollection(), inputOperands);
172          Assertions.assertNull(operands);
173     }
174     @Test
175     public void testNullVESMessage() {
176         String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_FILE);
177         Configuration config = mock(Configuration.class);
178         when(config.getKpiConfig()).thenReturn(strKpiConfigSumRatio);
179         List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(null, config);
180         Assertions.assertNull(vesList);
181     }
182     @Test
183     public void testEmptyVESMessage() {
184         String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_FILE);
185         Configuration config = mock(Configuration.class);
186         when(config.getKpiConfig()).thenReturn(strKpiConfigSumRatio);
187         String vesMessage = "{}";
188         List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
189         Assertions.assertNull(vesList);
190     }
191 }