b3f1eb6c45c9cbede96c262cdf96ce4aceccbe9e
[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.CoreMatchers.equalTo;
20 import static org.hamcrest.MatcherAssert.assertThat;
21 import static org.powermock.api.mockito.PowerMockito.when;
22
23 import java.util.HashMap;
24 import java.util.Map;
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;
40
41
42 @PrepareForTest({AaiQuery.class, HttpsUtils.class, MicroServiceConfig.class})
43 @RunWith(PowerMockRunner.class)
44 public class AaiQueryTest {
45
46     @Rule
47     public ExpectedException thrown = ExpectedException.none();
48
49     private AaiQuery aaiQuery;
50     private AaiResponseUtil aaiResponseUtil;
51
52     @Test
53     public void testAaiQuery_getAaiVnfData_ok() throws Exception {
54         PowerMock.resetAll();
55         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVnfDataResponse");
56         aaiResponseUtil = new AaiResponseUtil();
57         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
58
59         PowerMock.expectPrivate(aaiQuery, "getVnfDataResponse", "test1", "test2").andReturn("{}");
60
61         PowerMock.replayAll();
62         VnfEntity vnfEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVnfData", "test1", "test2");
63         PowerMock.verifyAll();
64
65         assertThat(vnfEntity == null, equalTo(true));
66     }
67
68     @Test
69     public void testAaiQuery_getAaiVnfData_exception() throws Exception {
70         PowerMock.resetAll();
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")
77                 .andReturn("{***}");
78
79         PowerMock.replayAll();
80         aaiQuery.getAaiVnfData("test1", "test2");
81         PowerMock.verifyAll();
82     }
83
84     @Test
85     public void testAaiQuery_getAaiVmData_ok() throws Exception {
86         PowerMock.resetAll();
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("{}");
98
99         PowerMockito.mockStatic(MicroServiceConfig.class);
100         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
101
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();
107
108         assertThat(vmEntity == null, equalTo(true));
109     }
110
111
112
113     @Test
114     public void testAaiQuery_getAaiVmData_exception() throws Exception {
115         PowerMock.resetAll();
116         thrown.expect(CorrelationException.class);
117         thrown.expectMessage("Failed to convert aai vm response data to vm entity");
118         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
119
120         aaiResponseUtil = new AaiResponseUtil();
121         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
122
123         PowerMockito.mockStatic(HttpsUtils.class);
124         Map<String, String> headers = new HashMap<>();
125         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
126         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
127         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
128         headers.put("Accept", "application/json");
129         String url = "http://10.96.33.33/api/aai-cloudInfrastructure/v11";
130
131         when(HttpsUtils.get(url, headers)).thenReturn("");
132
133         PowerMockito.mockStatic(MicroServiceConfig.class);
134         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
135
136         PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2")
137                 .andReturn("/aai/v11/cloud-infrastructure");
138
139         PowerMock.replayAll();
140         Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
141         PowerMock.verifyAll();
142     }
143
144     @Test
145     public void testAaiQuery_getAaiVmData_httpsutils_exception() throws Exception {
146         PowerMock.resetAll();
147         thrown.expect(CorrelationException.class);
148         thrown.expectMessage("Failed to get data from aai");
149         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
150
151         aaiResponseUtil = new AaiResponseUtil();
152         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
153
154         PowerMockito.mockStatic(HttpsUtils.class);
155         Map<String, String> headers = new HashMap<>();
156         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
157         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
158         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
159         headers.put("Accept", "application/json");
160         String url = "http://10.96.33.33/api/aai-cloudInfrastructure/v11";
161
162         when(HttpsUtils.get(url, headers)).thenThrow(new CorrelationException(""));
163
164         PowerMockito.mockStatic(MicroServiceConfig.class);
165         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
166
167         PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2")
168                 .andReturn("/aai/v11/cloud-infrastructure");
169         PowerMock.replayAll();
170         Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
171         PowerMock.verifyAll();
172     }
173
174     @Test
175     public void testAaiQuery_getVmResourceLinks_ok() throws Exception {
176         PowerMock.resetAll();
177         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResourceLinksResponse");
178
179         aaiResponseUtil = new AaiResponseUtil();
180         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
181
182         String result = "{\"result-data\":[{\"resource-type\":\"vserver\",\"resource-link\":\"le-vserver-id-val-51834\"}]}";
183
184         PowerMock.expectPrivate(aaiQuery, "getResourceLinksResponse", "test1", "test2").andReturn(result);
185         PowerMock.replayAll();
186         String resource = Whitebox.invokeMethod(aaiQuery, "getVmResourceLinks", "test1", "test2");
187         PowerMock.verifyAll();
188
189         assertThat(resource, equalTo("le-vserver-id-val-51834"));
190     }
191
192     @Test
193     public void testAaiQuery_getVmResourceLinks_exception() throws Exception {
194         PowerMock.resetAll();
195         thrown.expect(CorrelationException.class);
196         thrown.expectMessage("Failed to get aai resource link");
197         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResourceLinksResponse");
198
199         aaiResponseUtil = new AaiResponseUtil();
200         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
201
202         PowerMock.expectPrivate(aaiQuery, "getResourceLinksResponse", "test1", "test2").andReturn("");
203         PowerMock.replayAll();
204         String resource = Whitebox.invokeMethod(aaiQuery, "getVmResourceLinks", "test1", "test2");
205         PowerMock.verifyAll();
206
207         assertThat(resource, equalTo("le-vserver-id-val-51834"));
208     }
209
210     @Test
211     public void testAaiQuery_getResourceLinksResponse() throws Exception {
212         PowerMock.resetAll();
213         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
214
215         aaiResponseUtil = new AaiResponseUtil();
216         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
217
218         PowerMockito.mockStatic(MicroServiceConfig.class);
219         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("host_url");
220
221         PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
222         PowerMock.replayAll();
223         String resource = Whitebox.invokeMethod(aaiQuery, "getResourceLinksResponse", "test1", "test2");
224         PowerMock.verifyAll();
225
226         assertThat(resource, equalTo(""));
227     }
228
229     @Test
230     public void testAaiQuery_getVnfDataResponse() throws Exception {
231         PowerMock.resetAll();
232         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
233
234         aaiResponseUtil = new AaiResponseUtil();
235         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
236
237         PowerMockito.mockStatic(MicroServiceConfig.class);
238         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("host_url");
239
240         PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
241         PowerMock.replayAll();
242         String resource = Whitebox.invokeMethod(aaiQuery, "getVnfDataResponse", "test1", "test2");
243         PowerMock.verifyAll();
244
245         assertThat(resource, equalTo(""));
246     }
247
248     @Test
249     public void testAaiQuery_getResponse_ok() throws Exception {
250         PowerMock.resetAll();
251         aaiQuery = new AaiQuery();
252         PowerMockito.mockStatic(HttpsUtils.class);
253         Map<String, String> headers = new HashMap<>();
254         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
255         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
256         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
257         headers.put("Accept", "application/json");
258         String url = "host_url";
259
260         when(HttpsUtils.get(url, headers)).thenReturn("");
261
262         PowerMock.replayAll();
263         String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
264         PowerMock.verifyAll();
265
266         assertThat(resource, equalTo(""));
267     }
268
269     @Test
270     public void testAaiQuery_getResponse_exceptioin() throws Exception {
271         PowerMock.resetAll();
272         thrown.expect(CorrelationException.class);
273         thrown.expectMessage("Failed to get data from aai");
274         aaiQuery = new AaiQuery();
275
276         PowerMockito.mockStatic(HttpsUtils.class);
277         Map<String, String> headers = new HashMap<>();
278         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
279         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
280         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
281         headers.put("Accept", "application/json");
282         String url = "host_url";
283         when(HttpsUtils.get(url, headers)).thenThrow(new CorrelationException(""));
284         PowerMock.replayAll();
285         String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
286         PowerMock.verifyAll();
287         assertThat(resource, equalTo(""));
288     }
289
290     @Test
291     public void testAaiQuery_getHeaders() throws Exception {
292         PowerMock.resetAll();
293         aaiQuery = new AaiQuery();
294         PowerMock.replayAll();
295         Map actual = Whitebox.invokeMethod(aaiQuery, "getHeaders");
296         PowerMock.verifyAll();
297
298         assertThat(actual.get("X-TransactionId"), equalTo("9999"));
299         assertThat(actual.get("X-FromAppId"), equalTo("jimmy-postman"));
300         assertThat(actual.get("Authorization"), equalTo("Basic QUFJOkFBSQ=="));
301         assertThat(actual.get("Accept"), equalTo("application/json"));
302     }
303
304     @Test
305     public void testAaiQuery_getBaseUrl_msb() throws Exception {
306         PowerMock.resetAll();
307         aaiQuery = new AaiQuery();
308
309         PowerMockito.mockStatic(MicroServiceConfig.class);
310         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
311         when(MicroServiceConfig.getServiceConfigInfoFromCBS("nihao")).thenReturn("");
312
313         PowerMock.replayAll();
314         String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "/url");
315         PowerMock.verifyAll();
316         assertThat(actual, equalTo("http://10.96.33.33/url"));
317     }
318
319     @Test
320     public void testAaiQuery_getBaseUrl_aaiurl() throws Exception {
321         PowerMock.resetAll();
322         aaiQuery = new AaiQuery();
323
324         PowerMockito.mockStatic(MicroServiceConfig.class);
325         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenThrow(new NullPointerException());
326         when(MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config")).thenReturn("aai");
327
328         PowerMock.replayAll();
329         String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "url");
330         System.out.println(actual);
331         PowerMock.verifyAll();
332         assertThat(actual, equalTo("https://aaiurl"));
333     }
334
335     @Test
336     public void testAaiQuery_getBaseUrl_exception() throws Exception {
337         PowerMock.resetAll();
338         aaiQuery = new AaiQuery();
339
340         PowerMockito.mockStatic(MicroServiceConfig.class);
341         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenThrow(new NullPointerException());
342         when(MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config"))
343                 .thenThrow(new NullPointerException());
344
345         PowerMock.replayAll();
346         String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "url");
347         System.out.println(actual);
348         PowerMock.verifyAll();
349         assertThat(actual, equalTo(""));
350     }
351
352     @Test
353     public void testAaiQuery_getMsbSuffixAddr_Ok() throws Exception {
354         PowerMock.resetAll();
355         String url = "/aai/v11/network/generic-vnfs/generic-vnf?";
356         String expect = "/api/aai-network/v11/generic-vnfs/generic-vnf?";
357         aaiQuery = new AaiQuery();
358         PowerMock.replayAll();
359         String actual = Whitebox.invokeMethod(aaiQuery, "getMsbSuffixAddr", url);
360         PowerMock.verifyAll();
361         assertThat(actual, equalTo(expect));
362     }
363 }