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.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.getMsbServerAddrWithHttpPrefix()).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));
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");
122 aaiResponseUtil = new AaiResponseUtil();
123 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
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";
133 when(HttpsUtils.get(url, headers)).thenThrow(new CorrelationException(""));
135 PowerMockito.mockStatic(MicroServiceConfig.class);
136 when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
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();
146 public void testAaiQuery_getVmResourceLinks_ok() throws Exception {
147 PowerMock.resetAll();
148 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResourceLinksResponse");
150 aaiResponseUtil = new AaiResponseUtil();
151 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
153 String result = "{\"result-data\":[{\"resource-type\":\"vserver\",\"resource-link\":\"le-vserver-id-val-51834\"}]}";
155 PowerMock.expectPrivate(aaiQuery, "getResourceLinksResponse", "test1", "test2").andReturn(result);
156 PowerMock.replayAll();
157 String resource = Whitebox.invokeMethod(aaiQuery, "getVmResourceLinks", "test1", "test2");
158 PowerMock.verifyAll();
160 assertThat(resource, equalTo("le-vserver-id-val-51834"));
166 public void testAaiQuery_getResourceLinksResponse() throws Exception {
167 PowerMock.resetAll();
168 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
170 aaiResponseUtil = new AaiResponseUtil();
171 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
173 PowerMockito.mockStatic(MicroServiceConfig.class);
174 when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("host_url");
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();
181 assertThat(resource, equalTo(""));
185 public void testAaiQuery_getVnfDataResponse() throws Exception {
186 PowerMock.resetAll();
187 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
189 aaiResponseUtil = new AaiResponseUtil();
190 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
192 PowerMockito.mockStatic(MicroServiceConfig.class);
193 when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("host_url");
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();
200 assertThat(resource, equalTo(""));
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";
215 when(HttpsUtils.get(url, headers)).thenReturn("");
217 PowerMock.replayAll();
218 String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
219 PowerMock.verifyAll();
221 assertThat(resource, equalTo(""));
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();
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(""));
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();
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"));
260 public void testAaiQuery_getBaseUrl_msb() throws Exception {
261 PowerMock.resetAll();
262 aaiQuery = new AaiQuery();
264 PowerMockito.mockStatic(MicroServiceConfig.class);
265 when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
266 when(MicroServiceConfig.getServiceConfigInfoFromCBS("nihao")).thenReturn("");
268 PowerMock.replayAll();
269 String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "/url");
270 PowerMock.verifyAll();
271 assertThat(actual, equalTo("http://10.96.33.33/url"));
275 public void testAaiQuery_getBaseUrl_aaiurl() throws Exception {
276 PowerMock.resetAll();
277 aaiQuery = new AaiQuery();
279 PowerMockito.mockStatic(MicroServiceConfig.class);
280 when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenThrow(new NullPointerException());
281 when(MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config")).thenReturn("aai");
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"));
291 public void testAaiQuery_getBaseUrl_exception() throws Exception {
292 PowerMock.resetAll();
293 aaiQuery = new AaiQuery();
295 PowerMockito.mockStatic(MicroServiceConfig.class);
296 when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenThrow(new NullPointerException());
297 when(MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config"))
298 .thenThrow(new NullPointerException());
300 PowerMock.replayAll();
301 String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "url");
302 System.out.println(actual);
303 PowerMock.verifyAll();
304 assertThat(actual, equalTo(""));
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));