Modify ANR Payload aligned to A1 schema in SDNR
[dcaegen2/services/son-handler.git] / src / test / java / org / onap / dcaegen2 / services / sonhms / restclient / CpsClientTest.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  son-handler
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
10  *
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  *
20  *******************************************************************************/
21 package org.onap.dcaegen2.services.sonhms.restclient;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
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;
50
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 {
57
58     CpsClient cps = new CpsClient();
59     Configuration configuration = Configuration.getInstance();
60     private static final Logger log = org.slf4j.LoggerFactory.getLogger(CpsClient.class);
61
62     @Before
63     public void setup() {
64         MockitoAnnotations.initMocks(this);
65     }
66
67     @Test
68     public void getNbrListTest() {
69
70         String responseBody = "[{\"idNRCellRelation\":\"cell1\",\"attributes\":{\"nRTCI\":\"cell1\",\"nRPCI\":1,\"isHOAllowed\":true}},{\"idNRCellRelation\":\"cell2\",\"attributes\":{\"nRTCI\":\"cell2\",\"nRPCI\":2,\"isHOAllowed\":true}}]";
71
72         System.out.println(responseBody);
73         PowerMockito.mockStatic(SonHandlerRestTemplate.class);
74         PowerMockito.mockStatic(Configuration.class);
75         PowerMockito.when(Configuration.getInstance()).thenReturn(configuration);
76         PowerMockito
77                 .when(SonHandlerRestTemplate.sendPostRequest(Mockito.anyString(), Mockito.anyString(),
78                         Matchers.<ParameterizedTypeReference<String>>any()))
79                 .thenReturn(ResponseEntity.ok(responseBody));
80         try {
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"));
92                     nbrList.add(cell);
93                 }
94             }
95             assertEquals(nbrList, result);
96         } catch (Exception e) {
97             log.debug("CpsNotFoundException {}", e.toString());
98             ;
99         }
100
101     }
102
103     @Test
104     public void getPciTest() {
105
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);
110         PowerMockito
111                 .when(SonHandlerRestTemplate.sendPostRequest(Mockito.anyString(), Mockito.anyString(),
112                         Matchers.<ParameterizedTypeReference<String>>any()))
113                 .thenReturn(ResponseEntity.ok(responseBody));
114
115         try {
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);
121             }
122         } catch (CpsNotFoundException e) {
123             log.debug("CpsNotFoundException {}", e.toString());
124             ;
125         }
126
127     }
128
129     @Test
130     public void getPnfNameTest() {
131
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);
136         PowerMockito
137                 .when(SonHandlerRestTemplate.sendPostRequest(Mockito.anyString(), Mockito.anyString(),
138                         Matchers.<ParameterizedTypeReference<String>>any()))
139                 .thenReturn(ResponseEntity.ok(responseBody));
140         try {
141             String result = cps.getPnfName("110");
142             String response = ResponseEntity.ok(responseBody).getBody();
143             
144             JSONArray requestArray = new JSONArray(response);
145             for (int i=0;i<requestArray.length();i++) {
146                 assertEquals(requestArray.getJSONObject(i).getString("idGNBDUFunction"), result);
147             }
148         } catch (CpsNotFoundException e) {
149             log.debug("CpsNotFoundException {}", e.toString());
150             ;
151         }
152     }
153
154     @Test
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);
160         PowerMockito
161              .when(SonHandlerRestTemplate.sendPostRequest(Mockito.anyString(), Mockito.anyString(),
162                     Matchers.<ParameterizedTypeReference<String>>any()))
163              .thenReturn(ResponseEntity.ok(responseBody));
164         try {
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);
170            }
171         } catch (CpsNotFoundException e) {
172            log.debug("CpsNotFoundException {}", e.toString());
173         }
174     }
175
176
177
178     @Test
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);
184         PowerMockito
185                 .when(SonHandlerRestTemplate.sendPostRequest(Mockito.anyString(), Mockito.anyString(),
186                         Matchers.<ParameterizedTypeReference<String>>any()))
187                 .thenReturn(ResponseEntity.ok(responseBody));
188         try {
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());
195
196         }
197
198     }
199
200 }