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.core.IsEqual.equalTo;
20 import static org.junit.Assert.assertThat;
21 import static org.powermock.api.mockito.PowerMockito.when;
23 import java.util.HashMap;
25 import org.apache.http.HttpResponse;
26 import org.apache.http.client.methods.HttpGet;
27 import org.apache.http.impl.client.CloseableHttpClient;
28 import org.easymock.EasyMock;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
32 import org.junit.runner.RunWith;
33 import org.onap.holmes.common.aai.config.AaiConfig;
34 import org.onap.holmes.common.aai.entity.VmEntity;
35 import org.onap.holmes.common.aai.entity.VnfEntity;
36 import org.onap.holmes.common.config.MicroServiceConfig;
37 import org.onap.holmes.common.exception.CorrelationException;
38 import org.onap.holmes.common.utils.HttpsUtils;
39 import org.powermock.api.easymock.PowerMock;
40 import org.powermock.api.mockito.PowerMockito;
41 import org.powermock.core.classloader.annotations.PrepareForTest;
42 import org.powermock.modules.junit4.PowerMockRunner;
43 import org.powermock.reflect.Whitebox;
46 @PrepareForTest({AaiQuery.class, HttpsUtils.class, MicroServiceConfig.class, HttpGet.class})
47 @RunWith(PowerMockRunner.class)
48 public class AaiQueryTest {
51 public ExpectedException thrown = ExpectedException.none();
53 private AaiQuery aaiQuery;
54 private AaiResponseUtil aaiResponseUtil;
57 public void testAaiQuery_getAaiVnfData_ok() throws Exception {
59 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVnfDataResponse");
60 aaiResponseUtil = new AaiResponseUtil();
61 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
63 PowerMock.expectPrivate(aaiQuery, "getVnfDataResponse", "test1", "test2").andReturn("{}");
65 PowerMock.replayAll();
66 VnfEntity vnfEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVnfData", "test1", "test2");
67 PowerMock.verifyAll();
69 assertThat(vnfEntity == null, equalTo(true));
73 public void testAaiQuery_getAaiVnfData_exception() throws Exception {
75 thrown.expect(CorrelationException.class);
76 thrown.expectMessage("Failed to convert aai vnf response data to vnf entity");
77 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVnfDataResponse");
78 aaiResponseUtil = new AaiResponseUtil();
79 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
80 PowerMock.expectPrivate(aaiQuery, "getVnfDataResponse", "test1", "test2")
83 PowerMock.replayAll();
84 aaiQuery.getAaiVnfData("test1", "test2");
85 PowerMock.verifyAll();
89 public void testAaiQuery_getAaiVmData_ok() throws Exception {
91 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
92 aaiResponseUtil = new AaiResponseUtil();
93 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
94 PowerMockito.mockStatic(HttpsUtils.class);
95 Map<String, String> headers = new HashMap<>();
96 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
97 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
98 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
99 headers.put("Accept", "application/json");
100 String url = "http://10.96.33.33:80/api/aai-cloudInfrastructure/v11";
101 HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
102 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
103 when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
104 HttpGet httpGet = new HttpGet(url);
105 PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
106 when(HttpsUtils.get(httpGet, headers, httpClient)).thenReturn(httpResponse);
107 when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn("{}");
109 PowerMockito.mockStatic(MicroServiceConfig.class);
110 when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
112 PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2")
113 .andReturn("/aai/v11/cloud-infrastructure");
114 PowerMock.expectPrivate(httpClient, "close");
115 EasyMock.expectLastCall();
116 PowerMock.replayAll();
117 VmEntity vmEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
118 PowerMock.verifyAll();
120 assertThat(vmEntity == null, equalTo(true));
124 public void testAaiQuery_getAaiVmData_httpsutils_exception() throws Exception {
125 PowerMock.resetAll();
126 thrown.expect(CorrelationException.class);
127 thrown.expectMessage("Failed to get data from aai");
128 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
130 aaiResponseUtil = new AaiResponseUtil();
131 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
133 PowerMockito.mockStatic(HttpsUtils.class);
134 Map<String, String> headers = new HashMap<>();
135 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
136 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
137 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
138 headers.put("Accept", "application/json");
139 String url = "http://10.96.33.33:80/api/aai-cloudInfrastructure/v11";
140 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
141 when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
142 HttpGet httpGet = new HttpGet(url);
143 PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
144 when(HttpsUtils.get(httpGet, headers, httpClient)).thenThrow(new CorrelationException(""));
145 PowerMockito.mockStatic(MicroServiceConfig.class);
146 when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
147 PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2")
148 .andReturn("/aai/v11/cloud-infrastructure");
149 PowerMock.expectPrivate(httpClient,"close");
150 EasyMock.expectLastCall();
151 PowerMock.replayAll();
152 Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
153 PowerMock.verifyAll();
157 public void testAaiQuery_getVmResourceLinks_ok() throws Exception {
158 PowerMock.resetAll();
159 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResourceLinksResponse");
161 aaiResponseUtil = new AaiResponseUtil();
162 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
164 String result = "{\"result-data\":[{\"resource-type\":\"vserver\",\"resource-link\":\"le-vserver-id-val-51834\"}]}";
166 PowerMock.expectPrivate(aaiQuery, "getResourceLinksResponse", "test1", "test2").andReturn(result);
167 PowerMock.replayAll();
168 String resource = Whitebox.invokeMethod(aaiQuery, "getVmResourceLinks", "test1", "test2");
169 PowerMock.verifyAll();
171 assertThat(resource, equalTo("le-vserver-id-val-51834"));
177 public void testAaiQuery_getResourceLinksResponse() throws Exception {
178 PowerMock.resetAll();
179 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
181 aaiResponseUtil = new AaiResponseUtil();
182 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
184 PowerMockito.mockStatic(MicroServiceConfig.class);
185 when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("host_url");
187 PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
188 PowerMock.replayAll();
189 String resource = Whitebox.invokeMethod(aaiQuery, "getResourceLinksResponse", "test1", "test2");
190 PowerMock.verifyAll();
192 assertThat(resource, equalTo(""));
196 public void testAaiQuery_getVnfDataResponse() throws Exception {
197 PowerMock.resetAll();
198 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
200 aaiResponseUtil = new AaiResponseUtil();
201 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
203 PowerMockito.mockStatic(MicroServiceConfig.class);
204 when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("host_url");
206 PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
207 PowerMock.replayAll();
208 String resource = Whitebox.invokeMethod(aaiQuery, "getVnfDataResponse", "test1", "test2");
209 PowerMock.verifyAll();
211 assertThat(resource, equalTo(""));
215 public void testAaiQuery_getResponse_ok() throws Exception {
216 PowerMock.resetAll();
217 aaiQuery = new AaiQuery();
218 PowerMockito.mockStatic(HttpsUtils.class);
219 Map<String, String> headers = new HashMap<>();
220 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
221 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
222 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
223 headers.put("Accept", "application/json");
224 String url = "host_url";
226 HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
227 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
228 when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
229 HttpGet httpGet = new HttpGet(url);
230 PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
231 when(HttpsUtils.get(httpGet, headers, httpClient)).thenReturn(httpResponse);
232 when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn("");
233 PowerMock.expectPrivate(httpClient, "close");
234 EasyMock.expectLastCall();
236 PowerMock.replayAll();
237 String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
238 PowerMock.verifyAll();
240 assertThat(resource, equalTo(""));
244 public void testAaiQuery_getResponse_exceptioin() throws Exception {
245 PowerMock.resetAll();
246 thrown.expect(CorrelationException.class);
247 thrown.expectMessage("Failed to get data from aai");
248 aaiQuery = new AaiQuery();
250 PowerMockito.mockStatic(HttpsUtils.class);
251 Map<String, String> headers = new HashMap<>();
252 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
253 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
254 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
255 headers.put("Accept", "application/json");
256 String url = "host_url";
257 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
258 when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
259 HttpGet httpGet = new HttpGet(url);
260 PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
261 when(HttpsUtils.get(httpGet, headers, httpClient)).thenThrow(new CorrelationException(""));
262 PowerMock.expectPrivate(httpClient, "close");
263 EasyMock.expectLastCall();
264 PowerMock.replayAll();
265 String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
266 PowerMock.verifyAll();
267 assertThat(resource, equalTo(""));
271 public void testAaiQuery_getHeaders() throws Exception {
272 PowerMock.resetAll();
273 aaiQuery = new AaiQuery();
274 PowerMock.replayAll();
275 Map actual = Whitebox.invokeMethod(aaiQuery, "getHeaders");
276 PowerMock.verifyAll();
278 assertThat(actual.get("X-TransactionId"), equalTo("9999"));
279 assertThat(actual.get("X-FromAppId"), equalTo("jimmy-postman"));
280 assertThat(actual.get("Authorization"), equalTo("Basic QUFJOkFBSQ=="));
281 assertThat(actual.get("Accept"), equalTo("application/json"));
285 public void testAaiQuery_getBaseUrl_msb() throws Exception {
286 PowerMock.resetAll();
287 aaiQuery = new AaiQuery();
289 PowerMockito.mockStatic(MicroServiceConfig.class);
290 when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
291 when(MicroServiceConfig.getServiceConfigInfoFromCBS("nihao")).thenReturn("");
293 PowerMock.replayAll();
294 String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "/url");
295 PowerMock.verifyAll();
296 assertThat(actual, equalTo("http://10.96.33.33:80/url"));
300 public void testAaiQuery_getBaseUrl_aaiurl() throws Exception {
301 PowerMock.resetAll();
302 aaiQuery = new AaiQuery();
304 PowerMockito.mockStatic(MicroServiceConfig.class);
305 when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenThrow(new NullPointerException());
306 when(MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config")).thenReturn("aai");
308 PowerMock.replayAll();
309 String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "url");
310 System.out.println(actual);
311 PowerMock.verifyAll();
312 assertThat(actual, equalTo("https://aaiurl"));
316 public void testAaiQuery_getBaseUrl_exception() throws Exception {
317 PowerMock.resetAll();
318 aaiQuery = new AaiQuery();
320 PowerMockito.mockStatic(MicroServiceConfig.class);
321 when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenThrow(new NullPointerException());
322 when(MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config"))
323 .thenThrow(new NullPointerException());
325 PowerMock.replayAll();
326 String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "url");
327 System.out.println(actual);
328 PowerMock.verifyAll();
329 assertThat(actual, equalTo(""));
333 public void testAaiQuery_getMsbSuffixAddr_Ok() throws Exception {
334 PowerMock.resetAll();
335 String url = "/aai/v11/network/generic-vnfs/generic-vnf?";
336 String expect = "/api/aai-network/v11/generic-vnfs/generic-vnf?";
337 aaiQuery = new AaiQuery();
338 PowerMock.replayAll();
339 String actual = Whitebox.invokeMethod(aaiQuery, "getMsbSuffixAddr", url);
340 PowerMock.verifyAll();
341 assertThat(actual, equalTo(expect));