1 /*******************************************************************************
 
   2  *  ============LICENSE_START=======================================================
 
   4  *  ================================================================================
 
   5  *   Copyright (C) 2020 Wipro Limited.
 
   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.
 
  18  *     ============LICENSE_END=========================================================
 
  20  *******************************************************************************/
 
  22 package org.onap.slice.analysis.ms.service;
 
  24 import static org.junit.Assert.assertEquals;
 
  26 import java.io.IOException;
 
  27 import java.nio.file.Files;
 
  28 import java.nio.file.Paths;
 
  29 import java.util.HashMap;
 
  30 import java.util.List;
 
  33 import org.junit.Before;
 
  34 import org.junit.Test;
 
  35 import org.junit.runner.RunWith;
 
  36 import org.mockito.InjectMocks;
 
  37 import org.onap.slice.analysis.ms.models.MeasurementObject;
 
  38 import org.springframework.boot.test.context.SpringBootTest;
 
  39 import org.springframework.test.context.junit4.SpringRunner;
 
  40 import org.springframework.test.util.ReflectionTestUtils;
 
  42 import com.fasterxml.jackson.core.type.TypeReference;
 
  43 import com.fasterxml.jackson.databind.ObjectMapper;
 
  45 @RunWith(SpringRunner.class)
 
  46 @SpringBootTest(classes = SnssaiSamplesProcessorTest.class)
 
  47 public class SnssaiSamplesProcessorTest {
 
  48         ObjectMapper obj = new ObjectMapper();
 
  51         SnssaiSamplesProcessor snssaiSamplesProcessor;
 
  55                 Map<String, Map<String, Integer>> ricToThroughputMapping = new HashMap<>();
 
  56                 Map<String, Integer> ric1 = new HashMap<>();
 
  57                 Map<String, Integer> ric2 = new HashMap<>();
 
  58                 ric1.put("dLThptPerSlice",50);
 
  59                 ric1.put("uLThptPerSlice",40);
 
  60                 ric2.put("dLThptPerSlice",50);
 
  61                 ric2.put("uLThptPerSlice",30);          
 
  62                 ricToThroughputMapping.put("1", ric1);
 
  63                 ricToThroughputMapping.put("2", ric2);  
 
  64                 ReflectionTestUtils.setField(snssaiSamplesProcessor, "ricToThroughputMapping", ricToThroughputMapping);
 
  66                 Map<String, Map<String, Integer>> ricToPrbsMapping = null;
 
  67                 List<MeasurementObject> sliceMeasList = null;
 
  68                 Map<String, List<String>> ricToCellMapping = null;
 
  69                 Map<String, String> prbThroughputMapping = new HashMap<>(); 
 
  70                 prbThroughputMapping = new HashMap<>();
 
  71                 prbThroughputMapping.put("PrbUsedDl", "dLThptPerSlice");
 
  72                 prbThroughputMapping.put("PrbUsedUl", "uLThptPerSlice");
 
  75                         ricToPrbsMapping = obj.readValue(new String(Files.readAllBytes(Paths.get("src/test/resources/ricToPrbMap.json"))), new TypeReference<Map<String, Map<String, Integer>>>(){});
 
  76                 sliceMeasList = obj.readValue(new String(Files.readAllBytes(Paths.get("src/test/resources/sliceMeasurementList.json"))), new TypeReference<List<MeasurementObject>>(){});
 
  77                 ricToCellMapping = obj.readValue(new String(Files.readAllBytes(Paths.get("src/test/resources/ricToCellMapping.json"))), new TypeReference<Map<String, List<String>>>(){});
 
  79        catch (IOException e) { 
 
  82                 ReflectionTestUtils.setField(snssaiSamplesProcessor, "ricToPrbsMapping", ricToPrbsMapping);
 
  83                 ReflectionTestUtils.setField(snssaiSamplesProcessor, "minPercentageChange", 6);
 
  84                 ReflectionTestUtils.setField(snssaiSamplesProcessor, "snssaiMeasurementList", sliceMeasList);
 
  85                 ReflectionTestUtils.setField(snssaiSamplesProcessor, "ricToCellMapping", ricToCellMapping);
 
  86                 ReflectionTestUtils.setField(snssaiSamplesProcessor, "prbThroughputMapping", prbThroughputMapping);
 
  90         public void computeSumTest() {
 
  91                 assertEquals(Integer.valueOf(100), snssaiSamplesProcessor.computeSum("PrbUsedDl"));
 
  95         public void updateConfigurationTest() {
 
  96                 Map<String, Map<String, Integer>> ricToThroughputMappingExp = new HashMap<>();
 
  97                 Map<String, Integer> ric1 = new HashMap<>();
 
  98                 Map<String, Integer> ric2 = new HashMap<>();
 
  99                 ric1.put("dLThptPerSlice",50);
 
 100                 ric1.put("uLThptPerSlice",40);
 
 101                 ric2.put("dLThptPerSlice",50);
 
 102                 ric2.put("uLThptPerSlice",30);          
 
 103                 ricToThroughputMappingExp.put("1", ric1);
 
 104                 ricToThroughputMappingExp.put("2", ric2);       
 
 105                 snssaiSamplesProcessor.updateConfiguration();
 
 106                 assertEquals(ricToThroughputMappingExp,ReflectionTestUtils.getField(snssaiSamplesProcessor, "ricToThroughputMapping"));
 
 110         public void updateConfigurationTrueTest() {
 
 111                 Map<String, Map<String, Integer>> ricToThroughputMappingExp = new HashMap<>();
 
 112                 Map<String, Integer> ric2 = new HashMap<>();
 
 113                 ric2.put("dLThptPerSlice",50);
 
 114                 ric2.put("uLThptPerSlice",30);          
 
 115                 ricToThroughputMappingExp.put("2", ric2);       
 
 117                 Map<String, Map<String, Integer>> ricToThroughputMapping = new HashMap<>();
 
 118                 Map<String, Integer> ric1 = new HashMap<>();
 
 119                 ric2 = new HashMap<>();
 
 120                 ric2.put("dLThptPerSlice",50);
 
 121                 ric2.put("uLThptPerSlice",30);  
 
 122                 ricToThroughputMapping.put("1", ric1);  
 
 123                 ricToThroughputMapping.put("2", ric2);  
 
 124                 ReflectionTestUtils.setField(snssaiSamplesProcessor, "ricToThroughputMapping", ricToThroughputMapping);
 
 126                 snssaiSamplesProcessor.updateConfiguration();
 
 127                 System.out.println();
 
 128                 assertEquals(ricToThroughputMappingExp, ReflectionTestUtils.getField(snssaiSamplesProcessor, "ricToThroughputMapping"));
 
 132         public void calculatePercentageChangeTest() {
 
 133                 Map<String, Map<String, Object>> ricConfiguration =  null;
 
 134                 Map<String, Map<String, Integer>> exp = new HashMap<>();
 
 135                 Map<String, Integer> ric1 = new HashMap<>();
 
 136                 Map<String, Integer> ric2 = new HashMap<>();
 
 137                 ric1.put("dLThptPerSlice", 50);
 
 138                 ric2.put("dLThptPerSlice", 50);
 
 139                 ric2.put("uLThptPerSlice", 30);
 
 143                         ricConfiguration = obj.readValue(new String(Files.readAllBytes(Paths.get("src/test/resources/ricConfiguration.json"))), new TypeReference<Map<String, Map<String, Object>>>(){});
 
 145        catch (IOException e) { 
 
 148            snssaiSamplesProcessor.calculatePercentageChange(ricConfiguration, "uLThptPerSlice");
 
 149            assertEquals(exp,ReflectionTestUtils.getField(snssaiSamplesProcessor, "ricToThroughputMapping"));
 
 151            ricConfiguration.get("2").put("dLThptPerSlice",60);
 
 152            exp.get("1").remove("dLThptPerSlice");
 
 153            snssaiSamplesProcessor.calculatePercentageChange(ricConfiguration, "dLThptPerSlice");
 
 154            assertEquals(exp,ReflectionTestUtils.getField(snssaiSamplesProcessor, "ricToThroughputMapping"));
 
 158         public void sumOfPrbsAcrossCellsTest() {
 
 159                 Map<String, Map<String, Integer>> ricToPrbsMapping = new HashMap<>();
 
 160                 Map<String, Map<String, Integer>> ricToPrbsMappingExp = new HashMap<>();
 
 162                 ReflectionTestUtils.setField(snssaiSamplesProcessor, "ricToPrbsMapping", ricToPrbsMapping);
 
 165                 ricToPrbsMappingExp = obj.readValue(new String(Files.readAllBytes(Paths.get("src/test/resources/ricToPrbOutput.json"))), new TypeReference<Map<String, Map<String, Integer>>>(){});
 
 167         catch (IOException e) { 
 
 170         snssaiSamplesProcessor.sumOfPrbsAcrossCells("PrbUsedDl");
 
 171         assertEquals(ricToPrbsMappingExp, ReflectionTestUtils.getField(snssaiSamplesProcessor, "ricToPrbsMapping"));
 
 175         public void computeThroughputTest() {
 
 176                 Map<String, Map<String, Integer>> ricToThroughputMapping = new HashMap<>();
 
 177                 ReflectionTestUtils.setField(snssaiSamplesProcessor, "ricToThroughputMapping", ricToThroughputMapping);
 
 179                 Map<String, Map<String, Integer>> ricToThroughputMappingExp = new HashMap<>();
 
 181                         ricToThroughputMappingExp = obj.readValue(new String(Files.readAllBytes(Paths.get("src/test/resources/ricToThroughputMappingOutput.json"))), new TypeReference<Map<String, Map<String, Integer>>>(){});
 
 183         catch (IOException e) { 
 
 186                 Map<String, Integer> sliceConfiguration = new HashMap<String, Integer>();
 
 187                 sliceConfiguration.put("dLThptPerSlice",120);
 
 188                 sliceConfiguration.put("uLThptPerSlice",100);
 
 189                 snssaiSamplesProcessor.computeThroughput(sliceConfiguration, 100, "PrbUsedDl");
 
 190                 snssaiSamplesProcessor.computeThroughput(sliceConfiguration, 70, "PrbUsedUl");
 
 191                 assertEquals(ricToThroughputMappingExp, ReflectionTestUtils.getField(snssaiSamplesProcessor, "ricToThroughputMapping"));