Fix bug in wrong SNSSAI value being appended with MeasType string
[dcaegen2/services.git] / components / kpi-computation-ms / src / test / java / org / onap / dcaegen2 / kpi / computation / KpiComputationTest.java
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     private static final String VES_MESSAGE_MULTIPLESNSSAI_SLICING_FILE = "kpi/ves_message_slicing_multiplesnssai.json";
61
62     @Test
63     public void testKpiComputation() {
64
65         String strKpiConfig = FileUtils.getFileContents(KPI_CONFIG_FILE);
66
67         String vesMessage = FileUtils.getFileContents(VES_MESSAGE_FILE);
68
69         Configuration config = mock(Configuration.class);
70         when(config.getKpiConfig()).thenReturn(strKpiConfig);
71         List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
72
73         VesEvent vesEvent = vesList.get(0);
74         assertEquals(vesEvent.getEvent().getPerf3gppFields().getMeasDataCollection().getMeasInfoList().get(0)
75                 .getMeasValuesList().get(0).getMeasResults().get(0).getSvalue(), "40");
76     }
77
78     @Test
79     public void testKpiComputationRatio() {
80         String strKpiConfigRatio = FileUtils.getFileContents(KPI_CONFIG_RATIO_FILE);
81         String vesMessage = FileUtils.getFileContents(VES_MESSAGE_FILE);
82         Configuration config = mock(Configuration.class);
83         when(config.getKpiConfig()).thenReturn(strKpiConfigRatio);
84         List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
85         VesEvent vesEvent = vesList.get(0);
86         assertEquals(vesEvent.getEvent().getPerf3gppFields().getMeasDataCollection().getMeasInfoList().get(0)
87                  .getMeasValuesList().get(0).getMeasResults().get(0).getSvalue(), "50");
88     }
89
90     @Test
91     public void testKpiComputationSlicingRatio() {
92         String strKpiConfigRatio = FileUtils.getFileContents(KPI_CONFIG_SLICING_RATIO_FILE);
93         String vesMessage = FileUtils.getFileContents(VES_MESSAGE_SLICING_FILE);
94         Configuration config = mock(Configuration.class);
95         when(config.getKpiConfig()).thenReturn(strKpiConfigRatio);
96         List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
97         VesEvent vesEvent = vesList.get(0);
98         assertEquals(vesEvent.getEvent().getPerf3gppFields().getMeasDataCollection().getMeasInfoList().get(0)
99                      .getMeasValuesList().get(0).getMeasResults().get(0).getSvalue(), "158");
100     }
101
102     @Test
103     public void testKpiComputationSlicingMeasType() {
104         String strKpiConfigRatio = FileUtils.getFileContents(KPI_CONFIG_SLICING_RATIO_FILE);
105         String vesMessage = FileUtils.getFileContents(VES_MESSAGE_SLICING_FILE);
106         Configuration config = mock(Configuration.class);
107         when(config.getKpiConfig()).thenReturn(strKpiConfigRatio);
108         List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
109         VesEvent vesEvent = vesList.get(0);
110         assertEquals(vesEvent.getEvent().getPerf3gppFields().getMeasDataCollection().getMeasInfoList().get(0)
111                 .getMeasTypes().getMeasTypesList().get(0), "PDUSessionEstSR.01-B989BD");
112     }
113
114     @Test
115     public void testKpiComputationSlicingMultipleSNSSAI() {
116         String strKpiConfigRatio = FileUtils.getFileContents(KPI_CONFIG_SLICING_RATIO_FILE);
117         String vesMessage = FileUtils.getFileContents(VES_MESSAGE_MULTIPLESNSSAI_SLICING_FILE);
118         Configuration config = mock(Configuration.class);
119         when(config.getKpiConfig()).thenReturn(strKpiConfigRatio);
120         List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
121         VesEvent vesEvent = vesList.get(0);
122         VesEvent anotherVesEvent = vesList.get(18);
123         assertEquals(vesEvent.getEvent().getPerf3gppFields().getMeasDataCollection().getMeasInfoList().get(0)
124                 .getMeasTypes().getMeasTypesList().get(0), "PDUSessionEstSR.01-B989BD");
125         assertEquals(anotherVesEvent.getEvent().getPerf3gppFields().getMeasDataCollection().getMeasInfoList().get(0)
126                       .getMeasTypes().getMeasTypesList().get(0), "PDUSessionEstSR.01-B989BD");
127     }
128     
129     @Test
130     public void testKpiComputationSumRatio() {
131
132         String strKpiConfigSumRatio  = FileUtils.getFileContents(KPI_CONFIG_SUMRATIO_FILE);
133         String vesMessage = FileUtils.getFileContents(VES_MESSAGE_FILE);
134         Configuration config = mock(Configuration.class);
135         when(config.getKpiConfig()).thenReturn(strKpiConfigSumRatio);
136         List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
137         VesEvent vesEvent = vesList.get(0);
138         assertEquals(vesEvent.getEvent().getPerf3gppFields().getMeasDataCollection().getMeasInfoList().get(0)
139                 .getMeasValuesList().get(0).getMeasResults().get(0).getSvalue(), "67");
140     }
141
142     @Test
143     public void testKpiComputationSumRatioEmptyCheck() {
144         String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_SUMRATIO_FILE);
145         String vesMessage = FileUtils.getFileContents(VES_MESSAGE_EMPTY_FILE);
146         Configuration config = mock(Configuration.class);
147         when(config.getKpiConfig()).thenReturn(strKpiConfigSumRatio);
148         List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
149         assertEquals(0, vesList.size());
150     }
151
152     @Test
153     public void testKpiComputationSumRatioOperandsCheck() {
154         String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_SUMRATIO_FILE);
155
156         String vesMessage = FileUtils.getFileContents(VES_MESSAGE_NULL_FILE);
157         Configuration config = mock(Configuration.class);
158         when(config.getKpiConfig()).thenReturn(strKpiConfigSumRatio);
159         List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
160         assertEquals(0, vesList.size());
161     }
162
163     @Test
164     public void testKpiComputationSumRatioEventNameCheck() {
165         String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_SUMRATIO_FILE);
166
167         String vesMessage = FileUtils.getFileContents(VES_MESSAGE_EVENTNAME_FILE);
168         Configuration config = mock(Configuration.class);
169         when(config.getKpiConfig()).thenReturn(strKpiConfigSumRatio);
170         List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
171         assertEquals(null, vesList);
172     }
173     
174     @Test
175     public void testKpiComputationException() {
176         String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_SUMRATIO_FILE);
177
178         String vesMessage = FileUtils.getFileContents(VES_MESSAGE_FIELD_MISSING);
179         Configuration config = mock(Configuration.class);
180         when(config.getKpiConfig()).thenReturn(strKpiConfigSumRatio);
181         KpiComputationException kpiException = Assertions.assertThrows(KpiComputationException.class, () -> {
182           List<VesEvent>  vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
183         });
184         Assertions.assertEquals("Required Field: EventName not present", kpiException.getMessage());
185     }
186     @Test
187     public void testNullOperands() throws Exception {
188         Map<String, List<KpiOperand>> operands = null;
189         operands = Whitebox.invokeMethod(new KpiComputation(), "getOperands", new MeasDataCollection(), null);
190         Assertions.assertNull(operands);
191     }
192     @Test
193     public void testEmptyOperands() throws Exception {
194         Map<String, List<KpiOperand>> operands = null;
195         List<String> inputOperands = new ArrayList<String>();
196         operands = Whitebox.invokeMethod(new KpiComputation(), "getOperands", new MeasDataCollection(), inputOperands);
197         Assertions.assertNull(operands);
198     }
199     @Test
200     public void testNullVESMessage() {
201         String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_FILE);
202         Configuration config = mock(Configuration.class);
203         when(config.getKpiConfig()).thenReturn(strKpiConfigSumRatio);
204         List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(null, config);
205         Assertions.assertNull(vesList);
206     }
207     @Test
208     public void testEmptyVESMessage() {
209         String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_FILE);
210         Configuration config = mock(Configuration.class);
211         when(config.getKpiConfig()).thenReturn(strKpiConfigSumRatio);
212         String vesMessage = "{}";
213         List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
214         Assertions.assertNull(vesList);
215     }
216 }