2 * Copyright 2017 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.holmes.common.aai;
18 import static org.easymock.EasyMock.anyObject;
19 import static org.hamcrest.CoreMatchers.equalTo;
20 import static org.hamcrest.MatcherAssert.assertThat;
21 import static org.powermock.api.mockito.PowerMockito.when;
23 import java.util.HashMap;
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;
42 @PrepareForTest({AaiQuery.class, HttpsUtils.class, MicroServiceConfig.class})
43 @RunWith(PowerMockRunner.class)
44 public class AaiQueryTest {
47 public ExpectedException thrown = ExpectedException.none();
49 private AaiQuery aaiQuery;
50 private AaiResponseUtil aaiResponseUtil;
53 public void testAaiQuery_getAaiVnfData_ok() throws Exception {
55 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVnfDataResponse");
56 aaiResponseUtil = new AaiResponseUtil();
57 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
59 PowerMock.expectPrivate(aaiQuery, "getVnfDataResponse", "test1", "test2").andReturn("{}");
61 PowerMock.replayAll();
62 VnfEntity vnfEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVnfData", "test1", "test2");
63 PowerMock.verifyAll();
65 assertThat(vnfEntity == null, equalTo(true));
69 public void testAaiQuery_getAaiVnfData_exception() throws Exception {
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")
79 PowerMock.replayAll();
80 aaiQuery.getAaiVnfData("test1", "test2");
81 PowerMock.verifyAll();
85 public void testAaiQuery_getAaiVmData_ok() throws Exception {
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("{}");
99 PowerMockito.mockStatic(MicroServiceConfig.class);
100 when(MicroServiceConfig.getMsbServerAddr()).thenReturn("http://10.96.33.33:80");
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();
108 assertThat(vmEntity == null, equalTo(true));
114 public void testAaiQuery_getAaiVmData_exception() throws Exception {
115 PowerMock.resetAll();
116 thrown.expect(CorrelationException.class);
117 thrown.expectMessage("Failed to convert aai vm response data to vm entity");
118 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
120 aaiResponseUtil = new AaiResponseUtil();
121 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
123 PowerMockito.mockStatic(HttpsUtils.class);
124 Map<String, String> headers = new HashMap<>();
125 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
126 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
127 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
128 headers.put("Accept", "application/json");
129 String url = "http://10.96.33.33/api/aai-cloudInfrastructure/v11";
131 when(HttpsUtils.get(url, headers)).thenReturn("");
133 PowerMockito.mockStatic(MicroServiceConfig.class);
134 when(MicroServiceConfig.getMsbServerAddr()).thenReturn("http://10.96.33.33:80");
136 PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2")
137 .andReturn("/aai/v11/cloud-infrastructure");
139 PowerMock.replayAll();
140 Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
141 PowerMock.verifyAll();
145 public void testAaiQuery_getAaiVmData_httpsutils_exception() throws Exception {
146 PowerMock.resetAll();
147 thrown.expect(CorrelationException.class);
148 thrown.expectMessage("Failed to get data from aai");
149 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
151 aaiResponseUtil = new AaiResponseUtil();
152 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
154 PowerMockito.mockStatic(HttpsUtils.class);
155 Map<String, String> headers = new HashMap<>();
156 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
157 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
158 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
159 headers.put("Accept", "application/json");
160 String url = "http://10.96.33.33/api/aai-cloudInfrastructure/v11";
162 when(HttpsUtils.get(url, headers)).thenThrow(new CorrelationException(""));
164 PowerMockito.mockStatic(MicroServiceConfig.class);
165 when(MicroServiceConfig.getMsbServerAddr()).thenReturn("http://10.96.33.33:80");
167 PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2")
168 .andReturn("/aai/v11/cloud-infrastructure");
169 PowerMock.replayAll();
170 Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
171 PowerMock.verifyAll();
175 public void testAaiQuery_getVmResourceLinks_ok() throws Exception {
176 PowerMock.resetAll();
177 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResourceLinksResponse");
179 aaiResponseUtil = new AaiResponseUtil();
180 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
182 String result = "{\"result-data\":[{\"resource-type\":\"vserver\",\"resource-link\":\"le-vserver-id-val-51834\"}]}";
184 PowerMock.expectPrivate(aaiQuery, "getResourceLinksResponse", "test1", "test2").andReturn(result);
185 PowerMock.replayAll();
186 String resource = Whitebox.invokeMethod(aaiQuery, "getVmResourceLinks", "test1", "test2");
187 PowerMock.verifyAll();
189 assertThat(resource, equalTo("le-vserver-id-val-51834"));
193 public void testAaiQuery_getVmResourceLinks_exception() throws Exception {
194 PowerMock.resetAll();
195 thrown.expect(CorrelationException.class);
196 thrown.expectMessage("Failed to get aai resource link");
197 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResourceLinksResponse");
199 aaiResponseUtil = new AaiResponseUtil();
200 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
202 PowerMock.expectPrivate(aaiQuery, "getResourceLinksResponse", "test1", "test2").andReturn("");
203 PowerMock.replayAll();
204 String resource = Whitebox.invokeMethod(aaiQuery, "getVmResourceLinks", "test1", "test2");
205 PowerMock.verifyAll();
207 assertThat(resource, equalTo("le-vserver-id-val-51834"));
211 public void testAaiQuery_getResourceLinksResponse() throws Exception {
212 PowerMock.resetAll();
213 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
215 aaiResponseUtil = new AaiResponseUtil();
216 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
218 PowerMockito.mockStatic(MicroServiceConfig.class);
219 when(MicroServiceConfig.getMsbServerAddr()).thenReturn("host_url");
221 PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
222 PowerMock.replayAll();
223 String resource = Whitebox.invokeMethod(aaiQuery, "getResourceLinksResponse", "test1", "test2");
224 PowerMock.verifyAll();
226 assertThat(resource, equalTo(""));
230 public void testAaiQuery_getVnfDataResponse() throws Exception {
231 PowerMock.resetAll();
232 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
234 aaiResponseUtil = new AaiResponseUtil();
235 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
237 PowerMockito.mockStatic(MicroServiceConfig.class);
238 when(MicroServiceConfig.getMsbServerAddr()).thenReturn("host_url");
240 PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
241 PowerMock.replayAll();
242 String resource = Whitebox.invokeMethod(aaiQuery, "getVnfDataResponse", "test1", "test2");
243 PowerMock.verifyAll();
245 assertThat(resource, equalTo(""));
249 public void testAaiQuery_getResponse_ok() throws Exception {
250 PowerMock.resetAll();
251 aaiQuery = new AaiQuery();
252 PowerMockito.mockStatic(HttpsUtils.class);
253 Map<String, String> headers = new HashMap<>();
254 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
255 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
256 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
257 headers.put("Accept", "application/json");
258 String url = "host_url";
260 when(HttpsUtils.get(url, headers)).thenReturn("");
262 PowerMock.replayAll();
263 String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
264 PowerMock.verifyAll();
266 assertThat(resource, equalTo(""));
270 public void testAaiQuery_getResponse_exceptioin() throws Exception {
271 PowerMock.resetAll();
272 thrown.expect(CorrelationException.class);
273 thrown.expectMessage("Failed to get data from aai");
274 aaiQuery = new AaiQuery();
276 PowerMockito.mockStatic(HttpsUtils.class);
277 Map<String, String> headers = new HashMap<>();
278 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
279 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
280 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
281 headers.put("Accept", "application/json");
282 String url = "host_url";
283 when(HttpsUtils.get(url, headers)).thenThrow(new CorrelationException(""));
284 PowerMock.replayAll();
285 String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
286 PowerMock.verifyAll();
287 assertThat(resource, equalTo(""));
291 public void testAaiQuery_getHeaders() throws Exception {
292 PowerMock.resetAll();
293 aaiQuery = new AaiQuery();
294 PowerMock.replayAll();
295 Map actual = Whitebox.invokeMethod(aaiQuery, "getHeaders");
296 PowerMock.verifyAll();
298 assertThat(actual.get("X-TransactionId"), equalTo("9999"));
299 assertThat(actual.get("X-FromAppId"), equalTo("jimmy-postman"));
300 assertThat(actual.get("Authorization"), equalTo("Basic QUFJOkFBSQ=="));
301 assertThat(actual.get("Accept"), equalTo("application/json"));
305 public void testAaiQuery_getBaseUrl_msb() throws Exception {
306 PowerMock.resetAll();
307 aaiQuery = new AaiQuery();
309 PowerMockito.mockStatic(MicroServiceConfig.class);
310 when(MicroServiceConfig.getMsbServerAddr()).thenReturn("http://10.96.33.33:80");
311 when(MicroServiceConfig.getServiceAddrInfoFromCBS("nihao")).thenReturn("");
313 PowerMock.replayAll();
314 String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "/url");
315 PowerMock.verifyAll();
316 assertThat(actual, equalTo("http://10.96.33.33/url"));
320 public void testAaiQuery_getBaseUrl_aaiurl() throws Exception {
321 PowerMock.resetAll();
322 aaiQuery = new AaiQuery();
324 PowerMockito.mockStatic(MicroServiceConfig.class);
325 when(MicroServiceConfig.getMsbServerAddr()).thenThrow(new NullPointerException());
326 when(MicroServiceConfig.getServiceAddrInfoFromCBS("aai_config")).thenReturn("aai");
328 PowerMock.replayAll();
329 String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "url");
330 System.out.println(actual);
331 PowerMock.verifyAll();
332 assertThat(actual, equalTo("https://aaiurl"));
336 public void testAaiQuery_getBaseUrl_exception() throws Exception {
337 PowerMock.resetAll();
338 aaiQuery = new AaiQuery();
340 PowerMockito.mockStatic(MicroServiceConfig.class);
341 when(MicroServiceConfig.getMsbServerAddr()).thenThrow(new NullPointerException());
342 when(MicroServiceConfig.getServiceAddrInfoFromCBS("aai_config"))
343 .thenThrow(new NullPointerException());
345 PowerMock.replayAll();
346 String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "url");
347 System.out.println(actual);
348 PowerMock.verifyAll();
349 assertThat(actual, equalTo(""));
353 public void testAaiQuery_getMsbSuffixAddr_Ok() throws Exception {
354 PowerMock.resetAll();
355 String url = "/aai/v11/network/generic-vnfs/generic-vnf?";
356 String expect = "/api/aai-network/v11/generic-vnfs/generic-vnf?";
357 aaiQuery = new AaiQuery();
358 PowerMock.replayAll();
359 String actual = Whitebox.invokeMethod(aaiQuery, "getMsbSuffixAddr", url);
360 PowerMock.verifyAll();
361 assertThat(actual, equalTo(expect));