fix https bug
[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.impl.client.CloseableHttpClient;
27 import org.easymock.EasyMock;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
31 import org.junit.runner.RunWith;
32 import org.onap.holmes.common.aai.config.AaiConfig;
33 import org.onap.holmes.common.aai.entity.VmEntity;
34 import org.onap.holmes.common.aai.entity.VnfEntity;
35 import org.onap.holmes.common.config.MicroServiceConfig;
36 import org.onap.holmes.common.exception.CorrelationException;
37 import org.onap.holmes.common.utils.HttpsUtils;
38 import org.powermock.api.easymock.PowerMock;
39 import org.powermock.api.mockito.PowerMockito;
40 import org.powermock.core.classloader.annotations.PrepareForTest;
41 import org.powermock.modules.junit4.PowerMockRunner;
42 import org.powermock.reflect.Whitebox;
43
44
45 @PrepareForTest({AaiQuery.class, HttpsUtils.class, MicroServiceConfig.class})
46 @RunWith(PowerMockRunner.class)
47 public class AaiQueryTest {
48
49     @Rule
50     public ExpectedException thrown = ExpectedException.none();
51
52     private AaiQuery aaiQuery;
53     private AaiResponseUtil aaiResponseUtil;
54
55     @Test
56     public void testAaiQuery_getAaiVnfData_ok() throws Exception {
57         PowerMock.resetAll();
58         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVnfDataResponse");
59         aaiResponseUtil = new AaiResponseUtil();
60         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
61
62         PowerMock.expectPrivate(aaiQuery, "getVnfDataResponse", "test1", "test2").andReturn("{}");
63
64         PowerMock.replayAll();
65         VnfEntity vnfEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVnfData", "test1", "test2");
66         PowerMock.verifyAll();
67
68         assertThat(vnfEntity == null, equalTo(true));
69     }
70
71     @Test
72     public void testAaiQuery_getAaiVnfData_exception() throws Exception {
73         PowerMock.resetAll();
74         thrown.expect(CorrelationException.class);
75         thrown.expectMessage("Failed to convert aai vnf response data to vnf entity");
76         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVnfDataResponse");
77         aaiResponseUtil = new AaiResponseUtil();
78         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
79         PowerMock.expectPrivate(aaiQuery, "getVnfDataResponse", "test1", "test2")
80                 .andReturn("{***}");
81
82         PowerMock.replayAll();
83         aaiQuery.getAaiVnfData("test1", "test2");
84         PowerMock.verifyAll();
85     }
86
87     @Test
88     public void testAaiQuery_getAaiVmData_ok() throws Exception {
89         PowerMock.resetAll();
90         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
91         aaiResponseUtil = new AaiResponseUtil();
92         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
93         PowerMockito.mockStatic(HttpsUtils.class);
94         Map<String, String> headers = new HashMap<>();
95         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
96         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
97         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
98         headers.put("Accept", "application/json");
99         String url = "http://10.96.33.33/api/aai-cloudInfrastructure/v11";
100         HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
101         CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
102         when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
103         when(HttpsUtils.get(url, headers, httpClient)).thenReturn(httpResponse);
104         when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn("{}");
105
106         PowerMockito.mockStatic(MicroServiceConfig.class);
107         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
108
109         PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2")
110                 .andReturn("/aai/v11/cloud-infrastructure");
111         PowerMock.expectPrivate(httpClient, "close");
112         EasyMock.expectLastCall();
113         PowerMock.replayAll();
114         VmEntity vmEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
115         PowerMock.verifyAll();
116
117         assertThat(vmEntity == null, equalTo(true));
118     }
119
120     @Test
121     public void testAaiQuery_getAaiVmData_httpsutils_exception() throws Exception {
122         PowerMock.resetAll();
123         thrown.expect(CorrelationException.class);
124         thrown.expectMessage("Failed to get data from aai");
125         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
126
127         aaiResponseUtil = new AaiResponseUtil();
128         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
129
130         PowerMockito.mockStatic(HttpsUtils.class);
131         Map<String, String> headers = new HashMap<>();
132         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
133         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
134         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
135         headers.put("Accept", "application/json");
136         String url = "http://10.96.33.33/api/aai-cloudInfrastructure/v11";
137         CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
138         when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
139         when(HttpsUtils.get(url, headers, httpClient)).thenThrow(new CorrelationException(""));
140         PowerMockito.mockStatic(MicroServiceConfig.class);
141         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
142         PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2")
143                 .andReturn("/aai/v11/cloud-infrastructure");
144         PowerMock.expectPrivate(httpClient,"close");
145         EasyMock.expectLastCall();
146         PowerMock.replayAll();
147         Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
148         PowerMock.verifyAll();
149     }
150
151     @Test
152     public void testAaiQuery_getVmResourceLinks_ok() throws Exception {
153         PowerMock.resetAll();
154         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResourceLinksResponse");
155
156         aaiResponseUtil = new AaiResponseUtil();
157         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
158
159         String result = "{\"result-data\":[{\"resource-type\":\"vserver\",\"resource-link\":\"le-vserver-id-val-51834\"}]}";
160
161         PowerMock.expectPrivate(aaiQuery, "getResourceLinksResponse", "test1", "test2").andReturn(result);
162         PowerMock.replayAll();
163         String resource = Whitebox.invokeMethod(aaiQuery, "getVmResourceLinks", "test1", "test2");
164         PowerMock.verifyAll();
165
166         assertThat(resource, equalTo("le-vserver-id-val-51834"));
167     }
168
169
170
171     @Test
172     public void testAaiQuery_getResourceLinksResponse() throws Exception {
173         PowerMock.resetAll();
174         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
175
176         aaiResponseUtil = new AaiResponseUtil();
177         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
178
179         PowerMockito.mockStatic(MicroServiceConfig.class);
180         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("host_url");
181
182         PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
183         PowerMock.replayAll();
184         String resource = Whitebox.invokeMethod(aaiQuery, "getResourceLinksResponse", "test1", "test2");
185         PowerMock.verifyAll();
186
187         assertThat(resource, equalTo(""));
188     }
189
190     @Test
191     public void testAaiQuery_getVnfDataResponse() throws Exception {
192         PowerMock.resetAll();
193         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
194
195         aaiResponseUtil = new AaiResponseUtil();
196         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
197
198         PowerMockito.mockStatic(MicroServiceConfig.class);
199         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("host_url");
200
201         PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
202         PowerMock.replayAll();
203         String resource = Whitebox.invokeMethod(aaiQuery, "getVnfDataResponse", "test1", "test2");
204         PowerMock.verifyAll();
205
206         assertThat(resource, equalTo(""));
207     }
208
209     @Test
210     public void testAaiQuery_getResponse_ok() throws Exception {
211         PowerMock.resetAll();
212         aaiQuery = new AaiQuery();
213         PowerMockito.mockStatic(HttpsUtils.class);
214         Map<String, String> headers = new HashMap<>();
215         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
216         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
217         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
218         headers.put("Accept", "application/json");
219         String url = "host_url";
220
221         HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
222         CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
223         when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
224         when(HttpsUtils.get(url, headers, httpClient)).thenReturn(httpResponse);
225         when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn("");
226         PowerMock.expectPrivate(httpClient, "close");
227         EasyMock.expectLastCall();
228
229         PowerMock.replayAll();
230         String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
231         PowerMock.verifyAll();
232
233         assertThat(resource, equalTo(""));
234     }
235
236     @Test
237     public void testAaiQuery_getResponse_exceptioin() throws Exception {
238         PowerMock.resetAll();
239         thrown.expect(CorrelationException.class);
240         thrown.expectMessage("Failed to get data from aai");
241         aaiQuery = new AaiQuery();
242
243         PowerMockito.mockStatic(HttpsUtils.class);
244         Map<String, String> headers = new HashMap<>();
245         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
246         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
247         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
248         headers.put("Accept", "application/json");
249         String url = "host_url";
250         CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
251         when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
252         when(HttpsUtils.get(url, headers, httpClient)).thenThrow(new CorrelationException(""));
253         PowerMock.expectPrivate(httpClient, "close");
254         EasyMock.expectLastCall();
255         PowerMock.replayAll();
256         String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
257         PowerMock.verifyAll();
258         assertThat(resource, equalTo(""));
259     }
260
261     @Test
262     public void testAaiQuery_getHeaders() throws Exception {
263         PowerMock.resetAll();
264         aaiQuery = new AaiQuery();
265         PowerMock.replayAll();
266         Map actual = Whitebox.invokeMethod(aaiQuery, "getHeaders");
267         PowerMock.verifyAll();
268
269         assertThat(actual.get("X-TransactionId"), equalTo("9999"));
270         assertThat(actual.get("X-FromAppId"), equalTo("jimmy-postman"));
271         assertThat(actual.get("Authorization"), equalTo("Basic QUFJOkFBSQ=="));
272         assertThat(actual.get("Accept"), equalTo("application/json"));
273     }
274
275     @Test
276     public void testAaiQuery_getBaseUrl_msb() throws Exception {
277         PowerMock.resetAll();
278         aaiQuery = new AaiQuery();
279
280         PowerMockito.mockStatic(MicroServiceConfig.class);
281         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
282         when(MicroServiceConfig.getServiceConfigInfoFromCBS("nihao")).thenReturn("");
283
284         PowerMock.replayAll();
285         String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "/url");
286         PowerMock.verifyAll();
287         assertThat(actual, equalTo("http://10.96.33.33/url"));
288     }
289
290     @Test
291     public void testAaiQuery_getBaseUrl_aaiurl() throws Exception {
292         PowerMock.resetAll();
293         aaiQuery = new AaiQuery();
294
295         PowerMockito.mockStatic(MicroServiceConfig.class);
296         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenThrow(new NullPointerException());
297         when(MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config")).thenReturn("aai");
298
299         PowerMock.replayAll();
300         String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "url");
301         System.out.println(actual);
302         PowerMock.verifyAll();
303         assertThat(actual, equalTo("https://aaiurl"));
304     }
305
306     @Test
307     public void testAaiQuery_getBaseUrl_exception() throws Exception {
308         PowerMock.resetAll();
309         aaiQuery = new AaiQuery();
310
311         PowerMockito.mockStatic(MicroServiceConfig.class);
312         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenThrow(new NullPointerException());
313         when(MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config"))
314                 .thenThrow(new NullPointerException());
315
316         PowerMock.replayAll();
317         String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "url");
318         System.out.println(actual);
319         PowerMock.verifyAll();
320         assertThat(actual, equalTo(""));
321     }
322
323     @Test
324     public void testAaiQuery_getMsbSuffixAddr_Ok() throws Exception {
325         PowerMock.resetAll();
326         String url = "/aai/v11/network/generic-vnfs/generic-vnf?";
327         String expect = "/api/aai-network/v11/generic-vnfs/generic-vnf?";
328         aaiQuery = new AaiQuery();
329         PowerMock.replayAll();
330         String actual = Whitebox.invokeMethod(aaiQuery, "getMsbSuffixAddr", url);
331         PowerMock.verifyAll();
332         assertThat(actual, equalTo(expect));
333     }
334 }