2 * Copyright 2018-2021 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.google.gson.JsonObject;
18 import com.google.gson.JsonParser;
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.onap.holmes.common.utils.JerseyClient;
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;
32 import java.util.HashMap;
35 import static org.easymock.EasyMock.anyObject;
36 import static org.easymock.EasyMock.anyString;
37 import static org.hamcrest.CoreMatchers.equalTo;
38 import static org.junit.Assert.assertThat;
40 @RunWith(PowerMockRunner.class)
41 @PrepareForTest(JerseyClient.class)
42 public class AaiQuery4CcvpnTest {
45 public ExpectedException thrown = ExpectedException.none();
47 private static JsonObject data;
49 private static AaiQuery4Ccvpn aai = AaiQuery4Ccvpn.newInstance();
51 private static Map<String, Object> headers = new HashMap<>();
53 private static JerseyClient client;
56 static public void beforeClass() {
57 File file = new File(AaiQuery4CcvpnTest.class.getClassLoader().getResource("./ccvpn.data.json").getFile());
58 BufferedReader reader = null;
60 reader = new BufferedReader(new FileReader(file));
61 StringBuilder sb = new StringBuilder();
62 reader.lines().forEach(l -> sb.append(l));
63 data = JsonParser.parseString(sb.toString()).getAsJsonObject();
64 } catch (FileNotFoundException e) {
66 } catch (IOException e) {
72 } catch (IOException e) {
78 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
79 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
80 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
81 headers.put("Accept", "application/json");
82 headers.put("Content-Type", "application/json");
83 Whitebox.setInternalState(aai, "headers", headers);
87 public void before() {
88 PowerMock.mockStatic(JerseyClient.class);
89 client = PowerMock.createMock(JerseyClient.class);
90 EasyMock.expect(JerseyClient.newInstance()).andReturn(client).anyTimes();
99 public void test_getPath() throws Exception {
100 String path = "/aai/v14/business/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}";
101 String ret = Whitebox.invokeMethod(aai, "getPath", path);
102 assertThat(ret, equalTo("/api/aai-business/v14/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}"));
106 public void test_getLogicLink() {
107 mockGetMethod(data.get("logic-link").toString());
109 PowerMock.replayAll();
111 String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
113 PowerMock.verifyAll();
115 assertThat(linkId, equalTo("logic-link-1"));
121 public void test_getServiceInstance() {
122 mockGetMethod(data.get("vpn-binding").toString());
123 mockGetMethod(data.get("connectivity").toString());
124 mockGetMethod(data.get("service-instance-by-connectivity").toString());
125 mockGetMethod(data.get("service-instances-by-service-type").toString());
127 PowerMock.replayAll();
129 JsonObject instance = aai.getServiceInstance("network-1", "pnf-1", "interface-1", "DOWN");
131 PowerMock.verifyAll();
133 assertThat(instance.get("service-instance-id").getAsString(), equalTo("some id 1"));
134 assertThat(instance.get("globalSubscriberId").getAsString(), equalTo("e151059a-d924-4629-845f-264db19e50b4"));
135 assertThat(instance.get("serviceType").getAsString(), equalTo("volte"));
139 public void test_updateTerminalPointStatus() throws CorrelationException {
140 mockGetMethod(data.toString());
143 PowerMock.replayAll();
145 aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
147 PowerMock.verifyAll();
152 public void test_updateLogicLinkStatus() {
153 mockGetMethod(data.toString());
156 PowerMock.replayAll();
158 aai.updateLogicLinkStatus("link-1", new HashMap<>());
160 PowerMock.verifyAll();
163 private void mockGetMethod(String ret) {
164 EasyMock.expect(client.path(anyString())).andReturn(client);
165 EasyMock.expect(client.headers(anyObject())).andReturn(client);
166 EasyMock.expect(client.get(anyString())).andReturn(ret);
169 private void mockPutMethod(String ok) {
170 EasyMock.expect(client.path(anyString())).andReturn(client);
171 EasyMock.expect(client.headers(anyObject())).andReturn(client);
172 EasyMock.expect(client.put(anyString(), anyObject())).andReturn(ok);