add httpsutil and modify query from aai
[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 = "host_url";
97         when(HttpsUtils.get(url, headers)).thenReturn("{}");
98
99         PowerMockito.mockStatic(MicroServiceConfig.class);
100         when(MicroServiceConfig.getMsbServerAddr()).thenReturn("host_url");
101
102         PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2").andReturn("");
103         PowerMock.replayAll();
104         VmEntity vmEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
105         PowerMock.verifyAll();
106
107         assertThat(vmEntity == null, equalTo(true));
108     }
109
110
111
112     @Test
113     public void testAaiQuery_getAaiVmData_exception() throws Exception {
114         PowerMock.resetAll();
115         thrown.expect(CorrelationException.class);
116         thrown.expectMessage("Failed to convert aai vm response data to vm entity");
117         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
118
119         aaiResponseUtil = new AaiResponseUtil();
120         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
121
122         PowerMockito.mockStatic(HttpsUtils.class);
123         Map<String, String> headers = new HashMap<>();
124         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
125         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
126         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
127         headers.put("Accept", "application/json");
128         String url = "host_url";
129
130         when(HttpsUtils.get(url, headers)).thenReturn("");
131
132         PowerMockito.mockStatic(MicroServiceConfig.class);
133         when(MicroServiceConfig.getMsbServerAddr()).thenReturn("host_url");
134
135         PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2").andReturn("");
136
137         PowerMock.replayAll();
138         Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
139         PowerMock.verifyAll();
140     }
141
142     @Test
143     public void testAaiQuery_getAaiVmData_httpsutils_exception() throws Exception {
144         PowerMock.resetAll();
145         thrown.expect(CorrelationException.class);
146         thrown.expectMessage("Failed to get data from aai");
147         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
148
149         aaiResponseUtil = new AaiResponseUtil();
150         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
151
152         PowerMockito.mockStatic(HttpsUtils.class);
153         Map<String, String> headers = new HashMap<>();
154         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
155         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
156         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
157         headers.put("Accept", "application/json");
158         String url = "host_url";
159
160         when(HttpsUtils.get(url, headers)).thenThrow(new CorrelationException(""));
161
162         PowerMockito.mockStatic(MicroServiceConfig.class);
163         when(MicroServiceConfig.getMsbServerAddr()).thenReturn("host_url");
164
165         PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2").andReturn("");
166         PowerMock.replayAll();
167         Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
168         PowerMock.verifyAll();
169     }
170
171     @Test
172     public void testAaiQuery_getVmResourceLinks_ok() throws Exception {
173         PowerMock.resetAll();
174         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResourceLinksResponse");
175
176         aaiResponseUtil = new AaiResponseUtil();
177         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
178
179         String result = "{\"result-data\":[{\"resource-type\":\"vserver\",\"resource-link\":\"le-vserver-id-val-51834\"}]}";
180
181         PowerMock.expectPrivate(aaiQuery, "getResourceLinksResponse", "test1", "test2").andReturn(result);
182         PowerMock.replayAll();
183         String resource = Whitebox.invokeMethod(aaiQuery, "getVmResourceLinks", "test1", "test2");
184         PowerMock.verifyAll();
185
186         assertThat(resource, equalTo("le-vserver-id-val-51834"));
187     }
188
189     @Test
190     public void testAaiQuery_getVmResourceLinks_exception() throws Exception {
191         PowerMock.resetAll();
192         thrown.expect(CorrelationException.class);
193         thrown.expectMessage("Failed to get aai resource link");
194         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResourceLinksResponse");
195
196         aaiResponseUtil = new AaiResponseUtil();
197         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
198
199         PowerMock.expectPrivate(aaiQuery, "getResourceLinksResponse", "test1", "test2").andReturn("");
200         PowerMock.replayAll();
201         String resource = Whitebox.invokeMethod(aaiQuery, "getVmResourceLinks", "test1", "test2");
202         PowerMock.verifyAll();
203
204         assertThat(resource, equalTo("le-vserver-id-val-51834"));
205     }
206
207     @Test
208     public void testAaiQuery_getResourceLinksResponse() throws Exception {
209         PowerMock.resetAll();
210         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
211
212         aaiResponseUtil = new AaiResponseUtil();
213         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
214
215         PowerMockito.mockStatic(MicroServiceConfig.class);
216         when(MicroServiceConfig.getMsbServerAddr()).thenReturn("host_url");
217
218         PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
219         PowerMock.replayAll();
220         String resource = Whitebox.invokeMethod(aaiQuery, "getResourceLinksResponse", "test1", "test2");
221         PowerMock.verifyAll();
222
223         assertThat(resource, equalTo(""));
224     }
225
226     @Test
227     public void testAaiQuery_getVnfDataResponse() throws Exception {
228         PowerMock.resetAll();
229         aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResponse");
230
231         aaiResponseUtil = new AaiResponseUtil();
232         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
233
234         PowerMockito.mockStatic(MicroServiceConfig.class);
235         when(MicroServiceConfig.getMsbServerAddr()).thenReturn("host_url");
236
237         PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
238         PowerMock.replayAll();
239         String resource = Whitebox.invokeMethod(aaiQuery, "getVnfDataResponse", "test1", "test2");
240         PowerMock.verifyAll();
241
242         assertThat(resource, equalTo(""));
243     }
244
245     @Test
246     public void testAaiQuery_getResponse_ok() throws Exception {
247         PowerMock.resetAll();
248         aaiQuery = new AaiQuery();
249         PowerMockito.mockStatic(HttpsUtils.class);
250         Map<String, String> headers = new HashMap<>();
251         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
252         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
253         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
254         headers.put("Accept", "application/json");
255         String url = "host_url";
256
257         when(HttpsUtils.get(url, headers)).thenReturn("");
258
259         PowerMock.replayAll();
260         String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
261         PowerMock.verifyAll();
262
263         assertThat(resource, equalTo(""));
264     }
265
266     @Test
267     public void testAaiQuery_getResponse_exceptioin() throws Exception {
268         PowerMock.resetAll();
269         thrown.expect(CorrelationException.class);
270         thrown.expectMessage("Failed to get data from aai");
271         aaiQuery = new AaiQuery();
272
273         PowerMockito.mockStatic(HttpsUtils.class);
274         Map<String, String> headers = new HashMap<>();
275         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
276         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
277         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
278         headers.put("Accept", "application/json");
279         String url = "host_url";
280
281         when(HttpsUtils.get(url, headers)).thenThrow(new CorrelationException(""));
282
283         PowerMock.replayAll();
284         String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
285         PowerMock.verifyAll();
286
287         assertThat(resource, equalTo(""));
288
289     }
290
291     @Test
292     public void testAaiQuery_getHeaders() throws Exception {
293         PowerMock.resetAll();
294         aaiQuery = new AaiQuery();
295         PowerMock.replayAll();
296         Map actual = Whitebox.invokeMethod(aaiQuery, "getHeaders");
297         PowerMock.verifyAll();
298
299         assertThat(actual.get("X-TransactionId"), equalTo("9999"));
300         assertThat(actual.get("X-FromAppId"), equalTo("jimmy-postman"));
301         assertThat(actual.get("Authorization"), equalTo("Basic QUFJOkFBSQ=="));
302         assertThat(actual.get("Accept"), equalTo("application/json"));
303     }
304 }