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
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  19  * SPDX-License-Identifier: Apache-2.0
 
  20  * ============LICENSE_END=========================================================
 
  23 package org.onap.dcaegen2.kpi.computation;
 
  25 import static org.junit.Assert.assertEquals;
 
  26 import static org.mockito.Mockito.mock;
 
  27 import static org.mockito.Mockito.when;
 
  29 import java.util.ArrayList;
 
  30 import java.util.List;
 
  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;
 
  45 import java.math.BigDecimal;
 
  46 import java.math.RoundingMode;
 
  48 public class KpiComputationTest {
 
  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";
 
  63     public void testKpiComputation() {
 
  66         String strKpiConfig = FileUtils.getFileContents(KPI_CONFIG_FILE);
 
  68         String vesMessage = FileUtils.getFileContents(VES_MESSAGE_FILE);
 
  70         Configuration config = mock(Configuration.class);
 
  71         when(config.getKpiConfig()).thenReturn(strKpiConfig);
 
  72         List<VesEvent> vesList = new KpiComputation().checkAndDoComputation(vesMessage, config);
 
  74         VesEvent vesEvent = vesList.get(0);
 
  75         assertEquals(vesEvent.getEvent().getPerf3gppFields().getMeasDataCollection().getMeasInfoList().get(0)
 
  76                 .getMeasValuesList().get(0).getMeasResults().get(0).getSvalue(), "40");
 
  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");
 
  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");
 
 104     public void testKpiComputationSumRatio() {
 
 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");
 
 117     public void testKpiComputationSumRatioEmptyCheck() {
 
 118         String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_SUMRATIO_FILE);
 
 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());
 
 128     public void testKpiComputationSumRatioOperandsCheck() {
 
 129         String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_SUMRATIO_FILE);
 
 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());
 
 139     public void testKpiComputationSumRatioEventNameCheck() {
 
 140         String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_SUMRATIO_FILE);
 
 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);
 
 150     public void testKpiComputationException() {
 
 151         String strKpiConfigSumRatio = FileUtils.getFileContents(KPI_CONFIG_SUMRATIO_FILE);
 
 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);
 
 159         Assertions.assertEquals("Required Field: EventName not present", kpiException.getMessage());
 
 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);
 
 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);
 
 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);
 
 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);