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;
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;
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;
37 import java.lang.reflect.InvocationTargetException;
38 import java.lang.reflect.Method;
39 import java.util.HashMap;
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;
47 @RunWith(PowerMockRunner.class)
48 @PrepareForTest({ClientBuilder.class, Client.class, Builder.class, WebTarget.class, Response.class})
49 public class AaiQuery4CcvpnTest {
52 public ExpectedException thrown = ExpectedException.none();
54 private static JSONObject data;
56 private static AaiQuery4Ccvpn aai = AaiQuery4Ccvpn.newInstance();
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;
65 static public void beforeClass() {
66 System.setProperty(MSB_ADDR, "127.0.0.1:80");
68 File file = new File(AaiQuery4CcvpnTest.class.getClassLoader().getResource("./ccvpn.data.json").getFile());
69 BufferedReader reader = null;
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) {
77 } catch (IOException e) {
83 } catch (IOException e) {
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 headers.add("Content-Type", "application/json");
94 Whitebox.setInternalState(aai, "headers", headers);
98 public void before() {
99 PowerMock.mockStatic(ClientBuilder.class);
100 client = PowerMock.createMock(Client.class);
101 webTarget = PowerMock.createMock(WebTarget.class);
102 builder = PowerMock.createMock(Builder.class);
103 response = PowerMock.createMock(Response.class);
107 public void after() {
108 PowerMock.resetAll();
112 public void test_getPath() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
113 String path = "/aai/v14/business/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}";
115 Method method = AaiQuery4Ccvpn.class.getDeclaredMethod("getPath", String.class);
116 method.setAccessible(true);
118 String ret = (String) method.invoke(aai, path);
120 assertThat(ret, equalTo("/api/aai-business/v14/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}"));
125 public void test_getLogicLink_exception() throws CorrelationException {
127 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
128 EasyMock.expect(response.readEntity(String.class)).andReturn("Error!");
130 thrown.expect(CorrelationException.class);
132 PowerMock.replayAll();
134 String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
136 PowerMock.verifyAll();
138 assertThat(linkId, equalTo("logic-link-1"));
143 public void test_getLogicLink() throws CorrelationException {
145 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("logic-link").toJSONString());
146 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
148 PowerMock.replayAll();
150 String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
152 PowerMock.verifyAll();
154 assertThat(linkId, equalTo("logic-link-1"));
159 public void test_getServiceInstances_exception() {
161 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("vpn-binding").toJSONString());
162 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
165 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("connectivity").toJSONString());
166 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
169 EasyMock.expect(response.readEntity(String.class))
170 .andReturn(data.getJSONObject("service-instance-by-connectivity").toJSONString());
171 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
174 EasyMock.expect(response.readEntity(String.class))
175 .andReturn(data.getJSONObject("service-instances-by-service-type").toJSONString());
176 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
179 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
180 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
182 thrown.expect(RuntimeException.class);
184 PowerMock.replayAll();
186 JSONArray instances = aai.getServiceInstances("network-1", "pnf-1", "interface-1", "DOWN");
188 PowerMock.verifyAll();
190 assertThat(instances, equalTo("logic-link-1"));
195 public void test_getServiceInstances() {
197 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("vpn-binding").toJSONString());
198 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
201 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("connectivity").toJSONString());
202 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
205 EasyMock.expect(response.readEntity(String.class))
206 .andReturn(data.getJSONObject("service-instance-by-connectivity").toJSONString());
207 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
210 EasyMock.expect(response.readEntity(String.class))
211 .andReturn(data.getJSONObject("service-instances-by-service-type").toJSONString());
212 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
215 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
216 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
218 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
219 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
221 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
222 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
224 PowerMock.replayAll();
226 JSONArray instances = aai.getServiceInstances("network-1", "pnf-1", "interface-1", "DOWN");
228 PowerMock.verifyAll();
230 assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
231 assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
232 assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
233 assertThat(instances.getJSONObject(0).getString("input-parameters"), equalTo("This is the service instance recreation input looked up by CL."));
234 assertThat(instances.getJSONObject(0).getString("globalSubscriberId"), equalTo("e151059a-d924-4629-845f-264db19e50b4"));
235 assertThat(instances.getJSONObject(0).getString("serviceType"), equalTo("volte"));
239 public void test_getServiceInstances_1() throws Exception {
241 EasyMock.expect(response.readEntity(String.class))
242 .andReturn(data.getJSONObject("service-instances-by-service-type").toJSONString());
243 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
245 PowerMock.replayAll();
247 JSONArray instances = (JSONArray) Whitebox.invokeMethod(aai, "getServiceInstances",
248 "custom-1", "service-type-1");
250 PowerMock.verifyAll();
252 assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
253 assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
254 assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
258 public void test_getServiceInstances_1_exception() throws Exception {
260 EasyMock.expect(response.readEntity(String.class)).andReturn("Failed to get the service instance by type.");
261 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
263 thrown.expect(CorrelationException.class);
265 PowerMock.replayAll();
267 JSONArray instances = (JSONArray) Whitebox.invokeMethod(aai, "getServiceInstances",
268 "custom-1", "service-type-1");
270 PowerMock.verifyAll();
272 assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
273 assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
274 assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
278 public void test_updateTerminalPointStatus() throws CorrelationException {
280 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
282 PowerMock.replayAll();
284 aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
286 PowerMock.verifyAll();
290 public void test_updateTerminalPointStatus_exception() throws CorrelationException {
292 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
293 EasyMock.expect(response.readEntity(String.class)).andReturn("Failed to update the TP information.");
295 thrown.expect(CorrelationException.class);
297 PowerMock.replayAll();
299 aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
301 PowerMock.verifyAll();
305 public void test_updateLogicLinkStatus() throws CorrelationException {
307 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
309 PowerMock.replayAll();
311 aai.updateLogicLinkStatus("link-1", new HashMap<>());
313 PowerMock.verifyAll();
317 public void test_updateLogicLinkStatus_exception() throws CorrelationException {
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.");
322 thrown.expect(CorrelationException.class);
324 PowerMock.replayAll();
326 aai.updateLogicLinkStatus("link-1", new HashMap<>());
328 PowerMock.verifyAll();
332 private void mockGetMethod() {
334 EasyMock.expect(builder.get()).andReturn(response);
337 private void mockPatchMethod() {
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);
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);