97526d701bd5562dd195f75deaf63f2e1f05fb88
[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.mockito.junit.MockitoJUnitRunner;
38 import org.onap.usecaseui.server.bean.csmf.ServiceCreateResult;
39 import org.onap.usecaseui.server.bean.csmf.SlicingOrder;
40 import org.onap.usecaseui.server.bean.csmf.SlicingOrderDetail;
41 import org.onap.usecaseui.server.bean.intent.CCVPNInstance;
42 import org.onap.usecaseui.server.bean.intent.InstancePerformance;
43 import org.onap.usecaseui.server.bean.intent.IntentInstance;
44 import org.onap.usecaseui.server.bean.intent.IntentModel;
45 import org.onap.usecaseui.server.bean.nsmf.common.ServiceResult;
46 import org.onap.usecaseui.server.constant.IntentConstant;
47 import org.onap.usecaseui.server.service.csmf.SlicingService;
48 import org.onap.usecaseui.server.service.intent.IntentApiService;
49 import org.onap.usecaseui.server.service.lcm.domain.so.SOService;
50 import org.onap.usecaseui.server.service.lcm.domain.so.bean.OperationProgress;
51 import org.onap.usecaseui.server.service.lcm.domain.so.bean.OperationProgressInformation;
52 import org.onap.usecaseui.server.service.nsmf.ResourceMgtService;
53 import org.powermock.api.mockito.PowerMockito;
54 import org.powermock.api.support.membermodification.MemberModifier;
55 import org.powermock.modules.junit4.PowerMockRunner;
56
57 import static org.junit.Assert.*;
58 import static org.mockito.ArgumentMatchers.*;
59 import static org.powermock.api.mockito.PowerMockito.*;
60
61 import retrofit2.Call;
62 import retrofit2.Response;
63
64 import jakarta.annotation.Nullable;
65 import jakarta.annotation.Resource;
66
67 @RunWith(MockitoJUnitRunner.class)
68 public class IntentInstanceServiceImplTest {
69
70     public IntentInstanceServiceImplTest() {
71     }
72
73     @InjectMocks
74     private IntentInstanceServiceImpl intentInstanceService;
75
76     @Mock
77     private IntentApiService intentApiService;
78
79     @Mock
80     private SOService soService;
81
82     @Mock
83     @Resource(name = "ResourceMgtService")
84     private ResourceMgtService resourceMgtService;
85
86     @Mock
87     @Resource(name = "SlicingService")
88     private SlicingService slicingService;
89
90     @Mock
91     private SessionFactory sessionFactory;
92
93     @Mock
94     private Session session;
95
96     @Before
97     public void before() throws Exception {
98         MemberModifier.field(IntentInstanceServiceImpl.class, "sessionFactory").set(intentInstanceService , sessionFactory);
99         MemberModifier.field(IntentInstanceServiceImpl.class, "resourceMgtService").set(intentInstanceService , resourceMgtService);
100         MemberModifier.field(IntentInstanceServiceImpl.class, "slicingService").set(intentInstanceService , slicingService);
101         when(sessionFactory.openSession()).thenReturn(session);
102     }
103
104     @Test
105     public void queryIntentInstanceTest() {
106         CCVPNInstance instance = new CCVPNInstance();
107         instance.setInstanceId("1");
108         instance.setJobId("1");
109         instance.setStatus("1");
110
111         Query query = Mockito.mock(Query.class);
112         when(session.createQuery(anyString())).thenReturn(query);
113         List<IntentModel> list = new ArrayList<>();
114         when(query.list()).thenReturn(list);
115         when(query.uniqueResult()).thenReturn(10L);
116         assertTrue(intentInstanceService.queryIntentInstance(instance,1,2).getList().isEmpty());
117     }
118
119     @Test
120     public void queryIntentInstanceGetCountErrorTest() {
121         CCVPNInstance instance = new CCVPNInstance();
122         instance.setInstanceId("1");
123         instance.setJobId("1");
124         instance.setStatus("1");
125
126         Query query = Mockito.mock(Query.class);
127         when(session.createQuery(anyString())).thenReturn(query);
128         List<IntentModel> list = new ArrayList<>();
129         when(query.list()).thenReturn(list);
130         when(query.uniqueResult()).thenReturn(10);
131         assertTrue(intentInstanceService.queryIntentInstance(instance,1,2).getList().isEmpty());
132     }
133
134     @Test
135     public void queryIntentInstanceThrowErrorTest() {
136         CCVPNInstance instance = new CCVPNInstance();
137         instance.setInstanceId("1");
138         instance.setJobId("1");
139         instance.setStatus("1");
140
141         when(session.createQuery(anyString())).thenThrow(new RuntimeException());
142
143         assertEquals(intentInstanceService.queryIntentInstance(instance,1,2), null);
144     }
145     @Test
146     public void createCCVPNInstanceTest() throws IOException {
147         CCVPNInstance instance = new CCVPNInstance();
148         instance.setInstanceId("1");
149         instance.setJobId("1");
150         instance.setStatus("1");
151
152         Call mockCall = PowerMockito.mock(Call.class);
153         JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}");
154         Response<JSONObject> response = Response.success(body);
155         Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall);
156         Mockito.when(mockCall.execute()).thenReturn(response);
157
158         IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
159         doNothing().when(spy).saveIntentInstanceToAAI(isNull(),any(CCVPNInstance.class));
160
161         Transaction tx = Mockito.mock(Transaction.class);
162         Mockito.when(session.beginTransaction()).thenReturn(tx);
163         Serializable save = Mockito.mock(Serializable.class);
164         Mockito.when(session.save(any())).thenReturn(save);
165         Mockito.doNothing().when(tx).commit();
166
167         assertEquals(spy.createCCVPNInstance(instance), 1);
168     }
169
170     @Test
171     public void createCCVPNInstanceThrowErrorTest() throws IOException {
172         CCVPNInstance instance = new CCVPNInstance();
173         instance.setInstanceId("1");
174         instance.setJobId("1");
175         instance.setStatus("1");
176
177         Call mockCall = PowerMockito.mock(Call.class);
178         JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}");
179         Response<JSONObject> response = Response.success(body);
180         Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall);
181         Mockito.when(mockCall.execute()).thenReturn(response);
182
183         IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
184         doThrow(new RuntimeException()).when(spy).saveIntentInstanceToAAI(isNull(),any(CCVPNInstance.class));
185
186         Transaction tx = Mockito.mock(Transaction.class);
187         Mockito.when(session.beginTransaction()).thenReturn(tx);
188         Serializable save = Mockito.mock(Serializable.class);
189         Mockito.when(session.save(any())).thenReturn(save);
190         Mockito.doNothing().when(tx).commit();
191
192         assertEquals(spy.createCCVPNInstance(instance), 0);
193     }
194
195     @Test
196     public void createCCVPNInstanceInstanceIsNullTest() throws IOException {
197         assertEquals(intentInstanceService.createCCVPNInstance(null), 0);
198     }
199     @Test
200     public void createCCVPNInstanceInstanceJobIdIsNullTest() throws IOException {
201         CCVPNInstance instance = new CCVPNInstance();
202         instance.setInstanceId("1");
203         instance.setStatus("1");
204         assertEquals(intentInstanceService.createCCVPNInstance(instance), 0);
205     }
206
207     @Test
208     public void getIntentInstanceProgressTest() throws IOException {
209
210         Query query1 = Mockito.mock(Query.class);
211         when(session.createQuery("from CCVPNInstance where deleteState = 0 and status = '0'")).thenReturn(query1);
212         List<CCVPNInstance> q = new ArrayList<>();
213         CCVPNInstance instance = new CCVPNInstance();
214         instance.setInstanceId("1");
215         instance.setResourceInstanceId("1");
216         instance.setJobId("1");
217         q.add(instance);
218         when(query1.list()).thenReturn(q);
219
220         OperationProgressInformation operationProgressInformation = new OperationProgressInformation();
221         OperationProgress operationProgress = new OperationProgress();
222         operationProgress.setProgress(100);
223         operationProgressInformation.setOperationStatus(operationProgress);
224
225         JSONObject jsonObject = new JSONObject();
226         JSONObject operation = new JSONObject();
227         operation.put("progress", 100);
228         jsonObject.put("operation", operation);
229         Call mockCall = PowerMockito.mock(Call.class);
230         Response<JSONObject> response = Response.success(jsonObject);
231         Mockito.when(intentApiService.queryOperationProgress(anyString(),anyString())).thenReturn(mockCall);
232         Mockito.when(mockCall.execute()).thenReturn(response);
233
234         IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
235         doNothing().when(spy).saveIntentInstanceToAAI(anyString(),any(CCVPNInstance.class));
236
237         Transaction tx = Mockito.mock(Transaction.class);
238         Mockito.when(session.beginTransaction()).thenReturn(tx);
239         Serializable save = Mockito.mock(Serializable.class);
240         Mockito.when(session.save(any())).thenReturn(save);
241         Mockito.doNothing().when(tx).commit();
242
243         spy.getIntentInstanceProgress();
244         assertEquals(operation.getString("progress"),"100");
245     }
246     @Test
247     public void getIntentInstanceCreateStatusTest() throws IOException {
248
249         Query query1 = Mockito.mock(Query.class);
250         when(session.createQuery("from CCVPNInstance where deleteState = 0 and status = '0'")).thenReturn(query1);
251         List<CCVPNInstance> q = new ArrayList<>();
252         CCVPNInstance instance = new CCVPNInstance();
253         instance.setInstanceId("1");
254         instance.setResourceInstanceId("1");
255         instance.setJobId("1");
256         q.add(instance);
257         when(query1.list()).thenReturn(q);
258
259         OperationProgressInformation operationProgressInformation = new OperationProgressInformation();
260         OperationProgress operationProgress = new OperationProgress();
261         operationProgress.setProgress(100);
262         operationProgressInformation.setOperationStatus(operationProgress);
263
264         JSONObject jsonObject = new JSONObject();
265         jsonObject.put("orchestration-status", "created");
266         Call mockCall = PowerMockito.mock(Call.class);
267         Response<JSONObject> response = Response.success(jsonObject);
268         Mockito.when(intentApiService.getInstanceInfo(anyString())).thenReturn(mockCall);
269         Mockito.when(mockCall.execute()).thenReturn(response);
270
271         IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
272         doNothing().when(spy).saveIntentInstanceToAAI(anyString(),any(CCVPNInstance.class));
273
274         Transaction tx = Mockito.mock(Transaction.class);
275         Mockito.when(session.beginTransaction()).thenReturn(tx);
276         Serializable save = Mockito.mock(Serializable.class);
277         Mockito.when(session.save(any())).thenReturn(save);
278         Mockito.doNothing().when(tx).commit();
279
280         spy.getIntentInstanceCreateStatus();
281         assertEquals(jsonObject.getString("orchestration-status"),"created");
282     }
283
284     @Test
285     public void getFinishedInstanceInfo() {
286         Query query = Mockito.mock(Query.class);
287         when(session.createQuery(anyString())).thenReturn(query);
288         when(query.list()).thenReturn(new ArrayList());
289         assertTrue(intentInstanceService.getFinishedInstanceInfo().isEmpty());
290     }
291
292     @Test
293     public void getIntentInstanceBandwidth() throws IOException {
294         Query query1 = Mockito.mock(Query.class);
295         when(session.createQuery("from CCVPNInstance where deleteState = 0 and status = '1'")).thenReturn(query1);
296         List<CCVPNInstance> q = new ArrayList<>();
297         CCVPNInstance instance = new CCVPNInstance();
298         instance.setInstanceId("1");
299         instance.setResourceInstanceId("1");
300         q.add(instance);
301         when(query1.list()).thenReturn(q);
302
303         Call mockCall = PowerMockito.mock(Call.class);
304         JSONObject jsonObject = JSONObject.parseObject("{\n" +
305                 "    \"service-instance-id\":\"cll-101\",\n" +
306                 "    \"service-instance-name\":\"cloud-leased-line-101\",\n" +
307                 "    \"service-type\":\"CLL\",\n" +
308                 "    \"service-role\":\"cll\",\n" +
309                 "    \"environment-context\":\"cll\",\n" +
310                 "    \"model-invariant-id\":\"6790ab0e-034f-11eb-adc1-0242ac120002\",\n" +
311                 "    \"model-version-id\":\"6790ab0e-034f-11eb-adc1-0242ac120002\",\n" +
312                 "    \"resource-version\":\"1628714665927\",\n" +
313                 "    \"orchestration-status\":\"created\",\n" +
314                 "    \"allotted-resources\":{\n" +
315                 "        \"allotted-resource\":[\n" +
316                 "            {\n" +
317                 "                \"id\":\"cll-101-network-001\",\n" +
318                 "                \"resource-version\":\"1628714665798\",\n" +
319                 "                \"type\":\"TsciNetwork\",\n" +
320                 "                \"allotted-resource-name\":\"network_cll-101-network-001\",\n" +
321                 "                \"relationship-list\":{\n" +
322                 "                    \"relationship\":[\n" +
323                 "                        {\n" +
324                 "                            \"related-to\":\"logical-link\",\n" +
325                 "                            \"relationship-label\":\"org.onap.relationships.inventory.ComposedOf\",\n" +
326                 "                            \"related-link\":\"/aai/v24/network/logical-links/logical-link/tranportEp_UNI_ID_311_1\",\n" +
327                 "                            \"relationship-data\":[\n" +
328                 "                                {\n" +
329                 "                                    \"relationship-key\":\"logical-link.link-name\",\n" +
330                 "                                    \"relationship-value\":\"tranportEp_UNI_ID_311_1\"\n" +
331                 "                                }\n" +
332                 "                            ]\n" +
333                 "                        },\n" +
334                 "                        {\n" +
335                 "                            \"related-to\":\"network-policy\",\n" +
336                 "                            \"relationship-label\":\"org.onap.relationships.inventory.Uses\",\n" +
337                 "                            \"related-link\":\"/aai/v24/network/network-policies/network-policy/de00a0a0-be2e-4d19-974a-80a2bca6bdf9\",\n" +
338                 "                            \"relationship-data\":[\n" +
339                 "                                {\n" +
340                 "                                    \"relationship-key\":\"network-policy.network-policy-id\",\n" +
341                 "                                    \"relationship-value\":\"de00a0a0-be2e-4d19-974a-80a2bca6bdf9\"\n" +
342                 "                                }\n" +
343                 "                            ],\n" +
344                 "                            \"related-to-property\":[\n" +
345                 "                                {\n" +
346                 "                                    \"property-key\":\"network-policy.network-policy-fqdn\",\n" +
347                 "                                    \"property-value\":\"cll-101\"\n" +
348                 "                                }\n" +
349                 "                            ]\n" +
350                 "                        }\n" +
351                 "                    ]\n" +
352                 "                }\n" +
353                 "            }\n" +
354                 "        ]\n" +
355                 "    }\n" +
356                 "}");
357         Response<JSONObject> response = Response.success(jsonObject);
358         Mockito.when(intentApiService.getInstanceNetworkInfo(any())).thenReturn(mockCall);
359         Mockito.when(mockCall.execute()).thenReturn(response);
360
361         Call mockCall1 = PowerMockito.mock(Call.class);
362         JSONObject jsonObject1 = JSONObject.parseObject("{\n" +
363                 "    \"network-policy-id\":\"de00a0a0-be2e-4d19-974a-80a2bca6bdf9\",\n" +
364                 "    \"network-policy-fqdn\":\"cll-101\",\n" +
365                 "    \"resource-version\":\"1628714665619\",\n" +
366                 "    \"name\":\"TSCi policy\",\n" +
367                 "    \"type\":\"SLA\",\n" +
368                 "    \"latency\":2,\n" +
369                 "    \"max-bandwidth\":3000,\n" +
370                 "    \"relationship-list\":{\n" +
371                 "        \"relationship\":[\n" +
372                 "            {\n" +
373                 "                \"related-to\":\"allotted-resource\",\n" +
374                 "                \"relationship-label\":\"org.onap.relationships.inventory.Uses\",\n" +
375                 "                \"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" +
376                 "                \"relationship-data\":[\n" +
377                 "                    {\n" +
378                 "                        \"relationship-key\":\"customer.global-customer-id\",\n" +
379                 "                        \"relationship-value\":\"IBNCustomer\"\n" +
380                 "                    },\n" +
381                 "                    {\n" +
382                 "                        \"relationship-key\":\"service-subscription.service-type\",\n" +
383                 "                        \"relationship-value\":\"IBN\"\n" +
384                 "                    },\n" +
385                 "                    {\n" +
386                 "                        \"relationship-key\":\"service-instance.service-instance-id\",\n" +
387                 "                        \"relationship-value\":\"cll-101\"\n" +
388                 "                    },\n" +
389                 "                    {\n" +
390                 "                        \"relationship-key\":\"allotted-resource.id\",\n" +
391                 "                        \"relationship-value\":\"cll-101-network-001\"\n" +
392                 "                    }\n" +
393                 "                ],\n" +
394                 "                \"related-to-property\":[\n" +
395                 "                    {\n" +
396                 "                        \"property-key\":\"allotted-resource.description\"\n" +
397                 "                    },\n" +
398                 "                    {\n" +
399                 "                        \"property-key\":\"allotted-resource.allotted-resource-name\",\n" +
400                 "                        \"property-value\":\"network_cll-101-network-001\"\n" +
401                 "                    }\n" +
402                 "                ]\n" +
403                 "            }\n" +
404                 "        ]\n" +
405                 "    }\n" +
406                 "}");
407         Response<JSONObject> response1 = Response.success(jsonObject1);
408         Mockito.when(intentApiService.getInstanceNetworkPolicyInfo(any())).thenReturn(mockCall1);
409         Mockito.when(mockCall1.execute()).thenReturn(response1);
410
411         Call mockCall2 = PowerMockito.mock(Call.class);
412         JSONObject jsonObject2 = JSONObject.parseObject("{\n" +
413                 "    \"metadatum\":[\n" +
414                 "        {\n" +
415                 "            \"metaname\":\"ethernet-uni-id-1\",\n" +
416                 "            \"metaval\":\"1234\",\n" +
417                 "            \"resource-version\":\"1629409084707\"\n" +
418                 "        },\n" +
419                 "        {\n" +
420                 "            \"metaname\":\"ethernet-uni-id-2\",\n" +
421                 "            \"metaval\":\"5678\",\n" +
422                 "            \"resource-version\":\"1629409204904\"\n" +
423                 "        }\n" +
424                 "    ]\n" +
425                 "}");
426         Response<JSONObject> response2 = Response.success(jsonObject2);
427         Mockito.when(intentApiService.getInstanceBandwidth(any())).thenReturn(mockCall2);
428         Mockito.when(mockCall2.execute()).thenReturn(response2);
429
430         Transaction tx = Mockito.mock(Transaction.class);
431         Mockito.when(session.beginTransaction()).thenReturn(tx);
432         Serializable save = Mockito.mock(Serializable.class);
433         Mockito.when(session.save(any())).thenReturn(save);
434         Mockito.doNothing().when(tx).commit();
435
436         intentInstanceService.getIntentInstanceBandwidth();
437     }
438
439     @Test
440     public void deleteIntentInstance() throws IOException {
441         CCVPNInstance instance = new CCVPNInstance();
442         instance.setResourceInstanceId("1");
443
444         Query query = Mockito.mock(Query.class);
445         when(session.createQuery(anyString())).thenReturn(query);
446         when(query.setParameter(anyString(), anyString())).thenReturn(query);
447         when(query.uniqueResult()).thenReturn(instance);
448
449         Call mockCall = PowerMockito.mock(Call.class);
450         when(intentApiService.deleteIntentInstance(any())).thenReturn(mockCall);
451         when(mockCall.execute()).thenReturn(null);
452
453         Transaction tx = PowerMockito.mock(Transaction.class);
454         when(session.beginTransaction()).thenReturn(tx);
455         Serializable save = PowerMockito.mock(Serializable.class);
456         doNothing().when(session).delete(any());
457         doNothing().when(tx).commit();
458
459         IntentInstanceServiceImpl spy = spy(intentInstanceService);
460         doNothing().when(spy).deleteIntentInstanceToAAI(anyString());
461
462         spy.deleteIntentInstance("1");
463     }
464
465
466     @Test
467     public void invalidIntentInstanceTest() throws IOException {
468         CCVPNInstance instance = new CCVPNInstance();
469         instance.setResourceInstanceId("1");
470
471         Query query = Mockito.mock(Query.class);
472         when(session.createQuery(anyString())).thenReturn(query);
473         when(query.setParameter(anyString(), anyString())).thenReturn(query);
474         when(query.uniqueResult()).thenReturn(instance);
475
476         Call mockCall = PowerMockito.mock(Call.class);
477         when(intentApiService.deleteIntentInstance(any())).thenReturn(mockCall);
478         when(mockCall.execute()).thenReturn(null);
479
480         Transaction tx = PowerMockito.mock(Transaction.class);
481         when(session.beginTransaction()).thenReturn(tx);
482         Serializable save = PowerMockito.mock(Serializable.class);
483         doNothing().when(session).delete(any());
484         doNothing().when(tx).commit();
485
486         intentInstanceService.invalidIntentInstance("1");
487     }
488     @Test
489     public void queryInstancePerformanceDataTest() throws IOException {
490         CCVPNInstance instance = new CCVPNInstance();
491         instance.setResourceInstanceId("1");
492
493         InstancePerformance instancePerformance = new InstancePerformance();
494         instancePerformance.setBandwidth(2000);
495         instancePerformance.setMaxBandwidth(20000);
496         instancePerformance.setDate(new Date());
497         Object[] o = {null,instancePerformance};
498         List<Object[]> queryResult= new ArrayList<>();
499         queryResult.add(o);
500
501         Query query = Mockito.mock(Query.class);
502         when(session.createQuery(anyString())).thenReturn(query);
503         when(query.setParameter(anyString(), anyString())).thenReturn(query);
504         when(query.list()).thenReturn(queryResult);
505
506         intentInstanceService.queryInstancePerformanceData("1");
507
508
509
510
511
512         Call mockCall = PowerMockito.mock(Call.class);
513         when(intentApiService.deleteIntentInstance(any())).thenReturn(mockCall);
514         when(mockCall.execute()).thenReturn(null);
515
516         Transaction tx = PowerMockito.mock(Transaction.class);
517         when(session.beginTransaction()).thenReturn(tx);
518         Serializable save = PowerMockito.mock(Serializable.class);
519         doNothing().when(session).delete(any());
520         doNothing().when(tx).commit();
521
522         intentInstanceService.invalidIntentInstance("1");
523     }
524
525     @Test
526     public void activeIntentInstance() throws IOException {
527         CCVPNInstance instance = new CCVPNInstance();
528         instance.setInstanceId("1");
529         instance.setJobId("1");
530         instance.setStatus("1");
531
532         Query query = Mockito.mock(Query.class);
533         when(session.createQuery(anyString())).thenReturn(query);
534         when(query.setParameter(anyString(), anyString())).thenReturn(query);
535         when(query.uniqueResult()).thenReturn(instance);
536
537
538         Call mockCall = PowerMockito.mock(Call.class);
539         JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}");
540         Response<JSONObject> response = Response.success(body);
541         Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall);
542         Mockito.when(mockCall.execute()).thenReturn(response);
543
544         Transaction tx = Mockito.mock(Transaction.class);
545         Mockito.when(session.beginTransaction()).thenReturn(tx);
546         Serializable save = Mockito.mock(Serializable.class);
547         Mockito.when(session.save(any())).thenReturn(save);
548         Mockito.doNothing().when(tx).commit();
549
550         intentInstanceService.activeIntentInstance("1");
551
552     }
553
554     @Test
555     public void queryAccessNodeInfo() throws IOException {
556
557         Call mockCall = PowerMockito.mock(Call.class);
558         JSONObject body = JSONObject.parseObject("{\n" +
559                 "    \"network-route\": [\n" +
560                 "        {\n" +
561                 "            \"route-id\": \"tranportEp_src_ID_111_1\",\n" +
562                 "            \"type\": \"LEAF\",\n" +
563                 "            \"role\": \"3gppTransportEP\",\n" +
564                 "            \"function\": \"3gppTransportEP\",\n" +
565                 "            \"ip-address\": \"10.2.3.4\",\n" +
566                 "            \"prefix-length\": 24,\n" +
567                 "            \"next-hop\": \"networkId-providerId-10-clientId-0-topologyId-2-nodeId-10.1.1.1-ltpId-1000\",\n" +
568                 "            \"address-family\": \"ipv4\",\n" +
569                 "            \"resource-version\": \"1634198223345\"\n" +
570                 "        },\n" +
571                 "        {\n" +
572                 "            \"route-id\": \"tranportEp_src_ID_113_1\",\n" +
573                 "            \"type\": \"LEAF\",\n" +
574                 "            \"role\": \"3gppTransportEP\",\n" +
575                 "            \"function\": \"3gppTransportEP\",\n" +
576                 "            \"ip-address\": \"10.2.3.4\",\n" +
577                 "            \"prefix-length\": 24,\n" +
578                 "            \"next-hop\": \"networkId-providerId-10-clientId-0-topologyId-2-nodeId-10.1.1.3-ltpId-1000\",\n" +
579                 "            \"address-family\": \"ipv4\",\n" +
580                 "            \"resource-version\": \"1634198260496\"\n" +
581                 "        },\n" +
582                 "        {\n" +
583                 "            \"route-id\": \"tranportEp_src_ID_111_2\",\n" +
584                 "            \"type\": \"LEAF\",\n" +
585                 "            \"role\": \"3gppTransportEP\",\n" +
586                 "            \"function\": \"3gppTransportEP\",\n" +
587                 "            \"ip-address\": \"10.2.3.4\",\n" +
588                 "            \"prefix-length\": 24,\n" +
589                 "            \"next-hop\": \"networkId-providerId-10-clientId-0-topologyId-2-nodeId-10.1.1.1-ltpId-2000\",\n" +
590                 "            \"address-family\": \"ipv4\",\n" +
591                 "            \"resource-version\": \"1634198251534\"\n" +
592                 "        },\n" +
593                 "        {\n" +
594                 "            \"route-id\": \"tranportEp_dst_ID_212_1\",\n" +
595                 "            \"type\": \"ROOT\",\n" +
596                 "            \"role\": \"3gppTransportEP\",\n" +
597                 "            \"function\": \"3gppTransportEP\",\n" +
598                 "            \"ip-address\": \"10.2.3.4\",\n" +
599                 "            \"prefix-length\": 24,\n" +
600                 "            \"next-hop\": \"networkId-providerId-20-clientId-0-topologyId-2-nodeId-10.2.1.2-ltpId-512\",\n" +
601                 "            \"address-family\": \"ipv4\",\n" +
602                 "            \"resource-version\": \"1634198274852\"\n" +
603                 "        }\n" +
604                 "    ]\n" +
605                 "}");
606         Response<JSONObject> response = Response.success(body);
607         Mockito.when(intentApiService.queryNetworkRoute()).thenReturn(mockCall);
608         Mockito.when(mockCall.execute()).thenReturn(response);
609         Map<String, Object> result = (Map<String, Object>) intentInstanceService.queryAccessNodeInfo();
610         assertEquals(((List)result.get("accessNodeList")).size(), 3);
611     }
612
613     @Test
614     public void getInstanceStatusTest() {
615         List<CCVPNInstance> queryResult = new ArrayList<>();
616         CCVPNInstance instance = new CCVPNInstance();
617         instance.setInstanceId("id1");
618         instance.setStatus("1");
619         queryResult.add(instance);
620
621         Query query = Mockito.mock(Query.class);
622         when(session.createQuery(anyString())).thenReturn(query);
623         when(query.setParameter(anyString(), any())).thenReturn(query);
624         when(query.list()).thenReturn(queryResult);
625
626
627         JSONObject instanceStatus = intentInstanceService.getInstanceStatus(new JSONArray());
628         assertEquals(instanceStatus.getJSONArray("IntentInstances").getJSONObject(0).getString("id"), "id1");
629     }
630     @Test
631     public void formatBandwidthTest() {
632
633         String bandwidth = intentInstanceService.formatBandwidth("2Gbps");
634         assertEquals(bandwidth, "2000");
635     }
636     @Test
637     public void formatCloudPointTest() {
638
639         String bandwidth = intentInstanceService.formatCloudPoint("Cloud one");
640         assertEquals(bandwidth, "tranportEp_dst_ID_212_1");
641     }
642     @Test
643     public void formatAccessPointOneTest() {
644         String bandwidth = intentInstanceService.formatAccessPoint("Access one");
645         assertEquals(bandwidth, "tranportEp_src_ID_111_1");
646     }
647     @Test
648     public void formatAccessPointTwoTest() {
649         String bandwidth = intentInstanceService.formatAccessPoint("Access two");
650         assertEquals(bandwidth, "tranportEp_src_ID_111_2");
651     }
652     @Test
653     public void formatAccessPointThreeTest() {
654         String bandwidth = intentInstanceService.formatAccessPoint("Access three");
655         assertEquals(bandwidth, "tranportEp_src_ID_113_1");
656     }
657
658     @Test
659     public void addCustomerTest() throws IOException {
660
661         Call mockCall = PowerMockito.mock(Call.class);
662         Response<Object> response = Response.error(404, new ResponseBody() {
663             @Nullable
664             @Override
665             public MediaType contentType() {
666                 return null;
667             }
668
669             @Override
670             public long contentLength() {
671                 return 0;
672             }
673
674             @Override
675             public BufferedSource source() {
676                 return null;
677             }
678         });
679         when(intentApiService.queryCustomer(anyString())).thenReturn(mockCall);
680         when(mockCall.execute()).thenReturn(response);
681
682         Properties properties = new Properties();
683         properties.put("ccvpn.globalCustomerId", "IBNCustomer");
684         properties.put("ccvpn.subscriberName", "IBNCustomer");
685         properties.put("ccvpn.subscriberType", "INFRA");
686         properties.put("ccvpn.serviceType", "IBN");
687         IntentInstanceServiceImpl spy = spy(intentInstanceService);
688         doReturn(properties).when(spy).getProperties();
689
690         Call mockCall2 = PowerMockito.mock(Call.class);
691         when(intentApiService.addCustomer(anyString(),any())).thenReturn(mockCall2);
692
693         spy.addCustomer();
694         Mockito.verify(intentApiService,Mockito.times(1)).addCustomer(anyString(),any());
695     }
696
697
698     @Test
699     public void addSubscriptionTest() throws IOException {
700
701         Call mockCall = PowerMockito.mock(Call.class);
702         Response<Object> response = Response.error(404, new ResponseBody() {
703             @Nullable
704             @Override
705             public MediaType contentType() {
706                 return null;
707             }
708
709             @Override
710             public long contentLength() {
711                 return 0;
712             }
713
714             @Override
715             public BufferedSource source() {
716                 return null;
717             }
718         });
719         when(intentApiService.querySubscription(anyString(),anyString())).thenReturn(mockCall);
720         when(mockCall.execute()).thenReturn(response);
721
722         Properties properties = new Properties();
723         properties.put("ccvpn.globalCustomerId", "IBNCustomer");
724         properties.put("ccvpn.subscriberName", "IBNCustomer");
725         properties.put("ccvpn.subscriberType", "INFRA");
726         properties.put("ccvpn.serviceType", "IBN");
727         IntentInstanceServiceImpl spy = spy(intentInstanceService);
728         doReturn(properties).when(spy).getProperties();
729
730         Call mockCall2 = PowerMockito.mock(Call.class);
731         when(intentApiService.addSubscription(anyString(),anyString(),any())).thenReturn(mockCall2);
732
733         spy.addSubscription();
734         Mockito.verify(intentApiService,Mockito.times(1)).addSubscription(anyString(),anyString(),any());
735     }
736
737     @Test
738     public void saveIntentInstanceToAAITest() throws IOException {
739         IntentInstanceServiceImpl spy = spy(intentInstanceService);
740         doNothing().when(spy).addCustomer();
741         doNothing().when(spy).addSubscription();
742
743         Properties properties = new Properties();
744         properties.put("ccvpn.globalCustomerId", "IBNCustomer");
745         properties.put("ccvpn.subscriberName", "IBNCustomer");
746         properties.put("ccvpn.subscriberType", "INFRA");
747         properties.put("ccvpn.serviceType", "IBN");
748         doReturn(properties).when(spy).getProperties();
749
750         JSONObject body = new JSONObject();
751         body.put("resource-version",123);
752         Call mockCall = PowerMockito.mock(Call.class);
753         Response<JSONObject> response = Response.success(body);
754         when(intentApiService.queryServiceInstance(anyString(),anyString(),anyString())).thenReturn(mockCall);
755         when(mockCall.execute()).thenReturn(response);
756
757         CCVPNInstance instance = new CCVPNInstance();
758         instance.setName("name");
759         instance.setInstanceId("id");
760
761         Call mockCall2 = PowerMockito.mock(Call.class);
762         Response<JSONObject> response2 = Response.success(body);
763         when(intentApiService.saveServiceInstance(anyString(),anyString(),anyString(),any())).thenReturn(mockCall2);
764         when(mockCall2.execute()).thenReturn(response2);
765
766         spy.saveIntentInstanceToAAI("CCVPN-id",instance);
767         Mockito.verify(intentApiService, Mockito.times(1)).saveServiceInstance(anyString(),anyString(),anyString(),any());
768
769     }
770     @Test
771     public void deleteIntentInstanceToAAITest() throws IOException {
772         IntentInstanceServiceImpl spy = spy(intentInstanceService);
773         doNothing().when(spy).addCustomer();
774         doNothing().when(spy).addSubscription();
775
776         Properties properties = new Properties();
777         properties.put("ccvpn.globalCustomerId", "IBNCustomer");
778         properties.put("ccvpn.subscriberName", "IBNCustomer");
779         properties.put("ccvpn.subscriberType", "INFRA");
780         properties.put("ccvpn.serviceType", "IBN");
781         doReturn(properties).when(spy).getProperties();
782
783         JSONObject body = new JSONObject();
784         body.put("resource-version",123);
785         Call mockCall = PowerMockito.mock(Call.class);
786         Response<JSONObject> response = Response.success(body);
787         when(intentApiService.queryServiceInstance(anyString(),anyString(),anyString())).thenReturn(mockCall);
788         when(mockCall.execute()).thenReturn(response);
789
790         Call mockCall2 = PowerMockito.mock(Call.class);
791         Response<JSONObject> response2 = Response.success(body);
792         when(intentApiService.deleteServiceInstance(anyString(),anyString(),anyString(),anyString())).thenReturn(mockCall2);
793         when(mockCall2.execute()).thenReturn(response2);
794
795         spy.deleteIntentInstanceToAAI("CCVPN-id");
796         Mockito.verify(intentApiService, Mockito.times(1)).deleteServiceInstance(anyString(),anyString(),anyString(),any());
797
798     }
799     @Test
800     public void createIntentInstanceWithCCVPNInstanceTest() {
801         Map<String, Object> body = new HashMap<>();
802         body.put("intentContent", "this is intent content");
803         body.put("name", "this is name");
804         Transaction tx = PowerMockito.mock(Transaction.class);
805         when(session.beginTransaction()).thenReturn(tx);
806
807         Serializable save = Mockito.mock(Serializable.class);
808         when(session.save(any(IntentInstance.class))).thenReturn(save);
809
810         doNothing().when(tx).commit();
811         doNothing().when(session).close();
812         IntentInstance instance = intentInstanceService.createIntentInstance(body, "id", "name", IntentConstant.MODEL_TYPE_CCVPN);
813         assertEquals(instance.getBusinessInstanceId(), "id");
814     }
815     @Test
816     public void createIntentInstanceWithSlicingInstanceTest() {
817         Map<String, Object> slicingOrderInfo = new HashMap<>();
818         slicingOrderInfo.put("intentContent", "this is intent content");
819         slicingOrderInfo.put("name", "this is name");
820
821         Map<String, Object> body = new HashMap<>();
822         body.put("slicing_order_info", slicingOrderInfo);
823         Transaction tx = PowerMockito.mock(Transaction.class);
824         when(session.beginTransaction()).thenReturn(tx);
825
826         Serializable save = Mockito.mock(Serializable.class);
827         when(session.save(any(IntentInstance.class))).thenReturn(save);
828
829         doNothing().when(tx).commit();
830         doNothing().when(session).close();
831         IntentInstance instance = intentInstanceService.createIntentInstance(body, "id", "name", IntentConstant.MODEL_TYPE_5GS);
832         assertEquals(instance.getBusinessInstanceId(), "id");
833     }
834     @Test
835     public void createIntentInstanceWithThrowErrorTest() {
836         Map<String, Object> slicingOrderInfo = new HashMap<>();
837         slicingOrderInfo.put("intentContent", "this is intent content");
838         slicingOrderInfo.put("name", "this is name");
839
840         Map<String, Object> body = new HashMap<>();
841         body.put("slicing_order_info", slicingOrderInfo);
842         Transaction tx = PowerMockito.mock(Transaction.class);
843         when(session.beginTransaction()).thenReturn(tx);
844
845         when(session.save(any(IntentInstance.class))).thenThrow(new RuntimeException());
846
847         doNothing().when(session).close();
848         IntentInstance instance = intentInstanceService.createIntentInstance(body, "id", "name", IntentConstant.MODEL_TYPE_5GS);
849         assertNull(instance);
850     }
851
852     @Test
853     public void deleteIntentWithDeleteCCVPNInstanceTest() {
854
855         IntentInstanceServiceImpl spy = spy(intentInstanceService);
856
857         IntentInstance instance = new IntentInstance();
858         instance.setId(1);
859         instance.setIntentSource(IntentConstant.MODEL_TYPE_CCVPN);
860         instance.setBusinessInstanceId("1");
861
862         Query query = PowerMockito.mock(Query.class);
863         when(session.createQuery(anyString())).thenReturn(query);
864         when(query.setParameter("id", 1)).thenReturn(query);
865         when(query.uniqueResult()).thenReturn(instance);
866
867         doNothing().when(spy).deleteIntentInstance(anyString());
868
869         Transaction tx = PowerMockito.mock(Transaction.class);
870         when(session.beginTransaction()).thenReturn(tx);
871         doNothing().when(session).delete(any());
872         doNothing().when(tx).commit();
873         doNothing().when(session).close();
874
875         spy.deleteIntent(1);
876
877         Mockito.verify(spy, Mockito.times(1)).deleteIntentInstance("1");
878     }
879
880     @Test
881     public void deleteIntentWithDeleteSlicingInstanceTest() {
882
883
884         IntentInstance instance = new IntentInstance();
885         instance.setId(1);
886         instance.setIntentSource(IntentConstant.MODEL_TYPE_5GS);
887         instance.setBusinessInstanceId("1");
888
889         Query query = PowerMockito.mock(Query.class);
890         when(session.createQuery(anyString())).thenReturn(query);
891         when(query.setParameter("id", 1)).thenReturn(query);
892         when(query.uniqueResult()).thenReturn(instance);
893
894         ServiceResult serviceResult = new ServiceResult();
895         when(resourceMgtService.terminateSlicingService(anyString())).thenReturn(serviceResult);
896
897         Transaction tx = PowerMockito.mock(Transaction.class);
898         when(session.beginTransaction()).thenReturn(tx);
899         doNothing().when(session).delete(any());
900         doNothing().when(tx).commit();
901         doNothing().when(session).close();
902
903         intentInstanceService.deleteIntent(1);
904
905         Mockito.verify(resourceMgtService, Mockito.times(1)).terminateSlicingService(anyString());
906     }
907     @Test
908     public void deleteIntentWithThrowErrorTest() {
909
910
911         IntentInstance instance = new IntentInstance();
912         instance.setId(1);
913         instance.setIntentSource(IntentConstant.MODEL_TYPE_5GS);
914         instance.setBusinessInstanceId("1");
915
916         when(session.createQuery(anyString())).thenThrow(new RuntimeException());
917
918         doNothing().when(session).close();
919
920         intentInstanceService.deleteIntent(1);
921
922         Mockito.verify(resourceMgtService, Mockito.times(0)).terminateSlicingService(anyString());
923     }
924
925     @Test
926     public void getIntentInstanceListTest() {
927         IntentInstanceServiceImpl spy = spy(intentInstanceService);
928         doReturn(2).when(spy).getIntentInstanceAllCount();
929
930         Query query = PowerMockito.mock(Query.class);
931         when(session.createQuery("from IntentInstance order by id")).thenReturn(query);
932         when(query.setFirstResult(anyInt())).thenReturn(query);
933         when(query.setMaxResults(anyInt())).thenReturn(query);
934
935         List<IntentInstance> list = new ArrayList<>();
936         list.add(new IntentInstance());
937         list.add(new IntentInstance());
938         when(query.list()).thenReturn(list);
939         doNothing().when(session).close();
940         int totalRecords = spy.getIntentInstanceList(1, 10).getTotalRecords();
941         assertEquals(totalRecords,2);
942     }
943
944     @Test
945     public void getIntentInstanceListThrowErrorTest() {
946         IntentInstanceServiceImpl spy = spy(intentInstanceService);
947         doReturn(2).when(spy).getIntentInstanceAllCount();
948
949         when(session.createQuery("from IntentInstance order by id")).thenThrow(new RuntimeException());
950         doNothing().when(session).close();
951         assertEquals(spy.getIntentInstanceList(1, 10),null);
952     }
953
954     @Test
955     public void createSlicingServiceWithIntent() {
956         IntentInstanceServiceImpl spy = spy(intentInstanceService);
957
958         SlicingOrder slicingOrder = new SlicingOrder();
959         slicingOrder.setSlicing_order_info(new SlicingOrderDetail());
960         slicingOrder.getSlicing_order_info().setName("name");
961
962         ServiceResult serviceResult = new ServiceResult();
963         ServiceCreateResult serviceCreateResult = new ServiceCreateResult();
964         serviceCreateResult.setService_id("id");
965         serviceResult.setResult_body(serviceCreateResult);
966         when(slicingService.createSlicingService(any())).thenReturn(serviceResult);
967
968         IntentInstance instance = new IntentInstance();
969         doReturn(instance).when(spy).createIntentInstance(any(),anyString(),anyString(),anyString());
970
971         assertEquals(spy.createSlicingServiceWithIntent(slicingOrder), serviceResult);
972     }
973
974     @Test
975     public void getIntentInstanceAllCountTest() {
976
977         Query query = PowerMockito.mock(Query.class);
978         when(session.createQuery("select count(*) from IntentInstance")).thenReturn(query);
979         when(query.uniqueResult()).thenReturn(2L);
980
981
982         assertEquals(intentInstanceService.getIntentInstanceAllCount(),2);
983     }
984
985     @Test
986     public void getIntentInstanceAllCountThrowErrorTest() {
987
988         when(session.createQuery("select count(*) from IntentInstance")).thenThrow(new RuntimeException());
989         assertEquals(intentInstanceService.getIntentInstanceAllCount(),-1);
990     }
991
992     @Test
993     public void updateCCVPNInstanceTest() throws IOException {
994         CCVPNInstance instance = new CCVPNInstance();
995         instance.setInstanceId("1");
996         instance.setAccessPointOneBandWidth(1);
997
998         CCVPNInstance ccvpnInstance = new CCVPNInstance();
999         ccvpnInstance.setInstanceId(instance.getInstanceId());
1000
1001         Query query = mock(Query.class);
1002         when(session.createQuery("from CCVPNInstance where instanceId = :instanceId")).thenReturn(query);
1003         when(query.setParameter(anyString(), anyString())).thenReturn(query);
1004         when(query.uniqueResult()).thenReturn(ccvpnInstance);
1005
1006         IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
1007         doNothing().when(spy).saveIntentInstanceToAAI(anyString(),any(CCVPNInstance.class));
1008
1009         Transaction tx = Mockito.mock(Transaction.class);
1010         Mockito.when(session.beginTransaction()).thenReturn(tx);
1011         doNothing().when(session).update(ccvpnInstance);
1012         Mockito.doNothing().when(tx).commit();
1013
1014         assertEquals(spy.updateCCVPNInstance(instance), 1);
1015     }
1016 }