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;
24 import com.fasterxml.jackson.databind.ObjectMapper;
26 import fj.data.Either;
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
36 import java.util.concurrent.ExecutorService;
38 import org.junit.Assert;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.InjectMocks;
43 import org.mockito.Mock;
44 import org.mockito.Mockito;
45 import org.mockito.runners.MockitoJUnitRunner;
46 import org.onap.dcaegen2.services.sonhms.child.Graph;
47 import org.onap.dcaegen2.services.sonhms.entity.ClusterDetails;
48 import org.onap.dcaegen2.services.sonhms.exceptions.ConfigDbNotFoundException;
49 import org.onap.dcaegen2.services.sonhms.model.FapServiceList;
50 import org.onap.dcaegen2.services.sonhms.model.Notification;
51 import org.onap.dcaegen2.services.sonhms.utils.ClusterUtils;
52 import org.onap.dcaegen2.services.sonhms.utils.ClusterUtilsTest;
53 import org.onap.dcaegen2.services.sonhms.utils.ThreadUtils;
54 import org.springframework.boot.test.context.SpringBootTest;
56 @RunWith(MockitoJUnitRunner.class)
57 @SpringBootTest(classes = EventHandler.class)
58 public class EventHandlerTest {
61 ClusterUtils clusterutilsMock;
67 ThreadUtils threadUtilsMock;
69 private static Notification notification;
70 private static List<ClusterDetails> clusterDetails = new ArrayList<>();
73 EventHandler eventHandler;
78 notification = new Notification();
79 String notificationString = readFromFile("/notification3.json");
80 String clusterInfo1 = readFromFile("/clusterInfo1.json");
81 String clusterInfo2 = readFromFile("/clusterInfo2.json");
82 String clusterInfo3 = readFromFile("/clusterInfo3.json");
83 String clusterInfo4 = readFromFile("/clusterInfo4.json");
84 ObjectMapper mapper = new ObjectMapper();
87 notification = mapper.readValue(notificationString, Notification.class);
88 } catch (IOException e) {
89 // TODO Auto-generated catch block
92 System.out.println(notification.toString());
94 clusterDetails.add(new ClusterDetails("1", clusterInfo1, 35));
95 clusterDetails.add(new ClusterDetails("2", clusterInfo2, 36));
96 clusterDetails.add(new ClusterDetails("3", clusterInfo3, 37));
97 clusterDetails.add(new ClusterDetails("4", clusterInfo4, 38));
102 public void handleSdnrNotificationTest() {
104 String clusterInfo7 = readFromFile("/clusterInfo7.json");
105 Graph cluster = new Graph(clusterInfo7);
106 NotificationToClusterMapping mapping = new NotificationToClusterMapping();
107 Map<FapServiceList, String> cellsinCluster = new HashMap<>();
108 List<FapServiceList> newCells = new ArrayList<>();
109 newCells.add(notification.getPayload().getRadioAccess().getFapServiceList().get(0));
110 mapping.setCellsinCluster(cellsinCluster);
111 mapping.setNewCells(newCells);
112 Either<Graph, Integer> existingCluster = Either.right(404);
115 Mockito.when(clusterutilsMock.getAllClusters()).thenReturn(clusterDetails);
116 Mockito.when(clusterutilsMock.getClustersForNotification(notification, clusterDetails)).thenReturn(mapping);
117 Mockito.when(clusterutilsMock.getClusterForCell(Mockito.any(), Mockito.any())).thenReturn(existingCluster);
118 Mockito.when(threadUtilsMock.createNewThread(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(),Mockito.anyString())).thenReturn(true);
121 Mockito.when(clusterutilsMock.createCluster(Mockito.any())).thenReturn(cluster);
122 } catch (ConfigDbNotFoundException e1) {
123 // TODO Auto-generated catch block
124 e1.printStackTrace();
128 Assert.assertEquals(true, eventHandler.handleSdnrNotification(notification));
134 private static String readFromFile(String file) {
135 String content = new String();
138 InputStream is = ClusterUtilsTest.class.getResourceAsStream(file);
139 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
140 content = bufferedReader.readLine();
142 while((temp = bufferedReader.readLine()) != null) {
143 content = content.concat(temp);
145 content = content.trim();
146 bufferedReader.close();