de7a58c8e14ca9d75c0423e8e98c0490477755aa
[usecase-ui/server.git] /
1 /*
2  * Copyright (C) 2021 CTC, Inc. and others. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.usecaseui.server.service.intent.impl;
17
18 import java.io.IOException;
19 import java.io.Serializable;
20 import java.util.*;
21
22 import com.alibaba.fastjson.JSONArray;
23 import com.alibaba.fastjson.JSONObject;
24 import okhttp3.MediaType;
25 import okhttp3.ResponseBody;
26 import okio.BufferedSource;
27 import org.hibernate.Session;
28 import org.hibernate.SessionFactory;
29 import org.hibernate.Transaction;
30 import org.hibernate.query.Query;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.onap.usecaseui.server.bean.intent.CCVPNInstance;
38 import org.onap.usecaseui.server.bean.intent.InstancePerformance;
39 import org.onap.usecaseui.server.bean.intent.IntentModel;
40 import org.onap.usecaseui.server.service.intent.IntentApiService;
41 import org.onap.usecaseui.server.service.lcm.domain.so.SOService;
42 import org.onap.usecaseui.server.service.lcm.domain.so.bean.OperationProgress;
43 import org.onap.usecaseui.server.service.lcm.domain.so.bean.OperationProgressInformation;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.api.support.membermodification.MemberModifier;
46 import org.powermock.modules.junit4.PowerMockRunner;
47
48 import static org.junit.Assert.*;
49 import static org.mockito.ArgumentMatchers.*;
50 import static org.powermock.api.mockito.PowerMockito.*;
51
52 import retrofit2.Call;
53 import retrofit2.Response;
54
55 import javax.annotation.Nullable;
56
57 @RunWith(PowerMockRunner.class)
58 public class IntentInstanceServiceImplTest {
59
60     public IntentInstanceServiceImplTest() {
61     }
62
63     @InjectMocks
64     private IntentInstanceServiceImpl intentInstanceService;
65
66     @Mock
67     private IntentApiService intentApiService;
68
69     @Mock
70     private SOService soService;
71
72
73     @Mock
74     private SessionFactory sessionFactory;
75
76     @Mock
77     private Session session;
78
79     @Before
80     public void before() throws Exception {
81         MemberModifier.field(IntentInstanceServiceImpl.class, "sessionFactory").set(intentInstanceService , sessionFactory);
82         doReturn(session).when(sessionFactory,"openSession");
83     }
84
85     @Test
86     public void queryIntentInstanceTest() {
87         CCVPNInstance instance = new CCVPNInstance();
88         instance.setInstanceId("1");
89         instance.setJobId("1");
90         instance.setStatus("1");
91
92         Query query = Mockito.mock(Query.class);
93         when(session.createQuery(anyString())).thenReturn(query);
94         List<IntentModel> list = new ArrayList<>();
95         when(query.list()).thenReturn(list);
96         when(query.uniqueResult()).thenReturn(10L);
97         assertTrue(intentInstanceService.queryIntentInstance(instance,1,2).getList().isEmpty());
98     }
99
100     @Test
101     public void queryIntentInstanceGetCountErrorTest() {
102         CCVPNInstance instance = new CCVPNInstance();
103         instance.setInstanceId("1");
104         instance.setJobId("1");
105         instance.setStatus("1");
106
107         Query query = Mockito.mock(Query.class);
108         when(session.createQuery(anyString())).thenReturn(query);
109         List<IntentModel> list = new ArrayList<>();
110         when(query.list()).thenReturn(list);
111         when(query.uniqueResult()).thenReturn(10);
112         assertTrue(intentInstanceService.queryIntentInstance(instance,1,2).getList().isEmpty());
113     }
114
115     @Test
116     public void queryIntentInstanceThrowErrorTest() {
117         CCVPNInstance instance = new CCVPNInstance();
118         instance.setInstanceId("1");
119         instance.setJobId("1");
120         instance.setStatus("1");
121
122         when(session.createQuery(anyString())).thenThrow(new RuntimeException());
123
124         assertEquals(intentInstanceService.queryIntentInstance(instance,1,2), null);
125     }
126     @Test
127     public void createIntentInstanceTest() throws IOException {
128         CCVPNInstance instance = new CCVPNInstance();
129         instance.setInstanceId("1");
130         instance.setJobId("1");
131         instance.setStatus("1");
132
133         Call mockCall = PowerMockito.mock(Call.class);
134         JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}");
135         Response<JSONObject> response = Response.success(body);
136         Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall);
137         Mockito.when(mockCall.execute()).thenReturn(response);
138
139         IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
140         doNothing().when(spy).saveIntentInstanceToAAI(isNull(),any(CCVPNInstance.class));
141
142         Transaction tx = Mockito.mock(Transaction.class);
143         Mockito.when(session.beginTransaction()).thenReturn(tx);
144         Serializable save = Mockito.mock(Serializable.class);
145         Mockito.when(session.save(any())).thenReturn(save);
146         Mockito.doNothing().when(tx).commit();
147
148         assertEquals(spy.createIntentInstance(instance), 1);
149     }
150
151     @Test
152     public void createIntentInstanceThrowErrorTest() throws IOException {
153         CCVPNInstance instance = new CCVPNInstance();
154         instance.setInstanceId("1");
155         instance.setJobId("1");
156         instance.setStatus("1");
157
158         Call mockCall = PowerMockito.mock(Call.class);
159         JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}");
160         Response<JSONObject> response = Response.success(body);
161         Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall);
162         Mockito.when(mockCall.execute()).thenReturn(response);
163
164         IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
165         doThrow(new RuntimeException()).when(spy).saveIntentInstanceToAAI(isNull(),any(CCVPNInstance.class));
166
167         Transaction tx = Mockito.mock(Transaction.class);
168         Mockito.when(session.beginTransaction()).thenReturn(tx);
169         Serializable save = Mockito.mock(Serializable.class);
170         Mockito.when(session.save(any())).thenReturn(save);
171         Mockito.doNothing().when(tx).commit();
172
173         assertEquals(spy.createIntentInstance(instance), 0);
174     }
175
176     @Test
177     public void createIntentInstanceInstanceIsNullTest() throws IOException {
178         assertEquals(intentInstanceService.createIntentInstance(null), 0);
179     }
180     @Test
181     public void createIntentInstanceInstanceJobIdIsNullTest() throws IOException {
182         CCVPNInstance instance = new CCVPNInstance();
183         instance.setInstanceId("1");
184         instance.setStatus("1");
185         assertEquals(intentInstanceService.createIntentInstance(instance), 0);
186     }
187
188     @Test
189     public void getIntentInstanceProgressTest() throws IOException {
190
191         Query query1 = Mockito.mock(Query.class);
192         when(session.createQuery("from CCVPNInstance where deleteState = 0 and status = '0'")).thenReturn(query1);
193         List<CCVPNInstance> q = new ArrayList<>();
194         CCVPNInstance instance = new CCVPNInstance();
195         instance.setInstanceId("1");
196         instance.setResourceInstanceId("1");
197         instance.setJobId("1");
198         q.add(instance);
199         when(query1.list()).thenReturn(q);
200
201         OperationProgressInformation operationProgressInformation = new OperationProgressInformation();
202         OperationProgress operationProgress = new OperationProgress();
203         operationProgress.setProgress(100);
204         operationProgressInformation.setOperationStatus(operationProgress);
205
206         JSONObject jsonObject = new JSONObject();
207         JSONObject operation = new JSONObject();
208         operation.put("progress", 100);
209         jsonObject.put("operation", operation);
210         Call mockCall = PowerMockito.mock(Call.class);
211         Response<JSONObject> response = Response.success(jsonObject);
212         Mockito.when(intentApiService.queryOperationProgress(anyString(),anyString())).thenReturn(mockCall);
213         Mockito.when(mockCall.execute()).thenReturn(response);
214
215         IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
216         doNothing().when(spy).saveIntentInstanceToAAI(anyString(),any(CCVPNInstance.class));
217
218         Transaction tx = Mockito.mock(Transaction.class);
219         Mockito.when(session.beginTransaction()).thenReturn(tx);
220         Serializable save = Mockito.mock(Serializable.class);
221         Mockito.when(session.save(any())).thenReturn(save);
222         Mockito.doNothing().when(tx).commit();
223
224         spy.getIntentInstanceProgress();
225         assertEquals(operation.getString("progress"),"100");
226     }
227     @Test
228     public void getIntentInstanceCreateStatusTest() throws IOException {
229
230         Query query1 = Mockito.mock(Query.class);
231         when(session.createQuery("from CCVPNInstance where deleteState = 0 and status = '0'")).thenReturn(query1);
232         List<CCVPNInstance> q = new ArrayList<>();
233         CCVPNInstance instance = new CCVPNInstance();
234         instance.setInstanceId("1");
235         instance.setResourceInstanceId("1");
236         instance.setJobId("1");
237         q.add(instance);
238         when(query1.list()).thenReturn(q);
239
240         OperationProgressInformation operationProgressInformation = new OperationProgressInformation();
241         OperationProgress operationProgress = new OperationProgress();
242         operationProgress.setProgress(100);
243         operationProgressInformation.setOperationStatus(operationProgress);
244
245         JSONObject jsonObject = new JSONObject();
246         jsonObject.put("orchestration-status", "created");
247         Call mockCall = PowerMockito.mock(Call.class);
248         Response<JSONObject> response = Response.success(jsonObject);
249         Mockito.when(intentApiService.getInstanceInfo(anyString())).thenReturn(mockCall);
250         Mockito.when(mockCall.execute()).thenReturn(response);
251
252         IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
253         doNothing().when(spy).saveIntentInstanceToAAI(anyString(),any(CCVPNInstance.class));
254
255         Transaction tx = Mockito.mock(Transaction.class);
256         Mockito.when(session.beginTransaction()).thenReturn(tx);
257         Serializable save = Mockito.mock(Serializable.class);
258         Mockito.when(session.save(any())).thenReturn(save);
259         Mockito.doNothing().when(tx).commit();
260
261         spy.getIntentInstanceCreateStatus();
262         assertEquals(jsonObject.getString("orchestration-status"),"created");
263     }
264
265     @Test
266     public void getFinishedInstanceInfo() {
267         Query query = Mockito.mock(Query.class);
268         when(session.createQuery(anyString())).thenReturn(query);
269         when(query.list()).thenReturn(new ArrayList());
270         assertTrue(intentInstanceService.getFinishedInstanceInfo().isEmpty());
271     }
272
273     @Test
274     public void getIntentInstanceBandwidth() throws IOException {
275         Query query1 = Mockito.mock(Query.class);
276         when(session.createQuery("from CCVPNInstance where deleteState = 0 and status = '1'")).thenReturn(query1);
277         List<CCVPNInstance> q = new ArrayList<>();
278         CCVPNInstance instance = new CCVPNInstance();
279         instance.setInstanceId("1");
280         instance.setResourceInstanceId("1");
281         q.add(instance);
282         when(query1.list()).thenReturn(q);
283
284         Call mockCall = PowerMockito.mock(Call.class);
285         JSONObject jsonObject = JSONObject.parseObject("{\n" +
286                 "    \"service-instance-id\":\"cll-101\",\n" +
287                 "    \"service-instance-name\":\"cloud-leased-line-101\",\n" +
288                 "    \"service-type\":\"CLL\",\n" +
289                 "    \"service-role\":\"cll\",\n" +
290                 "    \"environment-context\":\"cll\",\n" +
291                 "    \"model-invariant-id\":\"6790ab0e-034f-11eb-adc1-0242ac120002\",\n" +
292                 "    \"model-version-id\":\"6790ab0e-034f-11eb-adc1-0242ac120002\",\n" +
293                 "    \"resource-version\":\"1628714665927\",\n" +
294                 "    \"orchestration-status\":\"created\",\n" +
295                 "    \"allotted-resources\":{\n" +
296                 "        \"allotted-resource\":[\n" +
297                 "            {\n" +
298                 "                \"id\":\"cll-101-network-001\",\n" +
299                 "                \"resource-version\":\"1628714665798\",\n" +
300                 "                \"type\":\"TsciNetwork\",\n" +
301                 "                \"allotted-resource-name\":\"network_cll-101-network-001\",\n" +
302                 "                \"relationship-list\":{\n" +
303                 "                    \"relationship\":[\n" +
304                 "                        {\n" +
305                 "                            \"related-to\":\"logical-link\",\n" +
306                 "                            \"relationship-label\":\"org.onap.relationships.inventory.ComposedOf\",\n" +
307                 "                            \"related-link\":\"/aai/v24/network/logical-links/logical-link/tranportEp_UNI_ID_311_1\",\n" +
308                 "                            \"relationship-data\":[\n" +
309                 "                                {\n" +
310                 "                                    \"relationship-key\":\"logical-link.link-name\",\n" +
311                 "                                    \"relationship-value\":\"tranportEp_UNI_ID_311_1\"\n" +
312                 "                                }\n" +
313                 "                            ]\n" +
314                 "                        },\n" +
315                 "                        {\n" +
316                 "                            \"related-to\":\"network-policy\",\n" +
317                 "                            \"relationship-label\":\"org.onap.relationships.inventory.Uses\",\n" +
318                 "                            \"related-link\":\"/aai/v24/network/network-policies/network-policy/de00a0a0-be2e-4d19-974a-80a2bca6bdf9\",\n" +
319                 "                            \"relationship-data\":[\n" +
320                 "                                {\n" +
321                 "                                    \"relationship-key\":\"network-policy.network-policy-id\",\n" +
322                 "                                    \"relationship-value\":\"de00a0a0-be2e-4d19-974a-80a2bca6bdf9\"\n" +
323                 "                                }\n" +
324                 "                            ],\n" +
325                 "                            \"related-to-property\":[\n" +
326                 "                                {\n" +
327                 "                                    \"property-key\":\"network-policy.network-policy-fqdn\",\n" +
328                 "                                    \"property-value\":\"cll-101\"\n" +
329                 "                                }\n" +
330                 "                            ]\n" +
331                 "                        }\n" +
332                 "                    ]\n" +
333                 "                }\n" +
334                 "            }\n" +
335                 "        ]\n" +
336                 "    }\n" +
337                 "}");
338         Response<JSONObject> response = Response.success(jsonObject);
339         Mockito.when(intentApiService.getInstanceNetworkInfo(any())).thenReturn(mockCall);
340         Mockito.when(mockCall.execute()).thenReturn(response);
341
342         Call mockCall1 = PowerMockito.mock(Call.class);
343         JSONObject jsonObject1 = JSONObject.parseObject("{\n" +
344                 "    \"network-policy-id\":\"de00a0a0-be2e-4d19-974a-80a2bca6bdf9\",\n" +
345                 "    \"network-policy-fqdn\":\"cll-101\",\n" +
346                 "    \"resource-version\":\"1628714665619\",\n" +
347                 "    \"name\":\"TSCi policy\",\n" +
348                 "    \"type\":\"SLA\",\n" +
349                 "    \"latency\":2,\n" +
350                 "    \"max-bandwidth\":3000,\n" +
351                 "    \"relationship-list\":{\n" +
352                 "        \"relationship\":[\n" +
353                 "            {\n" +
354                 "                \"related-to\":\"allotted-resource\",\n" +
355                 "                \"relationship-label\":\"org.onap.relationships.inventory.Uses\",\n" +
356                 "                \"related-link\":\"/aai/v24/business/customers/customer/IBNCustomer/service-subscriptions/service-subscription/IBN/service-instances/service-instance/cll-101/allotted-resources/allotted-resource/cll-101-network-001\",\n" +
357                 "                \"relationship-data\":[\n" +
358                 "                    {\n" +
359                 "                        \"relationship-key\":\"customer.global-customer-id\",\n" +
360                 "                        \"relationship-value\":\"IBNCustomer\"\n" +
361                 "                    },\n" +
362                 "                    {\n" +
363                 "                        \"relationship-key\":\"service-subscription.service-type\",\n" +
364                 "                        \"relationship-value\":\"IBN\"\n" +
365                 "                    },\n" +
366                 "                    {\n" +
367                 "                        \"relationship-key\":\"service-instance.service-instance-id\",\n" +
368                 "                        \"relationship-value\":\"cll-101\"\n" +
369                 "                    },\n" +
370                 "                    {\n" +
371                 "                        \"relationship-key\":\"allotted-resource.id\",\n" +
372                 "                        \"relationship-value\":\"cll-101-network-001\"\n" +
373                 "                    }\n" +
374                 "                ],\n" +
375                 "                \"related-to-property\":[\n" +
376                 "                    {\n" +
377                 "                        \"property-key\":\"allotted-resource.description\"\n" +
378                 "                    },\n" +
379                 "                    {\n" +
380                 "                        \"property-key\":\"allotted-resource.allotted-resource-name\",\n" +
381                 "                        \"property-value\":\"network_cll-101-network-001\"\n" +
382                 "                    }\n" +
383                 "                ]\n" +
384                 "            }\n" +
385                 "        ]\n" +
386                 "    }\n" +
387                 "}");
388         Response<JSONObject> response1 = Response.success(jsonObject1);
389         Mockito.when(intentApiService.getInstanceNetworkPolicyInfo(any())).thenReturn(mockCall1);
390         Mockito.when(mockCall1.execute()).thenReturn(response1);
391
392         Call mockCall2 = PowerMockito.mock(Call.class);
393         JSONObject jsonObject2 = JSONObject.parseObject("{\n" +
394                 "    \"metadatum\":[\n" +
395                 "        {\n" +
396                 "            \"metaname\":\"ethernet-uni-id-1\",\n" +
397                 "            \"metaval\":\"1234\",\n" +
398                 "            \"resource-version\":\"1629409084707\"\n" +
399                 "        },\n" +
400                 "        {\n" +
401                 "            \"metaname\":\"ethernet-uni-id-2\",\n" +
402                 "            \"metaval\":\"5678\",\n" +
403                 "            \"resource-version\":\"1629409204904\"\n" +
404                 "        }\n" +
405                 "    ]\n" +
406                 "}");
407         Response<JSONObject> response2 = Response.success(jsonObject2);
408         Mockito.when(intentApiService.getInstanceBandwidth(any())).thenReturn(mockCall2);
409         Mockito.when(mockCall2.execute()).thenReturn(response2);
410
411         Transaction tx = Mockito.mock(Transaction.class);
412         Mockito.when(session.beginTransaction()).thenReturn(tx);
413         Serializable save = Mockito.mock(Serializable.class);
414         Mockito.when(session.save(any())).thenReturn(save);
415         Mockito.doNothing().when(tx).commit();
416
417         intentInstanceService.getIntentInstanceBandwidth();
418     }
419
420     @Test
421     public void deleteIntentInstance() throws IOException {
422         CCVPNInstance instance = new CCVPNInstance();
423         instance.setResourceInstanceId("1");
424
425         Query query = Mockito.mock(Query.class);
426         when(session.createQuery(anyString())).thenReturn(query);
427         when(query.setParameter(anyString(), anyString())).thenReturn(query);
428         when(query.uniqueResult()).thenReturn(instance);
429
430         Call mockCall = PowerMockito.mock(Call.class);
431         when(intentApiService.deleteIntentInstance(any())).thenReturn(mockCall);
432         when(mockCall.execute()).thenReturn(null);
433
434         Transaction tx = PowerMockito.mock(Transaction.class);
435         when(session.beginTransaction()).thenReturn(tx);
436         Serializable save = PowerMockito.mock(Serializable.class);
437         doNothing().when(session).delete(any());
438         doNothing().when(tx).commit();
439
440         IntentInstanceServiceImpl spy = spy(intentInstanceService);
441         doNothing().when(spy).deleteIntentInstanceToAAI(anyString());
442
443         spy.deleteIntentInstance("1");
444     }
445
446
447     @Test
448     public void invalidIntentInstanceTest() throws IOException {
449         CCVPNInstance instance = new CCVPNInstance();
450         instance.setResourceInstanceId("1");
451
452         Query query = Mockito.mock(Query.class);
453         when(session.createQuery(anyString())).thenReturn(query);
454         when(query.setParameter(anyString(), anyString())).thenReturn(query);
455         when(query.uniqueResult()).thenReturn(instance);
456
457         Call mockCall = PowerMockito.mock(Call.class);
458         when(intentApiService.deleteIntentInstance(any())).thenReturn(mockCall);
459         when(mockCall.execute()).thenReturn(null);
460
461         Transaction tx = PowerMockito.mock(Transaction.class);
462         when(session.beginTransaction()).thenReturn(tx);
463         Serializable save = PowerMockito.mock(Serializable.class);
464         doNothing().when(session).delete(any());
465         doNothing().when(tx).commit();
466
467         intentInstanceService.invalidIntentInstance("1");
468     }
469     @Test
470     public void queryInstancePerformanceDataTest() throws IOException {
471         CCVPNInstance instance = new CCVPNInstance();
472         instance.setResourceInstanceId("1");
473
474         InstancePerformance instancePerformance = new InstancePerformance();
475         instancePerformance.setBandwidth(2000);
476         instancePerformance.setMaxBandwidth(20000);
477         instancePerformance.setDate(new Date());
478         Object[] o = {null,instancePerformance};
479         List<Object[]> queryResult= new ArrayList<>();
480         queryResult.add(o);
481
482         Query query = Mockito.mock(Query.class);
483         when(session.createQuery(anyString())).thenReturn(query);
484         when(query.setParameter(anyString(), anyString())).thenReturn(query);
485         when(query.list()).thenReturn(queryResult);
486
487         intentInstanceService.queryInstancePerformanceData("1");
488
489
490
491
492
493         Call mockCall = PowerMockito.mock(Call.class);
494         when(intentApiService.deleteIntentInstance(any())).thenReturn(mockCall);
495         when(mockCall.execute()).thenReturn(null);
496
497         Transaction tx = PowerMockito.mock(Transaction.class);
498         when(session.beginTransaction()).thenReturn(tx);
499         Serializable save = PowerMockito.mock(Serializable.class);
500         doNothing().when(session).delete(any());
501         doNothing().when(tx).commit();
502
503         intentInstanceService.invalidIntentInstance("1");
504     }
505
506     @Test
507     public void activeIntentInstance() throws IOException {
508         CCVPNInstance instance = new CCVPNInstance();
509         instance.setInstanceId("1");
510         instance.setJobId("1");
511         instance.setStatus("1");
512
513         Query query = Mockito.mock(Query.class);
514         when(session.createQuery(anyString())).thenReturn(query);
515         when(query.setParameter(anyString(), anyString())).thenReturn(query);
516         when(query.uniqueResult()).thenReturn(instance);
517
518
519         Call mockCall = PowerMockito.mock(Call.class);
520         JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}");
521         Response<JSONObject> response = Response.success(body);
522         Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall);
523         Mockito.when(mockCall.execute()).thenReturn(response);
524
525         Transaction tx = Mockito.mock(Transaction.class);
526         Mockito.when(session.beginTransaction()).thenReturn(tx);
527         Serializable save = Mockito.mock(Serializable.class);
528         Mockito.when(session.save(any())).thenReturn(save);
529         Mockito.doNothing().when(tx).commit();
530
531         intentInstanceService.activeIntentInstance("1");
532
533     }
534
535     @Test
536     public void queryAccessNodeInfo() throws IOException {
537
538         Call mockCall = PowerMockito.mock(Call.class);
539         JSONObject body = JSONObject.parseObject("{\n" +
540                 "    \"network-route\": [\n" +
541                 "        {\n" +
542                 "            \"route-id\": \"tranportEp_src_ID_111_1\",\n" +
543                 "            \"type\": \"LEAF\",\n" +
544                 "            \"role\": \"3gppTransportEP\",\n" +
545                 "            \"function\": \"3gppTransportEP\",\n" +
546                 "            \"ip-address\": \"10.2.3.4\",\n" +
547                 "            \"prefix-length\": 24,\n" +
548                 "            \"next-hop\": \"networkId-providerId-10-clientId-0-topologyId-2-nodeId-10.1.1.1-ltpId-1000\",\n" +
549                 "            \"address-family\": \"ipv4\",\n" +
550                 "            \"resource-version\": \"1634198223345\"\n" +
551                 "        },\n" +
552                 "        {\n" +
553                 "            \"route-id\": \"tranportEp_src_ID_113_1\",\n" +
554                 "            \"type\": \"LEAF\",\n" +
555                 "            \"role\": \"3gppTransportEP\",\n" +
556                 "            \"function\": \"3gppTransportEP\",\n" +
557                 "            \"ip-address\": \"10.2.3.4\",\n" +
558                 "            \"prefix-length\": 24,\n" +
559                 "            \"next-hop\": \"networkId-providerId-10-clientId-0-topologyId-2-nodeId-10.1.1.3-ltpId-1000\",\n" +
560                 "            \"address-family\": \"ipv4\",\n" +
561                 "            \"resource-version\": \"1634198260496\"\n" +
562                 "        },\n" +
563                 "        {\n" +
564                 "            \"route-id\": \"tranportEp_src_ID_111_2\",\n" +
565                 "            \"type\": \"LEAF\",\n" +
566                 "            \"role\": \"3gppTransportEP\",\n" +
567                 "            \"function\": \"3gppTransportEP\",\n" +
568                 "            \"ip-address\": \"10.2.3.4\",\n" +
569                 "            \"prefix-length\": 24,\n" +
570                 "            \"next-hop\": \"networkId-providerId-10-clientId-0-topologyId-2-nodeId-10.1.1.1-ltpId-2000\",\n" +
571                 "            \"address-family\": \"ipv4\",\n" +
572                 "            \"resource-version\": \"1634198251534\"\n" +
573                 "        },\n" +
574                 "        {\n" +
575                 "            \"route-id\": \"tranportEp_dst_ID_212_1\",\n" +
576                 "            \"type\": \"ROOT\",\n" +
577                 "            \"role\": \"3gppTransportEP\",\n" +
578                 "            \"function\": \"3gppTransportEP\",\n" +
579                 "            \"ip-address\": \"10.2.3.4\",\n" +
580                 "            \"prefix-length\": 24,\n" +
581                 "            \"next-hop\": \"networkId-providerId-20-clientId-0-topologyId-2-nodeId-10.2.1.2-ltpId-512\",\n" +
582                 "            \"address-family\": \"ipv4\",\n" +
583                 "            \"resource-version\": \"1634198274852\"\n" +
584                 "        }\n" +
585                 "    ]\n" +
586                 "}");
587         Response<JSONObject> response = Response.success(body);
588         Mockito.when(intentApiService.queryNetworkRoute()).thenReturn(mockCall);
589         Mockito.when(mockCall.execute()).thenReturn(response);
590         Map<String, Object> result = (Map<String, Object>) intentInstanceService.queryAccessNodeInfo();
591         assertEquals(((List)result.get("accessNodeList")).size(), 3);
592     }
593
594     @Test
595     public void getInstanceStatusTest() {
596         List<CCVPNInstance> queryResult = new ArrayList<>();
597         CCVPNInstance instance = new CCVPNInstance();
598         instance.setInstanceId("id1");
599         instance.setStatus("1");
600         queryResult.add(instance);
601
602         Query query = Mockito.mock(Query.class);
603         when(session.createQuery(anyString())).thenReturn(query);
604         when(query.setParameter(anyString(), any())).thenReturn(query);
605         when(query.list()).thenReturn(queryResult);
606
607
608         JSONObject instanceStatus = intentInstanceService.getInstanceStatus(new JSONArray());
609         assertEquals(instanceStatus.getJSONArray("IntentInstances").getJSONObject(0).getString("id"), "id1");
610     }
611     @Test
612     public void formatBandwidthTest() {
613
614         String bandwidth = intentInstanceService.formatBandwidth("2Gbps");
615         assertEquals(bandwidth, "2000");
616     }
617     @Test
618     public void formatCloudPointTest() {
619
620         String bandwidth = intentInstanceService.formatCloudPoint("Cloud one");
621         assertEquals(bandwidth, "tranportEp_dst_ID_212_1");
622     }
623     @Test
624     public void formatAccessPointOneTest() {
625         String bandwidth = intentInstanceService.formatAccessPoint("Access one");
626         assertEquals(bandwidth, "tranportEp_src_ID_111_1");
627     }
628     @Test
629     public void formatAccessPointTwoTest() {
630         String bandwidth = intentInstanceService.formatAccessPoint("Access two");
631         assertEquals(bandwidth, "tranportEp_src_ID_111_2");
632     }
633     @Test
634     public void formatAccessPointThreeTest() {
635         String bandwidth = intentInstanceService.formatAccessPoint("Access three");
636         assertEquals(bandwidth, "tranportEp_src_ID_113_1");
637     }
638
639     @Test
640     public void addCustomerTest() throws IOException {
641
642         Call mockCall = PowerMockito.mock(Call.class);
643         Response<Object> response = Response.error(404, new ResponseBody() {
644             @Nullable
645             @Override
646             public MediaType contentType() {
647                 return null;
648             }
649
650             @Override
651             public long contentLength() {
652                 return 0;
653             }
654
655             @Override
656             public BufferedSource source() {
657                 return null;
658             }
659         });
660         when(intentApiService.queryCustomer(anyString())).thenReturn(mockCall);
661         when(mockCall.execute()).thenReturn(response);
662
663         Properties properties = new Properties();
664         properties.put("ccvpn.globalCustomerId", "IBNCustomer");
665         properties.put("ccvpn.subscriberName", "IBNCustomer");
666         properties.put("ccvpn.subscriberType", "INFRA");
667         properties.put("ccvpn.serviceType", "IBN");
668         IntentInstanceServiceImpl spy = spy(intentInstanceService);
669         doReturn(properties).when(spy).getProperties();
670
671         Call mockCall2 = PowerMockito.mock(Call.class);
672         when(intentApiService.addCustomer(anyString(),any())).thenReturn(mockCall2);
673
674         spy.addCustomer();
675         Mockito.verify(intentApiService,Mockito.times(1)).addCustomer(anyString(),any());
676     }
677
678
679     @Test
680     public void addSubscriptionTest() throws IOException {
681
682         Call mockCall = PowerMockito.mock(Call.class);
683         Response<Object> response = Response.error(404, new ResponseBody() {
684             @Nullable
685             @Override
686             public MediaType contentType() {
687                 return null;
688             }
689
690             @Override
691             public long contentLength() {
692                 return 0;
693             }
694
695             @Override
696             public BufferedSource source() {
697                 return null;
698             }
699         });
700         when(intentApiService.querySubscription(anyString(),anyString())).thenReturn(mockCall);
701         when(mockCall.execute()).thenReturn(response);
702
703         Properties properties = new Properties();
704         properties.put("ccvpn.globalCustomerId", "IBNCustomer");
705         properties.put("ccvpn.subscriberName", "IBNCustomer");
706         properties.put("ccvpn.subscriberType", "INFRA");
707         properties.put("ccvpn.serviceType", "IBN");
708         IntentInstanceServiceImpl spy = spy(intentInstanceService);
709         doReturn(properties).when(spy).getProperties();
710
711         Call mockCall2 = PowerMockito.mock(Call.class);
712         when(intentApiService.addSubscription(anyString(),anyString(),any())).thenReturn(mockCall2);
713
714         spy.addSubscription();
715         Mockito.verify(intentApiService,Mockito.times(1)).addSubscription(anyString(),anyString(),any());
716     }
717
718     @Test
719     public void saveIntentInstanceToAAITest() throws IOException {
720         IntentInstanceServiceImpl spy = spy(intentInstanceService);
721         doNothing().when(spy).addCustomer();
722         doNothing().when(spy).addSubscription();
723
724         Properties properties = new Properties();
725         properties.put("ccvpn.globalCustomerId", "IBNCustomer");
726         properties.put("ccvpn.subscriberName", "IBNCustomer");
727         properties.put("ccvpn.subscriberType", "INFRA");
728         properties.put("ccvpn.serviceType", "IBN");
729         doReturn(properties).when(spy).getProperties();
730
731         JSONObject body = new JSONObject();
732         body.put("resource-version",123);
733         Call mockCall = PowerMockito.mock(Call.class);
734         Response<JSONObject> response = Response.success(body);
735         when(intentApiService.queryServiceInstance(anyString(),anyString(),anyString())).thenReturn(mockCall);
736         when(mockCall.execute()).thenReturn(response);
737
738         CCVPNInstance instance = new CCVPNInstance();
739         instance.setName("name");
740         instance.setInstanceId("id");
741
742         Call mockCall2 = PowerMockito.mock(Call.class);
743         Response<JSONObject> response2 = Response.success(body);
744         when(intentApiService.saveServiceInstance(anyString(),anyString(),anyString(),any())).thenReturn(mockCall2);
745         when(mockCall2.execute()).thenReturn(response2);
746
747         spy.saveIntentInstanceToAAI("CCVPN-id",instance);
748         Mockito.verify(intentApiService, Mockito.times(1)).saveServiceInstance(anyString(),anyString(),anyString(),any());
749
750     }
751     @Test
752     public void deleteIntentInstanceToAAITest() throws IOException {
753         IntentInstanceServiceImpl spy = spy(intentInstanceService);
754         doNothing().when(spy).addCustomer();
755         doNothing().when(spy).addSubscription();
756
757         Properties properties = new Properties();
758         properties.put("ccvpn.globalCustomerId", "IBNCustomer");
759         properties.put("ccvpn.subscriberName", "IBNCustomer");
760         properties.put("ccvpn.subscriberType", "INFRA");
761         properties.put("ccvpn.serviceType", "IBN");
762         doReturn(properties).when(spy).getProperties();
763
764         JSONObject body = new JSONObject();
765         body.put("resource-version",123);
766         Call mockCall = PowerMockito.mock(Call.class);
767         Response<JSONObject> response = Response.success(body);
768         when(intentApiService.queryServiceInstance(anyString(),anyString(),anyString())).thenReturn(mockCall);
769         when(mockCall.execute()).thenReturn(response);
770
771         Call mockCall2 = PowerMockito.mock(Call.class);
772         Response<JSONObject> response2 = Response.success(body);
773         when(intentApiService.deleteServiceInstance(anyString(),anyString(),anyString(),anyString())).thenReturn(mockCall2);
774         when(mockCall2.execute()).thenReturn(response2);
775
776         spy.deleteIntentInstanceToAAI("CCVPN-id");
777         Mockito.verify(intentApiService, Mockito.times(1)).deleteServiceInstance(anyString(),anyString(),anyString(),any());
778
779     }
780 }