2 * Copyright (C) 2021 CTC, Inc. and others. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.usecaseui.server.service.intent.impl;
18 import java.io.IOException;
19 import java.io.Serializable;
22 import com.alibaba.fastjson.JSONArray;
23 import com.alibaba.fastjson.JSONObject;
24 import okhttp3.MediaType;
25 import okhttp3.ResponseBody;
26 import okio.BufferedSource;
27 import org.hibernate.Session;
28 import org.hibernate.SessionFactory;
29 import org.hibernate.Transaction;
30 import org.hibernate.query.Query;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.onap.usecaseui.server.bean.csmf.ServiceCreateResult;
38 import org.onap.usecaseui.server.bean.csmf.SlicingOrder;
39 import org.onap.usecaseui.server.bean.csmf.SlicingOrderDetail;
40 import org.onap.usecaseui.server.bean.intent.CCVPNInstance;
41 import org.onap.usecaseui.server.bean.intent.InstancePerformance;
42 import org.onap.usecaseui.server.bean.intent.IntentInstance;
43 import org.onap.usecaseui.server.bean.intent.IntentModel;
44 import org.onap.usecaseui.server.bean.nsmf.common.ServiceResult;
45 import org.onap.usecaseui.server.constant.IntentConstant;
46 import org.onap.usecaseui.server.service.csmf.SlicingService;
47 import org.onap.usecaseui.server.service.intent.IntentApiService;
48 import org.onap.usecaseui.server.service.lcm.domain.so.SOService;
49 import org.onap.usecaseui.server.service.lcm.domain.so.bean.OperationProgress;
50 import org.onap.usecaseui.server.service.lcm.domain.so.bean.OperationProgressInformation;
51 import org.onap.usecaseui.server.service.nsmf.ResourceMgtService;
52 import org.powermock.api.mockito.PowerMockito;
53 import org.powermock.api.support.membermodification.MemberModifier;
54 import org.powermock.modules.junit4.PowerMockRunner;
56 import static org.junit.Assert.*;
57 import static org.mockito.ArgumentMatchers.*;
58 import static org.powermock.api.mockito.PowerMockito.*;
60 import retrofit2.Call;
61 import retrofit2.Response;
63 import javax.annotation.Nullable;
64 import javax.annotation.Resource;
66 @RunWith(PowerMockRunner.class)
67 public class IntentInstanceServiceImplTest {
69 public IntentInstanceServiceImplTest() {
73 private IntentInstanceServiceImpl intentInstanceService;
76 private IntentApiService intentApiService;
79 private SOService soService;
82 @Resource(name = "ResourceMgtService")
83 private ResourceMgtService resourceMgtService;
86 @Resource(name = "SlicingService")
87 private SlicingService slicingService;
90 private SessionFactory sessionFactory;
93 private Session session;
96 public void before() throws Exception {
97 MemberModifier.field(IntentInstanceServiceImpl.class, "sessionFactory").set(intentInstanceService , sessionFactory);
98 MemberModifier.field(IntentInstanceServiceImpl.class, "resourceMgtService").set(intentInstanceService , resourceMgtService);
99 MemberModifier.field(IntentInstanceServiceImpl.class, "slicingService").set(intentInstanceService , slicingService);
100 doReturn(session).when(sessionFactory,"openSession");
104 public void queryIntentInstanceTest() {
105 CCVPNInstance instance = new CCVPNInstance();
106 instance.setInstanceId("1");
107 instance.setJobId("1");
108 instance.setStatus("1");
110 Query query = Mockito.mock(Query.class);
111 when(session.createQuery(anyString())).thenReturn(query);
112 List<IntentModel> list = new ArrayList<>();
113 when(query.list()).thenReturn(list);
114 when(query.uniqueResult()).thenReturn(10L);
115 assertTrue(intentInstanceService.queryIntentInstance(instance,1,2).getList().isEmpty());
119 public void queryIntentInstanceGetCountErrorTest() {
120 CCVPNInstance instance = new CCVPNInstance();
121 instance.setInstanceId("1");
122 instance.setJobId("1");
123 instance.setStatus("1");
125 Query query = Mockito.mock(Query.class);
126 when(session.createQuery(anyString())).thenReturn(query);
127 List<IntentModel> list = new ArrayList<>();
128 when(query.list()).thenReturn(list);
129 when(query.uniqueResult()).thenReturn(10);
130 assertTrue(intentInstanceService.queryIntentInstance(instance,1,2).getList().isEmpty());
134 public void queryIntentInstanceThrowErrorTest() {
135 CCVPNInstance instance = new CCVPNInstance();
136 instance.setInstanceId("1");
137 instance.setJobId("1");
138 instance.setStatus("1");
140 when(session.createQuery(anyString())).thenThrow(new RuntimeException());
142 assertEquals(intentInstanceService.queryIntentInstance(instance,1,2), null);
145 public void createCCVPNInstanceTest() throws IOException {
146 CCVPNInstance instance = new CCVPNInstance();
147 instance.setInstanceId("1");
148 instance.setJobId("1");
149 instance.setStatus("1");
151 Call mockCall = PowerMockito.mock(Call.class);
152 JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}");
153 Response<JSONObject> response = Response.success(body);
154 Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall);
155 Mockito.when(mockCall.execute()).thenReturn(response);
157 IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
158 doNothing().when(spy).saveIntentInstanceToAAI(isNull(),any(CCVPNInstance.class));
160 Transaction tx = Mockito.mock(Transaction.class);
161 Mockito.when(session.beginTransaction()).thenReturn(tx);
162 Serializable save = Mockito.mock(Serializable.class);
163 Mockito.when(session.save(any())).thenReturn(save);
164 Mockito.doNothing().when(tx).commit();
166 assertEquals(spy.createCCVPNInstance(instance), 1);
170 public void createCCVPNInstanceThrowErrorTest() throws IOException {
171 CCVPNInstance instance = new CCVPNInstance();
172 instance.setInstanceId("1");
173 instance.setJobId("1");
174 instance.setStatus("1");
176 Call mockCall = PowerMockito.mock(Call.class);
177 JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}");
178 Response<JSONObject> response = Response.success(body);
179 Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall);
180 Mockito.when(mockCall.execute()).thenReturn(response);
182 IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
183 doThrow(new RuntimeException()).when(spy).saveIntentInstanceToAAI(isNull(),any(CCVPNInstance.class));
185 Transaction tx = Mockito.mock(Transaction.class);
186 Mockito.when(session.beginTransaction()).thenReturn(tx);
187 Serializable save = Mockito.mock(Serializable.class);
188 Mockito.when(session.save(any())).thenReturn(save);
189 Mockito.doNothing().when(tx).commit();
191 assertEquals(spy.createCCVPNInstance(instance), 0);
195 public void createCCVPNInstanceInstanceIsNullTest() throws IOException {
196 assertEquals(intentInstanceService.createCCVPNInstance(null), 0);
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);
207 public void getIntentInstanceProgressTest() throws IOException {
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");
217 when(query1.list()).thenReturn(q);
219 OperationProgressInformation operationProgressInformation = new OperationProgressInformation();
220 OperationProgress operationProgress = new OperationProgress();
221 operationProgress.setProgress(100);
222 operationProgressInformation.setOperationStatus(operationProgress);
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);
233 IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
234 doNothing().when(spy).saveIntentInstanceToAAI(anyString(),any(CCVPNInstance.class));
236 Transaction tx = Mockito.mock(Transaction.class);
237 Mockito.when(session.beginTransaction()).thenReturn(tx);
238 Serializable save = Mockito.mock(Serializable.class);
239 Mockito.when(session.save(any())).thenReturn(save);
240 Mockito.doNothing().when(tx).commit();
242 spy.getIntentInstanceProgress();
243 assertEquals(operation.getString("progress"),"100");
246 public void getIntentInstanceCreateStatusTest() throws IOException {
248 Query query1 = Mockito.mock(Query.class);
249 when(session.createQuery("from CCVPNInstance where deleteState = 0 and status = '0'")).thenReturn(query1);
250 List<CCVPNInstance> q = new ArrayList<>();
251 CCVPNInstance instance = new CCVPNInstance();
252 instance.setInstanceId("1");
253 instance.setResourceInstanceId("1");
254 instance.setJobId("1");
256 when(query1.list()).thenReturn(q);
258 OperationProgressInformation operationProgressInformation = new OperationProgressInformation();
259 OperationProgress operationProgress = new OperationProgress();
260 operationProgress.setProgress(100);
261 operationProgressInformation.setOperationStatus(operationProgress);
263 JSONObject jsonObject = new JSONObject();
264 jsonObject.put("orchestration-status", "created");
265 Call mockCall = PowerMockito.mock(Call.class);
266 Response<JSONObject> response = Response.success(jsonObject);
267 Mockito.when(intentApiService.getInstanceInfo(anyString())).thenReturn(mockCall);
268 Mockito.when(mockCall.execute()).thenReturn(response);
270 IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
271 doNothing().when(spy).saveIntentInstanceToAAI(anyString(),any(CCVPNInstance.class));
273 Transaction tx = Mockito.mock(Transaction.class);
274 Mockito.when(session.beginTransaction()).thenReturn(tx);
275 Serializable save = Mockito.mock(Serializable.class);
276 Mockito.when(session.save(any())).thenReturn(save);
277 Mockito.doNothing().when(tx).commit();
279 spy.getIntentInstanceCreateStatus();
280 assertEquals(jsonObject.getString("orchestration-status"),"created");
284 public void getFinishedInstanceInfo() {
285 Query query = Mockito.mock(Query.class);
286 when(session.createQuery(anyString())).thenReturn(query);
287 when(query.list()).thenReturn(new ArrayList());
288 assertTrue(intentInstanceService.getFinishedInstanceInfo().isEmpty());
292 public void getIntentInstanceBandwidth() throws IOException {
293 Query query1 = Mockito.mock(Query.class);
294 when(session.createQuery("from CCVPNInstance where deleteState = 0 and status = '1'")).thenReturn(query1);
295 List<CCVPNInstance> q = new ArrayList<>();
296 CCVPNInstance instance = new CCVPNInstance();
297 instance.setInstanceId("1");
298 instance.setResourceInstanceId("1");
300 when(query1.list()).thenReturn(q);
302 Call mockCall = PowerMockito.mock(Call.class);
303 JSONObject jsonObject = JSONObject.parseObject("{\n" +
304 " \"service-instance-id\":\"cll-101\",\n" +
305 " \"service-instance-name\":\"cloud-leased-line-101\",\n" +
306 " \"service-type\":\"CLL\",\n" +
307 " \"service-role\":\"cll\",\n" +
308 " \"environment-context\":\"cll\",\n" +
309 " \"model-invariant-id\":\"6790ab0e-034f-11eb-adc1-0242ac120002\",\n" +
310 " \"model-version-id\":\"6790ab0e-034f-11eb-adc1-0242ac120002\",\n" +
311 " \"resource-version\":\"1628714665927\",\n" +
312 " \"orchestration-status\":\"created\",\n" +
313 " \"allotted-resources\":{\n" +
314 " \"allotted-resource\":[\n" +
316 " \"id\":\"cll-101-network-001\",\n" +
317 " \"resource-version\":\"1628714665798\",\n" +
318 " \"type\":\"TsciNetwork\",\n" +
319 " \"allotted-resource-name\":\"network_cll-101-network-001\",\n" +
320 " \"relationship-list\":{\n" +
321 " \"relationship\":[\n" +
323 " \"related-to\":\"logical-link\",\n" +
324 " \"relationship-label\":\"org.onap.relationships.inventory.ComposedOf\",\n" +
325 " \"related-link\":\"/aai/v24/network/logical-links/logical-link/tranportEp_UNI_ID_311_1\",\n" +
326 " \"relationship-data\":[\n" +
328 " \"relationship-key\":\"logical-link.link-name\",\n" +
329 " \"relationship-value\":\"tranportEp_UNI_ID_311_1\"\n" +
334 " \"related-to\":\"network-policy\",\n" +
335 " \"relationship-label\":\"org.onap.relationships.inventory.Uses\",\n" +
336 " \"related-link\":\"/aai/v24/network/network-policies/network-policy/de00a0a0-be2e-4d19-974a-80a2bca6bdf9\",\n" +
337 " \"relationship-data\":[\n" +
339 " \"relationship-key\":\"network-policy.network-policy-id\",\n" +
340 " \"relationship-value\":\"de00a0a0-be2e-4d19-974a-80a2bca6bdf9\"\n" +
343 " \"related-to-property\":[\n" +
345 " \"property-key\":\"network-policy.network-policy-fqdn\",\n" +
346 " \"property-value\":\"cll-101\"\n" +
356 Response<JSONObject> response = Response.success(jsonObject);
357 Mockito.when(intentApiService.getInstanceNetworkInfo(any())).thenReturn(mockCall);
358 Mockito.when(mockCall.execute()).thenReturn(response);
360 Call mockCall1 = PowerMockito.mock(Call.class);
361 JSONObject jsonObject1 = JSONObject.parseObject("{\n" +
362 " \"network-policy-id\":\"de00a0a0-be2e-4d19-974a-80a2bca6bdf9\",\n" +
363 " \"network-policy-fqdn\":\"cll-101\",\n" +
364 " \"resource-version\":\"1628714665619\",\n" +
365 " \"name\":\"TSCi policy\",\n" +
366 " \"type\":\"SLA\",\n" +
367 " \"latency\":2,\n" +
368 " \"max-bandwidth\":3000,\n" +
369 " \"relationship-list\":{\n" +
370 " \"relationship\":[\n" +
372 " \"related-to\":\"allotted-resource\",\n" +
373 " \"relationship-label\":\"org.onap.relationships.inventory.Uses\",\n" +
374 " \"related-link\":\"/aai/v24/business/customers/customer/IBNCustomer/service-subscriptions/service-subscription/IBN/service-instances/service-instance/cll-101/allotted-resources/allotted-resource/cll-101-network-001\",\n" +
375 " \"relationship-data\":[\n" +
377 " \"relationship-key\":\"customer.global-customer-id\",\n" +
378 " \"relationship-value\":\"IBNCustomer\"\n" +
381 " \"relationship-key\":\"service-subscription.service-type\",\n" +
382 " \"relationship-value\":\"IBN\"\n" +
385 " \"relationship-key\":\"service-instance.service-instance-id\",\n" +
386 " \"relationship-value\":\"cll-101\"\n" +
389 " \"relationship-key\":\"allotted-resource.id\",\n" +
390 " \"relationship-value\":\"cll-101-network-001\"\n" +
393 " \"related-to-property\":[\n" +
395 " \"property-key\":\"allotted-resource.description\"\n" +
398 " \"property-key\":\"allotted-resource.allotted-resource-name\",\n" +
399 " \"property-value\":\"network_cll-101-network-001\"\n" +
406 Response<JSONObject> response1 = Response.success(jsonObject1);
407 Mockito.when(intentApiService.getInstanceNetworkPolicyInfo(any())).thenReturn(mockCall1);
408 Mockito.when(mockCall1.execute()).thenReturn(response1);
410 Call mockCall2 = PowerMockito.mock(Call.class);
411 JSONObject jsonObject2 = JSONObject.parseObject("{\n" +
412 " \"metadatum\":[\n" +
414 " \"metaname\":\"ethernet-uni-id-1\",\n" +
415 " \"metaval\":\"1234\",\n" +
416 " \"resource-version\":\"1629409084707\"\n" +
419 " \"metaname\":\"ethernet-uni-id-2\",\n" +
420 " \"metaval\":\"5678\",\n" +
421 " \"resource-version\":\"1629409204904\"\n" +
425 Response<JSONObject> response2 = Response.success(jsonObject2);
426 Mockito.when(intentApiService.getInstanceBandwidth(any())).thenReturn(mockCall2);
427 Mockito.when(mockCall2.execute()).thenReturn(response2);
429 Transaction tx = Mockito.mock(Transaction.class);
430 Mockito.when(session.beginTransaction()).thenReturn(tx);
431 Serializable save = Mockito.mock(Serializable.class);
432 Mockito.when(session.save(any())).thenReturn(save);
433 Mockito.doNothing().when(tx).commit();
435 intentInstanceService.getIntentInstanceBandwidth();
439 public void deleteIntentInstance() throws IOException {
440 CCVPNInstance instance = new CCVPNInstance();
441 instance.setResourceInstanceId("1");
443 Query query = Mockito.mock(Query.class);
444 when(session.createQuery(anyString())).thenReturn(query);
445 when(query.setParameter(anyString(), anyString())).thenReturn(query);
446 when(query.uniqueResult()).thenReturn(instance);
448 Call mockCall = PowerMockito.mock(Call.class);
449 when(intentApiService.deleteIntentInstance(any())).thenReturn(mockCall);
450 when(mockCall.execute()).thenReturn(null);
452 Transaction tx = PowerMockito.mock(Transaction.class);
453 when(session.beginTransaction()).thenReturn(tx);
454 Serializable save = PowerMockito.mock(Serializable.class);
455 doNothing().when(session).delete(any());
456 doNothing().when(tx).commit();
458 IntentInstanceServiceImpl spy = spy(intentInstanceService);
459 doNothing().when(spy).deleteIntentInstanceToAAI(anyString());
461 spy.deleteIntentInstance("1");
466 public void invalidIntentInstanceTest() throws IOException {
467 CCVPNInstance instance = new CCVPNInstance();
468 instance.setResourceInstanceId("1");
470 Query query = Mockito.mock(Query.class);
471 when(session.createQuery(anyString())).thenReturn(query);
472 when(query.setParameter(anyString(), anyString())).thenReturn(query);
473 when(query.uniqueResult()).thenReturn(instance);
475 Call mockCall = PowerMockito.mock(Call.class);
476 when(intentApiService.deleteIntentInstance(any())).thenReturn(mockCall);
477 when(mockCall.execute()).thenReturn(null);
479 Transaction tx = PowerMockito.mock(Transaction.class);
480 when(session.beginTransaction()).thenReturn(tx);
481 Serializable save = PowerMockito.mock(Serializable.class);
482 doNothing().when(session).delete(any());
483 doNothing().when(tx).commit();
485 intentInstanceService.invalidIntentInstance("1");
488 public void queryInstancePerformanceDataTest() throws IOException {
489 CCVPNInstance instance = new CCVPNInstance();
490 instance.setResourceInstanceId("1");
492 InstancePerformance instancePerformance = new InstancePerformance();
493 instancePerformance.setBandwidth(2000);
494 instancePerformance.setMaxBandwidth(20000);
495 instancePerformance.setDate(new Date());
496 Object[] o = {null,instancePerformance};
497 List<Object[]> queryResult= new ArrayList<>();
500 Query query = Mockito.mock(Query.class);
501 when(session.createQuery(anyString())).thenReturn(query);
502 when(query.setParameter(anyString(), anyString())).thenReturn(query);
503 when(query.list()).thenReturn(queryResult);
505 intentInstanceService.queryInstancePerformanceData("1");
511 Call mockCall = PowerMockito.mock(Call.class);
512 when(intentApiService.deleteIntentInstance(any())).thenReturn(mockCall);
513 when(mockCall.execute()).thenReturn(null);
515 Transaction tx = PowerMockito.mock(Transaction.class);
516 when(session.beginTransaction()).thenReturn(tx);
517 Serializable save = PowerMockito.mock(Serializable.class);
518 doNothing().when(session).delete(any());
519 doNothing().when(tx).commit();
521 intentInstanceService.invalidIntentInstance("1");
525 public void activeIntentInstance() throws IOException {
526 CCVPNInstance instance = new CCVPNInstance();
527 instance.setInstanceId("1");
528 instance.setJobId("1");
529 instance.setStatus("1");
531 Query query = Mockito.mock(Query.class);
532 when(session.createQuery(anyString())).thenReturn(query);
533 when(query.setParameter(anyString(), anyString())).thenReturn(query);
534 when(query.uniqueResult()).thenReturn(instance);
537 Call mockCall = PowerMockito.mock(Call.class);
538 JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}");
539 Response<JSONObject> response = Response.success(body);
540 Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall);
541 Mockito.when(mockCall.execute()).thenReturn(response);
543 Transaction tx = Mockito.mock(Transaction.class);
544 Mockito.when(session.beginTransaction()).thenReturn(tx);
545 Serializable save = Mockito.mock(Serializable.class);
546 Mockito.when(session.save(any())).thenReturn(save);
547 Mockito.doNothing().when(tx).commit();
549 intentInstanceService.activeIntentInstance("1");
554 public void queryAccessNodeInfo() throws IOException {
556 Call mockCall = PowerMockito.mock(Call.class);
557 JSONObject body = JSONObject.parseObject("{\n" +
558 " \"network-route\": [\n" +
560 " \"route-id\": \"tranportEp_src_ID_111_1\",\n" +
561 " \"type\": \"LEAF\",\n" +
562 " \"role\": \"3gppTransportEP\",\n" +
563 " \"function\": \"3gppTransportEP\",\n" +
564 " \"ip-address\": \"10.2.3.4\",\n" +
565 " \"prefix-length\": 24,\n" +
566 " \"next-hop\": \"networkId-providerId-10-clientId-0-topologyId-2-nodeId-10.1.1.1-ltpId-1000\",\n" +
567 " \"address-family\": \"ipv4\",\n" +
568 " \"resource-version\": \"1634198223345\"\n" +
571 " \"route-id\": \"tranportEp_src_ID_113_1\",\n" +
572 " \"type\": \"LEAF\",\n" +
573 " \"role\": \"3gppTransportEP\",\n" +
574 " \"function\": \"3gppTransportEP\",\n" +
575 " \"ip-address\": \"10.2.3.4\",\n" +
576 " \"prefix-length\": 24,\n" +
577 " \"next-hop\": \"networkId-providerId-10-clientId-0-topologyId-2-nodeId-10.1.1.3-ltpId-1000\",\n" +
578 " \"address-family\": \"ipv4\",\n" +
579 " \"resource-version\": \"1634198260496\"\n" +
582 " \"route-id\": \"tranportEp_src_ID_111_2\",\n" +
583 " \"type\": \"LEAF\",\n" +
584 " \"role\": \"3gppTransportEP\",\n" +
585 " \"function\": \"3gppTransportEP\",\n" +
586 " \"ip-address\": \"10.2.3.4\",\n" +
587 " \"prefix-length\": 24,\n" +
588 " \"next-hop\": \"networkId-providerId-10-clientId-0-topologyId-2-nodeId-10.1.1.1-ltpId-2000\",\n" +
589 " \"address-family\": \"ipv4\",\n" +
590 " \"resource-version\": \"1634198251534\"\n" +
593 " \"route-id\": \"tranportEp_dst_ID_212_1\",\n" +
594 " \"type\": \"ROOT\",\n" +
595 " \"role\": \"3gppTransportEP\",\n" +
596 " \"function\": \"3gppTransportEP\",\n" +
597 " \"ip-address\": \"10.2.3.4\",\n" +
598 " \"prefix-length\": 24,\n" +
599 " \"next-hop\": \"networkId-providerId-20-clientId-0-topologyId-2-nodeId-10.2.1.2-ltpId-512\",\n" +
600 " \"address-family\": \"ipv4\",\n" +
601 " \"resource-version\": \"1634198274852\"\n" +
605 Response<JSONObject> response = Response.success(body);
606 Mockito.when(intentApiService.queryNetworkRoute()).thenReturn(mockCall);
607 Mockito.when(mockCall.execute()).thenReturn(response);
608 Map<String, Object> result = (Map<String, Object>) intentInstanceService.queryAccessNodeInfo();
609 assertEquals(((List)result.get("accessNodeList")).size(), 3);
613 public void getInstanceStatusTest() {
614 List<CCVPNInstance> queryResult = new ArrayList<>();
615 CCVPNInstance instance = new CCVPNInstance();
616 instance.setInstanceId("id1");
617 instance.setStatus("1");
618 queryResult.add(instance);
620 Query query = Mockito.mock(Query.class);
621 when(session.createQuery(anyString())).thenReturn(query);
622 when(query.setParameter(anyString(), any())).thenReturn(query);
623 when(query.list()).thenReturn(queryResult);
626 JSONObject instanceStatus = intentInstanceService.getInstanceStatus(new JSONArray());
627 assertEquals(instanceStatus.getJSONArray("IntentInstances").getJSONObject(0).getString("id"), "id1");
630 public void formatBandwidthTest() {
632 String bandwidth = intentInstanceService.formatBandwidth("2Gbps");
633 assertEquals(bandwidth, "2000");
636 public void formatCloudPointTest() {
638 String bandwidth = intentInstanceService.formatCloudPoint("Cloud one");
639 assertEquals(bandwidth, "tranportEp_dst_ID_212_1");
642 public void formatAccessPointOneTest() {
643 String bandwidth = intentInstanceService.formatAccessPoint("Access one");
644 assertEquals(bandwidth, "tranportEp_src_ID_111_1");
647 public void formatAccessPointTwoTest() {
648 String bandwidth = intentInstanceService.formatAccessPoint("Access two");
649 assertEquals(bandwidth, "tranportEp_src_ID_111_2");
652 public void formatAccessPointThreeTest() {
653 String bandwidth = intentInstanceService.formatAccessPoint("Access three");
654 assertEquals(bandwidth, "tranportEp_src_ID_113_1");
658 public void addCustomerTest() throws IOException {
660 Call mockCall = PowerMockito.mock(Call.class);
661 Response<Object> response = Response.error(404, new ResponseBody() {
664 public MediaType contentType() {
669 public long contentLength() {
674 public BufferedSource source() {
678 when(intentApiService.queryCustomer(anyString())).thenReturn(mockCall);
679 when(mockCall.execute()).thenReturn(response);
681 Properties properties = new Properties();
682 properties.put("ccvpn.globalCustomerId", "IBNCustomer");
683 properties.put("ccvpn.subscriberName", "IBNCustomer");
684 properties.put("ccvpn.subscriberType", "INFRA");
685 properties.put("ccvpn.serviceType", "IBN");
686 IntentInstanceServiceImpl spy = spy(intentInstanceService);
687 doReturn(properties).when(spy).getProperties();
689 Call mockCall2 = PowerMockito.mock(Call.class);
690 when(intentApiService.addCustomer(anyString(),any())).thenReturn(mockCall2);
693 Mockito.verify(intentApiService,Mockito.times(1)).addCustomer(anyString(),any());
698 public void addSubscriptionTest() throws IOException {
700 Call mockCall = PowerMockito.mock(Call.class);
701 Response<Object> response = Response.error(404, new ResponseBody() {
704 public MediaType contentType() {
709 public long contentLength() {
714 public BufferedSource source() {
718 when(intentApiService.querySubscription(anyString(),anyString())).thenReturn(mockCall);
719 when(mockCall.execute()).thenReturn(response);
721 Properties properties = new Properties();
722 properties.put("ccvpn.globalCustomerId", "IBNCustomer");
723 properties.put("ccvpn.subscriberName", "IBNCustomer");
724 properties.put("ccvpn.subscriberType", "INFRA");
725 properties.put("ccvpn.serviceType", "IBN");
726 IntentInstanceServiceImpl spy = spy(intentInstanceService);
727 doReturn(properties).when(spy).getProperties();
729 Call mockCall2 = PowerMockito.mock(Call.class);
730 when(intentApiService.addSubscription(anyString(),anyString(),any())).thenReturn(mockCall2);
732 spy.addSubscription();
733 Mockito.verify(intentApiService,Mockito.times(1)).addSubscription(anyString(),anyString(),any());
737 public void saveIntentInstanceToAAITest() throws IOException {
738 IntentInstanceServiceImpl spy = spy(intentInstanceService);
739 doNothing().when(spy).addCustomer();
740 doNothing().when(spy).addSubscription();
742 Properties properties = new Properties();
743 properties.put("ccvpn.globalCustomerId", "IBNCustomer");
744 properties.put("ccvpn.subscriberName", "IBNCustomer");
745 properties.put("ccvpn.subscriberType", "INFRA");
746 properties.put("ccvpn.serviceType", "IBN");
747 doReturn(properties).when(spy).getProperties();
749 JSONObject body = new JSONObject();
750 body.put("resource-version",123);
751 Call mockCall = PowerMockito.mock(Call.class);
752 Response<JSONObject> response = Response.success(body);
753 when(intentApiService.queryServiceInstance(anyString(),anyString(),anyString())).thenReturn(mockCall);
754 when(mockCall.execute()).thenReturn(response);
756 CCVPNInstance instance = new CCVPNInstance();
757 instance.setName("name");
758 instance.setInstanceId("id");
760 Call mockCall2 = PowerMockito.mock(Call.class);
761 Response<JSONObject> response2 = Response.success(body);
762 when(intentApiService.saveServiceInstance(anyString(),anyString(),anyString(),any())).thenReturn(mockCall2);
763 when(mockCall2.execute()).thenReturn(response2);
765 spy.saveIntentInstanceToAAI("CCVPN-id",instance);
766 Mockito.verify(intentApiService, Mockito.times(1)).saveServiceInstance(anyString(),anyString(),anyString(),any());
770 public void deleteIntentInstanceToAAITest() throws IOException {
771 IntentInstanceServiceImpl spy = spy(intentInstanceService);
772 doNothing().when(spy).addCustomer();
773 doNothing().when(spy).addSubscription();
775 Properties properties = new Properties();
776 properties.put("ccvpn.globalCustomerId", "IBNCustomer");
777 properties.put("ccvpn.subscriberName", "IBNCustomer");
778 properties.put("ccvpn.subscriberType", "INFRA");
779 properties.put("ccvpn.serviceType", "IBN");
780 doReturn(properties).when(spy).getProperties();
782 JSONObject body = new JSONObject();
783 body.put("resource-version",123);
784 Call mockCall = PowerMockito.mock(Call.class);
785 Response<JSONObject> response = Response.success(body);
786 when(intentApiService.queryServiceInstance(anyString(),anyString(),anyString())).thenReturn(mockCall);
787 when(mockCall.execute()).thenReturn(response);
789 Call mockCall2 = PowerMockito.mock(Call.class);
790 Response<JSONObject> response2 = Response.success(body);
791 when(intentApiService.deleteServiceInstance(anyString(),anyString(),anyString(),anyString())).thenReturn(mockCall2);
792 when(mockCall2.execute()).thenReturn(response2);
794 spy.deleteIntentInstanceToAAI("CCVPN-id");
795 Mockito.verify(intentApiService, Mockito.times(1)).deleteServiceInstance(anyString(),anyString(),anyString(),any());
799 public void createIntentInstanceWithCCVPNInstanceTest() {
800 Map<String, Object> body = new HashMap<>();
801 body.put("intentContent", "this is intent content");
802 body.put("name", "this is name");
803 Transaction tx = PowerMockito.mock(Transaction.class);
804 when(session.beginTransaction()).thenReturn(tx);
806 Serializable save = Mockito.mock(Serializable.class);
807 when(session.save(any(IntentInstance.class))).thenReturn(save);
809 doNothing().when(tx).commit();
810 doNothing().when(session).close();
811 IntentInstance instance = intentInstanceService.createIntentInstance(body, "id", "name", IntentConstant.MODEL_TYPE_CCVPN);
812 assertEquals(instance.getBusinessInstanceId(), "id");
815 public void createIntentInstanceWithSlicingInstanceTest() {
816 Map<String, Object> slicingOrderInfo = new HashMap<>();
817 slicingOrderInfo.put("intentContent", "this is intent content");
818 slicingOrderInfo.put("name", "this is name");
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);
825 Serializable save = Mockito.mock(Serializable.class);
826 when(session.save(any(IntentInstance.class))).thenReturn(save);
828 doNothing().when(tx).commit();
829 doNothing().when(session).close();
830 IntentInstance instance = intentInstanceService.createIntentInstance(body, "id", "name", IntentConstant.MODEL_TYPE_5GS);
831 assertEquals(instance.getBusinessInstanceId(), "id");
834 public void createIntentInstanceWithThrowErrorTest() {
835 Map<String, Object> slicingOrderInfo = new HashMap<>();
836 slicingOrderInfo.put("intentContent", "this is intent content");
837 slicingOrderInfo.put("name", "this is name");
839 Map<String, Object> body = new HashMap<>();
840 body.put("slicing_order_info", slicingOrderInfo);
841 Transaction tx = PowerMockito.mock(Transaction.class);
842 when(session.beginTransaction()).thenReturn(tx);
844 when(session.save(any(IntentInstance.class))).thenThrow(new RuntimeException());
846 doNothing().when(session).close();
847 IntentInstance instance = intentInstanceService.createIntentInstance(body, "id", "name", IntentConstant.MODEL_TYPE_5GS);
848 assertNull(instance);
852 public void deleteIntentWithDeleteCCVPNInstanceTest() {
854 IntentInstanceServiceImpl spy = spy(intentInstanceService);
856 IntentInstance instance = new IntentInstance();
858 instance.setIntentSource(IntentConstant.MODEL_TYPE_CCVPN);
859 instance.setBusinessInstanceId("1");
861 Query query = PowerMockito.mock(Query.class);
862 when(session.createQuery(anyString())).thenReturn(query);
863 when(query.setParameter("id", 1)).thenReturn(query);
864 when(query.uniqueResult()).thenReturn(instance);
866 doNothing().when(spy).deleteIntentInstance(anyString());
868 Transaction tx = PowerMockito.mock(Transaction.class);
869 when(session.beginTransaction()).thenReturn(tx);
870 doNothing().when(session).delete(any());
871 doNothing().when(tx).commit();
872 doNothing().when(session).close();
876 Mockito.verify(spy, Mockito.times(1)).deleteIntentInstance("1");
880 public void deleteIntentWithDeleteSlicingInstanceTest() {
883 IntentInstance instance = new IntentInstance();
885 instance.setIntentSource(IntentConstant.MODEL_TYPE_5GS);
886 instance.setBusinessInstanceId("1");
888 Query query = PowerMockito.mock(Query.class);
889 when(session.createQuery(anyString())).thenReturn(query);
890 when(query.setParameter("id", 1)).thenReturn(query);
891 when(query.uniqueResult()).thenReturn(instance);
893 ServiceResult serviceResult = new ServiceResult();
894 when(resourceMgtService.terminateSlicingService(anyString())).thenReturn(serviceResult);
896 Transaction tx = PowerMockito.mock(Transaction.class);
897 when(session.beginTransaction()).thenReturn(tx);
898 doNothing().when(session).delete(any());
899 doNothing().when(tx).commit();
900 doNothing().when(session).close();
902 intentInstanceService.deleteIntent(1);
904 Mockito.verify(resourceMgtService, Mockito.times(1)).terminateSlicingService(anyString());
907 public void deleteIntentWithThrowErrorTest() {
910 IntentInstance instance = new IntentInstance();
912 instance.setIntentSource(IntentConstant.MODEL_TYPE_5GS);
913 instance.setBusinessInstanceId("1");
915 when(session.createQuery(anyString())).thenThrow(new RuntimeException());
917 doNothing().when(session).close();
919 intentInstanceService.deleteIntent(1);
921 Mockito.verify(resourceMgtService, Mockito.times(0)).terminateSlicingService(anyString());
925 public void getIntentInstanceListTest() {
926 IntentInstanceServiceImpl spy = spy(intentInstanceService);
927 doReturn(2).when(spy).getIntentInstanceAllCount();
929 Query query = PowerMockito.mock(Query.class);
930 when(session.createQuery("from IntentInstance order by id")).thenReturn(query);
931 when(query.setFirstResult(anyInt())).thenReturn(query);
932 when(query.setMaxResults(anyInt())).thenReturn(query);
934 List<IntentInstance> list = new ArrayList<>();
935 list.add(new IntentInstance());
936 list.add(new IntentInstance());
937 when(query.list()).thenReturn(list);
938 doNothing().when(session).close();
939 int totalRecords = spy.getIntentInstanceList(1, 10).getTotalRecords();
940 assertEquals(totalRecords,2);
944 public void getIntentInstanceListThrowErrorTest() {
945 IntentInstanceServiceImpl spy = spy(intentInstanceService);
946 doReturn(2).when(spy).getIntentInstanceAllCount();
948 when(session.createQuery("from IntentInstance order by id")).thenThrow(new RuntimeException());
949 doNothing().when(session).close();
950 assertEquals(spy.getIntentInstanceList(1, 10),null);
954 public void createSlicingServiceWithIntent() {
955 IntentInstanceServiceImpl spy = spy(intentInstanceService);
957 SlicingOrder slicingOrder = new SlicingOrder();
958 slicingOrder.setSlicing_order_info(new SlicingOrderDetail());
959 slicingOrder.getSlicing_order_info().setName("name");
961 ServiceResult serviceResult = new ServiceResult();
962 ServiceCreateResult serviceCreateResult = new ServiceCreateResult();
963 serviceCreateResult.setService_id("id");
964 serviceResult.setResult_body(serviceCreateResult);
965 when(slicingService.createSlicingService(any())).thenReturn(serviceResult);
967 IntentInstance instance = new IntentInstance();
968 doReturn(instance).when(spy).createIntentInstance(any(),anyString(),anyString(),anyString());
970 assertEquals(spy.createSlicingServiceWithIntent(slicingOrder), serviceResult);
974 public void getIntentInstanceAllCountTest() {
976 Query query = PowerMockito.mock(Query.class);
977 when(session.createQuery("select count(*) from IntentInstance")).thenReturn(query);
978 when(query.uniqueResult()).thenReturn(2L);
981 assertEquals(intentInstanceService.getIntentInstanceAllCount(),2);
985 public void getIntentInstanceAllCountThrowErrorTest() {
987 when(session.createQuery("select count(*) from IntentInstance")).thenThrow(new RuntimeException());
988 assertEquals(intentInstanceService.getIntentInstanceAllCount(),-1);
992 public void updateCCVPNInstanceTest() throws IOException {
993 CCVPNInstance instance = new CCVPNInstance();
994 instance.setInstanceId("1");
995 instance.setAccessPointOneBandWidth(1);
997 CCVPNInstance ccvpnInstance = new CCVPNInstance();
998 ccvpnInstance.setInstanceId(instance.getInstanceId());
1000 Query query = mock(Query.class);
1001 when(session.createQuery("from CCVPNInstance where instanceId = :instanceId")).thenReturn(query);
1002 when(query.setParameter(anyString(), anyString())).thenReturn(query);
1003 when(query.uniqueResult()).thenReturn(ccvpnInstance);
1005 IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
1006 doNothing().when(spy).saveIntentInstanceToAAI(anyString(),any(CCVPNInstance.class));
1008 Transaction tx = Mockito.mock(Transaction.class);
1009 Mockito.when(session.beginTransaction()).thenReturn(tx);
1010 doNothing().when(session).update(ccvpnInstance);
1011 Mockito.doNothing().when(tx).commit();
1013 assertEquals(spy.updateCCVPNInstance(instance), 1);