aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/dcaegen2/services/sonhms/child/TestChildThreadUtils.java
blob: 92b2da1e277be82dcddd8c609a415094a2bbd050 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*******************************************************************************
 *  ============LICENSE_START=======================================================
 *  son-handler
 *  ================================================================================
 *   Copyright (C) 2019 Wipro Limited.
 *   ==============================================================================
 *     Licensed under the Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at
 *  
 *          http://www.apache.org/licenses/LICENSE-2.0
 *  
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
 *     ============LICENSE_END=========================================================
 *  
 *******************************************************************************/

package org.onap.dcaegen2.services.sonhms.child;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.databind.ObjectMapper;

import fj.data.Either;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.onap.dcaegen2.services.sonhms.ConfigPolicy;
import org.onap.dcaegen2.services.sonhms.Configuration;
import org.onap.dcaegen2.services.sonhms.HoMetricsComponent;
import org.onap.dcaegen2.services.sonhms.dao.HandOverMetricsRepository;
import org.onap.dcaegen2.services.sonhms.dao.SonRequestsRepository;
import org.onap.dcaegen2.services.sonhms.dmaap.PolicyDmaapClient;
import org.onap.dcaegen2.services.sonhms.entity.SonRequests;
import org.onap.dcaegen2.services.sonhms.exceptions.ConfigDbNotFoundException;
import org.onap.dcaegen2.services.sonhms.model.CellPciPair;
import org.onap.dcaegen2.services.sonhms.model.HoDetails;
import org.onap.dcaegen2.services.sonhms.model.PolicyNotification;
import org.onap.dcaegen2.services.sonhms.restclient.AsyncResponseBody;
import org.onap.dcaegen2.services.sonhms.restclient.SdnrRestClient;
import org.onap.dcaegen2.services.sonhms.utils.BeanUtil;
import org.onap.dcaegen2.services.sonhms.utils.ClusterUtilsTest;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@PrepareForTest({ BeanUtil.class, SdnrRestClient.class })
@SpringBootTest(classes = TestChildThreadUtils.class)
public class TestChildThreadUtils {

	ChildThreadUtils childThreadUtils;
	@Mock
	private SonRequestsRepository repository;

	@Mock
	private PnfUtils pnfUtils;
	@Mock
	private PolicyDmaapClient policyDmaapClient;
	
	@Mock
	private HandOverMetricsRepository hoMetricRepository;
	
	@Mock
	private HoMetricsComponent hoMetricsComponent;
	
	@InjectMocks
	private ChildThreadUtils childThreadUtils2;

	@Before
	public void setup() {

		ConfigPolicy configPolicy = ConfigPolicy.getInstance();
		Configuration config = Configuration.getInstance();
		config.setMinCollision(5);
		config.setMinConfusion(5);
		Map<String, Object> configPolicyMap = new HashMap<>();
		configPolicyMap.put("PCI_MODCONFIG_POLICY_NAME", "ControlLoop-vPCI-fb41f388-a5f2-11e8-98d0-529269fb1459");
		configPolicy.setConfig(configPolicyMap);
		childThreadUtils = new ChildThreadUtils(configPolicy, pnfUtils,  policyDmaapClient, hoMetricsComponent);
		MockitoAnnotations.initMocks(this);
		
	}

	@Test
	public void savePciRequestTest() {
		SonRequests sonRequest = new SonRequests();
		sonRequest.setTransactionId("transactionId");
		sonRequest.setChildThreadId(1L);
		PowerMockito.mockStatic(BeanUtil.class);
		PowerMockito.when(BeanUtil.getBean(SonRequestsRepository.class))
				.thenReturn(repository);
		when(repository.save(sonRequest)).thenReturn(sonRequest);
		childThreadUtils2.saveRequest("transactionId",1L);;
		assertEquals(sonRequest, repository.save(sonRequest));
	}

	@Test
	public void triggerOrWaitTest() {
		Map<String, ArrayList<Integer>> collisionConfusionResult = new HashMap<String, ArrayList<Integer>>();
		ArrayList<Integer> list = new ArrayList<Integer>();
		list.add(6);
		list.add(7);
		collisionConfusionResult.put("cellId", list);

		Boolean result = childThreadUtils.triggerOrWait(collisionConfusionResult);
		assertTrue(result);
		Map<String, ArrayList<Integer>> collisionConfusionResult1 = new HashMap<String, ArrayList<Integer>>();

		ArrayList<Integer> list1 = new ArrayList<Integer>();
		list1.add(1);
		list1.add(2);
		collisionConfusionResult1.put("cell1", list1);
		result = childThreadUtils.triggerOrWait(collisionConfusionResult1);
		assertFalse(result);

	}

