2 * Copyright 2017-2020 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;
19 import org.apache.http.HttpResponse;
20 import org.apache.http.client.methods.HttpGet;
21 import org.apache.http.impl.client.CloseableHttpClient;
22 import org.easymock.EasyMock;
23 import org.junit.BeforeClass;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.junit.runner.RunWith;
28 import org.onap.holmes.common.aai.config.AaiConfig;
29 import org.onap.holmes.common.aai.entity.VmEntity;
30 import org.onap.holmes.common.aai.entity.VnfEntity;
31 import org.onap.holmes.common.config.MicroServiceConfig;
32 import org.onap.holmes.common.exception.CorrelationException;
33 import org.onap.holmes.common.utils.HttpsUtils;
34 import org.powermock.api.easymock.PowerMock;
35 import org.powermock.core.classloader.annotations.PowerMockIgnore;
36 import org.powermock.core.classloader.annotations.PrepareForTest;
37 import org.powermock.modules.junit4.PowerMockRunner;
38 import org.powermock.reflect.Whitebox;
40 import java.util.HashMap;
43 import static org.easymock.EasyMock.anyObject;
44 import static org.easymock.EasyMock.expect;
45 import static org.hamcrest.core.IsEqual.equalTo;
46 import static org.junit.Assert.assertThat;
49 @PrepareForTest({AaiQuery.class, HttpsUtils.class, MicroServiceConfig.class, HttpGet.class})
50 @PowerMockIgnore("javax.net.ssl.*")
51 @RunWith(PowerMockRunner.class)
52 public class AaiQueryTest {
55 public ExpectedException thrown = ExpectedException.none();
57 private AaiQuery aaiQuery;
58 private AaiResponseUtil aaiResponseUtil;
61 static public void before() {
62 System.setProperty("ENABLE_ENCRYPT", "true");
66 public void testAaiQuery_getAaiVnfData_ok() throws Exception {
68 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVnfDataResponse");
69 aaiResponseUtil = new AaiResponseUtil();
70 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
72 PowerMock.expectPrivate(aaiQuery, "getVnfDataResponse", "test1", "test2").andReturn("{}");
74 PowerMock.replayAll();
75 VnfEntity vnfEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVnfData", "test1", "test2");
76 PowerMock.verifyAll();
78 assertThat(vnfEntity == null, equalTo(true));
82 public void testAaiQuery_getAaiVnfData_exception() throws Exception {
84 thrown.expect(CorrelationException.class);
85 thrown.expectMessage("Failed to convert aai vnf response data to vnf entity");
86 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVnfDataResponse");
87 aaiResponseUtil = new AaiResponseUtil();
88 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
89 PowerMock.expectPrivate(aaiQuery, "getVnfDataResponse", "test1", "test2")
92 PowerMock.replayAll();
93 aaiQuery.getAaiVnfData("test1", "test2");
94 PowerMock.verifyAll();
98 public void testAaiQuery_getAaiVmData_ok() throws Exception {
100 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
101 aaiResponseUtil = new AaiResponseUtil();
102 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
103 PowerMock.mockStatic(HttpsUtils.class);
104 Map<String, String> headers = new HashMap<>();
105 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
106 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
107 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
108 headers.put("Accept", "application/json");
109 String url = "https://aai.onap:8443/aai/v11/cloud-infrastructure";
110 HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
111 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
112 expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient);
113 HttpGet httpGet = new HttpGet(url);
114 PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
115 expect(HttpsUtils.get(anyObject(HttpGet.class), anyObject(Map.class),
116 anyObject(CloseableHttpClient.class))).andReturn(httpResponse);
117 expect(HttpsUtils.extractResponseEntity(httpResponse)).andReturn("{}");
119 PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2")
120 .andReturn("/aai/v11/cloud-infrastructure");
121 PowerMock.expectPrivate(httpClient, "close");
122 EasyMock.expectLastCall();
123 PowerMock.replayAll();
124 VmEntity vmEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
125 PowerMock.verifyAll();
127 assertThat(vmEntity == null, equalTo(true));
131 public void testAaiQuery_getAaiVmData_httpsutils_exception() throws Exception {
132 PowerMock.resetAll();
133 thrown.expect(CorrelationException.class);
134 thrown.expectMessage("Failed to get data from aai");
135 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
137 aaiResponseUtil = new AaiResponseUtil();
138 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
140 PowerMock.mockStatic(HttpsUtils.class);
141 Map<String, String> headers = new HashMap<>();
142 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
143 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
144 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
145 headers.put("Accept", "application/json");
146 String url = "https://aai.onap:8443/aai/v11/cloud-infrastructure";
147 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
148 EasyMock.expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient);
149 HttpGet httpGet = new HttpGet(url);
150 PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
151 EasyMock.expect(HttpsUtils.get(anyObject(HttpGet.class), anyObject(Map.class),
152 anyObject(CloseableHttpClient.class))).andThrow(new CorrelationException(""));
153 PowerMock.mockStatic(MicroServiceConfig.class);
154 PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2")
155 .andReturn("/aai/v11/cloud-infrastructure");
156 PowerMock.expectPrivate(httpClient, "close");
157 EasyMock.expectLastCall();
158 PowerMock.replayAll();
159 Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
160 PowerMock.verifyAll();
164 public void testAaiQuery_getVmResourceLinks_ok() throws Exception {
165 PowerMock.resetAll();
166 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResourceLinksResponse");
168 aaiResponseUtil = new AaiResponseUtil();
169 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
171 String result = "{\"result-data\":[{\"resource-type\":\"vserver\",\"resource-link\":\"le-vserver-id-val-51834\"}]}";
173 PowerMock.expectPrivate(aaiQuery, "getResourceLinksResponse", "test1", "test2").andReturn(result);
174 PowerMock.replayAll();
175 String resource = Whitebox.invokeMethod(aaiQuery, "getVmResourceLinks", "test1", "test2");
176 PowerMock.verifyAll();
178 assertThat(resource, equalTo("le-vserver-id-val-51834"));
183 public void testAaiQuery_getResourceLinksResponse() throws Exception {
184 PowerMock.resetAll();
185 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
187 aaiResponseUtil = new AaiResponseUtil();
188 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
190 PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
191 PowerMock.replayAll();
192 String resource = Whitebox.invokeMethod(aaiQuery, "getResourceLinksResponse", "test1", "test2");
193 PowerMock.verifyAll();
195 assertThat(resource, equalTo(""));
199 public void testAaiQuery_getVnfDataResponse() throws Exception {
200 PowerMock.resetAll();
201 aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
203 aaiResponseUtil = new AaiResponseUtil();
204 Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
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 PowerMock.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 expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient);
229 HttpGet httpGet = new HttpGet(url);
230 PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
231 expect(HttpsUtils.get(anyObject(HttpGet.class), anyObject(Map.class),
232 anyObject(CloseableHttpClient.class))).andReturn(httpResponse);
233 expect(HttpsUtils.extractResponseEntity(httpResponse)).andReturn("");
234 PowerMock.expectPrivate(httpClient, "close");
235 EasyMock.expectLastCall();
237 PowerMock.replayAll();
238 String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
239 PowerMock.verifyAll();
241 assertThat(resource, equalTo(""));
245 public void testAaiQuery_getResponse_exceptioin() throws Exception {
246 PowerMock.resetAll();
247 thrown.expect(CorrelationException.class);
248 thrown.expectMessage("Failed to get data from aai");
249 aaiQuery = new AaiQuery();
251 PowerMock.mockStatic(HttpsUtils.class);
252 Map<String, String> headers = new HashMap<>();
253 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
254 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
255 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
256 headers.put("Accept", "application/json");
257 String url = "host_url";
258 CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
259 expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient);
260 HttpGet httpGet = new HttpGet(url);
261 PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
262 expect(HttpsUtils.get(httpGet, headers, httpClient)).andThrow(new CorrelationException(""));
263 PowerMock.expectPrivate(httpClient, "close");
264 EasyMock.expectLastCall();
265 PowerMock.replayAll();
266 String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
267 PowerMock.verifyAll();
268 assertThat(resource, equalTo(""));
272 public void testAaiQuery_getHeaders() throws Exception {
273 PowerMock.resetAll();
274 aaiQuery = new AaiQuery();
275 PowerMock.replayAll();
276 Map actual = Whitebox.invokeMethod(aaiQuery, "getHeaders");
277 PowerMock.verifyAll();
279 assertThat(actual.get("X-TransactionId"), equalTo("9999"));
280 assertThat(actual.get("X-FromAppId"), equalTo("jimmy-postman"));
281 assertThat(actual.get("Authorization"), equalTo("Basic QUFJOkFBSQ=="));
282 assertThat(actual.get("Accept"), equalTo("application/json"));
286 public void testAaiQuery_getBaseUrl_aaiurl() throws Exception {
287 PowerMock.resetAll();
288 aaiQuery = new AaiQuery();
290 PowerMock.mockStatic(MicroServiceConfig.class);
292 PowerMock.replayAll();
293 String actual = Whitebox.invokeMethod(aaiQuery, "getBaseUrl", "/url");
294 PowerMock.verifyAll();
296 assertThat(actual, equalTo("https://aai.onap:8443/url"));