2 * Copyright 2018-2023 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.core.classloader.annotations.SuppressStaticInitializationFor;
29 import org.powermock.modules.junit4.PowerMockRunner;
30 import org.powermock.reflect.Whitebox;
33 import java.util.HashMap;
36 import static org.easymock.EasyMock.anyObject;
37 import static org.easymock.EasyMock.anyString;
38 import static org.hamcrest.CoreMatchers.equalTo;
39 import static org.junit.Assert.assertThat;
41 @RunWith(PowerMockRunner.class)
42 @PrepareForTest(JerseyClient.class)
43 @SuppressStaticInitializationFor("org.onap.holmes.common.utils.JerseyClient")
44 public class AaiQuery4CcvpnTest {
47 public ExpectedException thrown = ExpectedException.none();
49 private static JsonObject data;
51 private static AaiQuery4Ccvpn aai = AaiQuery4Ccvpn.newInstance();
53 private static Map<String, Object> headers = new HashMap<>();
55 private static JerseyClient client;
58 static public void beforeClass() {
59 File file = new File(AaiQuery4CcvpnTest.class.getClassLoader().getResource("./ccvpn.data.json").getFile());
60 BufferedReader reader = null;
62 reader = new BufferedReader(new FileReader(file));
63 StringBuilder sb = new StringBuilder();
64 reader.lines().forEach(l -> sb.append(l));
65 data = JsonParser.parseString(sb.toString()).getAsJsonObject();
66 } catch (FileNotFoundException e) {
68 } catch (IOException e) {
74 } catch (IOException e) {
80 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
81 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
82 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
83 headers.put("Accept", "application/json");
84 headers.put("Content-Type", "application/json");
85 Whitebox.setInternalState(aai, "headers", headers);
89 public void before() {
90 PowerMock.mockStatic(JerseyClient.class);
91 client = PowerMock.createMock(JerseyClient.class);
92 EasyMock.expect(JerseyClient.newInstance()).andReturn(client).anyTimes();
101 public void test_getPath() throws Exception {
102 String path = "/aai/v14/business/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}";
103 String ret = Whitebox.invokeMethod(aai, "getPath", path);
104 assertThat(ret, equalTo("/api/aai-business/v14/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}"));
108 public void test_getLogicLink() {
109 mockGetMethod(data.get("logic-link").toString());
111 PowerMock.replayAll();
113 String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
115 PowerMock.verifyAll();
117 assertThat(linkId, equalTo("logic-link-1"));
123 public void test_getServiceInstance() {
124 mockGetMethod(data.get("vpn-binding").toString());
125 mockGetMethod(data.get("connectivity").toString());
126 mockGetMethod(data.get("service-instance-by-connectivity").toString());
127 mockGetMethod(data.get("service-instances-by-service-type").toString());
129 PowerMock.replayAll();
131 JsonObject instance = aai.getServiceInstance("network-1", "pnf-1", "interface-1", "DOWN");
133 PowerMock.verifyAll();
135 assertThat(instance.get("service-instance-id").getAsString(), equalTo("some id 1"));
136 assertThat(instance.get("globalSubscriberId").getAsString(), equalTo("e151059a-d924-4629-845f-264db19e50b4"));
137 assertThat(instance.get("serviceType").getAsString(), equalTo("volte"));
141 public void test_updateTerminalPointStatus() throws CorrelationException {
142 mockGetMethod(data.toString());
145 PowerMock.replayAll();
147 aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
149 PowerMock.verifyAll();
154 public void test_updateLogicLinkStatus() {
155 mockGetMethod(data.toString());
158 PowerMock.replayAll();
160 aai.updateLogicLinkStatus("link-1", new HashMap<>());
162 PowerMock.verifyAll();
165 private void mockGetMethod(String ret) {
166 EasyMock.expect(client.path(anyString())).andReturn(client);
167 EasyMock.expect(client.headers(anyObject())).andReturn(client);
168 EasyMock.expect(client.get(anyString())).andReturn(ret);
171 private void mockPutMethod(String ok) {
172 EasyMock.expect(client.path(anyString())).andReturn(client);
173 EasyMock.expect(client.headers(anyObject())).andReturn(client);
174 EasyMock.expect(client.put(anyString(), anyObject())).andReturn(ok);