summaryrefslogtreecommitdiffstats
path: root/components/slice-analysis-ms/src/test/java/org/onap/slice/analysis/ms/service/SnssaiSamplesProcessorTest.java
blob: e8cc214567e2e066fc43d4176df56e008abe5ac9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*******************************************************************************
 *  ============LICENSE_START=======================================================
 *  slice-analysis-ms
 *  ================================================================================
 *   Copyright (C) 2020 Wipro Limited.
 *   ==============================================================================
 *     Licensed under the Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
 *     ============LICENSE_END=========================================================
 *
 *******************************************************************************/

package org.onap.slice.analysis.ms.service;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.onap.slice.analysis.ms.models.MeasurementObject;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SnssaiSamplesProcessorTest.class)
public class SnssaiSamplesProcessorTest {
	ObjectMapper obj = new ObjectMapper();

	@InjectMocks
	SnssaiSamplesProcessor snssaiSamplesProcessor;
	
	@Before
	public void setup() {
		Map<String, Map<String, Integer>> ricToThroughputMapping = new HashMap<>();
		Map<String, Integer> ric1 = new HashMap<>();
		Map<String, Integer> ric2 = new HashMap<>();
		ric1.put("dLThptPerSlice",50);
		ric1.put("uLThptPerSlice",40);
		ric2.put("dLThptPerSlice",50);
		ric2.put("uLThptPerSlice",30);		
		ricToThroughputMapping.put("1", ric1);
		ricToThroughputMapping.put("2", ric2);	
		ReflectionTestUtils.setField(snssaiSamplesProcessor, "ricToThroughputMapping", ricToThroughputMapping);
	
		Map<String, Map<String, Integer>> ricToPrbsMapping = null;
		List<MeasurementObject> sliceMeasList = null;
		Map<String, List<String>> ricToCellMapping = null;
		Map<String, String> prbThroughputMapping = new HashMap<>(); 
		prbThroughputMapping = new HashMap<>();
		prbThroughputMapping.put("PrbUsedDl", "dLThptPerSlice");
		prbThroughputMapping.put("PrbUsedUl", "uLThptPerSlice");

		try { 
			ricToPrbsMapping = obj.readValue(new String(Files.readAllBytes(Paths.get("src/test/resources/ricToPrbMap.json"))), new TypeReference<Map<String, Map<String, Integer>>>(){});
        	sliceMeasList = obj.readValue(new String(Files.readAllBytes(Paths.get("src/test/resources/sliceMeasurementList.json"))), new TypeReference<List<MeasurementObject>>(){});
        	ricToCellMapping = obj.readValue(new String(Files.readAllBytes(Paths.get("src/test/resources/ricToCellMapping.json"))), new TypeReference<Map<String, List<String>>>(){});
		} 
       catch (IOException e) { 
            e.printStackTrace(); 
       }
		ReflectionTestUtils.setField(snssaiSamplesProcessor, "ricToPrbsMapping", ricToPrbsMapping);
		ReflectionTestUtils.setField(snssaiSamplesProcessor, "minPercentageChange", 6);
		ReflectionTestUtils.setField(snssaiSamplesProcessor, "snssaiMeasurementList", sliceMeasList);
		ReflectionTestUtils.setField(snssaiSamplesProcessor, "ricToCellMapping", ricToCellMapping);
		ReflectionTestUtils.setField(snssaiSamplesProcessor, "prbThroughputMapping", prbThroughputMapping);
	}
	
	@Test
	public void computeSumTest() {
		assertEquals(Integer.valueOf(100), snssaiSamplesProcessor.computeSum("PrbUsedDl"));
	}
	
	@Test
	public void updateConfigurationTest() {
		Map<String, Map<String, Integer>> ricToThroughputMappingExp = new HashMap<>();
		Map<String, Integer> ric1 = new HashMap<>();
		Map<String, Integer> ric2 = new HashMap<>();
		ric1.put("dLThptPerSlice",50);
		ric1.put("uLThptPerSlice",40);
		ric2.put("dLThptPerSlice",50);
		ric2.put("uLThptPerSlice",30);		
		ricToThroughputMappingExp.put("1", ric1);
		ricToThroughputMappingExp.put("2", ric2);	
		snssaiSamplesProcessor.updateConfiguration();
		assertEquals(ricToThroughputMappingExp,ReflectionTestUtils.getField(snssaiSamplesProcessor, "ricToThroughputMapping"));
	}
	
