2 * Copyright 2018 ZTE Corporation.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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
15 package org.onap.holmes.common.aai;
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;
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;
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;
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;
58 @RunWith(PowerMockRunner.class)
59 @PrepareForTest({ClientBuilder.class, Client.class, Builder.class, WebTarget.class, Response.class})
60 public class AaiQuery4CcvpnTest {
63 public ExpectedException thrown = ExpectedException.none();
65 private static JSONObject data;
67 private static AaiQuery4Ccvpn aai = AaiQuery4Ccvpn.newInstance();
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;
76 static public void beforeClass() {
77 System.setProperty(MSB_ADDR, "127.0.0.1:80");
79 File file = new File(AaiQuery4CcvpnTest.class.getClassLoader().getResource("./ccvpn.data.json").getFile());
80 BufferedReader reader = null;
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) {
88 } catch (IOException e) {
94 } catch (IOException e) {
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);
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);
118 public void after() {
119 PowerMock.resetAll();
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}";
126 Method method = AaiQuery4Ccvpn.class.getDeclaredMethod("getPath", String.class);
127 method.setAccessible(true);
129 String ret = (String) method.invoke(aai, path);
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}"));
136 public void test_getLogicLink_exception() throws CorrelationException {
138 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
139 EasyMock.expect(response.readEntity(String.class)).andReturn("Error!");
141 thrown.expect(CorrelationException.class);
143 PowerMock.replayAll();
145 String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
147 PowerMock.verifyAll();
149 assertThat(linkId, equalTo("logic-link-1"));
154 public void test_getLogicLink() throws CorrelationException {
156 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("logic-link").toJSONString());
157 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
159 PowerMock.replayAll();
161 String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
163 PowerMock.verifyAll();
165 assertThat(linkId, equalTo("logic-link-1"));
170 public void test_getServiceInstances_exception() {
172 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("vpn-binding").toJSONString());
173 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
176 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("connectivity").toJSONString());
177 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
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);
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);
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);
193 thrown.expect(RuntimeException.class);
195 PowerMock.replayAll();
197 JSONArray instances = aai.getServiceInstances("network-1", "pnf-1", "interface-1", "DOWN");
199 PowerMock.verifyAll();
201 assertThat(instances, equalTo("logic-link-1"));
206 public void test_getServiceInstances() {
208 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("vpn-binding").toJSONString());
209 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
212 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("connectivity").toJSONString());
213 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
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);
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);
226 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
227 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
229 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
230 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
232 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
233 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
235 PowerMock.replayAll();
237 JSONArray instances = aai.getServiceInstances("network-1", "pnf-1", "interface-1", "DOWN");
239 PowerMock.verifyAll();
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"));
250 public void test_getServiceInstances_1() throws Exception {
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);
256 PowerMock.replayAll();
258 JSONArray instances = (JSONArray) Whitebox.invokeMethod(aai, "getServiceInstances",
259 "custom-1", "service-type-1");
261 PowerMock.verifyAll();
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"));
269 public void test_getServiceInstances_1_exception() throws Exception {
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);
274 thrown.expect(CorrelationException.class);
276 PowerMock.replayAll();
278 JSONArray instances = (JSONArray) Whitebox.invokeMethod(aai, "getServiceInstances",
279 "custom-1", "service-type-1");
281 PowerMock.verifyAll();
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"));
289 public void test_updateTerminalPointStatus() throws CorrelationException {
291 EasyMock.expect(response.readEntity(String.class)).andReturn(data.toJSONString());
292 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
295 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
297 PowerMock.replayAll();
299 aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
301 PowerMock.verifyAll();
305 public void test_updateTerminalPointStatus_exception() throws CorrelationException {
307 EasyMock.expect(response.readEntity(String.class)).andReturn(data.toJSONString());
308 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
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.");
314 thrown.expect(CorrelationException.class);
316 PowerMock.replayAll();
318 aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
320 PowerMock.verifyAll();
324 public void test_updateLogicLinkStatus() throws CorrelationException {
326 EasyMock.expect(response.readEntity(String.class)).andReturn(data.toJSONString());
327 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
330 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
332 PowerMock.replayAll();
334 aai.updateLogicLinkStatus("link-1", new HashMap<>());
336 PowerMock.verifyAll();
340 public void test_updateLogicLinkStatus_exception() throws CorrelationException {
342 EasyMock.expect(response.readEntity(String.class)).andReturn(data.toJSONString());
343 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
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.");
349 thrown.expect(CorrelationException.class);
351 PowerMock.replayAll();
353 aai.updateLogicLinkStatus("link-1", new HashMap<>());
355 PowerMock.verifyAll();
359 private void mockGetMethod() {
361 EasyMock.expect(builder.get()).andReturn(response);
364 private void mockPatchMethod() {
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);
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);