	@Test
	public void getNotificationStringTest() {

		String policy_notif = readFromFile("/policy_notification.json");
		PolicyNotification expected = new PolicyNotification();
		ObjectMapper mapper = new ObjectMapper();

		try {
			expected = mapper.readValue(policy_notif, PolicyNotification.class);
		} catch (IOException e) {
			e.printStackTrace();
		}

		String pnfName = "ncserver23";
		List<CellPciPair> cellPciPairs = new ArrayList<>();

		cellPciPairs.add(new CellPciPair("Chn0330", 6));
		cellPciPairs.add(new CellPciPair("Chn0331", 7));
		String requestId = "a4130fd5-2291-4a83-8992-04e4c9f32731";
		Long alarmStart = Long.parseLong("1542445563201");

		String result = childThreadUtils.getNotificationString(pnfName, requestId, "payloadString", alarmStart, "action");
		PolicyNotification actual = new PolicyNotification();
		try {
			actual = mapper.readValue(result, PolicyNotification.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("actual :; "+result+"\nexp "+policy_notif);
		Assert.assertEquals(expected.hashCode(), actual.hashCode());
	}
	
	@Test
	public void sendToPolicyTest() throws ConfigDbNotFoundException {
	    
	    PowerMockito.mockStatic(SdnrRestClient.class);
	    PowerMockito.mockStatic(BeanUtil.class);

	    String asyncRspBodyString = readFromFile("/AsyncRespBody.json");
	    ObjectMapper mapper = new ObjectMapper();
	    AsyncResponseBody async = new AsyncResponseBody ();
        try {
            async = mapper.readValue(asyncRspBodyString, AsyncResponseBody.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PowerMockito.when(SdnrRestClient.getPci(Mockito.anyString())).thenReturn(3);
        PowerMockito.when(SdnrRestClient.getPnfName(Mockito.anyString())).thenReturn("pnfName");
        when(policyDmaapClient.sendNotificationToPolicy(Mockito.anyString())).thenReturn(true);
        Map<String,List<CellPciPair>> pnfsMap = new HashMap<String,List<CellPciPair>>();
        CellPciPair cell1 = new CellPciPair("cell0", 1);
        CellPciPair cell2 = new CellPciPair("cell1", 2);
        CellPciPair cell3 = new CellPciPair("cell2", 3);
        List<CellPciPair> pciPairList = new ArrayList<>();
        pciPairList.add(cell1);
        pciPairList.add(cell2);
        pciPairList.add(cell3);
        pnfsMap.put("pnf1", pciPairList);
        when(policyDmaapClient.handlePolicyResponse(Mockito.anyString())).thenReturn(true);
        when(pnfUtils.getPnfs(async.getSolutions())).thenReturn(pnfsMap);
        List<String> remNeighbors = new ArrayList<>();
        remNeighbors.add("EXP006");
        Map<String,List<String>> cellRemNeighborsPair = new HashMap<>();
        cellRemNeighborsPair.put("EXP003", remNeighbors);
        List<Map<String,List<String>>> list = new ArrayList<>();
        list.add(cellRemNeighborsPair);
        Map<String, List<Map<String,List<String>>>> expected = new HashMap<>();
        expected.put("pnfName", list);
        when(pnfUtils.getPnfsForAnrSolutions(async.getSolutions().getAnrSolutions())).thenReturn(expected);
        HoDetails hoDetails = new HoDetails();
        hoDetails.setDstCellId("EXP006");
        List<HoDetails> hoDetailsList = new ArrayList<>();
        hoDetailsList.add(hoDetails);
        Either<List<HoDetails>, Integer> hoMetrics = Either.left(hoDetailsList);
        when(hoMetricsComponent.getHoMetrics(Mockito.anyString())).thenReturn(hoMetrics);
        when(hoMetricsComponent.update(Mockito.anyString(), Mockito.anyString())).thenReturn(true);
        assertTrue(childThreadUtils2.sendToPolicy(async));
        
	}

	private static String readFromFile(String file) {
		String content = new String();
		try {

			InputStream is = ClusterUtilsTest.class.getResourceAsStream(file);
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
			content = bufferedReader.readLine();
			String temp;
			while ((temp = bufferedReader.readLine()) != null) {
				content = content.concat(temp);
			}
			content = content.trim();
			bufferedReader.close();
		} catch (Exception e) {
			e.printStackTrace();
			content = null;
		}
		return content;
	}
}