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;
39 import static org.onap.holmes.common.config.MicroServiceConfig.MSB_ADDR;
42 @RunWith(PowerMockRunner.class)
43 @PrepareForTest(JerseyClient.class)
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 System.setProperty(MSB_ADDR, "127.0.0.1:80");
61 File file = new File(AaiQuery4CcvpnTest.class.getClassLoader().getResource("./ccvpn.data.json").getFile());
62 BufferedReader reader = null;
64 reader = new BufferedReader(new FileReader(file));
65 StringBuilder sb = new StringBuilder();
66 reader.lines().forEach(l -> sb.append(l));
67 data = JsonParser.parseString(sb.toString()).getAsJsonObject();
68 } catch (FileNotFoundException e) {
70 } catch (IOException e) {
76 } catch (IOException e) {
82 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
83 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
84 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
85 headers.put("Accept", "application/json");
86 headers.put("Content-Type", "application/json");
87 Whitebox.setInternalState(aai, "headers", headers);
91 public void before() {
92 PowerMock.mockStatic(JerseyClient.class);
93 client = PowerMock.createMock(JerseyClient.class);
94 EasyMock.expect(JerseyClient.newInstance()).andReturn(client).anyTimes();
103 public void test_getPath() throws Exception {
104 String path = "/aai/v14/business/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}";
105 String ret = Whitebox.invokeMethod(aai, "getPath", path);
106 assertThat(ret, equalTo("/api/aai-business/v14/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}"));
110 public void test_getLogicLink() {
111 mockGetMethod(data.get("logic-link").toString());
113 PowerMock.replayAll();
115 String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
117 PowerMock.verifyAll();
119 assertThat(linkId, equalTo("logic-link-1"));
125 public void test_getServiceInstance() {
126 mockGetMethod(data.get("vpn-binding").toString());
127 mockGetMethod(data.get("connectivity").toString());
128 mockGetMethod(data.get("service-instance-by-connectivity").toString());
129 mockGetMethod(data.get("service-instances-by-service-type").toString());
131 PowerMock.replayAll();
133 JsonObject instance = aai.getServiceInstance("network-1", "pnf-1", "interface-1", "DOWN");
135 PowerMock.verifyAll();
137 assertThat(instance.get("service-instance-id").getAsString(), equalTo("some id 1"));
138 assertThat(instance.get("globalSubscriberId").getAsString(), equalTo("e151059a-d924-4629-845f-264db19e50b4"));
139 assertThat(instance.get("serviceType").getAsString(), equalTo("volte"));
143 public void test_updateTerminalPointStatus() throws CorrelationException {
144 mockGetMethod(data.toString());
147 PowerMock.replayAll();
149 aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
151 PowerMock.verifyAll();
156 public void test_updateLogicLinkStatus() {
157 mockGetMethod(data.toString());
160 PowerMock.replayAll();
162 aai.updateLogicLinkStatus("link-1", new HashMap<>());
164 PowerMock.verifyAll();
167 private void mockGetMethod(String ret) {
168 EasyMock.expect(client.path(anyString())).andReturn(client);
169 EasyMock.expect(client.headers(anyObject())).andReturn(client);
170 EasyMock.expect(client.get(anyString())).andReturn(ret);
173 private void mockPutMethod(String ok) {
174 EasyMock.expect(client.path(anyString())).andReturn(client);
175 EasyMock.expect(client.headers(anyObject())).andReturn(client);
176 EasyMock.expect(client.put(anyString(), anyObject())).andReturn(ok);