Make HTTP/s Optional
[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.easymock.EasyMock.expect;
20 import static org.hamcrest.core.IsEqual.equalTo;
21 import static org.junit.Assert.assertThat;
22 import static org.powermock.api.mockito.PowerMockito.when;
23
24 import java.util.HashMap;
25 import java.util.Map;
26 import org.apache.http.HttpResponse;
27 import org.apache.http.client.methods.HttpGet;
28 import org.apache.http.impl.client.CloseableHttpClient;
29 import org.easymock.EasyMock;
30 import org.junit.BeforeClass;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.rules.ExpectedException;
34 import org.junit.runner.RunWith;
35 import org.onap.holmes.common.aai.config.AaiConfig;
36 import org.onap.holmes.common.aai.entity.VmEntity;
37 import org.onap.holmes.common.aai.entity.VnfEntity;
38 import org.onap.holmes.common.config.MicroServiceConfig;
39 import org.onap.holmes.common.exception.CorrelationException;
40 import org.onap.holmes.common.utils.HttpsUtils;
41 import org.powermock.api.easymock.PowerMock;
42 import org.powermock.api.mockito.PowerMockito;
43 import org.powermock.core.classloader.annotations.PowerMockIgnore;
44 import org.powermock.core.classloader.annotations.PrepareForTest;
45 import org.powermock.modules.junit4.PowerMockRunner;
46 import org.powermock.reflect.Whitebox;
47
48
49 @PrepareForTest({AaiQuery.class, HttpsUtils.class, MicroServiceConfig.class, HttpGet.class})
50 @PowerMockIgnore("javax.net.ssl.*")
51 @RunWith(PowerMockRunner.class)
52 public class AaiQueryTest {
53
54     @Rule
55     public ExpectedException thrown = ExpectedException.none();
56
57     private AaiQuery aaiQuery;
58     private AaiResponseUtil aaiResponseUtil;
59
60     @BeforeClass
61     static public void before() {
62         System.setProperty("ENABLE_ENCRYPT", "true");
63     }
64
65     @Test
66     public void testAaiQuery_getAaiVnfData_ok() throws Exception {
67         PowerMock.resetAll();
68         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVnfDataResponse");
69         aaiResponseUtil = new AaiResponseUtil();
70         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
71
72         PowerMock.expectPrivate(aaiQuery, "getVnfDataResponse", "test1", "test2").andReturn("{}");
73
74         PowerMock.replayAll();
75         VnfEntity vnfEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVnfData", "test1", "test2");
76         PowerMock.verifyAll();
77
78         assertThat(vnfEntity == null, equalTo(true));
79     }
80
81     @Test
82     public void testAaiQuery_getAaiVnfData_exception() throws Exception {
83         PowerMock.resetAll();
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")
90                 .andReturn("{***}");
91
92         PowerMock.replayAll();
93         aaiQuery.getAaiVnfData("test1", "test2");
94         PowerMock.verifyAll();
95     }
96
97     @Test
98     public void testAaiQuery_getAaiVmData_ok() throws Exception {
99         PowerMock.resetAll();
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("{}");
118
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();
126
127         assertThat(vmEntity == null, equalTo(true));
128     }
129
130     @Test
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");
136
137         aaiResponseUtil = new AaiResponseUtil();
138         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
139
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();
161     }
162
163     @Test
164     public void testAaiQuery_getVmResourceLinks_ok() throws Exception {
165         PowerMock.resetAll();
166         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResourceLinksResponse");
167
168         aaiResponseUtil = new AaiResponseUtil();
169         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
170
171         String result = "{\"result-data\":[{\"resource-type\":\"vserver\",\"resource-link\":\"le-vserver-id-val-51834\"}]}";
172
173         PowerMock.expectPrivate(aaiQuery, "getResourceLinksResponse", "test1", "test2").andReturn(result);
174         PowerMock.replayAll();
175         String resource = Whitebox.invokeMethod(aaiQuery, "getVmResourceLinks", "test1", "test2");
176         PowerMock.verifyAll();
177
178         assertThat(resource, equalTo("le-vserver-id-val-51834"));
179     }
180
181
182
183     @Test
184     public void testAaiQuery_getResourceLinksResponse() 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, "getResourceLinksResponse", "test1", "test2");
197         PowerMock.verifyAll();
198
199         assertThat(resource, equalTo(""));
200     }
201
202     @Test
203     public void testAaiQuery_getVnfDataResponse() throws Exception {
204         PowerMock.resetAll();
205         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
206
207         aaiResponseUtil = new AaiResponseUtil();
208         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
209
210         PowerMockito.mockStatic(MicroServiceConfig.class);
211         when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("host_url");
212
213         PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
214         PowerMock.replayAll();
215         String resource = Whitebox.invokeMethod(aaiQuery, "getVnfDataResponse", "test1", "test2");
216         PowerMock.verifyAll();
217
218         assertThat(resource, equalTo(""));
219     }
220
221     @Test
222     public void testAaiQuery_getResponse_ok() throws Exception {
223         PowerMock.resetAll();
224         aaiQuery = new AaiQuery();
225         PowerMock.mockStatic(HttpsUtils.class);
226         Map<String, String> headers = new HashMap<>();
227         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
228         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
229         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
230         headers.put("Accept", "application/json");
231         String url = "host_url";
232
233         HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
234         CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
235         expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient);
236         HttpGet httpGet = new HttpGet(url);
237         PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
238         expect(HttpsUtils.get(anyObject(HttpGet.class), anyObject(Map.class),
239                 anyObject(CloseableHttpClient.class))).andReturn(httpResponse);
240         expect(HttpsUtils.extractResponseEntity(httpResponse)).andReturn("");
241         PowerMock.expectPrivate(httpClient, "close");
242         EasyMock.expectLastCall();
243
244         PowerMock.replayAll();
245         String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
246         PowerMock.verifyAll();
247
248         assertThat(resource, equalTo(""));
249     }
250
251     @Test
252     public void testAaiQuery_getResponse_exceptioin() throws Exception {
253         PowerMock.resetAll();
254         thrown.expect(CorrelationException.class);
255         thrown.expectMessage("Failed to get data from aai");
256         aaiQuery = new AaiQuery();
257
258         PowerMock.mockStatic(HttpsUtils.class);
259         Map<String, String> headers = new HashMap<>();
260         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
261         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
262         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
263         headers.put("Accept", "application/json");
264         String url = "host_url";
265         CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
266         expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient);
267         HttpGet httpGet = new HttpGet(url);
268         PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
269         expect(HttpsUtils.get(httpGet, headers, httpClient)).andThrow(new CorrelationException(""));
270         PowerMock.expectPrivate(httpClient, "close");
271         EasyMock.expectLastCall();
272         PowerMock.replayAll();
273         String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
274         PowerMock.verifyAll();
275         assertThat(resource, equalTo(""));
276     }
277
278     @Test
279     public void testAaiQuery_getHeaders() throws Exception {
280         PowerMock.resetAll();
281         aaiQuery = new AaiQuery();
282         PowerMock.replayAll();
283         Map actual = Whitebox.invokeMethod(aaiQuery, "getHeaders");
284         PowerMock.verifyAll();
285
286         assertThat(actual.get("X-TransactionId"), equalTo("9999"));
287         assertThat(actual.get("X-FromAppId"), equalTo("jimmy-postman"));
288         assertThat(actual.get("Authorization"), equalTo("Basic QUFJOkFBSQ=="));
289         assertThat(actual.get("Accept"), equalTo("application/json"));
290     }
291
292     @Test
293     public void testAaiQuery_getBaseUrl_aaiurl() throws Exception {
294         PowerMock.resetAll();
295         aaiQuery = new AaiQuery();
296
297         PowerMockito.mockStatic(MicroServiceConfig.class);
298
299         PowerMock.replayAll();
300         String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "/url");
301         PowerMock.verifyAll();
302
303         assertThat(actual, equalTo("https://aai.onap:8443/url"));
304     }
305
306     @Test
307     public void testAaiQuery_getMsbSuffixAddr_Ok() throws Exception {
308         PowerMock.resetAll();
309         String url = "/aai/v11/network/generic-vnfs/generic-vnf?";
310         String expect = "/api/aai-network/v11/generic-vnfs/generic-vnf?";
311         aaiQuery = new AaiQuery();
312         PowerMock.replayAll();
313         String actual = Whitebox.invokeMethod(aaiQuery, "getMsbSuffixAddr", url);
314         PowerMock.verifyAll();
315         assertThat(actual, equalTo(expect));
316     }
317 }