Fixed HTTP PATCH Failures
[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         Whitebox.setInternalState(aai, "headers", headers);
94     }
95
96     @Before
97     public void before() {
98         PowerMock.mockStatic(ClientBuilder.class);
99         client = PowerMock.createMock(Client.class);
100         webTarget = PowerMock.createMock(WebTarget.class);
101         builder = PowerMock.createMock(Builder.class);
102         response = PowerMock.createMock(Response.class);
103     }
104
105     @After
106     public void after() {
107         PowerMock.resetAll();
108     }
109
110     @Test
111     public void test_getPath() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
112         String path = "/aai/v14/business/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}";
113
114         Method method = AaiQuery4Ccvpn.class.getDeclaredMethod("getPath", String.class);
115         method.setAccessible(true);
116
117         String ret = (String) method.invoke(aai, path);
118
119         assertThat(ret, equalTo("/api/aai-business/v14/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}"));
120
121     }
122
123     @Test
124     public void test_getLogicLink_exception() {
125         mockGetMethod();
126         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
127
128         thrown.expect(RuntimeException.class);
129
130         PowerMock.replayAll();
131
132         String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
133
134         PowerMock.verifyAll();
135
136         assertThat(linkId, equalTo("logic-link-1"));
137
138     }
139
140     @Test
141     public void test_getLogicLink() {
142         mockGetMethod();
143         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("logic-link"));
144         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
145
146         PowerMock.replayAll();
147
148         String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
149
150         PowerMock.verifyAll();
151
152         assertThat(linkId, equalTo("logic-link-1"));
153
154     }
155
156     @Test
157     public void test_getServiceInstances_exception() {
158         mockGetMethod();
159         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("vpn-binding"));
160         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
161
162         mockGetMethod();
163         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("connectivity"));
164         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
165
166         mockGetMethod();
167         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instance-by-connectivity"));
168         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
169
170         mockGetMethod();
171         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type"));
172         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
173
174         mockGetMethod();
175         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
176         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
177
178         thrown.expect(RuntimeException.class);
179
180         PowerMock.replayAll();
181
182         JSONArray instances = aai.getServiceInstances("network-1", "pnf-1", "interface-1", "DOWN");
183
184         PowerMock.verifyAll();
185
186         assertThat(instances, equalTo("logic-link-1"));
187
188     }
189
190     @Test
191     public void test_getServiceInstances() {
192         mockGetMethod();
193         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("vpn-binding"));
194         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
195
196         mockGetMethod();
197         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("connectivity"));
198         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
199
200         mockGetMethod();
201         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instance-by-connectivity"));
202         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
203
204         mockGetMethod();
205         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type"));
206         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
207
208         mockGetMethod();
209         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
210         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
211         mockGetMethod();
212         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
213         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
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
218         PowerMock.replayAll();
219
220         JSONArray instances = aai.getServiceInstances("network-1", "pnf-1", "interface-1", "DOWN");
221
222         PowerMock.verifyAll();
223
224         assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
225         assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
226         assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
227         assertThat(instances.getJSONObject(0).getString("input-parameters"), equalTo("This is the service instance recreation input looked up by CL."));
228         assertThat(instances.getJSONObject(0).getString("globalSubscriberId"), equalTo("e151059a-d924-4629-845f-264db19e50b4"));
229         assertThat(instances.getJSONObject(0).getString("serviceType"), equalTo("volte"));
230     }
231
232     @Test
233     public void test_getServiceInstances_1() throws Exception {
234         mockGetMethod();
235         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type"));
236         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
237
238         PowerMock.replayAll();
239
240         JSONArray instances = (JSONArray) Whitebox.invokeMethod(aai, "getServiceInstances",
241                 "custom-1", "service-type-1");
242
243         PowerMock.verifyAll();
244
245         assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
246         assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
247         assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
248     }
249
250     @Test
251     public void test_getServiceInstances_1_exception() throws Exception {
252         mockGetMethod();
253         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type"));
254         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
255
256         thrown.expect(CorrelationException.class);
257
258         PowerMock.replayAll();
259
260         JSONArray instances = (JSONArray) Whitebox.invokeMethod(aai, "getServiceInstances",
261                 "custom-1", "service-type-1");
262
263         PowerMock.verifyAll();
264
265         assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
266         assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
267         assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
268     }
269
270     @Test
271     public void test_updateTerminalPointStatus() throws CorrelationException {
272         mockPatchMethod();
273         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
274
275         PowerMock.replayAll();
276
277         aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
278
279         PowerMock.verifyAll();
280     }
281
282     @Test
283     public void test_updateTerminalPointStatus_exception() throws CorrelationException {
284         mockPatchMethod();
285         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
286
287         thrown.expect(CorrelationException.class);
288
289         PowerMock.replayAll();
290
291         aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
292
293         PowerMock.verifyAll();
294     }
295
296     @Test
297     public void test_updateLogicLinkStatus() throws CorrelationException {
298         mockPatchMethod();
299         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
300
301         PowerMock.replayAll();
302
303         aai.updateLogicLinkStatus("link-1", new HashMap<>());
304
305         PowerMock.verifyAll();
306     }
307
308     @Test
309     public void test_updateLogicLinkStatus_exception() throws CorrelationException {
310         mockPatchMethod();
311         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
312
313         thrown.expect(CorrelationException.class);
314
315         PowerMock.replayAll();
316
317         aai.updateLogicLinkStatus("link-1", new HashMap<>());
318
319         PowerMock.verifyAll();
320
321     }
322
323     private void mockGetMethod() {
324         initCommonMock();
325         EasyMock.expect(builder.get()).andReturn(response);
326     }
327
328     private void mockPatchMethod() {
329         initCommonMock();
330         Invocation invocation = PowerMock.createMock(Invocation.class);
331         EasyMock.expect(builder.build(EasyMock.anyObject(String.class), EasyMock.anyObject(Entity.class))).andReturn(invocation);
332         EasyMock.expect(invocation.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true)).andReturn(invocation);
333         EasyMock.expect(invocation.invoke()).andReturn(response);
334     }
335
336     private void initCommonMock() {
337         EasyMock.expect(ClientBuilder.newClient()).andReturn(client);
338         EasyMock.expect(client.target(EasyMock.anyObject(String.class))).andReturn(webTarget);
339         EasyMock.expect(webTarget.path(EasyMock.anyObject(String.class))).andReturn(webTarget);
340         EasyMock.expect(webTarget.request()).andReturn(builder);
341         EasyMock.expect(builder.headers(headers)).andReturn(builder);
342     }
343 }