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.child;
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;
29 import com.fasterxml.jackson.databind.ObjectMapper;
31 import fj.data.Either;
33 import java.io.BufferedReader;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.io.InputStreamReader;
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.List;
42 import org.junit.Assert;
43 import org.junit.Before;
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.ConfigPolicy;
51 import org.onap.dcaegen2.services.sonhms.Configuration;
52 import org.onap.dcaegen2.services.sonhms.HoMetricsComponent;
53 import org.onap.dcaegen2.services.sonhms.dao.HandOverMetricsRepository;
54 import org.onap.dcaegen2.services.sonhms.dao.SonRequestsRepository;
55 import org.onap.dcaegen2.services.sonhms.dmaap.PolicyDmaapClient;
56 import org.onap.dcaegen2.services.sonhms.entity.SonRequests;
57 import org.onap.dcaegen2.services.sonhms.exceptions.ConfigDbNotFoundException;
58 import org.onap.dcaegen2.services.sonhms.model.CellPciPair;
59 import org.onap.dcaegen2.services.sonhms.model.HoDetails;
60 import org.onap.dcaegen2.services.sonhms.model.PolicyNotification;
61 import org.onap.dcaegen2.services.sonhms.restclient.AsyncResponseBody;
62 import org.onap.dcaegen2.services.sonhms.restclient.SdnrRestClient;
63 import org.onap.dcaegen2.services.sonhms.utils.BeanUtil;
64 import org.onap.dcaegen2.services.sonhms.utils.ClusterUtilsTest;
65 import org.powermock.api.mockito.PowerMockito;
66 import org.powermock.core.classloader.annotations.PrepareForTest;
67 import org.powermock.modules.junit4.PowerMockRunner;
68 import org.powermock.modules.junit4.PowerMockRunnerDelegate;
69 import org.springframework.boot.test.context.SpringBootTest;
70 import org.springframework.test.context.junit4.SpringRunner;
72 @RunWith(PowerMockRunner.class)
73 @PowerMockRunnerDelegate(SpringRunner.class)
74 @PrepareForTest({ BeanUtil.class, SdnrRestClient.class })
75 @SpringBootTest(classes = TestChildThreadUtils.class)
76 public class TestChildThreadUtils {
78 ChildThreadUtils childThreadUtils;
80 private SonRequestsRepository repository;
83 private PnfUtils pnfUtils;
85 private PolicyDmaapClient policyDmaapClient;
88 private HandOverMetricsRepository hoMetricRepository;
91 private HoMetricsComponent hoMetricsComponent;
94 private ChildThreadUtils childThreadUtils2;
99 ConfigPolicy configPolicy = ConfigPolicy.getInstance();
100 Configuration config = Configuration.getInstance();
101 config.setMinCollision(5);
102 config.setMinConfusion(5);
103 Map<String, Object> configPolicyMap = new HashMap<>();
104 configPolicyMap.put("PCI_MODCONFIG_POLICY_NAME", "ControlLoop-vPCI-fb41f388-a5f2-11e8-98d0-529269fb1459");
105 configPolicy.setConfig(configPolicyMap);
106 childThreadUtils = new ChildThreadUtils(configPolicy, pnfUtils, policyDmaapClient, hoMetricsComponent);
107 MockitoAnnotations.initMocks(this);
112 public void savePciRequestTest() {
113 SonRequests sonRequest = new SonRequests();
114 sonRequest.setTransactionId("transactionId");
115 sonRequest.setChildThreadId(1L);
116 PowerMockito.mockStatic(BeanUtil.class);
117 PowerMockito.when(BeanUtil.getBean(SonRequestsRepository.class))
118 .thenReturn(repository);
119 when(repository.save(sonRequest)).thenReturn(sonRequest);
120 childThreadUtils2.saveRequest("transactionId",1L);;
121 assertEquals(sonRequest, repository.save(sonRequest));
125 public void triggerOrWaitTest() {
126 Map<String, ArrayList<Integer>> collisionConfusionResult = new HashMap<String, ArrayList<Integer>>();
127 ArrayList<Integer> list = new ArrayList<Integer>();
130 collisionConfusionResult.put("cellId", list);
132 Boolean result = childThreadUtils.triggerOrWait(collisionConfusionResult);
134 Map<String, ArrayList<Integer>> collisionConfusionResult1 = new HashMap<String, ArrayList<Integer>>();
136 ArrayList<Integer> list1 = new ArrayList<Integer>();
139 collisionConfusionResult1.put("cell1", list1);
140 result = childThreadUtils.triggerOrWait(collisionConfusionResult1);
146 public void getNotificationStringTest() {
148 String policy_notif = readFromFile("/policy_notification.json");
149 PolicyNotification expected = new PolicyNotification();
150 ObjectMapper mapper = new ObjectMapper();
153 expected = mapper.readValue(policy_notif, PolicyNotification.class);
154 } catch (IOException e) {
158 String pnfName = "ncserver23";
159 List<CellPciPair> cellPciPairs = new ArrayList<>();
161 cellPciPairs.add(new CellPciPair("Chn0330", 6));
162 cellPciPairs.add(new CellPciPair("Chn0331", 7));
163 String requestId = "a4130fd5-2291-4a83-8992-04e4c9f32731";
164 Long alarmStart = Long.parseLong("1542445563201");
166 String result = childThreadUtils.getNotificationString(pnfName, requestId, "payloadString", alarmStart, "ModifyConfig");
167 PolicyNotification actual = new PolicyNotification();
169 actual = mapper.readValue(result, PolicyNotification.class);
170 } catch (IOException e) {
173 System.out.println("actual :; "+result+"\nexp "+policy_notif);
174 Assert.assertEquals(expected.hashCode(), actual.hashCode());
178 public void sendToPolicyTest() throws ConfigDbNotFoundException {
180 PowerMockito.mockStatic(SdnrRestClient.class);
181 PowerMockito.mockStatic(BeanUtil.class);
183 String asyncRspBodyString = readFromFile("/AsyncRespBody.json");
184 ObjectMapper mapper = new ObjectMapper();
185 AsyncResponseBody async = new AsyncResponseBody ();
187 async = mapper.readValue(asyncRspBodyString, AsyncResponseBody.class);
188 } catch (Exception e) {
191 PowerMockito.when(SdnrRestClient.getPci(Mockito.anyString())).thenReturn(3);
192 PowerMockito.when(SdnrRestClient.getPnfName(Mockito.anyString())).thenReturn("pnfName");
193 when(policyDmaapClient.sendNotificationToPolicy(Mockito.anyString())).thenReturn(true);
194 Map<String,List<CellPciPair>> pnfsMap = new HashMap<String,List<CellPciPair>>();
195 CellPciPair cell1 = new CellPciPair("cell0", 1);
196 CellPciPair cell2 = new CellPciPair("cell1", 2);
197 CellPciPair cell3 = new CellPciPair("cell2", 3);
198 List<CellPciPair> pciPairList = new ArrayList<>();
199 pciPairList.add(cell1);
200 pciPairList.add(cell2);
201 pciPairList.add(cell3);
202 pnfsMap.put("pnf1", pciPairList);
203 when(policyDmaapClient.handlePolicyResponse(Mockito.anyString())).thenReturn(true);
204 when(pnfUtils.getPnfs(async.getSolutions())).thenReturn(pnfsMap);
205 List<String> remNeighbors = new ArrayList<>();
206 remNeighbors.add("EXP006");
207 Map<String,List<String>> cellRemNeighborsPair = new HashMap<>();
208 cellRemNeighborsPair.put("EXP003", remNeighbors);
209 List<Map<String,List<String>>> list = new ArrayList<>();
210 list.add(cellRemNeighborsPair);
211 Map<String, List<Map<String,List<String>>>> expected = new HashMap<>();
212 expected.put("pnfName", list);
213 when(pnfUtils.getPnfsForAnrSolutions(async.getSolutions().getAnrSolutions())).thenReturn(expected);
214 HoDetails hoDetails = new HoDetails();
215 hoDetails.setDstCellId("EXP006");
216 List<HoDetails> hoDetailsList = new ArrayList<>();
217 hoDetailsList.add(hoDetails);
218 Either<List<HoDetails>, Integer> hoMetrics = Either.left(hoDetailsList);
219 when(hoMetricsComponent.getHoMetrics(Mockito.anyString())).thenReturn(hoMetrics);
220 when(hoMetricsComponent.update(Mockito.anyString(), Mockito.anyString())).thenReturn(true);
221 assertTrue(childThreadUtils2.sendToPolicy(async));
225 private static String readFromFile(String file) {
226 String content = new String();
229 InputStream is = ClusterUtilsTest.class.getResourceAsStream(file);
230 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
231 content = bufferedReader.readLine();
233 while ((temp = bufferedReader.readLine()) != null) {
234 content = content.concat(temp);
236 content = content.trim();
237 bufferedReader.close();
238 } catch (Exception e) {