Added the Port Info for AAI Queries
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / aai / AaiQueryTest.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
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;
22
23 import java.util.HashMap;
24 import java.util.Map;
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;
45
46
47 @PrepareForTest({AaiQuery.class, HttpsUtils.class, MicroServiceConfig.class, HttpGet.class})
48 @PowerMockIgnore("javax.net.ssl.*")
49 @RunWith(PowerMockRunner.class)
50 public class AaiQueryTest {
51
52     @Rule
53     public ExpectedException thrown = ExpectedException.none();
54
55     private AaiQuery aaiQuery;
56     private AaiResponseUtil aaiResponseUtil;
57
58     @Test
59     public void testAaiQuery_getAaiVnfData_ok() throws Exception {
60         PowerMock.resetAll();
61         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVnfDataResponse");
62         aaiResponseUtil = new AaiResponseUtil();
63         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
64
65         PowerMock.expectPrivate(aaiQuery, "getVnfDataResponse", "test1", "test2").andReturn("{}");
66
67         PowerMock.replayAll();
68         VnfEntity vnfEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVnfData", "test1", "test2");
69         PowerMock.verifyAll();
70
71         assertThat(vnfEntity == null, equalTo(true));
72     }
73
74     @Test
75     public void testAaiQuery_getAaiVnfData_exception() throws Exception {
76         PowerMock.resetAll();
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")
83                 .andReturn("{***}");
84
85         PowerMock.replayAll();
86         aaiQuery.getAaiVnfData("test1", "test2");
87         PowerMock.verifyAll();
88     }
89
90     @Test
91     public void testAaiQuery_getAaiVmData_ok() throws Exception {
92         PowerMock.resetAll();
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("{}");
110
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();
118
119         assertThat(vmEntity == null, equalTo(true));
120     }
121
122     @Test
123
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");
129
130         aaiResponseUtil = new AaiResponseUtil();
131         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
132
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();
153     }
154
155     @Test
156     public void testAaiQuery_getVmResourceLinks_ok() throws Exception {
157         PowerMock.resetAll();
158         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResourceLinksResponse");
159
160         aaiResponseUtil = new AaiResponseUtil();
161         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
162
163         String result = "{\"result-data\":[{\"resource-type\":\"vserver\",\"resource-link\":\"le-vserver-id-val-51834\"}]}";
164
165         PowerMock.expectPrivate(aaiQuery, "getResourceLinksResponse", "test1", "test2").andReturn(result);
166         PowerMock.replayAll();
167         String resource = Whitebox.invokeMethod(aaiQuery, "getVmResourceLinks", "test1", "test2");
168         PowerMock.verifyAll();
169
170         assertThat(resource, equalTo("le-vserver-id-val-51834"));
171     }
172
173
174
175     @Test
176     public void testAaiQuery_getResourceLinksResponse() throws Exception {
177         PowerMock.resetAll();
178         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
179
180         aaiResponseUtil = new AaiResponseUtil();
181         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
182
183         PowerMockito.mockStatic(MicroServiceConfig.class);
184         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("host_url");
185
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();
190
191         assertThat(resource, equalTo(""));
192     }
193
194     @Test
195     public void testAaiQuery_getVnfDataResponse() throws Exception {
196         PowerMock.resetAll();
197         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
198
199         aaiResponseUtil = new AaiResponseUtil();
200         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
201
202         PowerMockito.mockStatic(MicroServiceConfig.class);
203         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("host_url");
204
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();
209
210         assertThat(resource, equalTo(""));
211     }
212
213     @Test
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";
224
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();
234
235         PowerMock.replayAll();
236         String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
237         PowerMock.verifyAll();
238
239         assertThat(resource, equalTo(""));
240     }
241
242     @Test
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();
248
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(""));
267     }
268
269     @Test
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();
276
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"));
281     }
282
283     @Test
284     public void testAaiQuery_getBaseUrl_aaiurl() throws Exception {
285         PowerMock.resetAll();
286         aaiQuery = new AaiQuery();
287
288         PowerMockito.mockStatic(MicroServiceConfig.class);
289
290         PowerMock.replayAll();
291         String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "/url");
292         PowerMock.verifyAll();
293
294         assertThat(actual, equalTo("https://aai.onap:8443/url"));
295     }
296
297     @Test
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));
307     }
308 }