1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021-2022 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 *******************************************************************************/
21 package org.onap.dcaegen2.services.sonhms.restclient;
23 import static org.junit.Assert.assertEquals;
25 import java.util.ArrayList;
26 import java.util.List;
28 import org.json.JSONArray;
29 import org.json.JSONObject;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Matchers;
34 import org.mockito.Mockito;
35 import org.mockito.MockitoAnnotations;
36 import org.onap.dcaegen2.services.sonhms.Configuration;
37 import org.onap.dcaegen2.services.sonhms.exceptions.CpsNotFoundException;
38 import org.onap.dcaegen2.services.sonhms.model.CellPciPair;
39 import org.onap.dcaegen2.services.sonhms.utils.SonHandlerRestTemplate;
40 import org.powermock.api.mockito.PowerMockito;
41 import org.powermock.core.classloader.annotations.PowerMockIgnore;
42 import org.powermock.core.classloader.annotations.PrepareForTest;
43 import org.powermock.modules.junit4.PowerMockRunner;
44 import org.powermock.modules.junit4.PowerMockRunnerDelegate;
45 import org.slf4j.Logger;
46 import org.springframework.boot.test.context.SpringBootTest;
47 import org.springframework.core.ParameterizedTypeReference;
48 import org.springframework.http.ResponseEntity;
49 import org.springframework.test.context.junit4.SpringRunner;
51 @RunWith(PowerMockRunner.class)
52 @PowerMockIgnore({ "com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*" })
53 @PowerMockRunnerDelegate(SpringRunner.class)
54 @PrepareForTest({ SonHandlerRestTemplate.class, Configuration.class })
55 @SpringBootTest(classes = CpsClientTest.class)
56 public class CpsClientTest {
58 CpsClient cps = new CpsClient();
59 Configuration configuration = Configuration.getInstance();
60 private static final Logger log = org.slf4j.LoggerFactory.getLogger(CpsClient.class);
64 MockitoAnnotations.initMocks(this);
68 public void getNbrListTest() {
70 String responseBody = "[{\"idNRCellRelation\":\"cell1\",\"attributes\":{\"nRTCI\":\"cell1\",\"nRPCI\":1,\"isHOAllowed\":true}},{\"idNRCellRelation\":\"cell2\",\"attributes\":{\"nRTCI\":\"cell2\",\"nRPCI\":2,\"isHOAllowed\":true}}]";
72 System.out.println(responseBody);
73 PowerMockito.mockStatic(SonHandlerRestTemplate.class);
74 PowerMockito.mockStatic(Configuration.class);
75 PowerMockito.when(Configuration.getInstance()).thenReturn(configuration);
77 .when(SonHandlerRestTemplate.sendPostRequest(Mockito.anyString(), Mockito.anyString(),
78 Matchers.<ParameterizedTypeReference<String>>any()))
79 .thenReturn(ResponseEntity.ok(responseBody));
81 List<CellPciPair> result = cps.getNbrList("1");
82 List<CellPciPair> nbrList = new ArrayList<>();
83 String response = ResponseEntity.ok(responseBody).getBody();
84 System.out.println("response" + response);
85 JSONArray nbrListObj = new JSONArray(response);
86 System.out.println(nbrListObj);
87 for (int i = 0; i < nbrListObj.length(); i++) {
88 JSONObject cellObj = nbrListObj.getJSONObject(i);
89 JSONObject obj = cellObj.getJSONObject("attributes");
90 if (obj.getBoolean("isHOAllowed")) {
91 CellPciPair cell = new CellPciPair(obj.getString("nRTCI"), obj.getInt("nRPCI"));
95 assertEquals(nbrList, result);
96 } catch (Exception e) {
97 log.debug("CpsNotFoundException {}", e.toString());
104 public void getPciTest() {
106 String responseBody = "[{\n" + " \"nRPCI\": \"11\",\n" + " \"value\": 0\n" + "}]";
107 PowerMockito.mockStatic(SonHandlerRestTemplate.class);
108 PowerMockito.mockStatic(Configuration.class);
109 PowerMockito.when(Configuration.getInstance()).thenReturn(configuration);
111 .when(SonHandlerRestTemplate.sendPostRequest(Mockito.anyString(), Mockito.anyString(),
112 Matchers.<ParameterizedTypeReference<String>>any()))
113 .thenReturn(ResponseEntity.ok(responseBody));
116 int result = cps.getPci("1");
117 String response = ResponseEntity.ok(responseBody).getBody();
118 JSONArray requestArray = new JSONArray(response);
119 for (int i=0;i<requestArray.length();i++) {
120 assertEquals(requestArray.getJSONObject(i).getInt("nRPCI"), result);
122 } catch (CpsNotFoundException e) {
123 log.debug("CpsNotFoundException {}", e.toString());
130 public void getPnfNameTest() {
132 String responseBody = "[{\n" + " \"idGNBDUFunction\": \"110\",\n" + " \"value\": \"string\"\n" + "}]";
133 PowerMockito.mockStatic(SonHandlerRestTemplate.class);
134 PowerMockito.mockStatic(Configuration.class);
135 PowerMockito.when(Configuration.getInstance()).thenReturn(configuration);
137 .when(SonHandlerRestTemplate.sendPostRequest(Mockito.anyString(), Mockito.anyString(),
138 Matchers.<ParameterizedTypeReference<String>>any()))
139 .thenReturn(ResponseEntity.ok(responseBody));
141 String result = cps.getPnfName("110");
142 String response = ResponseEntity.ok(responseBody).getBody();
144 JSONArray requestArray = new JSONArray(response);
145 for (int i=0;i<requestArray.length();i++) {
146 assertEquals(requestArray.getJSONObject(i).getString("idGNBDUFunction"), result);
148 } catch (CpsNotFoundException e) {
149 log.debug("CpsNotFoundException {}", e.toString());
155 public void getRicIdTest() {
156 String responseBody = "[{\"idNearRTRIC\":\"22\"}]";
157 PowerMockito.mockStatic(SonHandlerRestTemplate.class);
158 PowerMockito.mockStatic(Configuration.class);
159 PowerMockito.when(Configuration.getInstance()).thenReturn(configuration);
161 .when(SonHandlerRestTemplate.sendPostRequest(Mockito.anyString(), Mockito.anyString(),
162 Matchers.<ParameterizedTypeReference<String>>any()))
163 .thenReturn(ResponseEntity.ok(responseBody));
165 String result = cps.getRicId("1");
166 String response = ResponseEntity.ok(responseBody).getBody();
167 JSONArray requestArray = new JSONArray(response);
168 for (int i=0;i<requestArray.length();i++) {
169 assertEquals(requestArray.getJSONObject(i).getString("idNearRTRIC"), result);
171 } catch (CpsNotFoundException e) {
172 log.debug("CpsNotFoundException {}", e.toString());
179 public void getCellData() {
180 String responseBody = "{\"networkId\":\"netw1000\"}";
181 PowerMockito.mockStatic(SonHandlerRestTemplate.class);
182 PowerMockito.mockStatic(Configuration.class);
183 PowerMockito.when(Configuration.getInstance()).thenReturn(configuration);
185 .when(SonHandlerRestTemplate.sendPostRequest(Mockito.anyString(), Mockito.anyString(),
186 Matchers.<ParameterizedTypeReference<String>>any()))
187 .thenReturn(ResponseEntity.ok(responseBody));
189 JSONObject result = cps.getCellData("1");
190 String response = ResponseEntity.ok(responseBody).getBody();
191 JSONObject respObj = new JSONObject(response);
192 assertEquals(respObj.get("networkId"), result.get("networkId"));
193 } catch (CpsNotFoundException e) {
194 log.debug("CpsNotFoundException {}", e.toString());