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.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.junit.runner.RunWith;
30 import org.onap.holmes.common.aai.config.AaiConfig;
31 import org.onap.holmes.common.aai.entity.VmEntity;
32 import org.onap.holmes.common.aai.entity.VnfEntity;
33 import org.onap.holmes.common.config.MicroServiceConfig;
34 import org.onap.holmes.common.exception.CorrelationException;
35 import org.onap.holmes.common.utils.HttpsUtils;
36 import org.powermock.api.easymock.PowerMock;
37 import org.powermock.api.mockito.PowerMockito;
38 import org.powermock.core.classloader.annotations.PrepareForTest;
39 import org.powermock.modules.junit4.PowerMockRunner;
40 import org.powermock.reflect.Whitebox;
43 @PrepareForTest({AaiQuery.class, HttpsUtils.class, MicroServiceConfig.class})
44 @RunWith(PowerMockRunner.class)
45 public class AaiQueryTest {
48 public ExpectedException thrown = ExpectedException.none();
50 private AaiQuery aaiQuery;
51 private AaiResponseUtil aaiResponseUtil;
54 public void testAaiQuery_getAaiVnfData_ok() throws Exception {
56 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVnfDataResponse");
57 aaiResponseUtil = new AaiResponseUtil();
58 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
60 PowerMock.expectPrivate(aaiQuery, "getVnfDataResponse", "test1", "test2").andReturn("{}");
62 PowerMock.replayAll();
63 VnfEntity vnfEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVnfData", "test1", "test2");
64 PowerMock.verifyAll();
66 assertThat(vnfEntity == null, equalTo(true));
70 public void testAaiQuery_getAaiVnfData_exception() throws Exception {
72 thrown.expect(CorrelationException.class);
73 thrown.expectMessage("Failed to convert aai vnf response data to vnf entity");
74 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVnfDataResponse");
75 aaiResponseUtil = new AaiResponseUtil();
76 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
77 PowerMock.expectPrivate(aaiQuery, "getVnfDataResponse", "test1", "test2")
80 PowerMock.replayAll();
81 aaiQuery.getAaiVnfData("test1", "test2");
82 PowerMock.verifyAll();
86 public void testAaiQuery_getAaiVmData_ok() throws Exception {
88 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
89 aaiResponseUtil = new AaiResponseUtil();
90 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
91 PowerMockito.mockStatic(HttpsUtils.class);
92 Map<String, String> headers = new HashMap<>();
93 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
94 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
95 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
96 headers.put("Accept", "application/json");
97 String url = "http://10.96.33.33/api/aai-cloudInfrastructure/v11";
98 HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
99 when(HttpsUtils.get(url, headers)).thenReturn(httpResponse);
100 when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn("{}");
102 PowerMockito.mockStatic(MicroServiceConfig.class);
103 when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
105 PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2")
106 .andReturn("/aai/v11/cloud-infrastructure");
107 PowerMock.replayAll();
108 VmEntity vmEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
109 PowerMock.verifyAll();
111 assertThat(vmEntity == null, equalTo(true));
115 public void testAaiQuery_getAaiVmData_httpsutils_exception() throws Exception {
116 PowerMock.resetAll();
117 thrown.expect(CorrelationException.class);
118 thrown.expectMessage("Failed to get data from aai");
119 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
121 aaiResponseUtil = new AaiResponseUtil();
122 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
124 PowerMockito.mockStatic(HttpsUtils.class);
125 Map<String, String> headers = new HashMap<>();
126 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
127 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
128 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
129 headers.put("Accept", "application/json");
130 String url = "http://10.96.33.33/api/aai-cloudInfrastructure/v11";
132 when(HttpsUtils.get(url, headers)).thenThrow(new CorrelationException(""));
134 PowerMockito.mockStatic(MicroServiceConfig.class);
135 when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
137 PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2")
138 .andReturn("/aai/v11/cloud-infrastructure");
139 PowerMock.replayAll();
140 Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
141 PowerMock.verifyAll();
145 public void testAaiQuery_getVmResourceLinks_ok() throws Exception {
146 PowerMock.resetAll();
147 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResourceLinksResponse");
149 aaiResponseUtil = new AaiResponseUtil();
150 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
152 String result = "{\"result-data\":[{\"resource-type\":\"vserver\",\"resource-link\":\"le-vserver-id-val-51834\"}]}";
154 PowerMock.expectPrivate(aaiQuery, "getResourceLinksResponse", "test1", "test2").andReturn(result);
155 PowerMock.replayAll();
156 String resource = Whitebox.invokeMethod(aaiQuery, "getVmResourceLinks", "test1", "test2");
157 PowerMock.verifyAll();
159 assertThat(resource, equalTo("le-vserver-id-val-51834"));
165 public void testAaiQuery_getResourceLinksResponse() throws Exception {
166 PowerMock.resetAll();
167 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
169 aaiResponseUtil = new AaiResponseUtil();
170 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
172 PowerMockito.mockStatic(MicroServiceConfig.class);
173 when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("host_url");
175 PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
176 PowerMock.replayAll();
177 String resource = Whitebox.invokeMethod(aaiQuery, "getResourceLinksResponse", "test1", "test2");
178 PowerMock.verifyAll();
180 assertThat(resource, equalTo(""));
184 public void testAaiQuery_getVnfDataResponse() throws Exception {
185 PowerMock.resetAll();
186 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
188 aaiResponseUtil = new AaiResponseUtil();
189 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
191 PowerMockito.mockStatic(MicroServiceConfig.class);
192 when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("host_url");
194 PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
195 PowerMock.replayAll();
196 String resource = Whitebox.invokeMethod(aaiQuery, "getVnfDataResponse", "test1", "test2");
197 PowerMock.verifyAll();
199 assertThat(resource, equalTo(""));
203 public void testAaiQuery_getResponse_ok() throws Exception {
204 PowerMock.resetAll();
205 aaiQuery = new AaiQuery();
206 PowerMockito.mockStatic(HttpsUtils.class);
207 Map<String, String> headers = new HashMap<>();
208 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
209 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
210 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
211 headers.put("Accept", "application/json");
212 String url = "host_url";
214 HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
215 when(HttpsUtils.get(url, headers)).thenReturn(httpResponse);
216 when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn("");
218 PowerMock.replayAll();
219 String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
220 PowerMock.verifyAll();
222 assertThat(resource, equalTo(""));
226 public void testAaiQuery_getResponse_exceptioin() throws Exception {
227 PowerMock.resetAll();
228 thrown.expect(CorrelationException.class);
229 thrown.expectMessage("Failed to get data from aai");
230 aaiQuery = new AaiQuery();
232 PowerMockito.mockStatic(HttpsUtils.class);
233 Map<String, String> headers = new HashMap<>();
234 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
235 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
236 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
237 headers.put("Accept", "application/json");
238 String url = "host_url";
239 when(HttpsUtils.get(url, headers)).thenThrow(new CorrelationException(""));
240 PowerMock.replayAll();
241 String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
242 PowerMock.verifyAll();
243 assertThat(resource, equalTo(""));
247 public void testAaiQuery_getHeaders() throws Exception {
248 PowerMock.resetAll();
249 aaiQuery = new AaiQuery();
250 PowerMock.replayAll();
251 Map actual = Whitebox.invokeMethod(aaiQuery, "getHeaders");
252 PowerMock.verifyAll();
254 assertThat(actual.get("X-TransactionId"), equalTo("9999"));
255 assertThat(actual.get("X-FromAppId"), equalTo("jimmy-postman"));
256 assertThat(actual.get("Authorization"), equalTo("Basic QUFJOkFBSQ=="));
257 assertThat(actual.get("Accept"), equalTo("application/json"));
261 public void testAaiQuery_getBaseUrl_msb() throws Exception {
262 PowerMock.resetAll();
263 aaiQuery = new AaiQuery();
265 PowerMockito.mockStatic(MicroServiceConfig.class);
266 when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
267 when(MicroServiceConfig.getServiceConfigInfoFromCBS("nihao")).thenReturn("");
269 PowerMock.replayAll();
270 String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "/url");
271 PowerMock.verifyAll();
272 assertThat(actual, equalTo("http://10.96.33.33/url"));
276 public void testAaiQuery_getBaseUrl_aaiurl() throws Exception {
277 PowerMock.resetAll();
278 aaiQuery = new AaiQuery();
280 PowerMockito.mockStatic(MicroServiceConfig.class);
281 when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenThrow(new NullPointerException());
282 when(MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config")).thenReturn("aai");
284 PowerMock.replayAll();
285 String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "url");
286 System.out.println(actual);
287 PowerMock.verifyAll();
288 assertThat(actual, equalTo("https://aaiurl"));
292 public void testAaiQuery_getBaseUrl_exception() throws Exception {
293 PowerMock.resetAll();
294 aaiQuery = new AaiQuery();
296 PowerMockito.mockStatic(MicroServiceConfig.class);
297 when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenThrow(new NullPointerException());
298 when(MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config"))
299 .thenThrow(new NullPointerException());
301 PowerMock.replayAll();
302 String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "url");
303 System.out.println(actual);
304 PowerMock.verifyAll();
305 assertThat(actual, equalTo(""));
309 public void testAaiQuery_getMsbSuffixAddr_Ok() throws Exception {
310 PowerMock.resetAll();
311 String url = "/aai/v11/network/generic-vnfs/generic-vnf?";
312 String expect = "/api/aai-network/v11/generic-vnfs/generic-vnf?";
313 aaiQuery = new AaiQuery();
314 PowerMock.replayAll();
315 String actual = Whitebox.invokeMethod(aaiQuery, "getMsbSuffixAddr", url);
316 PowerMock.verifyAll();
317 assertThat(actual, equalTo(expect));