Add Several Fields to the AAI section
[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 = AaiQuery4Ccvpn.newInstance();
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         mockGetMethod();
173         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
174         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
175
176         thrown.expect(RuntimeException.class);
177
178         PowerMock.replayAll();
179
180         JSONArray instances = aai.getServiceInstances("network-1", "pnf-1", "interface-1", "DOWN");
181
182         PowerMock.verifyAll();
183
184         assertThat(instances, equalTo("logic-link-1"));
185
186     }
187
188     @Test
189     public void test_getServiceInstances() {
190         mockGetMethod();
191         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("vpn-binding"));
192         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
193
194         mockGetMethod();
195         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("connectivity"));
196         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
197
198         mockGetMethod();
199         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instance-by-connectivity"));
200         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
201
202         mockGetMethod();
203         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type"));
204         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
205
206         mockGetMethod();
207         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
208         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
209         mockGetMethod();
210         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
211         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
212         mockGetMethod();
213         EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString());
214         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
215
216         PowerMock.replayAll();
217
218         JSONArray instances = aai.getServiceInstances("network-1", "pnf-1", "interface-1", "DOWN");
219
220         PowerMock.verifyAll();
221
222         assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
223         assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
224         assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
225         assertThat(instances.getJSONObject(0).getString("input-parameters"), equalTo("This is the service instance recreation input looked up by CL."));
226         assertThat(instances.getJSONObject(0).getString("globalSubscriberId"), equalTo("e151059a-d924-4629-845f-264db19e50b4"));
227         assertThat(instances.getJSONObject(0).getString("serviceType"), equalTo("volte"));
228     }
229
230     @Test
231     public void test_getServiceInstances_1() throws Exception {
232         mockGetMethod();
233         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type"));
234         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
235
236         PowerMock.replayAll();
237
238         JSONArray instances = (JSONArray)Whitebox.invokeMethod(aai, "getServiceInstances",
239                 "custom-1", "service-type-1");
240
241         PowerMock.verifyAll();
242
243         assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
244         assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
245         assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
246     }
247
248     @Test
249     public void test_getServiceInstances_1_exception() throws Exception {
250         mockGetMethod();
251         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type"));
252         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
253
254         thrown.expect(CorrelationException.class);
255
256         PowerMock.replayAll();
257
258         JSONArray instances = (JSONArray)Whitebox.invokeMethod(aai, "getServiceInstances",
259                 "custom-1", "service-type-1");
260
261         PowerMock.verifyAll();
262
263         assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
264         assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
265         assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
266     }
267
268     @Test
269     public void test_updateTerminalPointStatus() throws CorrelationException {
270         mockPatchMethod();
271         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
272
273         PowerMock.replayAll();
274
275         aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
276
277         PowerMock.verifyAll();
278     }
279
280     @Test
281     public void test_updateTerminalPointStatus_exception() throws CorrelationException {
282         mockPatchMethod();
283         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
284
285         thrown.expect(CorrelationException.class);
286
287         PowerMock.replayAll();
288
289         aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
290
291         PowerMock.verifyAll();
292     }
293
294     @Test
295     public void test_updateLogicLinkStatus() throws CorrelationException {
296         mockPatchMethod();
297         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
298
299         PowerMock.replayAll();
300
301         aai.updateLogicLinkStatus("link-1", new HashMap<>());
302
303         PowerMock.verifyAll();
304     }
305
306     @Test
307     public void test_updateLogicLinkStatus_exception() throws CorrelationException {
308         mockPatchMethod();
309         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
310
311         thrown.expect(CorrelationException.class);
312
313         PowerMock.replayAll();
314
315         aai.updateLogicLinkStatus("link-1", new HashMap<>());
316
317         PowerMock.verifyAll();
318
319     }
320
321     private void mockGetMethod() {
322         initCommonMock();
323         EasyMock.expect(builder.get()).andReturn(response);
324     }
325
326     private void mockPatchMethod() {
327         initCommonMock();
328         EasyMock.expect(builder.method(EasyMock.anyObject(String.class), EasyMock.anyObject(Entity.class))).andReturn(response);
329     }
330
331     private void initCommonMock() {
332         EasyMock.expect(ClientBuilder.newClient()).andReturn(client);
333         EasyMock.expect(client.target(EasyMock.anyObject(String.class))).andReturn(webTarget);
334         EasyMock.expect(webTarget.path(EasyMock.anyObject(String.class))).andReturn(webTarget);
335         EasyMock.expect(webTarget.request()).andReturn(builder);
336         EasyMock.expect(builder.headers(headers)).andReturn(builder);
337     }
338 }