2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 China Mobile.
4 * Copyright (C) 2022 Deutsche Telekom AG. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.dcaegen2.kpi.computation;
24 import static org.junit.Assert.assertEquals;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
28 import java.util.List;
30 import org.junit.Test;
31 import org.onap.dcaegen2.kpi.computation.KpiComputation;
32 import org.onap.dcaegen2.kpi.models.Configuration;
33 import org.onap.dcaegen2.kpi.models.VesEvent;
35 import java.math.BigDecimal;
36 import java.math.RoundingMode;
38 public class KpiComputationTest {
40 private static final String KPI_CONFIG_FILE = "kpi/kpi_config.json";
41 private static final String VES_MESSAGE_FILE = "kpi/ves_message.json";
42 private static final String KPI_CONFIG_RATIO_FILE = "kpi/kpi_config_ratio.json";
43 private static final String KPI_CONFIG_SUMRATIO_FILE = "kpi/kpi_config_sumratio.json";
44 private static final String VES_MESSAGE_EMPTY_FILE = "kpi/ves_message_empty.json";
45 private static final String VES_MESSAGE_NULL_FILE = "kpi/ves_message_null.json";
46 private static final String VES_MESSAGE_EVENTNAME_FILE = "kpi/ves_message_eventname.json";
49 public void testKpiComputation() {
52 String strKpiConfig = FileUtils.getFileContents(KPI_CONFIG_FILE);
54 String vesMessage = FileUtils.getFileContents(VES_MESSAGE_FILE);
56 Configuration config = mock(Configuration.class);
57 when(config.getKpiConfig()).thenReturn(strKpiConfig);
58 List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
60 VesEvent vesEvent = vesList.get(0);
61 assertEquals(vesEvent.getEvent().getPerf3gppFields().getMeasDataCollection().getMeasInfoList().get(0)
62 .getMeasValuesList().get(0).getMeasResults().get(0).getSvalue(), "40");
66 public void testKpiComputationRatio() {
67 String strKpiConfigRatio = FileUtils.getFileContents(KPI_CONFIG_RATIO_FILE);
68 String vesMessage = FileUtils.getFileContents(VES_MESSAGE_FILE);
69 Configuration config = mock(Configuration.class);
70 when(config.getKpiConfig()).thenReturn(strKpiConfigRatio);
71 List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
72 VesEvent vesEvent = vesList.get(0);
73 assertEquals(vesEvent.getEvent().getPerf3gppFields().getMeasDataCollection().getMeasInfoList().get(0)
74 .getMeasValuesList().get(0).getMeasResults().get(0).getSvalue(), "50");
78 public void testKpiComputationSumRatio() {
80 String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_SUMRATIO_FILE);
81 String vesMessage = FileUtils.getFileContents(VES_MESSAGE_FILE);
82 Configuration config = mock(Configuration.class);
83 when(config.getKpiConfig()).thenReturn(strKpiConfigSumRatio);
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(), "67");
91 public void testKpiComputationSumRatioEmptyCheck() {
92 String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_SUMRATIO_FILE);
94 String vesMessage = FileUtils.getFileContents(VES_MESSAGE_EMPTY_FILE);
95 Configuration config = mock(Configuration.class);
96 when(config.getKpiConfig()).thenReturn(strKpiConfigSumRatio);
97 List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
98 assertEquals(0, vesList.size());
102 public void testKpiComputationSumRatioOperandsCheck() {
103 String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_SUMRATIO_FILE);
105 String vesMessage = FileUtils.getFileContents(VES_MESSAGE_NULL_FILE);
106 Configuration config = mock(Configuration.class);
107 when(config.getKpiConfig()).thenReturn(strKpiConfigSumRatio);
108 List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
109 assertEquals(0, vesList.size());
113 public void testKpiComputationSumRatioEventNameCheck() {
114 String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_SUMRATIO_FILE);
116 String vesMessage = FileUtils.getFileContents(VES_MESSAGE_EVENTNAME_FILE);
117 Configuration config = mock(Configuration.class);
118 when(config.getKpiConfig()).thenReturn(strKpiConfigSumRatio);
119 List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
120 assertEquals(null, vesList);