e9c134f7404f821529db999eee52e56423f2bf6f
[dcaegen2/services.git] /
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.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.InjectMocks;
38 import org.onap.slice.analysis.ms.models.MeasurementObject;
39 import org.springframework.boot.test.context.SpringBootTest;
40 import org.springframework.test.context.junit4.SpringRunner;
41 import org.springframework.test.util.ReflectionTestUtils;
42
43 import com.fasterxml.jackson.core.type.TypeReference;
44 import com.fasterxml.jackson.databind.ObjectMapper;
45
46 @RunWith(SpringRunner.class)
47 @SpringBootTest(classes = AverageCalculatorTest.class)
48 public class AverageCalculatorTest {
49         
50         @InjectMocks
51         AverageCalculator averageCalculator;
52         
53         @Before
54         public void setup() {
55                 List<String> pmNames = new ArrayList<>();
56                 pmNames.add("PrbUsedDl");
57                 pmNames.add("PrbUsedUl");
58                 ReflectionTestUtils.setField(averageCalculator, "pmNames", pmNames);
59         }
60         
61         @Test
62         public void findAverageOfSamplesTest() {
63                 ObjectMapper obj = new ObjectMapper();
64                 List<List<MeasurementObject>> input = null;
65                 List<MeasurementObject> output = null;
66         try { 
67               input = obj.readValue(new String(Files.readAllBytes(Paths.get("src/test/resources/measurementObjectList.json"))), new TypeReference<List<List<MeasurementObject>>>(){});
68               output = obj.readValue(new String(Files.readAllBytes(Paths.get("src/test/resources/average.json"))), new TypeReference<List<MeasurementObject>>(){});
69         } 
70         catch (IOException e) { 
71             e.printStackTrace(); 
72         } 
73         
74         assertEquals(output, averageCalculator.findAverageOfSamples(input));
75         }
76         
77         @Test
78         public void findAvgTest() {
79                 List<MeasurementObject> result = new ArrayList<>();     
80                 Map<String, Integer> pmData = new HashMap<>();
81                 pmData.put("PrbUsedDl", 50);
82                 pmData.put("PrbUsedUl", 48);
83                 result.add(new MeasurementObject("cell11", pmData));
84                 pmData.put("PrbUsedDl", 40);
85                 pmData.put("PrbUsedUl", 38);
86                 result.add(new MeasurementObject("cell12", pmData));
87                 
88                 List<MeasurementObject> exp = new ArrayList<>();        
89                 pmData.put("PrbUsedDl", 25);
90                 pmData.put("PrbUsedUl", 24);
91                 exp.add(new MeasurementObject("cell11", pmData));
92                 pmData.put("PrbUsedDl", 20);
93                 pmData.put("PrbUsedUl", 19);
94                 exp.add(new MeasurementObject("cell12", pmData));
95
96                 assertEquals(exp, averageCalculator.findAvg(result, 2));
97         }
98         
99         @Test
100         public void findAvgSum() {
101                 Map<String, Integer> existingMap = new HashMap<>();
102                 existingMap.put("PrbUsedDl", 50);
103                 existingMap.put("PrbUsedUl", 48);
104                 
105                 Map<String, Integer> currentMap = new HashMap<>();
106                 currentMap.put("PrbUsedDl", 40);
107                 currentMap.put("PrbUsedUl", 38);
108
109                 Map<String, Integer> result = new HashMap<>();
110                 result.put("PrbUsedDl", 90);
111                 result.put("PrbUsedUl", 86);
112                 
113                 assertEquals(new MeasurementObject("cell1", result), 
114                                 averageCalculator.findSum(new MeasurementObject("cell1", existingMap), new MeasurementObject("cell1", currentMap)));
115         }
116 }
117