c094e4f3d783a80fd29ba3d978ee1eda579e03b4
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / aai / AaiQuery4CcvpnTest.java
1 /**
2  * Copyright 2018 ZTE Corporation.
3  * <p>
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  * <p>
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * <p>
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14
15 package org.onap.holmes.common.aai;
16
17 import com.alibaba.fastjson.JSONArray;
18 import com.alibaba.fastjson.JSONObject;
19 import org.easymock.EasyMock;
20 import org.glassfish.jersey.client.HttpUrlConnectorProvider;
21 import org.junit.After;
22 import org.junit.Before;
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.exception.CorrelationException;
30 import org.powermock.api.easymock.PowerMock;
31 import org.powermock.core.classloader.annotations.PrepareForTest;
32 import org.powermock.modules.junit4.PowerMockRunner;
33 import org.powermock.reflect.Whitebox;
34
35 import javax.ws.rs.client.Client;
36 import javax.ws.rs.client.ClientBuilder;
37 import javax.ws.rs.client.Entity;
38 import javax.ws.rs.client.Invocation;
39 import javax.ws.rs.client.Invocation.Builder;
40 import javax.ws.rs.client.WebTarget;
41 import javax.ws.rs.core.MultivaluedHashMap;
42 import javax.ws.rs.core.MultivaluedMap;
43 import javax.ws.rs.core.Response;
44 import java.io.BufferedReader;
45 import java.io.File;
46 import java.io.FileNotFoundException;
47 import java.io.FileReader;
48 import java.io.IOException;
49 import java.lang.reflect.InvocationTargetException;
50 import java.lang.reflect.Method;
51 import java.util.HashMap;
52
53 import static org.hamcrest.CoreMatchers.equalTo;
54 import static org.junit.Assert.assertThat;
55 import static org.onap.holmes.common.config.MicroServiceConfig.MSB_ADDR;
56
57
58 @RunWith(PowerMockRunner.class)
59 @PrepareForTest({ClientBuilder.class, Client.class, Builder.class, WebTarget.class, Response.class})
60 public class AaiQuery4CcvpnTest {
61
62     @Rule
63     public ExpectedException thrown = ExpectedException.none();
64
65     private static JSONObject data;
66
67     private static AaiQuery4Ccvpn aai = AaiQuery4Ccvpn.newInstance();
68
69     private static MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
70     private static Client client;
71     private static WebTarget webTarget;
72     private static Builder builder;
73     private static Response response;
74
75     @BeforeClass
76     static public void beforeClass() {
77         System.setProperty(MSB_ADDR, "127.0.0.1:80");
78
79         File file = new File(AaiQuery4CcvpnTest.class.getClassLoader().getResource("./ccvpn.data.json").getFile());
80         BufferedReader reader = null;
81         try {
82             reader = new BufferedReader(new FileReader(file));
83             StringBuilder sb = new StringBuilder();
84             reader.lines().forEach(l -> sb.append(l));
85             data = JSONObject.parseObject(sb.toString());
86         } catch (FileNotFoundException e) {
87             // Do nothing
88         } catch (IOException e) {
89             // Do nothing
90         } finally {
91             if (reader != null) {
92                 try {
93                     reader.close();
94                 } catch (IOException e) {
95                     // Do nothing
96                 }
97             }
98         }
99
100         headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
101         headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID);
102         headers.add("Authorization", AaiConfig.getAuthenticationCredentials());
103         headers.add("Accept", "application/json");
104         headers.add("Content-Type", "application/json");
105         Whitebox.setInternalState(aai, "headers", headers);
106     }
107
108     @Before
109     public void before() {
110         PowerMock.mockStatic(ClientBuilder.class);
111         client = PowerMock.createMock(Client.class);
112         webTarget = PowerMock.createMock(WebTarget.class);
113         builder = PowerMock.createMock(Builder.class);
114         response = PowerMock.createMock(Response.class);
115     }
116
117     @After
118     public void after() {
119         PowerMock.resetAll();
120     }
121
122     @Test
123     public void test_getPath() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
124         String path = "/aai/v14/business/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}";
125
126         Method method = AaiQuery4Ccvpn.class.getDeclaredMethod("getPath", String.class);
127         method.setAccessible(true);
128
129         String ret = (String) method.invoke(aai, path);
130
131         assertThat(ret, equalTo("/api/aai-business/v14/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}"));
132
133     }
134
135     @Test
136     public void test_getLogicLink_exception() throws CorrelationException {
137         mockGetMethod();
138         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
139         EasyMock.expect(response.readEntity(String.class)).andReturn("Error!");
140
141         thrown.expect(CorrelationException.class);
142
143         PowerMock.replayAll();
144
145         String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
146
147         PowerMock.verifyAll();
148
149         assertThat(linkId, equalTo("logic-link-1"));
150
151     }
152
153     @Test
154     public void test_getLogicLink() throws CorrelationException {
155         mockGetMethod();
156         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("logic-link").toJSONString());
157         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
158
159         PowerMock.replayAll();
160
161         String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
162
163         PowerMock.verifyAll();
164
165         assertThat(linkId, equalTo("logic-link-1"));
166
167     }
168
169     @Test
170     public void test_getServiceInstances_exception() {
171         mockGetMethod();
172         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("vpn-binding").toJSONString());
173         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
174
175         mockGetMethod();
176         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("connectivity").toJSONString());
177         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
178
179         mockGetMethod();
180         EasyMock.expect(response.readEntity(String.class))
181                 .andReturn(data.getJSONObject("service-instance-by-connectivity").toJSONString());
182         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
183
184         mockGetMethod();
185         EasyMock.expect(response.readEntity(String.class))
186                 .andReturn(data.getJSONObject("service-instances-by-service-type").toJSONString());
187         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
188
189         mockGetMethod();
190         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
191         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
192
193         thrown.expect(RuntimeException.class);
194
195         PowerMock.replayAll();
196
197         JSONArray instances = aai.getServiceInstances("network-1", "pnf-1", "interface-1", "DOWN");
198
199         PowerMock.verifyAll();
200
201         assertThat(instances, equalTo("logic-link-1"));
202
203     }
204
205     @Test
206     public void test_getServiceInstances() {
207         mockGetMethod();
208         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("vpn-binding").toJSONString());
209         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
210
211         mockGetMethod();
212         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("connectivity").toJSONString());
213         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
214
215         mockGetMethod();
216         EasyMock.expect(response.readEntity(String.class))
217                 .andReturn(data.getJSONObject("service-instance-by-connectivity").toJSONString());
218         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
219
220         mockGetMethod();
221         EasyMock.expect(response.readEntity(String.class))
222                 .andReturn(data.getJSONObject("service-instances-by-service-type").toJSONString());
223         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
224
225         mockGetMethod();
226         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
227         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
228         mockGetMethod();
229         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
230         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
231         mockGetMethod();
232         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
233         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
234
235         PowerMock.replayAll();
236
237         JSONArray instances = aai.getServiceInstances("network-1", "pnf-1", "interface-1", "DOWN");
238
239         PowerMock.verifyAll();
240
241         assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
242         assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
243         assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
244         assertThat(instances.getJSONObject(0).getString("input-parameters"), equalTo("This is the service instance recreation input looked up by CL."));
245         assertThat(instances.getJSONObject(0).getString("globalSubscriberId"), equalTo("e151059a-d924-4629-845f-264db19e50b4"));
246         assertThat(instances.getJSONObject(0).getString("serviceType"), equalTo("volte"));
247     }
248
249     @Test
250     public void test_getServiceInstances_1() throws Exception {
251         mockGetMethod();
252         EasyMock.expect(response.readEntity(String.class))
253                 .andReturn(data.getJSONObject("service-instances-by-service-type").toJSONString());
254         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
255
256         PowerMock.replayAll();
257
258         JSONArray instances = (JSONArray) Whitebox.invokeMethod(aai, "getServiceInstances",
259                                                                 "custom-1", "service-type-1");
260
261         PowerMock.verifyAll();
262
263         assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
264         assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
265         assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
266     }
267
268     @Test
269     public void test_getServiceInstances_1_exception() throws Exception {
270         mockGetMethod();
271         EasyMock.expect(response.readEntity(String.class)).andReturn("Failed to get the service instance by type.");
272         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
273
274         thrown.expect(CorrelationException.class);
275
276         PowerMock.replayAll();
277
278         JSONArray instances = (JSONArray) Whitebox.invokeMethod(aai, "getServiceInstances",
279                                                                 "custom-1", "service-type-1");
280
281         PowerMock.verifyAll();
282
283         assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
284         assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
285         assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
286     }
287
288     @Test
289     public void test_updateTerminalPointStatus() throws CorrelationException {
290         mockGetMethod();
291         EasyMock.expect(response.readEntity(String.class)).andReturn(data.toJSONString());
292         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
293
294         mockPatchMethod();
295         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
296
297         PowerMock.replayAll();
298
299         aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
300
301         PowerMock.verifyAll();
302     }
303
304     @Test
305     public void test_updateTerminalPointStatus_exception() throws CorrelationException {
306         mockGetMethod();
307         EasyMock.expect(response.readEntity(String.class)).andReturn(data.toJSONString());
308         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
309
310         mockPatchMethod();
311         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
312         EasyMock.expect(response.readEntity(String.class)).andReturn("Failed to update the TP information.");
313
314         thrown.expect(CorrelationException.class);
315
316         PowerMock.replayAll();
317
318         aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
319
320         PowerMock.verifyAll();
321     }
322
323     @Test
324     public void test_updateLogicLinkStatus() throws CorrelationException {
325         mockGetMethod();
326         EasyMock.expect(response.readEntity(String.class)).andReturn(data.toJSONString());
327         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
328
329         mockPatchMethod();
330         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
331
332         PowerMock.replayAll();
333
334         aai.updateLogicLinkStatus("link-1", new HashMap<>());
335
336         PowerMock.verifyAll();
337     }
338
339     @Test
340     public void test_updateLogicLinkStatus_exception() throws CorrelationException {
341         mockGetMethod();
342         EasyMock.expect(response.readEntity(String.class)).andReturn(data.toJSONString());
343         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
344
345         mockPatchMethod();
346         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
347         EasyMock.expect(response.readEntity(String.class)).andReturn("Failed to update the logic link information.");
348
349         thrown.expect(CorrelationException.class);
350
351         PowerMock.replayAll();
352
353         aai.updateLogicLinkStatus("link-1", new HashMap<>());
354
355         PowerMock.verifyAll();
356
357     }
358
359     private void mockGetMethod() {
360         initCommonMock();
361         EasyMock.expect(builder.get()).andReturn(response);
362     }
363
364     private void mockPatchMethod() {
365         initCommonMock();
366         Invocation invocation = PowerMock.createMock(Invocation.class);
367         EasyMock.expect(builder.build(EasyMock.anyObject(String.class), EasyMock.anyObject(Entity.class))).andReturn(invocation);
368         EasyMock.expect(invocation.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true)).andReturn(invocation);
369         EasyMock.expect(invocation.invoke()).andReturn(response);
370     }
371
372     private void initCommonMock() {
373         EasyMock.expect(ClientBuilder.newClient()).andReturn(client);
374         EasyMock.expect(client.target(EasyMock.anyObject(String.class))).andReturn(webTarget);
375         EasyMock.expect(webTarget.path(EasyMock.anyObject(String.class))).andReturn(webTarget);
376         EasyMock.expect(webTarget.request()).andReturn(builder);
377         EasyMock.expect(builder.headers(headers)).andReturn(builder);
378     }
379 }