	@Test
	public void updateConfigurationTrueTest() {
		Map<String, Map<String, Integer>> ricToThroughputMappingExp = new HashMap<>();
		Map<String, Integer> ric2 = new HashMap<>();
		ric2.put("dLThptPerSlice",50);
		ric2.put("uLThptPerSlice",30);		
		ricToThroughputMappingExp.put("2", ric2);	
		
		Map<String, Map<String, Integer>> ricToThroughputMapping = new HashMap<>();
		Map<String, Integer> ric1 = new HashMap<>();
		ric2 = new HashMap<>();
		ric2.put("dLThptPerSlice",50);
		ric2.put("uLThptPerSlice",30);	
		ricToThroughputMapping.put("1", ric1);	
		ricToThroughputMapping.put("2", ric2);	
		ReflectionTestUtils.setField(snssaiSamplesProcessor, "ricToThroughputMapping", ricToThroughputMapping);

		snssaiSamplesProcessor.updateConfiguration();
		System.out.println();
		assertEquals(ricToThroughputMappingExp, ReflectionTestUtils.getField(snssaiSamplesProcessor, "ricToThroughputMapping"));
	}
	
	@Test
	public void calculatePercentageChangeTest() {
		Map<String, Map<String, Object>> ricConfiguration =  null;
		Map<String, Map<String, Integer>> exp = new HashMap<>();
		Map<String, Integer> ric1 = new HashMap<>();
		Map<String, Integer> ric2 = new HashMap<>();
		ric1.put("dLThptPerSlice", 50);
		ric2.put("dLThptPerSlice", 50);
		ric2.put("uLThptPerSlice", 30);
		exp.put("1", ric1);
		exp.put("2", ric2);	
		try { 
			ricConfiguration = obj.readValue(new String(Files.readAllBytes(Paths.get("src/test/resources/ricConfiguration.json"))), new TypeReference<Map<String, Map<String, Object>>>(){});
       } 
       catch (IOException e) { 
            e.printStackTrace(); 
       }
	   snssaiSamplesProcessor.calculatePercentageChange(ricConfiguration, "uLThptPerSlice");
	   assertEquals(exp,ReflectionTestUtils.getField(snssaiSamplesProcessor, "ricToThroughputMapping"));
	   
	   ricConfiguration.get("2").put("dLThptPerSlice",60);
	   exp.get("1").remove("dLThptPerSlice");
	   snssaiSamplesProcessor.calculatePercentageChange(ricConfiguration, "dLThptPerSlice");
	   assertEquals(exp,ReflectionTestUtils.getField(snssaiSamplesProcessor, "ricToThroughputMapping"));
	}
	
	@Test
	public void sumOfPrbsAcrossCellsTest() {
		Map<String, Map<String, Integer>> ricToPrbsMapping = new HashMap<>();
		Map<String, Map<String, Integer>> ricToPrbsMappingExp = new HashMap<>();

		ReflectionTestUtils.setField(snssaiSamplesProcessor, "ricToPrbsMapping", ricToPrbsMapping);

        try { 
        	ricToPrbsMappingExp = obj.readValue(new String(Files.readAllBytes(Paths.get("src/test/resources/ricToPrbOutput.json"))), new TypeReference<Map<String, Map<String, Integer>>>(){});
        } 
        catch (IOException e) { 
            e.printStackTrace(); 
        } 
        snssaiSamplesProcessor.sumOfPrbsAcrossCells("PrbUsedDl");
        assertEquals(ricToPrbsMappingExp, ReflectionTestUtils.getField(snssaiSamplesProcessor, "ricToPrbsMapping"));
	}
	
	@Test
	public void computeThroughputTest() {
		Map<String, Map<String, Integer>> ricToThroughputMapping = new HashMap<>();
		ReflectionTestUtils.setField(snssaiSamplesProcessor, "ricToThroughputMapping", ricToThroughputMapping);

		Map<String, Map<String, Integer>> ricToThroughputMappingExp = new HashMap<>();
		try { 
			ricToThroughputMappingExp = obj.readValue(new String(Files.readAllBytes(Paths.get("src/test/resources/ricToThroughputMappingOutput.json"))), new TypeReference<Map<String, Map<String, Integer>>>(){});
        } 
        catch (IOException e) { 
            e.printStackTrace(); 
        } 
		Map<String, Integer> sliceConfiguration = new HashMap<String, Integer>();
		sliceConfiguration.put("dLThptPerSlice",120);
		sliceConfiguration.put("uLThptPerSlice",100);
		snssaiSamplesProcessor.computeThroughput(sliceConfiguration, 100, "PrbUsedDl");
		snssaiSamplesProcessor.computeThroughput(sliceConfiguration, 70, "PrbUsedUl");
		assertEquals(ricToThroughputMappingExp, ReflectionTestUtils.getField(snssaiSamplesProcessor, "ricToThroughputMapping"));
	}
}