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;
21 import org.junit.rules.ExpectedException;
22 import org.junit.runner.RunWith;
23 import org.onap.holmes.common.aai.config.AaiConfig;
24 import org.onap.holmes.common.exception.CorrelationException;
25 import org.powermock.api.easymock.PowerMock;
26 import org.powermock.core.classloader.annotations.PrepareForTest;
27 import org.powermock.modules.junit4.PowerMockRunner;
28 import org.powermock.reflect.Whitebox;
30 import javax.ws.rs.client.*;
31 import javax.ws.rs.client.Invocation.Builder;
32 import javax.ws.rs.core.MultivaluedHashMap;
33 import javax.ws.rs.core.MultivaluedMap;
34 import javax.ws.rs.core.Response;
36 import java.lang.reflect.InvocationTargetException;
37 import java.lang.reflect.Method;
38 import java.util.HashMap;
40 import static org.hamcrest.CoreMatchers.equalTo;
41 import static org.junit.Assert.assertThat;
42 import static org.onap.holmes.common.config.MicroServiceConfig.MSB_ADDR;
45 @RunWith(PowerMockRunner.class)
46 @PrepareForTest({ClientBuilder.class, Client.class, Builder.class, WebTarget.class, Response.class})
47 public class AaiQuery4CcvpnTest {
50 public ExpectedException thrown = ExpectedException.none();
52 private static JSONObject data;
54 private static AaiQuery4Ccvpn aai = AaiQuery4Ccvpn.newInstance();
56 private static MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
57 private static Client client;
58 private static WebTarget webTarget;
59 private static Builder builder;
60 private static Response response;
63 static public void beforeClass() {
64 System.setProperty(MSB_ADDR, "127.0.0.1:80");
66 File file = new File(AaiQuery4CcvpnTest.class.getClassLoader().getResource("./ccvpn.data.json").getFile());
67 BufferedReader reader = null;
69 reader = new BufferedReader(new FileReader(file));
70 StringBuilder sb = new StringBuilder();
71 reader.lines().forEach(l -> sb.append(l));
72 data = JSONObject.parseObject(sb.toString());
73 } catch (FileNotFoundException e) {
75 } catch (IOException e) {
81 } catch (IOException e) {
87 headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
88 headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID);
89 headers.add("Authorization", AaiConfig.getAuthenticationCredentials());
90 headers.add("Accept", "application/json");
91 Whitebox.setInternalState(aai, "headers", headers);
95 public void before() {
96 PowerMock.mockStatic(ClientBuilder.class);
97 client = PowerMock.createMock(Client.class);
98 webTarget = PowerMock.createMock(WebTarget.class);
99 builder = PowerMock.createMock(Builder.class);
100 response = PowerMock.createMock(Response.class);
104 public void after() {
105 PowerMock.resetAll();
109 public void test_getPath() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
110 String path = "/aai/v14/business/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}";
112 Method method = AaiQuery4Ccvpn.class.getDeclaredMethod("getPath", String.class);
113 method.setAccessible(true);
115 String ret = (String) method.invoke(aai, path);
117 assertThat(ret, equalTo("/api/aai-business/v14/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}"));
122 public void test_getLogicLink_exception() {
124 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
126 thrown.expect(RuntimeException.class);
128 PowerMock.replayAll();
130 String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
132 PowerMock.verifyAll();
134 assertThat(linkId, equalTo("logic-link-1"));
139 public void test_getLogicLink() {
141 EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("logic-link"));
142 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
144 PowerMock.replayAll();
146 String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
148 PowerMock.verifyAll();
150 assertThat(linkId, equalTo("logic-link-1"));
155 public void test_getServiceInstances_exception() {
157 EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("vpn-binding"));
158 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
161 EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("connectivity"));
162 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
165 EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instance-by-connectivity"));
166 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
169 EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type"));
170 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
173 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
174 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
176 thrown.expect(RuntimeException.class);
178 PowerMock.replayAll();
180 JSONArray instances = aai.getServiceInstances("network-1", "pnf-1", "interface-1", "DOWN");
182 PowerMock.verifyAll();
184 assertThat(instances, equalTo("logic-link-1"));
189 public void test_getServiceInstances() {
191 EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("vpn-binding"));
192 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
195 EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("connectivity"));
196 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
199 EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instance-by-connectivity"));
200 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
203 EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type"));
204 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
207 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
208 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
210 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
211 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
213 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
214 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
216 PowerMock.replayAll();
218 JSONArray instances = aai.getServiceInstances("network-1", "pnf-1", "interface-1", "DOWN");
220 PowerMock.verifyAll();
222 assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
223 assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
224 assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
225 assertThat(instances.getJSONObject(0).getString("input-parameters"), equalTo("This is the service instance recreation input looked up by CL."));
226 assertThat(instances.getJSONObject(0).getString("globalSubscriberId"), equalTo("e151059a-d924-4629-845f-264db19e50b4"));
227 assertThat(instances.getJSONObject(0).getString("serviceType"), equalTo("volte"));
231 public void test_getServiceInstances_1() throws Exception {
233 EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type"));
234 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
236 PowerMock.replayAll();
238 JSONArray instances = (JSONArray)Whitebox.invokeMethod(aai, "getServiceInstances",
239 "custom-1", "service-type-1");
241 PowerMock.verifyAll();
243 assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
244 assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
245 assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
249 public void test_getServiceInstances_1_exception() throws Exception {
251 EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type"));
252 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
254 thrown.expect(CorrelationException.class);
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_updateTerminalPointStatus() throws CorrelationException {
271 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
273 PowerMock.replayAll();
275 aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
277 PowerMock.verifyAll();
281 public void test_updateTerminalPointStatus_exception() throws CorrelationException {
283 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
285 thrown.expect(CorrelationException.class);
287 PowerMock.replayAll();
289 aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
291 PowerMock.verifyAll();
295 public void test_updateLogicLinkStatus() throws CorrelationException {
297 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
299 PowerMock.replayAll();
301 aai.updateLogicLinkStatus("link-1", new HashMap<>());
303 PowerMock.verifyAll();
307 public void test_updateLogicLinkStatus_exception() throws CorrelationException {
309 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
311 thrown.expect(CorrelationException.class);
313 PowerMock.replayAll();
315 aai.updateLogicLinkStatus("link-1", new HashMap<>());
317 PowerMock.verifyAll();
321 private void mockGetMethod() {
323 EasyMock.expect(builder.get()).andReturn(response);
326 private void mockPatchMethod() {
328 EasyMock.expect(builder.method(EasyMock.anyObject(String.class), EasyMock.anyObject(Entity.class))).andReturn(response);
331 private void initCommonMock() {
332 EasyMock.expect(ClientBuilder.newClient()).andReturn(client);
333 EasyMock.expect(client.target(EasyMock.anyObject(String.class))).andReturn(webTarget);
334 EasyMock.expect(webTarget.path(EasyMock.anyObject(String.class))).andReturn(webTarget);
335 EasyMock.expect(webTarget.request()).andReturn(builder);
336 EasyMock.expect(builder.headers(headers)).andReturn(builder);