Released Version 1.4.7
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / aai / AaiQuery4CcvpnTest.java
1 /**
2  * Copyright 2018-2023 ZTE Corporation.
3  * <p>
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
6  * <p>
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * <p>
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
12  * the License.
13  */
14
15 package org.onap.holmes.common.aai;
16
17 import com.google.gson.JsonObject;
18 import com.google.gson.JsonParser;
19 import org.easymock.EasyMock;
20 import org.junit.*;
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;
31
32 import java.io.*;
33 import java.util.HashMap;
34 import java.util.Map;
35
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;
40
41 @RunWith(PowerMockRunner.class)
42 @PrepareForTest(JerseyClient.class)
43 @SuppressStaticInitializationFor("org.onap.holmes.common.utils.JerseyClient")
44 public class AaiQuery4CcvpnTest {
45
46     @Rule
47     public ExpectedException thrown = ExpectedException.none();
48
49     private static JsonObject data;
50
51     private static AaiQuery4Ccvpn aai = AaiQuery4Ccvpn.newInstance();
52
53     private static Map<String, Object> headers = new HashMap<>();
54
55     private static JerseyClient client;
56
57     @BeforeClass
58     static public void beforeClass() {
59         File file = new File(AaiQuery4CcvpnTest.class.getClassLoader().getResource("./ccvpn.data.json").getFile());
60         BufferedReader reader = null;
61         try {
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) {
67             // Do nothing
68         } catch (IOException e) {
69             // Do nothing
70         } finally {
71             if (reader != null) {
72                 try {
73                     reader.close();
74                 } catch (IOException e) {
75                     // Do nothing
76                 }
77             }
78         }
79
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);
86     }
87
88     @Before
89     public void before() {
90         PowerMock.mockStatic(JerseyClient.class);
91         client = PowerMock.createMock(JerseyClient.class);
92         EasyMock.expect(JerseyClient.newInstance()).andReturn(client).anyTimes();
93     }
94
95     @After
96     public void after() {
97         PowerMock.resetAll();
98     }
99
100     @Test
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}"));
105     }
106
107     @Test
108     public void test_getLogicLink() {
109         mockGetMethod(data.get("logic-link").toString());
110
111         PowerMock.replayAll();
112
113         String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
114
115         PowerMock.verifyAll();
116
117         assertThat(linkId, equalTo("logic-link-1"));
118
119     }
120
121
122     @Test
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());
128
129         PowerMock.replayAll();
130
131         JsonObject instance = aai.getServiceInstance("network-1", "pnf-1", "interface-1", "DOWN");
132
133         PowerMock.verifyAll();
134
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"));
138     }
139
140     @Test
141     public void test_updateTerminalPointStatus() throws CorrelationException {
142         mockGetMethod(data.toString());
143         mockPutMethod("ok");
144
145         PowerMock.replayAll();
146
147         aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
148
149         PowerMock.verifyAll();
150     }
151
152
153     @Test
154     public void test_updateLogicLinkStatus() {
155         mockGetMethod(data.toString());
156         mockPutMethod("ok");
157
158         PowerMock.replayAll();
159
160         aai.updateLogicLinkStatus("link-1", new HashMap<>());
161
162         PowerMock.verifyAll();
163     }
164
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);
169     }
170
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);
175     }
176 }