03305a1056eb90acc3b4acd7fc952ae9565fcaf9
[dcaegen2/services/son-handler.git] / src / test / java / org / onap / dcaegen2 / services / sonhms / utils / ClusterUtilsTest.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  son-handler
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
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.dcaegen2.services.sonhms.utils;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26
27 import com.fasterxml.jackson.core.type.TypeReference;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29
30 import fj.data.Either;
31
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;
39 import java.util.Map;
40 import java.util.UUID;
41
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;
65
66 @RunWith(PowerMockRunner.class)
67 @PowerMockRunnerDelegate(SpringRunner.class)
68 @PrepareForTest({ SdnrRestClient.class, BeanUtil.class })
69 @SpringBootTest(classes = ClusterUtils.class)
70 public class ClusterUtilsTest {
71
72     @Mock
73     private ClusterDetailsRepository clusterDetailsRepositoryMock;
74
75     @InjectMocks
76     ClusterUtils clusterUtils;
77
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<>();
83
84     @BeforeClass
85     public static void setup() {
86
87         notification1 = new Notification();
88         notification2 = new Notification();
89         clusterDetailsForGetClusterDetailsFromClusterIdTest = new ArrayList<ClusterDetails>();
90
91         String notificationString1 = readFromFile("/notification1.json");
92         String notificationString2 = readFromFile("/notification2.json");
93         String clusterDetailsListString = readFromFile("/ClusterDetailsTest.json");
94
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);
101
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));
106
107         ObjectMapper mapper = new ObjectMapper();
108
109         try {
110             notification1 = mapper.readValue(notificationString1, Notification.class);
111             notification2 = mapper.readValue(notificationString2, Notification.class);
112             clusterDetailsForGetClusterDetailsFromClusterIdTest = mapper.readValue(clusterDetailsListString,
113                     new TypeReference<List<ClusterDetails>>() {
114                     });
115         } catch (IOException e) {
116             // TODO Auto-generated catch block
117             e.printStackTrace();
118         }
119
120     }
121
122     @Before
123     public void setupTest() {
124         clusterUtils = new ClusterUtils();
125         MockitoAnnotations.initMocks(this);
126     }
127
128     @Test
129     public void getClustersForNotificationTest() {
130
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>());
136
137         NotificationToClusterMapping result = clusterUtils.getClustersForNotification(notification1, clusterDetails);
138         assertEquals(expected, result);
139
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);
145
146         result = clusterUtils.getClustersForNotification(notification2, clusterDetails);
147         assertEquals(expected, result);
148     }
149
150     @Test
151     public void createClusterTest() throws ConfigDbNotFoundException {
152
153         Map<CellPciPair, ArrayList<CellPciPair>> clusterMap = new HashMap<CellPciPair, ArrayList<CellPciPair>>();
154
155         List<CellPciPair> firstNbrList = new ArrayList<>();
156         List<CellPciPair> nbrList = new ArrayList<>();
157
158         firstNbrList.add(new CellPciPair("48", 0));
159         nbrList.add(new CellPciPair("44", 3));
160
161         PowerMockito.mockStatic(SdnrRestClient.class);
162
163         PowerMockito.when(SdnrRestClient.getNbrList(Mockito.anyString())).thenReturn(nbrList);
164
165         clusterMap.put(new CellPciPair("45", 310), (ArrayList<CellPciPair>) firstNbrList);
166
167         assertEquals(cluster, clusterUtils.createCluster(clusterMap));
168
169     }
170
171     @Test
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();
181         }
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();
188         }
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();
195         }
196         assertEquals(expectedValue, responseVal);
197
198     }
199
200     @Test
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));
212
213     }
214
215     @Test
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());
227
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);
234
235         List<Graph> emptyList = new ArrayList<Graph>();
236
237         result = clusterUtils.getClusterForCell(fapServiceList, emptyList);
238         assertTrue(result.isRight());
239         resultRight = result.right().value();
240         assertEquals(404, resultRight);
241
242     }
243
244     @Test
245     public void modifyClusterTest() {
246
247         String clusterInfo = readFromFile("/clusterInfo2.json");
248         String clusterInfo2 = readFromFile("/clusterInfo6.json");
249
250         Graph cluster = new Graph(clusterInfo);
251         Graph expected = new Graph(clusterInfo2);
252         Map<CellPciPair, ArrayList<CellPciPair>> clusterMap = new HashMap<CellPciPair, ArrayList<CellPciPair>>();
253
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));
258     }
259
260     private static String readFromFile(String file) {
261         String content = new String();
262         try {
263
264             InputStream is = ClusterUtilsTest.class.getResourceAsStream(file);
265             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
266             content = bufferedReader.readLine();
267             String temp;
268             while ((temp = bufferedReader.readLine()) != null) {
269                 content = content.concat(temp);
270             }
271             content = content.trim();
272             bufferedReader.close();
273         } catch (Exception e) {
274             content = null;
275         }
276         return content;
277     }
278 }