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 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;
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;
67 @RunWith(PowerMockRunner.class)
68 @PowerMockRunnerDelegate(SpringRunner.class)
69 @PrepareForTest({ BeanUtil.class, SdnrRestClient.class })
70 @SpringBootTest(classes = TestChildThreadUtils.class)
71 public class TestChildThreadUtils {
73 ChildThreadUtils childThreadUtils;
75 private SonRequestsRepository repository;
78 private PnfUtils pnfUtils;
80 private PolicyDmaapClient policyDmaapClient;
83 private ChildThreadUtils childThreadUtils2;
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);
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));
114 public void triggerOrWaitTest() {
115 Map<String, ArrayList<Integer>> collisionConfusionResult = new HashMap<String, ArrayList<Integer>>();
116 ArrayList<Integer> list = new ArrayList<Integer>();
119 collisionConfusionResult.put("cellId", list);
121 Boolean result = childThreadUtils.triggerOrWait(collisionConfusionResult);
123 Map<String, ArrayList<Integer>> collisionConfusionResult1 = new HashMap<String, ArrayList<Integer>>();
125 ArrayList<Integer> list1 = new ArrayList<Integer>();
128 collisionConfusionResult1.put("cell1", list1);
129 result = childThreadUtils.triggerOrWait(collisionConfusionResult1);
135 public void getNotificationStringTest() {
137 String policy_notif = readFromFile("/policy_notification.json");
138 PolicyNotification expected = new PolicyNotification();
139 ObjectMapper mapper = new ObjectMapper();
142 expected = mapper.readValue(policy_notif, PolicyNotification.class);
143 } catch (IOException e) {
147 String pnfName = "ncserver23";
148 List<CellPciPair> cellPciPairs = new ArrayList<>();
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");
155 String result = childThreadUtils.getNotificationString(pnfName, requestId, "payloadString", alarmStart, "action");
156 PolicyNotification actual = new PolicyNotification();
158 actual = mapper.readValue(result, PolicyNotification.class);
159 } catch (IOException e) {
162 System.out.println("actual :; "+result+"\nexp "+policy_notif);
163 Assert.assertEquals(expected.hashCode(), actual.hashCode());
167 public void sendToPolicyTest() throws ConfigDbNotFoundException {
169 PowerMockito.mockStatic(SdnrRestClient.class);
170 String asyncRspBodyString = readFromFile("/AsyncRespBody.json");
171 ObjectMapper mapper = new ObjectMapper();
172 AsyncResponseBody async = new AsyncResponseBody ();
174 async = mapper.readValue(asyncRspBodyString, AsyncResponseBody.class);
175 } catch (Exception e) {
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(policyDmaapClient.handlePolicyResponse(Mockito.anyString())).thenReturn(true);
191 when(pnfUtils.getPnfs(async.getSolutions())).thenReturn(pnfsMap);
192 List<String> remNeighbors = new ArrayList<>();
193 remNeighbors.add("EXP006");
194 Map<String,List<String>> cellRemNeighborsPair = new HashMap<>();
195 cellRemNeighborsPair.put("EXP003", remNeighbors);
196 List<Map<String,List<String>>> list = new ArrayList<>();
197 list.add(cellRemNeighborsPair);
198 Map<String, List<Map<String,List<String>>>> expected = new HashMap<>();
199 expected.put("pnfName", list);
200 when(pnfUtils.getPnfsForAnrSolutions(async.getSolutions().getAnrSolutions())).thenReturn(expected);
201 assertTrue(childThreadUtils2.sendToPolicy(async));
205 private static String readFromFile(String file) {
206 String content = new String();
209 InputStream is = ClusterUtilsTest.class.getResourceAsStream(file);
210 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
211 content = bufferedReader.readLine();
213 while ((temp = bufferedReader.readLine()) != null) {
214 content = content.concat(temp);
216 content = content.trim();
217 bufferedReader.close();
218 } catch (Exception e) {