Fix bugs and formatting issues
[dcaegen2/services/son-handler.git] / src / test / java / org / onap / dcaegen2 / services / sonhms / restclient / SdnrRestClientTest.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  son-handler
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
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
22 package org.onap.dcaegen2.services.sonhms.restclient;
23
24 import static org.junit.Assert.assertEquals;
25
26 import java.util.ArrayList;
27 import java.util.List;
28
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;
50
51 @RunWith(PowerMockRunner.class)
52 @PowerMockRunnerDelegate(SpringRunner.class)
53 @PrepareForTest({ SonHandlerRestTemplate.class,Configuration.class })
54 @SpringBootTest(classes = SdnrRestClientTest.class)
55 public class SdnrRestClientTest {
56             
57     
58     
59     Configuration configuration = Configuration.getInstance();
60     private static final Logger log = org.slf4j.LoggerFactory.getLogger(SdnrRestClient.class);
61     @Before
62     public void setup() {
63         MockitoAnnotations.initMocks(this);
64     }
65     
66     
67     @Test
68         public void getNbrListTest() {
69         
70         String responseBody="{\n" + 
71                 "  \"cellId\": \"string\",\n" + 
72                 "  \"nbrList\": [\n" + 
73                 "    {\n" + 
74                 "      \"ho\": true,\n" + 
75                 "      \"pciValue\": 0,\n" + 
76                 "      \"targetCellId\": \"string\"\n" + 
77                 "    }\n" + 
78                 "  ]\n" + 
79                 "}";
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));
85                 try {
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"));
95                         nbrList.add(cell);
96                     }
97                 }
98                         assertEquals(nbrList,result);
99                 } catch (ConfigDbNotFoundException e) {
100                         log.debug("ConfigDbNotFoundException {}",e.toString());;
101                 }
102                 
103        }
104     @Test
105         public void getPciTest() {
106         
107         String responseBody="{\n" + 
108                         "  \"attribute-name\": \"string\",\n" + 
109                         "  \"value\": 0\n" + 
110                         "}";
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));
116                 try {
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());;
123                 }
124                 
125        }
126     @Test
127         public void getPnfNameTest() {
128         
129         String responseBody="{\n" + 
130                         "  \"attribute-name\": \"string\",\n" + 
131                         "  \"value\": \"string\"\n" + 
132                         "}";
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));
138                 try {
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());;
145                 }
146                 
147        }
148 }