Fix bugs and formatting issues
[dcaegen2/services/son-handler.git] / src / test / java / org / onap / dcaegen2 / services / sonhms / child / TestChildThreadUtils.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.child;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Mockito.when;
28
29 import com.fasterxml.jackson.databind.ObjectMapper;
30
31 import java.io.BufferedReader;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39
40 import org.junit.Assert;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.InjectMocks;
45 import org.mockito.Mock;
46 import org.mockito.Mockito;
47 import org.mockito.MockitoAnnotations;
48 import org.onap.dcaegen2.services.sonhms.ConfigPolicy;
49 import org.onap.dcaegen2.services.sonhms.Configuration;
50 import org.onap.dcaegen2.services.sonhms.dao.SonRequestsRepository;
51 import org.onap.dcaegen2.services.sonhms.dmaap.PolicyDmaapClient;
52 import org.onap.dcaegen2.services.sonhms.entity.SonRequests;
53 import org.onap.dcaegen2.services.sonhms.exceptions.ConfigDbNotFoundException;
54 import org.onap.dcaegen2.services.sonhms.model.CellPciPair;
55 import org.onap.dcaegen2.services.sonhms.model.PolicyNotification;
56 import org.onap.dcaegen2.services.sonhms.restclient.AsyncResponseBody;
57 import org.onap.dcaegen2.services.sonhms.restclient.SdnrRestClient;
58 import org.onap.dcaegen2.services.sonhms.utils.BeanUtil;
59 import org.onap.dcaegen2.services.sonhms.utils.ClusterUtilsTest;
60 import org.powermock.api.mockito.PowerMockito;
61 import org.powermock.core.classloader.annotations.PrepareForTest;
62 import org.powermock.modules.junit4.PowerMockRunner;
63 import org.powermock.modules.junit4.PowerMockRunnerDelegate;
64 import org.springframework.boot.test.context.SpringBootTest;
65 import org.springframework.test.context.junit4.SpringRunner;
66
67 @RunWith(PowerMockRunner.class)
68 @PowerMockRunnerDelegate(SpringRunner.class)
69 @PrepareForTest({ BeanUtil.class, SdnrRestClient.class })
70 @SpringBootTest(classes = TestChildThreadUtils.class)
71 public class TestChildThreadUtils {
72
73         ChildThreadUtils childThreadUtils;
74         @Mock
75         private SonRequestsRepository repository;
76
77         @Mock
78         private PnfUtils pnfUtils;
79         @Mock
80         private PolicyDmaapClient policyDmaapClient;
81         
82         @InjectMocks
83         private ChildThreadUtils childThreadUtils2;
84
85         @Before
86         public void setup() {
87
88                 ConfigPolicy configPolicy = ConfigPolicy.getInstance();
89                 Configuration config = Configuration.getInstance();
90                 config.setMinCollision(5);
91                 config.setMinConfusion(5);
92                 Map<String, Object> configPolicyMap = new HashMap<>();
93                 configPolicyMap.put("PCI_MODCONFIG_POLICY_NAME", "ControlLoop-vPCI-fb41f388-a5f2-11e8-98d0-529269fb1459");
94                 configPolicy.setConfig(configPolicyMap);
95                 childThreadUtils = new ChildThreadUtils(configPolicy, pnfUtils,  policyDmaapClient);
96                 MockitoAnnotations.initMocks(this);
97                 
98         }
99
100         @Test
101         public void savePciRequestTest() {
102                 SonRequests sonRequest = new SonRequests();
103                 sonRequest.setTransactionId("transactionId");
104                 sonRequest.setChildThreadId(1L);
105                 PowerMockito.mockStatic(BeanUtil.class);
106                 PowerMockito.when(BeanUtil.getBean(SonRequestsRepository.class))
107                                 .thenReturn(repository);
108                 when(repository.save(sonRequest)).thenReturn(sonRequest);
109                 childThreadUtils2.saveRequest("transactionId",1L);;
110                 assertEquals(sonRequest, repository.save(sonRequest));
111         }
112
113         @Test
114         public void triggerOrWaitTest() {
115                 Map<String, ArrayList<Integer>> collisionConfusionResult = new HashMap<String, ArrayList<Integer>>();
116                 ArrayList<Integer> list = new ArrayList<Integer>();
117                 list.add(6);
118                 list.add(7);
119                 collisionConfusionResult.put("cellId", list);
120
121                 Boolean result = childThreadUtils.triggerOrWait(collisionConfusionResult);
122                 assertTrue(result);
123                 Map<String, ArrayList<Integer>> collisionConfusionResult1 = new HashMap<String, ArrayList<Integer>>();
124
125                 ArrayList<Integer> list1 = new ArrayList<Integer>();
126                 list1.add(1);
127                 list1.add(2);
128                 collisionConfusionResult1.put("cell1", list1);
129                 result = childThreadUtils.triggerOrWait(collisionConfusionResult1);
130                 assertFalse(result);
131
132         }
133
134         @Test
135         public void getNotificationStringTest() {
136
137                 String policy_notif = readFromFile("/policy_notification.json");
138                 PolicyNotification expected = new PolicyNotification();
139                 ObjectMapper mapper = new ObjectMapper();
140
141                 try {
142                         expected = mapper.readValue(policy_notif, PolicyNotification.class);
143                 } catch (IOException e) {
144                         e.printStackTrace();
145                 }
146
147                 String pnfName = "ncserver23";
148                 List<CellPciPair> cellPciPairs = new ArrayList<>();
149
150                 cellPciPairs.add(new CellPciPair("Chn0330", 6));
151                 cellPciPairs.add(new CellPciPair("Chn0331", 7));
152                 String requestId = "a4130fd5-2291-4a83-8992-04e4c9f32731";
153                 Long alarmStart = Long.parseLong("1542445563201");
154
155                 String result = childThreadUtils.getNotificationString(pnfName, requestId, "payloadString", alarmStart, "action");
156                 PolicyNotification actual = new PolicyNotification();
157                 try {
158                         actual = mapper.readValue(result, PolicyNotification.class);
159                 } catch (IOException e) {
160                         e.printStackTrace();
161                 }
162                 System.out.println("actual :; "+result+"\nexp "+policy_notif);
163                 Assert.assertEquals(expected.hashCode(), actual.hashCode());
164         }
165         
166         @Test
167         public void sendToPolicyTest() throws ConfigDbNotFoundException {
168             
169             PowerMockito.mockStatic(SdnrRestClient.class);
170             String asyncRspBodyString = readFromFile("/AsyncRespBody.json");
171             ObjectMapper mapper = new ObjectMapper();
172             AsyncResponseBody async = new AsyncResponseBody ();
173         try {
174             async = mapper.readValue(asyncRspBodyString, AsyncResponseBody.class);
175         } catch (Exception e) {
176             e.printStackTrace();
177         }
178         PowerMockito.when(SdnrRestClient.getPci(Mockito.anyString())).thenReturn(3);
179         PowerMockito.when(SdnrRestClient.getPnfName(Mockito.anyString())).thenReturn("pnfName");
180         when(policyDmaapClient.sendNotificationToPolicy(Mockito.anyString())).thenReturn(true);
181         Map<String,List<CellPciPair>> pnfsMap = new HashMap<String,List<CellPciPair>>();
182         CellPciPair cell1 = new CellPciPair("cell0", 1);
183         CellPciPair cell2 = new CellPciPair("cell1", 2);
184         CellPciPair cell3 = new CellPciPair("cell2", 3);
185         List<CellPciPair> pciPairList = new ArrayList<>();
186         pciPairList.add(cell1);
187         pciPairList.add(cell2);
188         pciPairList.add(cell3);
189         pnfsMap.put("pnf1", pciPairList);
190         when(pnfUtils.getPnfs(async.getSolutions())).thenReturn(pnfsMap);
191         List<String> remNeighbors = new ArrayList<>();
192         remNeighbors.add("EXP006");
193         Map<String,List<String>> cellRemNeighborsPair = new HashMap<>();
194         cellRemNeighborsPair.put("EXP003", remNeighbors);
195         List<Map<String,List<String>>> list = new ArrayList<>();
196         list.add(cellRemNeighborsPair);
197         Map<String, List<Map<String,List<String>>>> expected = new HashMap<>();
198         expected.put("pnfName", list);
199         when(pnfUtils.getPnfsForAnrSolutions(async.getSolutions().getAnrSolutions())).thenReturn(expected);
200         assertTrue(childThreadUtils2.sendToPolicy(async));
201         
202         }
203
204         private static String readFromFile(String file) {
205                 String content = new String();
206                 try {
207
208                         InputStream is = ClusterUtilsTest.class.getResourceAsStream(file);
209                         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
210                         content = bufferedReader.readLine();
211                         String temp;
212                         while ((temp = bufferedReader.readLine()) != null) {
213                                 content = content.concat(temp);
214                         }
215                         content = content.trim();
216                         bufferedReader.close();
217                 } catch (Exception e) {
218                         e.printStackTrace();
219                         content = null;
220                 }
221                 return content;
222         }
223 }