1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.dcaegen2.services.sonhms.utils;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
27 import com.fasterxml.jackson.core.type.TypeReference;
28 import com.fasterxml.jackson.databind.ObjectMapper;
30 import fj.data.Either;
32 import java.io.BufferedReader;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.InputStreamReader;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
40 import java.util.UUID;
42 import org.junit.Before;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.InjectMocks;
47 import org.mockito.Mock;
48 import org.mockito.Mockito;
49 import org.mockito.MockitoAnnotations;
50 import org.onap.dcaegen2.services.sonhms.NotificationToClusterMapping;
51 import org.onap.dcaegen2.services.sonhms.child.Graph;
52 import org.onap.dcaegen2.services.sonhms.dao.ClusterDetailsRepository;
53 import org.onap.dcaegen2.services.sonhms.entity.ClusterDetails;
54 import org.onap.dcaegen2.services.sonhms.exceptions.ConfigDbNotFoundException;
55 import org.onap.dcaegen2.services.sonhms.model.CellPciPair;
56 import org.onap.dcaegen2.services.sonhms.model.FapServiceList;
57 import org.onap.dcaegen2.services.sonhms.model.Notification;
58 import org.onap.dcaegen2.services.sonhms.restclient.SdnrRestClient;
59 import org.powermock.api.mockito.PowerMockito;
60 import org.powermock.core.classloader.annotations.PrepareForTest;
61 import org.powermock.modules.junit4.PowerMockRunner;
62 import org.powermock.modules.junit4.PowerMockRunnerDelegate;
63 import org.springframework.boot.test.context.SpringBootTest;
64 import org.springframework.test.context.junit4.SpringRunner;
66 @RunWith(PowerMockRunner.class)
67 @PowerMockRunnerDelegate(SpringRunner.class)
68 @PrepareForTest({ SdnrRestClient.class, BeanUtil.class })
69 @SpringBootTest(classes = ClusterUtils.class)
70 public class ClusterUtilsTest {
73 private ClusterDetailsRepository clusterDetailsRepositoryMock;
76 ClusterUtils clusterUtils;
78 private static Notification notification1;
79 private static Notification notification2;
80 private static List<ClusterDetails> clusterDetailsForGetClusterDetailsFromClusterIdTest;
81 private static Graph cluster;
82 private static List<ClusterDetails> clusterDetails = new ArrayList<>();
85 public static void setup() {
87 notification1 = new Notification();
88 notification2 = new Notification();
89 clusterDetailsForGetClusterDetailsFromClusterIdTest = new ArrayList<ClusterDetails>();
91 String notificationString1 = readFromFile("/notification1.json");
92 String notificationString2 = readFromFile("/notification2.json");
93 String clusterDetailsListString = readFromFile("/ClusterDetailsTest.json");
95 String clusterInfo1 = readFromFile("/clusterInfo1.json");
96 String clusterInfo2 = readFromFile("/clusterInfo2.json");
97 String clusterInfo3 = readFromFile("/clusterInfo3.json");
98 String clusterInfo4 = readFromFile("/clusterInfo4.json");
99 String clusterInfo = readFromFile("/clusterInfo5.json");
100 cluster = new Graph(clusterInfo);
102 clusterDetails.add(new ClusterDetails("1", clusterInfo1, 35));
103 clusterDetails.add(new ClusterDetails("2", clusterInfo2, 36));
104 clusterDetails.add(new ClusterDetails("3", clusterInfo3, 37));
105 clusterDetails.add(new ClusterDetails("4", clusterInfo4, 38));
107 ObjectMapper mapper = new ObjectMapper();
110 notification1 = mapper.readValue(notificationString1, Notification.class);
111 notification2 = mapper.readValue(notificationString2, Notification.class);
112 clusterDetailsForGetClusterDetailsFromClusterIdTest = mapper.readValue(clusterDetailsListString,
113 new TypeReference<List<ClusterDetails>>() {
115 } catch (IOException e) {
116 // TODO Auto-generated catch block
123 public void setupTest() {
124 clusterUtils = new ClusterUtils();
125 MockitoAnnotations.initMocks(this);
129 public void getClustersForNotificationTest() {
131 NotificationToClusterMapping expected = new NotificationToClusterMapping();
132 Map<FapServiceList, String> cellsinCluster = new HashMap<>();
133 cellsinCluster.put(notification1.getPayload().getRadioAccess().getFapServiceList().get(0), "2");
134 expected.setCellsinCluster(cellsinCluster);
135 expected.setNewCells(new ArrayList<FapServiceList>());
137 NotificationToClusterMapping result = clusterUtils.getClustersForNotification(notification1, clusterDetails);
138 assertEquals(expected, result);
140 expected = new NotificationToClusterMapping();
141 List<FapServiceList> newCells = new ArrayList<>();
142 newCells.add(notification2.getPayload().getRadioAccess().getFapServiceList().get(0));
143 expected.setCellsinCluster(new HashMap<>());
144 expected.setNewCells(newCells);
146 result = clusterUtils.getClustersForNotification(notification2, clusterDetails);
147 assertEquals(expected, result);
151 public void createClusterTest() throws ConfigDbNotFoundException {
153 Map<CellPciPair, ArrayList<CellPciPair>> clusterMap = new HashMap<CellPciPair, ArrayList<CellPciPair>>();
155 List<CellPciPair> firstNbrList = new ArrayList<>();
156 List<CellPciPair> nbrList = new ArrayList<>();
158 firstNbrList.add(new CellPciPair("48", 0));
159 nbrList.add(new CellPciPair("44", 3));
161 PowerMockito.mockStatic(SdnrRestClient.class);
163 PowerMockito.when(SdnrRestClient.getNbrList(Mockito.anyString())).thenReturn(nbrList);
165 clusterMap.put(new CellPciPair("45", 310), (ArrayList<CellPciPair>) firstNbrList);
167 assertEquals(cluster, clusterUtils.createCluster(clusterMap));
172 public void getClusterDetailsFromClusterIdTest() {
173 ClusterDetails responseValue = null;
174 Integer responseVal = null;
175 Integer expectedValue = 404;
176 Either<ClusterDetails, Integer> response = clusterUtils.getClusterDetailsFromClusterId("0",
177 clusterDetailsForGetClusterDetailsFromClusterIdTest);
178 assertTrue(response.isLeft());
179 if (response.isLeft()) {
180 responseValue = response.left().value();
182 assertEquals(clusterDetailsForGetClusterDetailsFromClusterIdTest.get(0), responseValue);
183 response = clusterUtils.getClusterDetailsFromClusterId("1",
184 clusterDetailsForGetClusterDetailsFromClusterIdTest);
185 assertTrue(response.isLeft());
186 if (response.isLeft()) {
187 responseValue = response.left().value();
189 assertEquals(clusterDetailsForGetClusterDetailsFromClusterIdTest.get(1), responseValue);
190 response = clusterUtils.getClusterDetailsFromClusterId("9",
191 clusterDetailsForGetClusterDetailsFromClusterIdTest);
192 assertTrue(response.isRight());
193 if (response.isRight()) {
194 responseVal = response.right().value();
196 assertEquals(expectedValue, responseVal);
201 public void saveClusterTest() {
202 ClusterDetails details = new ClusterDetails();
203 details.setClusterId("123e4567-e89b-12d3-a456-426655440000");
204 details.setClusterInfo("cellPciNeighbourString");
205 details.setChildThreadId(978668);
206 PowerMockito.mockStatic(BeanUtil.class);
207 PowerMockito.when(BeanUtil.getBean(ClusterDetailsRepository.class)).thenReturn(clusterDetailsRepositoryMock);
208 Mockito.when(clusterDetailsRepositoryMock.save(details)).thenReturn(details);
209 Long threadId = (long) 978668;
210 clusterUtils.saveCluster(cluster, UUID.fromString("123e4567-e89b-12d3-a456-426655440000"), threadId);
211 assertEquals(details, clusterDetailsRepositoryMock.save(details));
216 public void getClusterForCellTest() {
217 FapServiceList fapServiceList = notification1.getPayload().getRadioAccess().getFapServiceList().get(0);
218 String clusterInfo1 = readFromFile("/clusterInfo1.json");
219 String clusterInfo2 = readFromFile("/clusterInfo2.json");
220 Graph graph1 = new Graph(clusterInfo1);
221 Graph graph2 = new Graph(clusterInfo2);
222 List<Graph> newClusters = new ArrayList<Graph>();
223 newClusters.add(graph1);
224 newClusters.add(graph2);
225 Either<Graph, Integer> result = clusterUtils.getClusterForCell(fapServiceList, newClusters);
226 assertTrue(result.isLeft());
228 newClusters = new ArrayList<>();
229 newClusters.add(graph1);
230 result = clusterUtils.getClusterForCell(fapServiceList, newClusters);
231 assertTrue(result.isRight());
232 int resultRight = result.right().value();
233 assertEquals(404, resultRight);
235 List<Graph> emptyList = new ArrayList<Graph>();
237 result = clusterUtils.getClusterForCell(fapServiceList, emptyList);
238 assertTrue(result.isRight());
239 resultRight = result.right().value();
240 assertEquals(404, resultRight);
245 public void modifyClusterTest() {
247 String clusterInfo = readFromFile("/clusterInfo2.json");
248 String clusterInfo2 = readFromFile("/clusterInfo6.json");
250 Graph cluster = new Graph(clusterInfo);
251 Graph expected = new Graph(clusterInfo2);
252 Map<CellPciPair, ArrayList<CellPciPair>> clusterMap = new HashMap<CellPciPair, ArrayList<CellPciPair>>();
254 ArrayList<CellPciPair> firstNbrList = new ArrayList<>();
255 firstNbrList.add(new CellPciPair("48",0));
256 clusterMap.put(new CellPciPair("45",310),firstNbrList);
257 assertEquals(expected, clusterUtils.modifyCluster(cluster,clusterMap));
260 private static String readFromFile(String file) {
261 String content = new String();
264 InputStream is = ClusterUtilsTest.class.getResourceAsStream(file);
265 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
266 content = bufferedReader.readLine();
268 while ((temp = bufferedReader.readLine()) != null) {
269 content = content.concat(temp);
271 content = content.trim();
272 bufferedReader.close();
273 } catch (Exception e) {