Added AAI Query Tool
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / aai / AaiQuery4CcvpnTest.java
1 /**
2  * Copyright 2018 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.alibaba.fastjson.JSONArray;
18 import com.alibaba.fastjson.JSONObject;
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.powermock.api.easymock.PowerMock;
26 import org.powermock.core.classloader.annotations.PrepareForTest;
27 import org.powermock.modules.junit4.PowerMockRunner;
28 import org.powermock.reflect.Whitebox;
29
30 import javax.ws.rs.client.*;
31 import javax.ws.rs.client.Invocation.Builder;
32 import javax.ws.rs.core.MultivaluedHashMap;
33 import javax.ws.rs.core.MultivaluedMap;
34 import javax.ws.rs.core.Response;
35 import java.io.*;
36 import java.lang.reflect.InvocationTargetException;
37 import java.lang.reflect.Method;
38 import java.util.HashMap;
39
40 import static org.hamcrest.CoreMatchers.equalTo;
41 import static org.junit.Assert.assertThat;
42 import static org.onap.holmes.common.config.MicroServiceConfig.MSB_ADDR;
43
44
45 @RunWith(PowerMockRunner.class)
46 @PrepareForTest({ClientBuilder.class, Client.class, Builder.class, WebTarget.class, Response.class})
47 public class AaiQuery4CcvpnTest {
48
49     @Rule
50     public ExpectedException thrown = ExpectedException.none();
51
52     private static JSONObject data;
53
54     private static AaiQuery4Ccvpn aai = new AaiQuery4Ccvpn();
55
56     private static MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
57     private static Client client;
58     private static WebTarget webTarget;
59     private static Builder builder;
60     private static Response response;
61
62     @BeforeClass
63     static public void beforeClass() {
64         System.setProperty(MSB_ADDR, "127.0.0.1:80");
65
66         File file = new File(AaiQuery4CcvpnTest.class.getClassLoader().getResource("./ccvpn.data.json").getFile());
67         BufferedReader reader = null;
68         try {
69             reader = new BufferedReader(new FileReader(file));
70             StringBuilder sb = new StringBuilder();
71             reader.lines().forEach(l -> sb.append(l));
72             data = JSONObject.parseObject(sb.toString());
73         } catch (FileNotFoundException e) {
74             // Do nothing
75         } catch (IOException e) {
76             // Do nothing
77         } finally {
78             if (reader != null) {
79                 try {
80                     reader.close();
81                 } catch (IOException e) {
82                     // Do nothing
83                 }
84             }
85         }
86
87         headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
88         headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID);
89         headers.add("Authorization", AaiConfig.getAuthenticationCredentials());
90         headers.add("Accept", "application/json");
91         Whitebox.setInternalState(aai, "headers", headers);
92     }
93
94     @Before
95     public void before() {
96         PowerMock.mockStatic(ClientBuilder.class);
97         client = PowerMock.createMock(Client.class);
98         webTarget = PowerMock.createMock(WebTarget.class);
99         builder = PowerMock.createMock(Builder.class);
100         response = PowerMock.createMock(Response.class);
101     }
102
103     @After
104     public void after() {
105         PowerMock.resetAll();
106     }
107
108     @Test
109     public void test_getPath() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
110         String path = "/aai/v14/business/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}";
111
112         Method method = AaiQuery4Ccvpn.class.getDeclaredMethod("getPath", String.class);
113         method.setAccessible(true);
114
115         String ret = (String) method.invoke(aai, path);
116
117         assertThat(ret, equalTo("/api/aai-business/v14/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}"));
118
119     }
120
121     @Test
122     public void test_getLogicLink_exception() {
123         mockGetMethod();
124         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
125
126         thrown.expect(RuntimeException.class);
127
128         PowerMock.replayAll();
129
130         String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
131
132         PowerMock.verifyAll();
133
134         assertThat(linkId, equalTo("logic-link-1"));
135
136     }
137
138     @Test
139     public void test_getLogicLink() {
140         mockGetMethod();
141         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("logic-link"));
142         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
143
144         PowerMock.replayAll();
145
146         String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
147
148         PowerMock.verifyAll();
149
150         assertThat(linkId, equalTo("logic-link-1"));
151
152     }
153
154     @Test
155     public void test_getServiceInstances_exception() {
156         mockGetMethod();
157         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("vpn-binding"));
158         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
159
160         mockGetMethod();
161         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("connectivity"));
162         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
163
164         mockGetMethod();
165         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instance-by-connectivity"));
166         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
167
168         mockGetMethod();
169         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type"));
170         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
171
172         thrown.expect(RuntimeException.class);
173
174         PowerMock.replayAll();
175
176         JSONArray instances = aai.getServiceInstances("network-1", "pnf-1", "interface-1", "DOWN");
177
178         PowerMock.verifyAll();
179
180         assertThat(instances, equalTo("logic-link-1"));
181
182     }
183
184     @Test
185     public void test_getServiceInstances() {
186         mockGetMethod();
187         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("vpn-binding"));
188         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
189
190         mockGetMethod();
191         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("connectivity"));
192         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
193
194         mockGetMethod();
195         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instance-by-connectivity"));
196         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
197
198         mockGetMethod();
199         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type"));
200         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
201
202         PowerMock.replayAll();
203
204         JSONArray instances = aai.getServiceInstances("network-1", "pnf-1", "interface-1", "DOWN");
205
206         PowerMock.verifyAll();
207
208         assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
209         assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
210         assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
211     }
212
213     @Test
214     public void test_getServiceInstances_1() throws CorrelationException {
215         mockGetMethod();
216         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type"));
217         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
218
219         PowerMock.replayAll();
220
221         JSONArray instances = aai.getServiceInstances("custom-1", "service-type-1");
222
223         PowerMock.verifyAll();
224
225         assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
226         assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
227         assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
228     }
229
230     @Test
231     public void test_getServiceInstances_1_exception() throws CorrelationException {
232         mockGetMethod();
233         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type"));
234         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
235
236         thrown.expect(CorrelationException.class);
237
238         PowerMock.replayAll();
239
240         JSONArray instances = aai.getServiceInstances("custom-1", "service-type-1");
241
242         PowerMock.verifyAll();
243
244         assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
245         assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
246         assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
247     }
248
249     @Test
250     public void test_updateTerminalPointStatus() throws CorrelationException {
251         mockPatchMethod();
252         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
253
254         PowerMock.replayAll();
255
256         aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
257
258         PowerMock.verifyAll();
259     }
260
261     @Test
262     public void test_updateTerminalPointStatus_exception() throws CorrelationException {
263         mockPatchMethod();
264         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
265
266         thrown.expect(CorrelationException.class);
267
268         PowerMock.replayAll();
269
270         aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
271
272         PowerMock.verifyAll();
273     }
274
275     @Test
276     public void test_updateLogicLinkStatus() throws CorrelationException {
277         mockPatchMethod();
278         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
279
280         PowerMock.replayAll();
281
282         aai.updateLogicLinkStatus("link-1", new HashMap<>());
283
284         PowerMock.verifyAll();
285     }
286
287     @Test
288     public void test_updateLogicLinkStatus_exception() throws CorrelationException {
289         mockPatchMethod();
290         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
291
292         thrown.expect(CorrelationException.class);
293
294         PowerMock.replayAll();
295
296         aai.updateLogicLinkStatus("link-1", new HashMap<>());
297
298         PowerMock.verifyAll();
299
300     }
301
302     private void mockGetMethod() {
303         initCommonMock();
304         EasyMock.expect(builder.get()).andReturn(response);
305     }
306
307     private void mockPatchMethod() {
308         initCommonMock();
309         EasyMock.expect(builder.method(EasyMock.anyObject(String.class), EasyMock.anyObject(Entity.class))).andReturn(response);
310     }
311
312     private void initCommonMock() {
313         EasyMock.expect(ClientBuilder.newClient()).andReturn(client);
314         EasyMock.expect(client.target(EasyMock.anyObject(String.class))).andReturn(webTarget);
315         EasyMock.expect(webTarget.path(EasyMock.anyObject(String.class))).andReturn(webTarget);
316         EasyMock.expect(webTarget.request()).andReturn(builder);
317         EasyMock.expect(builder.headers(headers)).andReturn(builder);
318     }
319 }