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.PowerMockIgnore;
42 import org.powermock.core.classloader.annotations.PrepareForTest;
43 import org.powermock.modules.junit4.PowerMockRunner;
44 import org.powermock.reflect.Whitebox;
47 @PrepareForTest({AaiQuery.class, HttpsUtils.class, MicroServiceConfig.class, HttpGet.class})
48 @PowerMockIgnore("javax.net.ssl.*")
49 @RunWith(PowerMockRunner.class)
50 public class AaiQueryTest {
53 public ExpectedException thrown = ExpectedException.none();
55 private AaiQuery aaiQuery;
56 private AaiResponseUtil aaiResponseUtil;
59 public void testAaiQuery_getAaiVnfData_ok() throws Exception {
61 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVnfDataResponse");
62 aaiResponseUtil = new AaiResponseUtil();
63 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
65 PowerMock.expectPrivate(aaiQuery, "getVnfDataResponse", "test1", "test2").andReturn("{}");
67 PowerMock.replayAll();
68 VnfEntity vnfEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVnfData", "test1", "test2");
69 PowerMock.verifyAll();
71 assertThat(vnfEntity == null, equalTo(true));
75 public void testAaiQuery_getAaiVnfData_exception() throws Exception {
77 thrown.expect(CorrelationException.class);
78 thrown.expectMessage("Failed to convert aai vnf response data to vnf entity");
79 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVnfDataResponse");
80 aaiResponseUtil = new AaiResponseUtil();
81 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
82 PowerMock.expectPrivate(aaiQuery, "getVnfDataResponse", "test1", "test2")
85 PowerMock.replayAll();
86 aaiQuery.getAaiVnfData("test1", "test2");
87 PowerMock.verifyAll();
91 public void testAaiQuery_getAaiVmData_ok() throws Exception {
93 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
94 aaiResponseUtil = new AaiResponseUtil();
95 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
96 PowerMockito.mockStatic(HttpsUtils.class);
97 Map<String, String> headers = new HashMap<>();
98 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
99 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
100 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
101 headers.put("Accept", "application/json");
102 String url = "https://aai.onap:8443/aai/v11/cloud-infrastructure";
103 HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
104 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
105 when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
106 HttpGet httpGet = new HttpGet(url);
107 PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
108 when(HttpsUtils.get(httpGet, headers, httpClient)).thenReturn(httpResponse);
109 when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn("{}");
111 PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2")
112 .andReturn("/aai/v11/cloud-infrastructure");
113 PowerMock.expectPrivate(httpClient, "close");
114 EasyMock.expectLastCall();
115 PowerMock.replayAll();
116 VmEntity vmEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
117 PowerMock.verifyAll();
119 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 = "https://aai.onap:8443/aai/v11/cloud-infrastructure";
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 PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2")
147 .andReturn("/aai/v11/cloud-infrastructure");
148 PowerMock.expectPrivate(httpClient,"close");
149 EasyMock.expectLastCall();
150 PowerMock.replayAll();
151 Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
152 PowerMock.verifyAll();
156 public void testAaiQuery_getVmResourceLinks_ok() throws Exception {
157 PowerMock.resetAll();
158 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResourceLinksResponse");
160 aaiResponseUtil = new AaiResponseUtil();
161 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
163 String result = "{\"result-data\":[{\"resource-type\":\"vserver\",\"resource-link\":\"le-vserver-id-val-51834\"}]}";
165 PowerMock.expectPrivate(aaiQuery, "getResourceLinksResponse", "test1", "test2").andReturn(result);
166 PowerMock.replayAll();
167 String resource = Whitebox.invokeMethod(aaiQuery, "getVmResourceLinks", "test1", "test2");
168 PowerMock.verifyAll();
170 assertThat(resource, equalTo("le-vserver-id-val-51834"));
176 public void testAaiQuery_getResourceLinksResponse() throws Exception {
177 PowerMock.resetAll();
178 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
180 aaiResponseUtil = new AaiResponseUtil();
181 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
183 PowerMockito.mockStatic(MicroServiceConfig.class);
184 when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("host_url");
186 PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
187 PowerMock.replayAll();
188 String resource = Whitebox.invokeMethod(aaiQuery, "getResourceLinksResponse", "test1", "test2");
189 PowerMock.verifyAll();
191 assertThat(resource, equalTo(""));
195 public void testAaiQuery_getVnfDataResponse() throws Exception {
196 PowerMock.resetAll();
197 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
199 aaiResponseUtil = new AaiResponseUtil();
200 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
202 PowerMockito.mockStatic(MicroServiceConfig.class);
203 when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("host_url");
205 PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
206 PowerMock.replayAll();
207 String resource = Whitebox.invokeMethod(aaiQuery, "getVnfDataResponse", "test1", "test2");
208 PowerMock.verifyAll();
210 assertThat(resource, equalTo(""));
214 public void testAaiQuery_getResponse_ok() throws Exception {
215 PowerMock.resetAll();
216 aaiQuery = new AaiQuery();
217 PowerMockito.mockStatic(HttpsUtils.class);
218 Map<String, String> headers = new HashMap<>();
219 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
220 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
221 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
222 headers.put("Accept", "application/json");
223 String url = "host_url";
225 HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
226 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
227 when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
228 HttpGet httpGet = new HttpGet(url);
229 PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
230 when(HttpsUtils.get(httpGet, headers, httpClient)).thenReturn(httpResponse);
231 when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn("");
232 PowerMock.expectPrivate(httpClient, "close");
233 EasyMock.expectLastCall();
235 PowerMock.replayAll();
236 String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
237 PowerMock.verifyAll();
239 assertThat(resource, equalTo(""));
243 public void testAaiQuery_getResponse_exceptioin() throws Exception {
244 PowerMock.resetAll();
245 thrown.expect(CorrelationException.class);
246 thrown.expectMessage("Failed to get data from aai");
247 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";
256 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
257 when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
258 HttpGet httpGet = new HttpGet(url);
259 PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
260 when(HttpsUtils.get(httpGet, headers, httpClient)).thenThrow(new CorrelationException(""));
261 PowerMock.expectPrivate(httpClient, "close");
262 EasyMock.expectLastCall();
263 PowerMock.replayAll();
264 String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
265 PowerMock.verifyAll();
266 assertThat(resource, equalTo(""));
270 public void testAaiQuery_getHeaders() throws Exception {
271 PowerMock.resetAll();
272 aaiQuery = new AaiQuery();
273 PowerMock.replayAll();
274 Map actual = Whitebox.invokeMethod(aaiQuery, "getHeaders");
275 PowerMock.verifyAll();
277 assertThat(actual.get("X-TransactionId"), equalTo("9999"));
278 assertThat(actual.get("X-FromAppId"), equalTo("jimmy-postman"));
279 assertThat(actual.get("Authorization"), equalTo("Basic QUFJOkFBSQ=="));
280 assertThat(actual.get("Accept"), equalTo("application/json"));
284 public void testAaiQuery_getBaseUrl_aaiurl() throws Exception {
285 PowerMock.resetAll();
286 aaiQuery = new AaiQuery();
288 PowerMockito.mockStatic(MicroServiceConfig.class);
290 PowerMock.replayAll();
291 String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "/url");
292 PowerMock.verifyAll();
294 assertThat(actual, equalTo("https://aai.onap:8443/url"));
298 public void testAaiQuery_getMsbSuffixAddr_Ok() throws Exception {
299 PowerMock.resetAll();
300 String url = "/aai/v11/network/generic-vnfs/generic-vnf?";
301 String expect = "/api/aai-network/v11/generic-vnfs/generic-vnf?";
302 aaiQuery = new AaiQuery();
303 PowerMock.replayAll();
304 String actual = Whitebox.invokeMethod(aaiQuery, "getMsbSuffixAddr", url);
305 PowerMock.verifyAll();
306 assertThat(actual, equalTo(expect));