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 Whitebox.setInternalState(aai, "headers", headers);
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);
106 public void after() {
107 PowerMock.resetAll();
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}";
114 Method method = AaiQuery4Ccvpn.class.getDeclaredMethod("getPath", String.class);
115 method.setAccessible(true);
117 String ret = (String) method.invoke(aai, path);
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}"));
124 public void test_getLogicLink_exception() throws CorrelationException {
126 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
127 EasyMock.expect(response.readEntity(String.class)).andReturn("Error!");
129 thrown.expect(CorrelationException.class);
131 PowerMock.replayAll();
133 String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
135 PowerMock.verifyAll();
137 assertThat(linkId, equalTo("logic-link-1"));
142 public void test_getLogicLink() throws CorrelationException {
144 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("logic-link").toJSONString());
145 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
147 PowerMock.replayAll();
149 String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
151 PowerMock.verifyAll();
153 assertThat(linkId, equalTo("logic-link-1"));
158 public void test_getServiceInstances_exception() {
160 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("vpn-binding").toJSONString());
161 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
164 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("connectivity").toJSONString());
165 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
168 EasyMock.expect(response.readEntity(String.class))
169 .andReturn(data.getJSONObject("service-instance-by-connectivity").toJSONString());
170 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
173 EasyMock.expect(response.readEntity(String.class))
174 .andReturn(data.getJSONObject("service-instances-by-service-type").toJSONString());
175 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
178 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
179 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
181 thrown.expect(RuntimeException.class);
183 PowerMock.replayAll();
185 JSONArray instances = aai.getServiceInstances("network-1", "pnf-1", "interface-1", "DOWN");
187 PowerMock.verifyAll();
189 assertThat(instances, equalTo("logic-link-1"));
194 public void test_getServiceInstances() {
196 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("vpn-binding").toJSONString());
197 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
200 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("connectivity").toJSONString());
201 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
204 EasyMock.expect(response.readEntity(String.class))
205 .andReturn(data.getJSONObject("service-instance-by-connectivity").toJSONString());
206 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
209 EasyMock.expect(response.readEntity(String.class))
210 .andReturn(data.getJSONObject("service-instances-by-service-type").toJSONString());
211 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
214 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
215 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
217 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
218 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
220 EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
221 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
223 PowerMock.replayAll();
225 JSONArray instances = aai.getServiceInstances("network-1", "pnf-1", "interface-1", "DOWN");
227 PowerMock.verifyAll();
229 assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
230 assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
231 assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
232 assertThat(instances.getJSONObject(0).getString("input-parameters"), equalTo("This is the service instance recreation input looked up by CL."));
233 assertThat(instances.getJSONObject(0).getString("globalSubscriberId"), equalTo("e151059a-d924-4629-845f-264db19e50b4"));
234 assertThat(instances.getJSONObject(0).getString("serviceType"), equalTo("volte"));
238 public void test_getServiceInstances_1() throws Exception {
240 EasyMock.expect(response.readEntity(String.class))
241 .andReturn(data.getJSONObject("service-instances-by-service-type").toJSONString());
242 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
244 PowerMock.replayAll();
246 JSONArray instances = (JSONArray) Whitebox.invokeMethod(aai, "getServiceInstances",
247 "custom-1", "service-type-1");
249 PowerMock.verifyAll();
251 assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
252 assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
253 assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
257 public void test_getServiceInstances_1_exception() throws Exception {
259 EasyMock.expect(response.readEntity(String.class)).andReturn("Failed to get the service instance by type.");
260 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
262 thrown.expect(CorrelationException.class);
264 PowerMock.replayAll();
266 JSONArray instances = (JSONArray) Whitebox.invokeMethod(aai, "getServiceInstances",
267 "custom-1", "service-type-1");
269 PowerMock.verifyAll();
271 assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
272 assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
273 assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
277 public void test_updateTerminalPointStatus() throws CorrelationException {
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.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
292 EasyMock.expect(response.readEntity(String.class)).andReturn("Failed to update the TP information.");
294 thrown.expect(CorrelationException.class);
296 PowerMock.replayAll();
298 aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
300 PowerMock.verifyAll();
304 public void test_updateLogicLinkStatus() throws CorrelationException {
306 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
308 PowerMock.replayAll();
310 aai.updateLogicLinkStatus("link-1", new HashMap<>());
312 PowerMock.verifyAll();
316 public void test_updateLogicLinkStatus_exception() throws CorrelationException {
318 EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
319 EasyMock.expect(response.readEntity(String.class)).andReturn("Failed to update the logic link information.");
321 thrown.expect(CorrelationException.class);
323 PowerMock.replayAll();
325 aai.updateLogicLinkStatus("link-1", new HashMap<>());
327 PowerMock.verifyAll();
331 private void mockGetMethod() {
333 EasyMock.expect(builder.get()).andReturn(response);
336 private void mockPatchMethod() {
338 Invocation invocation = PowerMock.createMock(Invocation.class);
339 EasyMock.expect(builder.build(EasyMock.anyObject(String.class), EasyMock.anyObject(Entity.class))).andReturn(invocation);
340 EasyMock.expect(invocation.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true)).andReturn(invocation);
341 EasyMock.expect(invocation.invoke()).andReturn(response);
344 private void initCommonMock() {
345 EasyMock.expect(ClientBuilder.newClient()).andReturn(client);
346 EasyMock.expect(client.target(EasyMock.anyObject(String.class))).andReturn(webTarget);
347 EasyMock.expect(webTarget.path(EasyMock.anyObject(String.class))).andReturn(webTarget);
348 EasyMock.expect(webTarget.request()).andReturn(builder);
349 EasyMock.expect(builder.headers(headers)).andReturn(builder);