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.restclient;
24 import static org.junit.Assert.assertEquals;
26 import java.util.ArrayList;
27 import java.util.List;
29 import org.json.JSONArray;
30 import org.json.JSONObject;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Matchers;
35 import org.mockito.Mockito;
36 import org.mockito.MockitoAnnotations;
37 import org.onap.dcaegen2.services.sonhms.Configuration;
38 import org.onap.dcaegen2.services.sonhms.exceptions.ConfigDbNotFoundException;
39 import org.onap.dcaegen2.services.sonhms.model.CellPciPair;
40 import org.onap.dcaegen2.services.sonhms.utils.SonHandlerRestTemplate;
41 import org.powermock.api.mockito.PowerMockito;
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 @PowerMockRunnerDelegate(SpringRunner.class)
53 @PrepareForTest({ SonHandlerRestTemplate.class,Configuration.class })
54 @SpringBootTest(classes = SdnrRestClientTest.class)
55 public class SdnrRestClientTest {
59 Configuration configuration = Configuration.getInstance();
60 private static final Logger log = org.slf4j.LoggerFactory.getLogger(SdnrRestClient.class);
63 MockitoAnnotations.initMocks(this);
68 public void getNbrListTest() {
70 String responseBody="{\n" +
71 " \"cellId\": \"string\",\n" +
75 " \"pciValue\": 0,\n" +
76 " \"targetCellId\": \"string\"\n" +
80 PowerMockito.mockStatic(SonHandlerRestTemplate.class);
81 PowerMockito.mockStatic(Configuration.class);
82 PowerMockito.when(Configuration.getInstance()).thenReturn(configuration);
83 PowerMockito.when(SonHandlerRestTemplate.sendGetRequest(Mockito.anyString(),Matchers.<ParameterizedTypeReference<String>>any()))
84 .thenReturn(ResponseEntity.ok(responseBody));
86 List<CellPciPair> result=SdnrRestClient.getNbrList("1");
87 List<CellPciPair> nbrList = new ArrayList<>();
88 String response=ResponseEntity.ok(responseBody).getBody();
89 JSONObject responseJson = new JSONObject(response);
90 JSONArray nbrListObj = responseJson.getJSONArray("nbrList");
91 for (int i = 0; i < nbrListObj.length(); i++) {
92 JSONObject cellObj = nbrListObj.getJSONObject(i);
93 if (cellObj.getBoolean("ho")) {
94 CellPciPair cell = new CellPciPair(cellObj.getString("targetCellId"), cellObj.getInt("pciValue"));
98 assertEquals(nbrList,result);
99 } catch (ConfigDbNotFoundException e) {
100 log.debug("ConfigDbNotFoundException {}",e.toString());;
105 public void getPciTest() {
107 String responseBody="{\n" +
108 " \"attribute-name\": \"string\",\n" +
111 PowerMockito.mockStatic(SonHandlerRestTemplate.class);
112 PowerMockito.mockStatic(Configuration.class);
113 PowerMockito.when(Configuration.getInstance()).thenReturn(configuration);
114 PowerMockito.when(SonHandlerRestTemplate.sendGetRequest(Mockito.anyString(),Matchers.<ParameterizedTypeReference<String>>any()))
115 .thenReturn(ResponseEntity.ok(responseBody));
117 int result=SdnrRestClient.getPci("1");
118 String response=ResponseEntity.ok(responseBody).getBody();
119 JSONObject respObj = new JSONObject(response);
120 assertEquals(respObj.getInt("value"),result);
121 } catch (ConfigDbNotFoundException e) {
122 log.debug("ConfigDbNotFoundException {}",e.toString());;
127 public void getPnfNameTest() {
129 String responseBody="{\n" +
130 " \"attribute-name\": \"string\",\n" +
131 " \"value\": \"string\"\n" +
133 PowerMockito.mockStatic(SonHandlerRestTemplate.class);
134 PowerMockito.mockStatic(Configuration.class);
135 PowerMockito.when(Configuration.getInstance()).thenReturn(configuration);
136 PowerMockito.when(SonHandlerRestTemplate.sendGetRequest(Mockito.anyString(),Matchers.<ParameterizedTypeReference<String>>any()))
137 .thenReturn(ResponseEntity.ok(responseBody));
139 String result=SdnrRestClient.getPnfName("1");
140 String response=ResponseEntity.ok(responseBody).getBody();
141 JSONObject respObj = new JSONObject(response);
142 assertEquals(respObj.getString("value"),result);
143 } catch (ConfigDbNotFoundException e) {
144 log.debug("ConfigDbNotFoundException {}",e.toString());;