Test Replace Jackson with GSON
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / aai / AaiQueryTest.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.holmes.common.aai;
18 import static org.easymock.EasyMock.anyObject;
19 import static org.hamcrest.core.IsEqual.equalTo;
20 import static org.junit.Assert.assertThat;
21 import static org.powermock.api.mockito.PowerMockito.when;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.junit.runner.RunWith;
29 import org.onap.holmes.common.aai.config.AaiConfig;
30 import org.onap.holmes.common.aai.entity.VmEntity;
31 import org.onap.holmes.common.aai.entity.VnfEntity;
32 import org.onap.holmes.common.config.MicroServiceConfig;
33 import org.onap.holmes.common.exception.CorrelationException;
34 import org.onap.holmes.common.utils.HttpsUtils;
35 import org.powermock.api.easymock.PowerMock;
36 import org.powermock.api.mockito.PowerMockito;
37 import org.powermock.core.classloader.annotations.PrepareForTest;
38 import org.powermock.modules.junit4.PowerMockRunner;
39 import org.powermock.reflect.Whitebox;
40
41
42 @PrepareForTest({AaiQuery.class, HttpsUtils.class, MicroServiceConfig.class})
43 @RunWith(PowerMockRunner.class)
44 public class AaiQueryTest {
45
46     @Rule
47     public ExpectedException thrown = ExpectedException.none();
48
49     private AaiQuery aaiQuery;
50     private AaiResponseUtil aaiResponseUtil;
51
52     @Test
53     public void testAaiQuery_getAaiVnfData_ok() throws Exception {
54         PowerMock.resetAll();
55         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVnfDataResponse");
56         aaiResponseUtil = new AaiResponseUtil();
57         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
58
59         PowerMock.expectPrivate(aaiQuery, "getVnfDataResponse", "test1", "test2").andReturn("{}");
60
61         PowerMock.replayAll();
62         VnfEntity vnfEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVnfData", "test1", "test2");
63         PowerMock.verifyAll();
64
65         assertThat(vnfEntity == null, equalTo(true));
66     }
67
68     @Test
69     public void testAaiQuery_getAaiVnfData_exception() throws Exception {
70         PowerMock.resetAll();
71         thrown.expect(CorrelationException.class);
72         thrown.expectMessage("Failed to convert aai vnf response data to vnf entity");
73         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVnfDataResponse");
74         aaiResponseUtil = new AaiResponseUtil();
75         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
76         PowerMock.expectPrivate(aaiQuery, "getVnfDataResponse", "test1", "test2")
77                 .andReturn("{***}");
78
79         PowerMock.replayAll();
80         aaiQuery.getAaiVnfData("test1", "test2");
81         PowerMock.verifyAll();
82     }
83
84     @Test
85     public void testAaiQuery_getAaiVmData_ok() throws Exception {
86         PowerMock.resetAll();
87         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
88         aaiResponseUtil = new AaiResponseUtil();
89         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
90         PowerMockito.mockStatic(HttpsUtils.class);
91         Map<String, String> headers = new HashMap<>();
92         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
93         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
94         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
95         headers.put("Accept", "application/json");
96         String url = "http://10.96.33.33/api/aai-cloudInfrastructure/v11";
97         when(HttpsUtils.get(url, headers)).thenReturn("{}");
98
99         PowerMockito.mockStatic(MicroServiceConfig.class);
100         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
101
102         PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2")
103                 .andReturn("/aai/v11/cloud-infrastructure");
104         PowerMock.replayAll();
105         VmEntity vmEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
106         PowerMock.verifyAll();
107
108         assertThat(vmEntity == null, equalTo(true));
109     }
110
111
112
113
114
115     @Test
116     public void testAaiQuery_getAaiVmData_httpsutils_exception() throws Exception {
117         PowerMock.resetAll();
118         thrown.expect(CorrelationException.class);
119         thrown.expectMessage("Failed to get data from aai");
120         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
121
122         aaiResponseUtil = new AaiResponseUtil();
123         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
124
125         PowerMockito.mockStatic(HttpsUtils.class);
126         Map<String, String> headers = new HashMap<>();
127         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
128         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
129         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
130         headers.put("Accept", "application/json");
131         String url = "http://10.96.33.33/api/aai-cloudInfrastructure/v11";
132
133         when(HttpsUtils.get(url, headers)).thenThrow(new CorrelationException(""));
134
135         PowerMockito.mockStatic(MicroServiceConfig.class);
136         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
137
138         PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2")
139                 .andReturn("/aai/v11/cloud-infrastructure");
140         PowerMock.replayAll();
141         Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
142         PowerMock.verifyAll();
143     }
144
145     @Test
146     public void testAaiQuery_getVmResourceLinks_ok() throws Exception {
147         PowerMock.resetAll();
148         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResourceLinksResponse");
149
150         aaiResponseUtil = new AaiResponseUtil();
151         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
152
153         String result = "{\"result-data\":[{\"resource-type\":\"vserver\",\"resource-link\":\"le-vserver-id-val-51834\"}]}";
154
155         PowerMock.expectPrivate(aaiQuery, "getResourceLinksResponse", "test1", "test2").andReturn(result);
156         PowerMock.replayAll();
157         String resource = Whitebox.invokeMethod(aaiQuery, "getVmResourceLinks", "test1", "test2");
158         PowerMock.verifyAll();
159
160         assertThat(resource, equalTo("le-vserver-id-val-51834"));
161     }
162
163
164
165     @Test
166     public void testAaiQuery_getResourceLinksResponse() throws Exception {
167         PowerMock.resetAll();
168         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
169
170         aaiResponseUtil = new AaiResponseUtil();
171         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
172
173         PowerMockito.mockStatic(MicroServiceConfig.class);
174         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("host_url");
175
176         PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
177         PowerMock.replayAll();
178         String resource = Whitebox.invokeMethod(aaiQuery, "getResourceLinksResponse", "test1", "test2");
179         PowerMock.verifyAll();
180
181         assertThat(resource, equalTo(""));
182     }
183
184     @Test
185     public void testAaiQuery_getVnfDataResponse() throws Exception {
186         PowerMock.resetAll();
187         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
188
189         aaiResponseUtil = new AaiResponseUtil();
190         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
191
192         PowerMockito.mockStatic(MicroServiceConfig.class);
193         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("host_url");
194
195         PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
196         PowerMock.replayAll();
197         String resource = Whitebox.invokeMethod(aaiQuery, "getVnfDataResponse", "test1", "test2");
198         PowerMock.verifyAll();
199
200         assertThat(resource, equalTo(""));
201     }
202
203     @Test
204     public void testAaiQuery_getResponse_ok() throws Exception {
205         PowerMock.resetAll();
206         aaiQuery = new AaiQuery();
207         PowerMockito.mockStatic(HttpsUtils.class);
208         Map<String, String> headers = new HashMap<>();
209         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
210         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
211         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
212         headers.put("Accept", "application/json");
213         String url = "host_url";
214
215         when(HttpsUtils.get(url, headers)).thenReturn("");
216
217         PowerMock.replayAll();
218         String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
219         PowerMock.verifyAll();
220
221         assertThat(resource, equalTo(""));
222     }
223
224     @Test
225     public void testAaiQuery_getResponse_exceptioin() throws Exception {
226         PowerMock.resetAll();
227         thrown.expect(CorrelationException.class);
228         thrown.expectMessage("Failed to get data from aai");
229         aaiQuery = new AaiQuery();
230
231         PowerMockito.mockStatic(HttpsUtils.class);
232         Map<String, String> headers = new HashMap<>();
233         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
234         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
235         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
236         headers.put("Accept", "application/json");
237         String url = "host_url";
238         when(HttpsUtils.get(url, headers)).thenThrow(new CorrelationException(""));
239         PowerMock.replayAll();
240         String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
241         PowerMock.verifyAll();
242         assertThat(resource, equalTo(""));
243     }
244
245     @Test
246     public void testAaiQuery_getHeaders() throws Exception {
247         PowerMock.resetAll();
248         aaiQuery = new AaiQuery();
249         PowerMock.replayAll();
250         Map actual = Whitebox.invokeMethod(aaiQuery, "getHeaders");
251         PowerMock.verifyAll();
252
253         assertThat(actual.get("X-TransactionId"), equalTo("9999"));
254         assertThat(actual.get("X-FromAppId"), equalTo("jimmy-postman"));
255         assertThat(actual.get("Authorization"), equalTo("Basic QUFJOkFBSQ=="));
256         assertThat(actual.get("Accept"), equalTo("application/json"));
257     }
258
259     @Test
260     public void testAaiQuery_getBaseUrl_msb() throws Exception {
261         PowerMock.resetAll();
262         aaiQuery = new AaiQuery();
263
264         PowerMockito.mockStatic(MicroServiceConfig.class);
265         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
266         when(MicroServiceConfig.getServiceConfigInfoFromCBS("nihao")).thenReturn("");
267
268         PowerMock.replayAll();
269         String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "/url");
270         PowerMock.verifyAll();
271         assertThat(actual, equalTo("http://10.96.33.33/url"));
272     }
273
274     @Test
275     public void testAaiQuery_getBaseUrl_aaiurl() throws Exception {
276         PowerMock.resetAll();
277         aaiQuery = new AaiQuery();
278
279         PowerMockito.mockStatic(MicroServiceConfig.class);
280         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenThrow(new NullPointerException());
281         when(MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config")).thenReturn("aai");
282
283         PowerMock.replayAll();
284         String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "url");
285         System.out.println(actual);
286         PowerMock.verifyAll();
287         assertThat(actual, equalTo("https://aaiurl"));
288     }
289
290     @Test
291     public void testAaiQuery_getBaseUrl_exception() throws Exception {
292         PowerMock.resetAll();
293         aaiQuery = new AaiQuery();
294
295         PowerMockito.mockStatic(MicroServiceConfig.class);
296         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenThrow(new NullPointerException());
297         when(MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config"))
298                 .thenThrow(new NullPointerException());
299
300         PowerMock.replayAll();
301         String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "url");
302         System.out.println(actual);
303         PowerMock.verifyAll();
304         assertThat(actual, equalTo(""));
305     }
306
307     @Test
308     public void testAaiQuery_getMsbSuffixAddr_Ok() throws Exception {
309         PowerMock.resetAll();
310         String url = "/aai/v11/network/generic-vnfs/generic-vnf?";
311         String expect = "/api/aai-network/v11/generic-vnfs/generic-vnf?";
312         aaiQuery = new AaiQuery();
313         PowerMock.replayAll();
314         String actual = Whitebox.invokeMethod(aaiQuery, "getMsbSuffixAddr", url);
315         PowerMock.verifyAll();
316         assertThat(actual, equalTo(expect));
317     }
318 }