Merge "Fix Issues - Fix certificate issue for authenticated topic - Fix consumer...
[dcaegen2/services.git] / components / slice-analysis-ms / src / test / java / org / onap / slice / analysis / ms / service / SnssaiSamplesProcessorTest.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  slice-analysis-ms
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
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  *     ============LICENSE_END=========================================================
19  *
20  *******************************************************************************/
21
22 package org.onap.slice.analysis.ms.service;
23
24 import static org.junit.Assert.assertEquals;
25
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;
31 import java.util.Map;
32
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;
41
42 import com.fasterxml.jackson.core.type.TypeReference;
43 import com.fasterxml.jackson.databind.ObjectMapper;
44
45 @RunWith(SpringRunner.class)
46 @SpringBootTest(classes = SnssaiSamplesProcessorTest.class)
47 public class SnssaiSamplesProcessorTest {
48         ObjectMapper obj = new ObjectMapper();
49
50         @InjectMocks
51         SnssaiSamplesProcessor snssaiSamplesProcessor;
52         
53         @Before
54         public void setup() {
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);
65         
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");
73
74                 try { 
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>>>(){});
78                 } 
79        catch (IOException e) { 
80             e.printStackTrace(); 
81        }
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);
87         }
88         
89         @Test
90         public void computeSumTest() {
91                 assertEquals(Integer.valueOf(100), snssaiSamplesProcessor.computeSum("PrbUsedDl"));
92         }
93         
94         @Test
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"));
107         }
108         
109         @Test
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);       
116                 
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);
125
126                 snssaiSamplesProcessor.updateConfiguration();
127                 System.out.println();
128                 assertEquals(ricToThroughputMappingExp, ReflectionTestUtils.getField(snssaiSamplesProcessor, "ricToThroughputMapping"));
129         }
130         
131         @Test
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);
140                 exp.put("1", ric1);
141                 exp.put("2", ric2);     
142                 try { 
143                         ricConfiguration = obj.readValue(new String(Files.readAllBytes(Paths.get("src/test/resources/ricConfiguration.json"))), new TypeReference<Map<String, Map<String, Object>>>(){});
144        } 
145        catch (IOException e) { 
146             e.printStackTrace(); 
147        }
148            snssaiSamplesProcessor.calculatePercentageChange(ricConfiguration, "uLThptPerSlice");
149            assertEquals(exp,ReflectionTestUtils.getField(snssaiSamplesProcessor, "ricToThroughputMapping"));
150            
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"));
155         }
156         
157         @Test
158         public void sumOfPrbsAcrossCellsTest() {
159                 Map<String, Map<String, Integer>> ricToPrbsMapping = new HashMap<>();
160                 Map<String, Map<String, Integer>> ricToPrbsMappingExp = new HashMap<>();
161
162                 ReflectionTestUtils.setField(snssaiSamplesProcessor, "ricToPrbsMapping", ricToPrbsMapping);
163
164         try { 
165                 ricToPrbsMappingExp = obj.readValue(new String(Files.readAllBytes(Paths.get("src/test/resources/ricToPrbOutput.json"))), new TypeReference<Map<String, Map<String, Integer>>>(){});
166         } 
167         catch (IOException e) { 
168             e.printStackTrace(); 
169         } 
170         snssaiSamplesProcessor.sumOfPrbsAcrossCells("PrbUsedDl");
171         assertEquals(ricToPrbsMappingExp, ReflectionTestUtils.getField(snssaiSamplesProcessor, "ricToPrbsMapping"));
172         }
173         
174         @Test
175         public void computeThroughputTest() {
176                 Map<String, Map<String, Integer>> ricToThroughputMapping = new HashMap<>();
177                 ReflectionTestUtils.setField(snssaiSamplesProcessor, "ricToThroughputMapping", ricToThroughputMapping);
178
179                 Map<String, Map<String, Integer>> ricToThroughputMappingExp = new HashMap<>();
180                 try { 
181                         ricToThroughputMappingExp = obj.readValue(new String(Files.readAllBytes(Paths.get("src/test/resources/ricToThroughputMappingOutput.json"))), new TypeReference<Map<String, Map<String, Integer>>>(){});
182         } 
183         catch (IOException e) { 
184             e.printStackTrace(); 
185         } 
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"));
192         }
193 }