Fixed CCVPN closed loop integration issue
[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.JSONObject;
18 import org.easymock.EasyMock;
19 import org.glassfish.jersey.client.HttpUrlConnectorProvider;
20 import org.junit.After;
21 import org.junit.Before;
22 import org.junit.BeforeClass;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.junit.runner.RunWith;
27 import org.onap.holmes.common.aai.config.AaiConfig;
28 import org.onap.holmes.common.exception.CorrelationException;
29 import org.powermock.api.easymock.PowerMock;
30 import org.powermock.core.classloader.annotations.PrepareForTest;
31 import org.powermock.modules.junit4.PowerMockRunner;
32 import org.powermock.reflect.Whitebox;
33
34 import javax.ws.rs.client.Client;
35 import javax.ws.rs.client.ClientBuilder;
36 import javax.ws.rs.client.Entity;
37 import javax.ws.rs.client.Invocation;
38 import javax.ws.rs.client.Invocation.Builder;
39 import javax.ws.rs.client.WebTarget;
40 import javax.ws.rs.core.MultivaluedHashMap;
41 import javax.ws.rs.core.MultivaluedMap;
42 import javax.ws.rs.core.Response;
43 import java.io.BufferedReader;
44 import java.io.File;
45 import java.io.FileNotFoundException;
46 import java.io.FileReader;
47 import java.io.IOException;
48 import java.lang.reflect.InvocationTargetException;
49 import java.lang.reflect.Method;
50 import java.util.HashMap;
51
52 import static org.hamcrest.CoreMatchers.equalTo;
53 import static org.junit.Assert.assertThat;
54 import static org.onap.holmes.common.config.MicroServiceConfig.MSB_ADDR;
55
56
57 @RunWith(PowerMockRunner.class)
58 @PrepareForTest({ClientBuilder.class, Client.class, Builder.class, WebTarget.class, Response.class})
59 public class AaiQuery4CcvpnTest {
60
61     @Rule
62     public ExpectedException thrown = ExpectedException.none();
63
64     private static JSONObject data;
65
66     private static AaiQuery4Ccvpn aai = AaiQuery4Ccvpn.newInstance();
67
68     private static MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
69     private static Client client;
70     private static WebTarget webTarget;
71     private static Builder builder;
72     private static Response response;
73
74     @BeforeClass
75     static public void beforeClass() {
76         System.setProperty(MSB_ADDR, "127.0.0.1:80");
77
78         File file = new File(AaiQuery4CcvpnTest.class.getClassLoader().getResource("./ccvpn.data.json").getFile());
79         BufferedReader reader = null;
80         try {
81             reader = new BufferedReader(new FileReader(file));
82             StringBuilder sb = new StringBuilder();
83             reader.lines().forEach(l -> sb.append(l));
84             data = JSONObject.parseObject(sb.toString());
85         } catch (FileNotFoundException e) {
86             // Do nothing
87         } catch (IOException e) {
88             // Do nothing
89         } finally {
90             if (reader != null) {
91                 try {
92                     reader.close();
93                 } catch (IOException e) {
94                     // Do nothing
95                 }
96             }
97         }
98
99         headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
100         headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID);
101         headers.add("Authorization", AaiConfig.getAuthenticationCredentials());
102         headers.add("Accept", "application/json");
103         headers.add("Content-Type", "application/json");
104         Whitebox.setInternalState(aai, "headers", headers);
105     }
106
107     @Before
108     public void before() {
109         PowerMock.mockStatic(ClientBuilder.class);
110         client = PowerMock.createMock(Client.class);
111         webTarget = PowerMock.createMock(WebTarget.class);
112         builder = PowerMock.createMock(Builder.class);
113         response = PowerMock.createMock(Response.class);
114     }
115
116     @After
117     public void after() {
118         PowerMock.resetAll();
119     }
120
121     @Test
122     public void test_getPath() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
123         String path = "/aai/v14/business/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}";
124
125         Method method = AaiQuery4Ccvpn.class.getDeclaredMethod("getPath", String.class);
126         method.setAccessible(true);
127
128         String ret = (String) method.invoke(aai, path);
129
130         assertThat(ret, equalTo("/api/aai-business/v14/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}"));
131
132     }
133
134     @Test
135     public void test_getLogicLink_exception() throws CorrelationException {
136         mockGetMethod();
137         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
138         EasyMock.expect(response.readEntity(String.class)).andReturn("Error!");
139
140         thrown.expect(CorrelationException.class);
141
142         PowerMock.replayAll();
143
144         String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
145
146         PowerMock.verifyAll();
147
148         assertThat(linkId, equalTo("logic-link-1"));
149
150     }
151
152     @Test
153     public void test_getLogicLink() throws CorrelationException {
154         mockGetMethod();
155         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("logic-link").toJSONString());
156         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
157
158         PowerMock.replayAll();
159
160         String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
161
162         PowerMock.verifyAll();
163
164         assertThat(linkId, equalTo("logic-link-1"));
165
166     }
167
168     @Test
169     public void test_getServiceInstances_exception() {
170         mockGetMethod();
171         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("vpn-binding").toJSONString());
172         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
173
174         mockGetMethod();
175         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("connectivity").toJSONString());
176         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
177
178         mockGetMethod();
179         EasyMock.expect(response.readEntity(String.class))
180                 .andReturn(data.getJSONObject("service-instance-by-connectivity").toJSONString());
181         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
182
183         mockGetMethod();
184         EasyMock.expect(response.readEntity(String.class))
185                 .andReturn(data.getJSONObject("service-instances-by-service-type").toJSONString());
186         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
187
188         mockGetMethod();
189         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
190         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
191
192         thrown.expect(RuntimeException.class);
193
194         PowerMock.replayAll();
195
196         JSONObject instance = aai.getServiceInstance("network-1", "pnf-1", "interface-1", "DOWN");
197
198         PowerMock.verifyAll();
199
200         assertThat(instance, equalTo("logic-link-1"));
201
202     }
203
204     @Test
205     public void test_getServiceInstance() {
206         mockGetMethod();
207         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("vpn-binding").toJSONString());
208         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
209
210         mockGetMethod();
211         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("connectivity").toJSONString());
212         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
213
214         mockGetMethod();
215         EasyMock.expect(response.readEntity(String.class))
216                 .andReturn(data.getJSONObject("service-instance-by-connectivity").toJSONString());
217         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
218
219         mockGetMethod();
220         EasyMock.expect(response.readEntity(String.class))
221                 .andReturn(data.getJSONObject("service-instances-by-service-type").toJSONString());
222         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
223
224         PowerMock.replayAll();
225
226         JSONObject instance = aai.getServiceInstance("network-1", "pnf-1", "interface-1", "DOWN");
227
228         PowerMock.verifyAll();
229
230         assertThat(instance.getString("service-instance-id"), equalTo("some id 1"));
231         assertThat(instance.getString("globalSubscriberId"), equalTo("e151059a-d924-4629-845f-264db19e50b4"));
232         assertThat(instance.getString("serviceType"), equalTo("volte"));
233     }
234
235     @Test
236     public void test_getServiceInstance_1() throws Exception {
237         mockGetMethod();
238         EasyMock.expect(response.readEntity(String.class))
239                 .andReturn(data.getJSONObject("service-instances-by-service-type").toJSONString());
240         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
241
242         PowerMock.replayAll();
243
244         JSONObject instance = Whitebox.invokeMethod(aai, "getServiceInstance",
245                                                     "custom-1", "service-type-1");
246
247         PowerMock.verifyAll();
248
249         assertThat(instance.getString("service-instance-id"), equalTo("some id 1"));
250     }
251
252     @Test
253     public void test_getServiceInstance_1_exception() throws Exception {
254         mockGetMethod();
255         EasyMock.expect(response.readEntity(String.class)).andReturn("Failed to get the service instance by type.");
256         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
257
258         thrown.expect(CorrelationException.class);
259
260         PowerMock.replayAll();
261
262         JSONObject instance = Whitebox.invokeMethod(aai, "getServiceInstance",
263                                                     "custom-1", "service-type-1");
264
265         PowerMock.verifyAll();
266
267         assertThat(instance.getString("service-instance-id"), equalTo("some id 1"));
268         assertThat(instance.getString("service-instance-id"), equalTo("some id 2"));
269         assertThat(instance.getString("service-instance-id"), equalTo("some id 3"));
270     }
271
272     @Test
273     public void test_updateTerminalPointStatus() throws CorrelationException {
274         mockGetMethod();
275         EasyMock.expect(response.readEntity(String.class)).andReturn(data.toJSONString());
276         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
277
278         mockPatchMethod();
279         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
280
281         PowerMock.replayAll();
282
283         aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
284
285         PowerMock.verifyAll();
286     }
287
288     @Test
289     public void test_updateTerminalPointStatus_exception() 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.NOT_FOUND).times(2);
296         EasyMock.expect(response.readEntity(String.class)).andReturn("Failed to update the TP information.");
297
298         thrown.expect(CorrelationException.class);
299
300         PowerMock.replayAll();
301
302         aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
303
304         PowerMock.verifyAll();
305     }
306
307     @Test
308     public void test_updateLogicLinkStatus() throws CorrelationException {
309         mockGetMethod();
310         EasyMock.expect(response.readEntity(String.class)).andReturn(data.toJSONString());
311         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
312
313         mockPatchMethod();
314         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
315
316         PowerMock.replayAll();
317
318         aai.updateLogicLinkStatus("link-1", new HashMap<>());
319
320         PowerMock.verifyAll();
321     }
322
323     @Test
324     public void test_updateLogicLinkStatus_exception() 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.NOT_FOUND).times(2);
331         EasyMock.expect(response.readEntity(String.class)).andReturn("Failed to update the logic link information.");
332
333         thrown.expect(CorrelationException.class);
334
335         PowerMock.replayAll();
336
337         aai.updateLogicLinkStatus("link-1", new HashMap<>());
338
339         PowerMock.verifyAll();
340
341     }
342
343     private void mockGetMethod() {
344         initCommonMock();
345         EasyMock.expect(builder.get()).andReturn(response);
346     }
347
348     private void mockPatchMethod() {
349         initCommonMock();
350         Invocation invocation = PowerMock.createMock(Invocation.class);
351         EasyMock.expect(builder.build(EasyMock.anyObject(String.class), EasyMock.anyObject(Entity.class))).andReturn(invocation);
352         EasyMock.expect(invocation.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true)).andReturn(invocation);
353         EasyMock.expect(invocation.invoke()).andReturn(response);
354     }
355
356     private void initCommonMock() {
357         EasyMock.expect(ClientBuilder.newClient()).andReturn(client);
358         EasyMock.expect(client.target(EasyMock.anyObject(String.class))).andReturn(webTarget);
359         EasyMock.expect(webTarget.path(EasyMock.anyObject(String.class))).andReturn(webTarget);
360         EasyMock.expect(webTarget.request()).andReturn(builder);
361         EasyMock.expect(builder.headers(headers)).andReturn(builder);
362     }
363 }