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