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