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.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;
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;
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;
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;
57 @RunWith(PowerMockRunner.class)
58 @PrepareForTest({ClientBuilder.class, Client.class, Builder.class, WebTarget.class, Response.class})
59 public class AaiQuery4CcvpnTest {
62 public ExpectedException thrown = ExpectedException.none();
64 private static JSONObject data;
66 private static AaiQuery4Ccvpn aai = AaiQuery4Ccvpn.newInstance();
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;
75 static public void beforeClass() {
76 System.setProperty(MSB_ADDR, "127.0.0.1:80");
78 File file = new File(AaiQuery4CcvpnTest.class.getClassLoader().getResource("./ccvpn.data.json").getFile());
79 BufferedReader reader = null;
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) {
87 } catch (IOException e) {
93 } catch (IOException e) {
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);
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);
117 public void after() {
118 PowerMock.resetAll();
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}";
125 Method method = AaiQuery4Ccvpn.class.getDeclaredMethod("getPath", String.class);
126 method.setAccessible(true);
128 String ret = (String) method.invoke(aai, path);
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}"));
135 public void test_getLogicLink_exception() throws CorrelationException {
137 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
138 EasyMock.expect(response.readEntity(String.class)).andReturn("Error!");
140 thrown.expect(CorrelationException.class);
142 PowerMock.replayAll();
144 String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
146 PowerMock.verifyAll();
148 assertThat(linkId, equalTo("logic-link-1"));
153 public void test_getLogicLink() throws CorrelationException {
155 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("logic-link").toJSONString());
156 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
158 PowerMock.replayAll();
160 String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
162 PowerMock.verifyAll();
164 assertThat(linkId, equalTo("logic-link-1"));
169 public void test_getServiceInstances_exception() {
171 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("vpn-binding").toJSONString());
172 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
175 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("connectivity").toJSONString());
176 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
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);
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);
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);
192 thrown.expect(RuntimeException.class);
194 PowerMock.replayAll();
196 JSONObject instance = aai.getServiceInstance("network-1", "pnf-1", "interface-1", "DOWN");
198 PowerMock.verifyAll();
200 assertThat(instance, equalTo("logic-link-1"));
205 public void test_getServiceInstance() {
207 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("vpn-binding").toJSONString());
208 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
211 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("connectivity").toJSONString());
212 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
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);
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);
224 PowerMock.replayAll();
226 JSONObject instance = aai.getServiceInstance("network-1", "pnf-1", "interface-1", "DOWN");
228 PowerMock.verifyAll();
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"));
236 public void test_getServiceInstance_1() throws Exception {
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);
242 PowerMock.replayAll();
244 JSONObject instance = Whitebox.invokeMethod(aai, "getServiceInstance",
245 "custom-1", "service-type-1");
247 PowerMock.verifyAll();
249 assertThat(instance.getString("service-instance-id"), equalTo("some id 1"));
253 public void test_getServiceInstance_1_exception() throws Exception {
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);
258 thrown.expect(CorrelationException.class);
260 PowerMock.replayAll();
262 JSONObject instance = Whitebox.invokeMethod(aai, "getServiceInstance",
263 "custom-1", "service-type-1");
265 PowerMock.verifyAll();
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"));
273 public void test_updateTerminalPointStatus() throws CorrelationException {
275 EasyMock.expect(response.readEntity(String.class)).andReturn(data.toJSONString());
276 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
279 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
281 PowerMock.replayAll();
283 aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
285 PowerMock.verifyAll();
289 public void test_updateTerminalPointStatus_exception() 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.NOT_FOUND).times(2);
296 EasyMock.expect(response.readEntity(String.class)).andReturn("Failed to update the TP information.");
298 thrown.expect(CorrelationException.class);
300 PowerMock.replayAll();
302 aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
304 PowerMock.verifyAll();
308 public void test_updateLogicLinkStatus() throws CorrelationException {
310 EasyMock.expect(response.readEntity(String.class)).andReturn(data.toJSONString());
311 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
314 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
316 PowerMock.replayAll();
318 aai.updateLogicLinkStatus("link-1", new HashMap<>());
320 PowerMock.verifyAll();
324 public void test_updateLogicLinkStatus_exception() 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.NOT_FOUND).times(2);
331 EasyMock.expect(response.readEntity(String.class)).andReturn("Failed to update the logic link information.");
333 thrown.expect(CorrelationException.class);
335 PowerMock.replayAll();
337 aai.updateLogicLinkStatus("link-1", new HashMap<>());
339 PowerMock.verifyAll();
343 private void mockGetMethod() {
345 EasyMock.expect(builder.get()).andReturn(response);
348 private void mockPatchMethod() {
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);
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);