Fixed the CLM Issues
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / aai / AaiQuery4CcvpnTest.java
1 /**
2  * Copyright 2018-2020 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.google.gson.JsonObject;
18 import com.google.gson.JsonParser;
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
41 import static org.hamcrest.CoreMatchers.equalTo;
42 import static org.junit.Assert.assertThat;
43 import static org.onap.holmes.common.config.MicroServiceConfig.MSB_ADDR;
44
45
46 @RunWith(PowerMockRunner.class)
47 @PrepareForTest({ClientBuilder.class, Client.class, Builder.class, WebTarget.class, Response.class})
48 public class AaiQuery4CcvpnTest {
49
50     @Rule
51     public ExpectedException thrown = ExpectedException.none();
52
53     private static JsonObject data;
54
55     private static AaiQuery4Ccvpn aai = AaiQuery4Ccvpn.newInstance();
56
57     private static MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
58     private static Client client;
59     private static WebTarget webTarget;
60     private static Builder builder;
61     private static Response response;
62
63     @BeforeClass
64     static public void beforeClass() {
65         System.setProperty(MSB_ADDR, "127.0.0.1:80");
66
67         File file = new File(AaiQuery4CcvpnTest.class.getClassLoader().getResource("./ccvpn.data.json").getFile());
68         BufferedReader reader = null;
69         try {
70             reader = new BufferedReader(new FileReader(file));
71             StringBuilder sb = new StringBuilder();
72             reader.lines().forEach(l -> sb.append(l));
73             data = JsonParser.parseString(sb.toString()).getAsJsonObject();
74         } catch (FileNotFoundException e) {
75             // Do nothing
76         } catch (IOException e) {
77             // Do nothing
78         } finally {
79             if (reader != null) {
80                 try {
81                     reader.close();
82                 } catch (IOException e) {
83                     // Do nothing
84                 }
85             }
86         }
87
88         headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
89         headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID);
90         headers.add("Authorization", AaiConfig.getAuthenticationCredentials());
91         headers.add("Accept", "application/json");
92         headers.add("Content-Type", "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() throws CorrelationException {
125         mockGetMethod();
126         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
127         EasyMock.expect(response.readEntity(String.class)).andReturn("Error!");
128
129         thrown.expect(CorrelationException.class);
130
131         PowerMock.replayAll();
132
133         String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
134
135         PowerMock.verifyAll();
136
137         assertThat(linkId, equalTo("logic-link-1"));
138
139     }
140
141     @Test
142     public void test_getLogicLink() throws CorrelationException {
143         mockGetMethod();
144         EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("logic-link").toString());
145         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
146
147         PowerMock.replayAll();
148
149         String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
150
151         PowerMock.verifyAll();
152
153         assertThat(linkId, equalTo("logic-link-1"));
154
155     }
156
157     @Test
158     public void test_getServiceInstances_exception() {
159         mockGetMethod();
160         EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("vpn-binding").toString());
161         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
162
163         mockGetMethod();
164         EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("connectivity").toString());
165         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
166
167         mockGetMethod();
168         EasyMock.expect(response.readEntity(String.class))
169                 .andReturn(data.get("service-instance-by-connectivity").toString());
170         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
171
172         mockGetMethod();
173         EasyMock.expect(response.readEntity(String.class))
174                 .andReturn(data.get("service-instances-by-service-type").toString());
175         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
176
177         mockGetMethod();
178         EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("service-instance").toString());
179         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
180
181         thrown.expect(RuntimeException.class);
182
183         PowerMock.replayAll();
184
185         JsonObject instance = aai.getServiceInstance("network-1", "pnf-1", "interface-1", "DOWN");
186
187         PowerMock.verifyAll();
188
189         assertThat(instance, equalTo("logic-link-1"));
190
191     }
192
193     @Test
194     public void test_getServiceInstance() {
195         mockGetMethod();
196         EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("vpn-binding").toString());
197         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
198
199         mockGetMethod();
200         EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("connectivity").toString());
201         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
202
203         mockGetMethod();
204         EasyMock.expect(response.readEntity(String.class))
205                 .andReturn(data.get("service-instance-by-connectivity").toString());
206         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
207
208         mockGetMethod();
209         EasyMock.expect(response.readEntity(String.class))
210                 .andReturn(data.get("service-instances-by-service-type").toString());
211         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
212
213         PowerMock.replayAll();
214
215         JsonObject instance = aai.getServiceInstance("network-1", "pnf-1", "interface-1", "DOWN");
216
217         PowerMock.verifyAll();
218
219         assertThat(instance.get("service-instance-id").getAsString(), equalTo("some id 1"));
220         assertThat(instance.get("globalSubscriberId").getAsString(), equalTo("e151059a-d924-4629-845f-264db19e50b4"));
221         assertThat(instance.get("serviceType").getAsString(), equalTo("volte"));
222     }
223
224     @Test
225     public void test_getServiceInstance_1() throws Exception {
226         mockGetMethod();
227         EasyMock.expect(response.readEntity(String.class))
228                 .andReturn(data.get("service-instances-by-service-type").toString());
229         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
230
231         PowerMock.replayAll();
232
233         JsonObject instance = Whitebox.invokeMethod(aai, "getServiceInstance",
234                                                     "custom-1", "service-type-1");
235
236         PowerMock.verifyAll();
237
238         assertThat(instance.get("service-instance-id").getAsString(), equalTo("some id 1"));
239     }
240
241     @Test
242     public void test_getServiceInstance_1_exception() throws Exception {
243         mockGetMethod();
244         EasyMock.expect(response.readEntity(String.class)).andReturn("Failed to get the service instance by type.");
245         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
246
247         thrown.expect(CorrelationException.class);
248
249         PowerMock.replayAll();
250
251         JsonObject instance = Whitebox.invokeMethod(aai, "getServiceInstance",
252                                                     "custom-1", "service-type-1");
253
254         PowerMock.verifyAll();
255
256         assertThat(instance.get("service-instance-id").getAsString(), equalTo("some id 1"));
257         assertThat(instance.get("service-instance-id").getAsString(), equalTo("some id 2"));
258         assertThat(instance.get("service-instance-id").getAsString(), equalTo("some id 3"));
259     }
260
261     @Test
262     public void test_updateTerminalPointStatus() throws CorrelationException {
263         mockGetMethod();
264         EasyMock.expect(response.readEntity(String.class)).andReturn(data.toString());
265         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
266
267         mockPatchMethod();
268         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
269
270         PowerMock.replayAll();
271
272         aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
273
274         PowerMock.verifyAll();
275     }
276
277     @Test
278     public void test_updateTerminalPointStatus_exception() throws CorrelationException {
279         mockGetMethod();
280         EasyMock.expect(response.readEntity(String.class)).andReturn(data.toString());
281         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
282
283         mockPatchMethod();
284         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
285         EasyMock.expect(response.readEntity(String.class)).andReturn("Failed to update the TP information.");
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         mockGetMethod();
299         EasyMock.expect(response.readEntity(String.class)).andReturn(data.toString());
300         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
301
302         mockPatchMethod();
303         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
304
305         PowerMock.replayAll();
306
307         aai.updateLogicLinkStatus("link-1", new HashMap<>());
308
309         PowerMock.verifyAll();
310     }
311
312     @Test
313     public void test_updateLogicLinkStatus_exception() throws CorrelationException {
314         mockGetMethod();
315         EasyMock.expect(response.readEntity(String.class)).andReturn(data.toString());
316         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
317
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 }