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