Fixed the CLM Issues
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / aai / AaiQueryTest.java
1 /**
2  * Copyright 2017-2020 ZTE Corporation.
3  * <p>
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  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
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
19 import org.apache.http.HttpResponse;
20 import org.apache.http.client.methods.HttpGet;
21 import org.apache.http.impl.client.CloseableHttpClient;
22 import org.easymock.EasyMock;
23 import org.junit.BeforeClass;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.junit.runner.RunWith;
28 import org.onap.holmes.common.aai.config.AaiConfig;
29 import org.onap.holmes.common.aai.entity.VmEntity;
30 import org.onap.holmes.common.aai.entity.VnfEntity;
31 import org.onap.holmes.common.config.MicroServiceConfig;
32 import org.onap.holmes.common.exception.CorrelationException;
33 import org.onap.holmes.common.utils.HttpsUtils;
34 import org.powermock.api.easymock.PowerMock;
35 import org.powermock.core.classloader.annotations.PowerMockIgnore;
36 import org.powermock.core.classloader.annotations.PrepareForTest;
37 import org.powermock.modules.junit4.PowerMockRunner;
38 import org.powermock.reflect.Whitebox;
39
40 import java.util.HashMap;
41 import java.util.Map;
42
43 import static org.easymock.EasyMock.anyObject;
44 import static org.easymock.EasyMock.expect;
45 import static org.hamcrest.core.IsEqual.equalTo;
46 import static org.junit.Assert.assertThat;
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     @Test
183     public void testAaiQuery_getResourceLinksResponse() throws Exception {
184         PowerMock.resetAll();
185         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
186
187         aaiResponseUtil = new AaiResponseUtil();
188         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
189
190         PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
191         PowerMock.replayAll();
192         String resource = Whitebox.invokeMethod(aaiQuery, "getResourceLinksResponse", "test1", "test2");
193         PowerMock.verifyAll();
194
195         assertThat(resource, equalTo(""));
196     }
197
198     @Test
199     public void testAaiQuery_getVnfDataResponse() throws Exception {
200         PowerMock.resetAll();
201         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
202
203         aaiResponseUtil = new AaiResponseUtil();
204         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
205
206         PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
207         PowerMock.replayAll();
208         String resource = Whitebox.invokeMethod(aaiQuery, "getVnfDataResponse", "test1", "test2");
209         PowerMock.verifyAll();
210
211         assertThat(resource, equalTo(""));
212     }
213
214     @Test
215     public void testAaiQuery_getResponse_ok() throws Exception {
216         PowerMock.resetAll();
217         aaiQuery = new AaiQuery();
218         PowerMock.mockStatic(HttpsUtils.class);
219         Map<String, String> headers = new HashMap<>();
220         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
221         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
222         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
223         headers.put("Accept", "application/json");
224         String url = "host_url";
225
226         HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
227         CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
228         expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient);
229         HttpGet httpGet = new HttpGet(url);
230         PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
231         expect(HttpsUtils.get(anyObject(HttpGet.class), anyObject(Map.class),
232                 anyObject(CloseableHttpClient.class))).andReturn(httpResponse);
233         expect(HttpsUtils.extractResponseEntity(httpResponse)).andReturn("");
234         PowerMock.expectPrivate(httpClient, "close");
235         EasyMock.expectLastCall();
236
237         PowerMock.replayAll();
238         String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
239         PowerMock.verifyAll();
240
241         assertThat(resource, equalTo(""));
242     }
243
244     @Test
245     public void testAaiQuery_getResponse_exceptioin() throws Exception {
246         PowerMock.resetAll();
247         thrown.expect(CorrelationException.class);
248         thrown.expectMessage("Failed to get data from aai");
249         aaiQuery = new AaiQuery();
250
251         PowerMock.mockStatic(HttpsUtils.class);
252         Map<String, String> headers = new HashMap<>();
253         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
254         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
255         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
256         headers.put("Accept", "application/json");
257         String url = "host_url";
258         CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
259         expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient);
260         HttpGet httpGet = new HttpGet(url);
261         PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
262         expect(HttpsUtils.get(httpGet, headers, httpClient)).andThrow(new CorrelationException(""));
263         PowerMock.expectPrivate(httpClient, "close");
264         EasyMock.expectLastCall();
265         PowerMock.replayAll();
266         String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
267         PowerMock.verifyAll();
268         assertThat(resource, equalTo(""));
269     }
270
271     @Test
272     public void testAaiQuery_getHeaders() throws Exception {
273         PowerMock.resetAll();
274         aaiQuery = new AaiQuery();
275         PowerMock.replayAll();
276         Map actual = Whitebox.invokeMethod(aaiQuery, "getHeaders");
277         PowerMock.verifyAll();
278
279         assertThat(actual.get("X-TransactionId"), equalTo("9999"));
280         assertThat(actual.get("X-FromAppId"), equalTo("jimmy-postman"));
281         assertThat(actual.get("Authorization"), equalTo("Basic QUFJOkFBSQ=="));
282         assertThat(actual.get("Accept"), equalTo("application/json"));
283     }
284
285     @Test
286     public void testAaiQuery_getBaseUrl_aaiurl() throws Exception {
287         PowerMock.resetAll();
288         aaiQuery = new AaiQuery();
289
290         PowerMock.mockStatic(MicroServiceConfig.class);
291
292         PowerMock.replayAll();
293         String actual = Whitebox.invokeMethod(aaiQuery, "getBaseUrl", "/url");
294         PowerMock.verifyAll();
295
296         assertThat(actual, equalTo("https://aai.onap:8443/url"));
297     }
298 }