[DCAEGEN2] Enhance (KPI-MS) KpiComputation for SUM-RATIO operation.
[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  * ================================================================================
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.dcaegen2.kpi.computation;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27
28 import java.util.List;
29
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;
34
35 import java.math.BigDecimal;
36 import java.math.RoundingMode;
37
38 public class KpiComputationTest {
39
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";
47
48     @Test
49     public void testKpiComputation() {
50
51
52         String strKpiConfig = FileUtils.getFileContents(KPI_CONFIG_FILE);
53
54         String vesMessage = FileUtils.getFileContents(VES_MESSAGE_FILE);
55
56         Configuration config = mock(Configuration.class);
57         when(config.getKpiConfig()).thenReturn(strKpiConfig);
58         List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
59
60         VesEvent vesEvent = vesList.get(0);
61         assertEquals(vesEvent.getEvent().getPerf3gppFields().getMeasDataCollection().getMeasInfoList().get(0)
62                 .getMeasValuesList().get(0).getMeasResults().get(0).getSvalue(), "40");
63     }
64
65     @Test
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");
75     }
76
77     @Test
78     public void testKpiComputationSumRatio() {
79
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");
88     }
89
90     @Test
91     public void testKpiComputationSumRatioEmptyCheck() {
92         String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_SUMRATIO_FILE);
93
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());
99     }
100
101     @Test
102     public void testKpiComputationSumRatioOperandsCheck() {
103         String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_SUMRATIO_FILE);
104
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());
110     }
111
112     @Test
113     public void testKpiComputationSumRatioEventNameCheck() {
114         String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_SUMRATIO_FILE);
115
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);
121     }
122
123 }