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 = "host_url";
97 when(HttpsUtils.get(url, headers)).thenReturn("{}");
99 PowerMockito.mockStatic(MicroServiceConfig.class);
100 when(MicroServiceConfig.getMsbServerAddr()).thenReturn("host_url");
102 PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2").andReturn("");
103 PowerMock.replayAll();
104 VmEntity vmEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
105 PowerMock.verifyAll();
107 assertThat(vmEntity == null, equalTo(true));
113 public void testAaiQuery_getAaiVmData_exception() throws Exception {
114 PowerMock.resetAll();
115 thrown.expect(CorrelationException.class);
116 thrown.expectMessage("Failed to convert aai vm response data to vm entity");
117 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
119 aaiResponseUtil = new AaiResponseUtil();
120 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
122 PowerMockito.mockStatic(HttpsUtils.class);
123 Map<String, String> headers = new HashMap<>();
124 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
125 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
126 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
127 headers.put("Accept", "application/json");
128 String url = "host_url";
130 when(HttpsUtils.get(url, headers)).thenReturn("");
132 PowerMockito.mockStatic(MicroServiceConfig.class);
133 when(MicroServiceConfig.getMsbServerAddr()).thenReturn("host_url");
135 PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2").andReturn("");
137 PowerMock.replayAll();
138 Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
139 PowerMock.verifyAll();
143 public void testAaiQuery_getAaiVmData_httpsutils_exception() throws Exception {
144 PowerMock.resetAll();
145 thrown.expect(CorrelationException.class);
146 thrown.expectMessage("Failed to get data from aai");
147 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
149 aaiResponseUtil = new AaiResponseUtil();
150 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
152 PowerMockito.mockStatic(HttpsUtils.class);
153 Map<String, String> headers = new HashMap<>();
154 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
155 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
156 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
157 headers.put("Accept", "application/json");
158 String url = "host_url";
160 when(HttpsUtils.get(url, headers)).thenThrow(new CorrelationException(""));
162 PowerMockito.mockStatic(MicroServiceConfig.class);
163 when(MicroServiceConfig.getMsbServerAddr()).thenReturn("host_url");
165 PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2").andReturn("");
166 PowerMock.replayAll();
167 Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
168 PowerMock.verifyAll();
172 public void testAaiQuery_getVmResourceLinks_ok() throws Exception {
173 PowerMock.resetAll();
174 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResourceLinksResponse");
176 aaiResponseUtil = new AaiResponseUtil();
177 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
179 String result = "{\"result-data\":[{\"resource-type\":\"vserver\",\"resource-link\":\"le-vserver-id-val-51834\"}]}";
181 PowerMock.expectPrivate(aaiQuery, "getResourceLinksResponse", "test1", "test2").andReturn(result);
182 PowerMock.replayAll();
183 String resource = Whitebox.invokeMethod(aaiQuery, "getVmResourceLinks", "test1", "test2");
184 PowerMock.verifyAll();
186 assertThat(resource, equalTo("le-vserver-id-val-51834"));
190 public void testAaiQuery_getVmResourceLinks_exception() throws Exception {
191 PowerMock.resetAll();
192 thrown.expect(CorrelationException.class);
193 thrown.expectMessage("Failed to get aai resource link");
194 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResourceLinksResponse");
196 aaiResponseUtil = new AaiResponseUtil();
197 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
199 PowerMock.expectPrivate(aaiQuery, "getResourceLinksResponse", "test1", "test2").andReturn("");
200 PowerMock.replayAll();
201 String resource = Whitebox.invokeMethod(aaiQuery, "getVmResourceLinks", "test1", "test2");
202 PowerMock.verifyAll();
204 assertThat(resource, equalTo("le-vserver-id-val-51834"));
208 public void testAaiQuery_getResourceLinksResponse() throws Exception {
209 PowerMock.resetAll();
210 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
212 aaiResponseUtil = new AaiResponseUtil();
213 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
215 PowerMockito.mockStatic(MicroServiceConfig.class);
216 when(MicroServiceConfig.getMsbServerAddr()).thenReturn("host_url");
218 PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
219 PowerMock.replayAll();
220 String resource = Whitebox.invokeMethod(aaiQuery, "getResourceLinksResponse", "test1", "test2");
221 PowerMock.verifyAll();
223 assertThat(resource, equalTo(""));
227 public void testAaiQuery_getVnfDataResponse() throws Exception {
228 PowerMock.resetAll();
229 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
231 aaiResponseUtil = new AaiResponseUtil();
232 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
234 PowerMockito.mockStatic(MicroServiceConfig.class);
235 when(MicroServiceConfig.getMsbServerAddr()).thenReturn("host_url");
237 PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
238 PowerMock.replayAll();
239 String resource = Whitebox.invokeMethod(aaiQuery, "getVnfDataResponse", "test1", "test2");
240 PowerMock.verifyAll();
242 assertThat(resource, equalTo(""));
246 public void testAaiQuery_getResponse_ok() throws Exception {
247 PowerMock.resetAll();
248 aaiQuery = new AaiQuery();
249 PowerMockito.mockStatic(HttpsUtils.class);
250 Map<String, String> headers = new HashMap<>();
251 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
252 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
253 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
254 headers.put("Accept", "application/json");
255 String url = "host_url";
257 when(HttpsUtils.get(url, headers)).thenReturn("");
259 PowerMock.replayAll();
260 String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
261 PowerMock.verifyAll();
263 assertThat(resource, equalTo(""));
267 public void testAaiQuery_getResponse_exceptioin() throws Exception {
268 PowerMock.resetAll();
269 thrown.expect(CorrelationException.class);
270 thrown.expectMessage("Failed to get data from aai");
271 aaiQuery = new AaiQuery();
273 PowerMockito.mockStatic(HttpsUtils.class);
274 Map<String, String> headers = new HashMap<>();
275 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
276 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
277 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
278 headers.put("Accept", "application/json");
279 String url = "host_url";
281 when(HttpsUtils.get(url, headers)).thenThrow(new CorrelationException(""));
283 PowerMock.replayAll();
284 String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
285 PowerMock.verifyAll();
287 assertThat(resource, equalTo(""));
292 public void testAaiQuery_getHeaders() throws Exception {
293 PowerMock.resetAll();
294 aaiQuery = new AaiQuery();
295 PowerMock.replayAll();
296 Map actual = Whitebox.invokeMethod(aaiQuery, "getHeaders");
297 PowerMock.verifyAll();
299 assertThat(actual.get("X-TransactionId"), equalTo("9999"));
300 assertThat(actual.get("X-FromAppId"), equalTo("jimmy-postman"));
301 assertThat(actual.get("Authorization"), equalTo("Basic QUFJOkFBSQ=="));
302 assertThat(actual.get("Accept"), equalTo("application/json"));
306 public void testAaiQuery_getBaseUrl_msb() throws Exception {
307 PowerMock.resetAll();
308 aaiQuery = new AaiQuery();
310 PowerMockito.mockStatic(MicroServiceConfig.class);
311 when(MicroServiceConfig.getMsbServerAddr()).thenReturn("msb");
312 when(MicroServiceConfig.getServiceAddrInfoFromCBS("nihao")).thenReturn("");
314 PowerMock.replayAll();
315 String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "url");
316 PowerMock.verifyAll();
317 assertThat(actual, equalTo("msburl"));
321 public void testAaiQuery_getBaseUrl_aaiurl() throws Exception {
322 PowerMock.resetAll();
323 aaiQuery = new AaiQuery();
325 PowerMockito.mockStatic(MicroServiceConfig.class);
326 when(MicroServiceConfig.getMsbServerAddr()).thenThrow(new NullPointerException());
327 when(MicroServiceConfig.getServiceAddrInfoFromCBS("aai_config")).thenReturn("aai");
329 PowerMock.replayAll();
330 String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "url");
331 System.out.println(actual);
332 PowerMock.verifyAll();
333 assertThat(actual, equalTo("https:\\\\aaiurl"));
337 public void testAaiQuery_getBaseUrl_exception() throws Exception {
338 PowerMock.resetAll();
339 aaiQuery = new AaiQuery();
341 PowerMockito.mockStatic(MicroServiceConfig.class);
342 when(MicroServiceConfig.getMsbServerAddr()).thenThrow(new NullPointerException());
343 when(MicroServiceConfig.getServiceAddrInfoFromCBS("aai_config"))
344 .thenThrow(new NullPointerException());
346 PowerMock.replayAll();
347 String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "url");
348 System.out.println(actual);
349 PowerMock.verifyAll();
350 assertThat(actual, equalTo(""));
354 public void testAaiQuery_getMsbSuffixAddr_Ok() throws Exception {
355 PowerMock.resetAll();
356 String url = "/aai/v11/network/generic-vnfs/generic-vnf?";
357 String expect = "/aai/network/v11/generic-vnfs/generic-vnf?";
358 aaiQuery = new AaiQuery();
359 PowerMock.replayAll();
360 String actual = Whitebox.invokeMethod(aaiQuery, "getMsbSuffixAddr", url);
361 PowerMock.verifyAll();
362 assertThat(actual, equalTo(expect));