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