Fix bugs and formatting issues
[dcaegen2/services/son-handler.git] / src / test / java / org / onap / dcaegen2 / services / sonhms / EventHandlerTest.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;
23
24 import com.fasterxml.jackson.databind.ObjectMapper;
25
26 import fj.data.Either;
27
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;
35 import java.util.Map;
36 import java.util.concurrent.ExecutorService;
37
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;
55
56 @RunWith(MockitoJUnitRunner.class)
57 @SpringBootTest(classes = EventHandler.class)
58 public class EventHandlerTest {
59
60     @Mock 
61     ClusterUtils clusterutilsMock;
62     
63     @Mock
64     ExecutorService pool;
65     
66     @Mock
67     ThreadUtils threadUtilsMock;
68     
69     private static Notification notification;
70     private static List<ClusterDetails> clusterDetails = new ArrayList<>();
71     
72     @InjectMocks
73     EventHandler eventHandler;
74     
75     @Before
76     public void setup() {
77         
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();
85         
86         try {
87             notification = mapper.readValue(notificationString, Notification.class);
88         } catch (IOException e) {
89             // TODO Auto-generated catch block
90             e.printStackTrace();
91         }
92         System.out.println(notification.toString());
93         
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));
98   
99     }
100     
101     @Test
102     public void handleSdnrNotificationTest() {
103         
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);
113         
114         
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);
119         
120         try {
121             Mockito.when(clusterutilsMock.createCluster(Mockito.any())).thenReturn(cluster);
122         } catch (ConfigDbNotFoundException e1) {
123             // TODO Auto-generated catch block
124             e1.printStackTrace();
125         }
126
127         
128         Assert.assertEquals(true, eventHandler.handleSdnrNotification(notification));
129         
130         
131         
132     }
133     
134     private static String readFromFile(String file) {
135         String content  = new String();
136         try {
137             
138             InputStream is = ClusterUtilsTest.class.getResourceAsStream(file);
139             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
140             content = bufferedReader.readLine();
141             String temp;
142             while((temp = bufferedReader.readLine()) != null) {
143                 content = content.concat(temp);
144             }
145             content = content.trim();
146             bufferedReader.close();
147         }
148         catch(Exception e) {
149             content  = null;
150         }
151         return content;
152     }
153 }