Add some comments to public methods
[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 Exception {
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 = (JSONArray)Whitebox.invokeMethod(aai, "getServiceInstances",
222                 "custom-1", "service-type-1");
223
224         PowerMock.verifyAll();
225
226         assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
227         assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
228         assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
229     }
230
231     @Test
232     public void test_getServiceInstances_1_exception() throws Exception {
233         mockGetMethod();
234         EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type"));
235         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
236
237         thrown.expect(CorrelationException.class);
238
239         PowerMock.replayAll();
240
241         JSONArray instances = (JSONArray)Whitebox.invokeMethod(aai, "getServiceInstances",
242                 "custom-1", "service-type-1");
243
244         PowerMock.verifyAll();
245
246         assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
247         assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
248         assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
249     }
250
251     @Test
252     public void test_updateTerminalPointStatus() throws CorrelationException {
253         mockPatchMethod();
254         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
255
256         PowerMock.replayAll();
257
258         aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
259
260         PowerMock.verifyAll();
261     }
262
263     @Test
264     public void test_updateTerminalPointStatus_exception() throws CorrelationException {
265         mockPatchMethod();
266         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
267
268         thrown.expect(CorrelationException.class);
269
270         PowerMock.replayAll();
271
272         aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
273
274         PowerMock.verifyAll();
275     }
276
277     @Test
278     public void test_updateLogicLinkStatus() throws CorrelationException {
279         mockPatchMethod();
280         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
281
282         PowerMock.replayAll();
283
284         aai.updateLogicLinkStatus("link-1", new HashMap<>());
285
286         PowerMock.verifyAll();
287     }
288
289     @Test
290     public void test_updateLogicLinkStatus_exception() throws CorrelationException {
291         mockPatchMethod();
292         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
293
294         thrown.expect(CorrelationException.class);
295
296         PowerMock.replayAll();
297
298         aai.updateLogicLinkStatus("link-1", new HashMap<>());
299
300         PowerMock.verifyAll();
301
302     }
303
304     private void mockGetMethod() {
305         initCommonMock();
306         EasyMock.expect(builder.get()).andReturn(response);
307     }
308
309     private void mockPatchMethod() {
310         initCommonMock();
311         EasyMock.expect(builder.method(EasyMock.anyObject(String.class), EasyMock.anyObject(Entity.class))).andReturn(response);
312     }
313
314     private void initCommonMock() {
315         EasyMock.expect(ClientBuilder.newClient()).andReturn(client);
316         EasyMock.expect(client.target(EasyMock.anyObject(String.class))).andReturn(webTarget);
317         EasyMock.expect(webTarget.path(EasyMock.anyObject(String.class))).andReturn(webTarget);
318         EasyMock.expect(webTarget.request()).andReturn(builder);
319         EasyMock.expect(builder.headers(headers)).andReturn(builder);
320     }
321 }