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