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.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;
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.*;
64 import retrofit2.Call;
65 import retrofit2.Response;
67 import jakarta.annotation.Nullable;
68 import jakarta.annotation.Resource;
70 @RunWith(MockitoJUnitRunner.class)
71 public class IntentInstanceServiceImplTest {
73 public IntentInstanceServiceImplTest() {
77 private IntentInstanceServiceImpl intentInstanceService;
80 private IntentAaiClient intentAaiClient;
83 private IntentSoService intentSoService;
86 private IntentProperties intentProperties;
89 @Resource(name = "ResourceMgtService")
90 private ResourceMgtService resourceMgtService;
93 @Resource(name = "SlicingService")
94 private SlicingService slicingService;
97 private SessionFactory sessionFactory;
100 private Session session;
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);
111 when(intentProperties.getGlobalCustomerId()).thenReturn("someCustomer");
112 when(intentProperties.getSubscriberName()).thenReturn("someSubscriber");
113 when(intentProperties.getSubscriberType()).thenReturn("someSubscriberType");
118 public void queryIntentInstanceTest() {
119 CCVPNInstance instance = new CCVPNInstance();
120 instance.setInstanceId("1");
121 instance.setJobId("1");
122 instance.setStatus("1");
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());
133 public void queryIntentInstanceGetCountErrorTest() {
134 CCVPNInstance instance = new CCVPNInstance();
135 instance.setInstanceId("1");
136 instance.setJobId("1");
137 instance.setStatus("1");
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());
148 public void queryIntentInstanceThrowErrorTest() {
149 CCVPNInstance instance = new CCVPNInstance();
150 instance.setInstanceId("1");
151 instance.setJobId("1");
152 instance.setStatus("1");
154 when(session.createQuery(anyString())).thenThrow(new RuntimeException());
156 assertEquals(intentInstanceService.queryIntentInstance(instance,1,2), null);
159 public void createCCVPNInstanceTest() throws IOException {
160 CCVPNInstance instance = new CCVPNInstance();
161 instance.setInstanceId("1");
162 instance.setJobId("1");
163 instance.setStatus("1");
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);
171 IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
172 doNothing().when(spy).saveIntentInstanceToAAI(isNull(),any(CCVPNInstance.class));
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();
180 assertEquals(spy.createCCVPNInstance(instance), 1);
184 public void createCCVPNInstanceThrowErrorTest() throws IOException {
185 CCVPNInstance instance = new CCVPNInstance();
186 instance.setInstanceId("1");
187 instance.setJobId("1");
188 instance.setStatus("1");
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);
196 IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
197 doThrow(new RuntimeException()).when(spy).saveIntentInstanceToAAI(isNull(),any(CCVPNInstance.class));
199 Transaction tx = Mockito.mock(Transaction.class);
200 Serializable save = Mockito.mock(Serializable.class);
201 assertEquals(spy.createCCVPNInstance(instance), 0);
205 public void createCCVPNInstanceInstanceIsNullTest() throws IOException {
206 assertEquals(intentInstanceService.createCCVPNInstance(null), 0);
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);
217 public void getIntentInstanceProgressTest() throws IOException {
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");
227 when(query1.list()).thenReturn(q);
229 OperationProgressInformation operationProgressInformation = new OperationProgressInformation();
230 OperationProgress operationProgress = new OperationProgress();
231 operationProgress.setProgress(100);
232 operationProgressInformation.setOperationStatus(operationProgress);
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);
243 IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
244 doNothing().when(spy).saveIntentInstanceToAAI(anyString(),any(CCVPNInstance.class));
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();
251 spy.getIntentInstanceProgress();
252 assertEquals(operation.getString("progress"),"100");
255 public void getIntentInstanceCreateStatusTest() throws IOException {
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");
265 when(query1.list()).thenReturn(q);
267 OperationProgressInformation operationProgressInformation = new OperationProgressInformation();
268 OperationProgress operationProgress = new OperationProgress();
269 operationProgress.setProgress(100);
270 operationProgressInformation.setOperationStatus(operationProgress);
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);
279 IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
280 doNothing().when(spy).saveIntentInstanceToAAI(anyString(),any(CCVPNInstance.class));
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();
287 spy.getIntentInstanceCreateStatus();
288 assertEquals(jsonObject.getString("orchestration-status"),"created");
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());
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");
308 when(query1.list()).thenReturn(q);
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" +
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" +
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" +
336 " \"relationship-key\":\"logical-link.link-name\",\n" +
337 " \"relationship-value\":\"tranportEp_UNI_ID_311_1\"\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" +
347 " \"relationship-key\":\"network-policy.network-policy-id\",\n" +
348 " \"relationship-value\":\"de00a0a0-be2e-4d19-974a-80a2bca6bdf9\"\n" +
351 " \"related-to-property\":[\n" +
353 " \"property-key\":\"network-policy.network-policy-fqdn\",\n" +
354 " \"property-value\":\"cll-101\"\n" +
364 Response<JSONObject> response = Response.success(jsonObject);
365 Mockito.when(intentAaiClient.getInstanceNetworkInfo(any())).thenReturn(mockCall);
366 Mockito.when(mockCall.execute()).thenReturn(response);
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" +
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" +
385 " \"relationship-key\":\"customer.global-customer-id\",\n" +
386 " \"relationship-value\":\"IBNCustomer\"\n" +
389 " \"relationship-key\":\"service-subscription.service-type\",\n" +
390 " \"relationship-value\":\"IBN\"\n" +
393 " \"relationship-key\":\"service-instance.service-instance-id\",\n" +
394 " \"relationship-value\":\"cll-101\"\n" +
397 " \"relationship-key\":\"allotted-resource.id\",\n" +
398 " \"relationship-value\":\"cll-101-network-001\"\n" +
401 " \"related-to-property\":[\n" +
403 " \"property-key\":\"allotted-resource.description\"\n" +
406 " \"property-key\":\"allotted-resource.allotted-resource-name\",\n" +
407 " \"property-value\":\"network_cll-101-network-001\"\n" +
414 Response<JSONObject> response1 = Response.success(jsonObject1);
415 Mockito.when(intentAaiClient.getInstanceNetworkPolicyInfo(any())).thenReturn(mockCall1);
416 Mockito.when(mockCall1.execute()).thenReturn(response1);
418 Call mockCall2 = PowerMockito.mock(Call.class);
419 JSONObject jsonObject2 = JSONObject.parseObject("{\n" +
420 " \"metadatum\":[\n" +
422 " \"metaname\":\"ethernet-uni-id-1\",\n" +
423 " \"metaval\":\"1234\",\n" +
424 " \"resource-version\":\"1629409084707\"\n" +
427 " \"metaname\":\"ethernet-uni-id-2\",\n" +
428 " \"metaval\":\"5678\",\n" +
429 " \"resource-version\":\"1629409204904\"\n" +
433 Response<JSONObject> response2 = Response.success(jsonObject2);
434 Mockito.when(intentAaiClient.getInstanceBandwidth(any())).thenReturn(mockCall2);
435 Mockito.when(mockCall2.execute()).thenReturn(response2);
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();
443 intentInstanceService.getIntentInstanceBandwidth();
447 public void deleteIntentInstance() throws IOException {
448 CCVPNInstance instance = new CCVPNInstance();
449 instance.setResourceInstanceId("1");
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);
456 Call mockCall = PowerMockito.mock(Call.class);
457 when(intentSoService.deleteIntentInstance(any())).thenReturn(mockCall);
458 when(mockCall.execute()).thenReturn(null);
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();
466 IntentInstanceServiceImpl spy = spy(intentInstanceService);
467 doNothing().when(spy).deleteIntentInstanceToAAI(anyString());
469 spy.deleteIntentInstance("1");
474 public void invalidIntentInstanceTest() throws IOException {
475 CCVPNInstance instance = new CCVPNInstance();
476 instance.setResourceInstanceId("1");
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);
483 Call mockCall = PowerMockito.mock(Call.class);
484 when(intentSoService.deleteIntentInstance(any())).thenReturn(mockCall);
485 when(mockCall.execute()).thenReturn(null);
487 Transaction tx = PowerMockito.mock(Transaction.class);
488 when(session.beginTransaction()).thenReturn(tx);
489 doNothing().when(tx).commit();
491 intentInstanceService.invalidIntentInstance("1");
494 public void queryInstancePerformanceDataTest() throws IOException {
495 CCVPNInstance instance = new CCVPNInstance();
496 instance.setResourceInstanceId("1");
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<>();
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);
511 intentInstanceService.queryInstancePerformanceData("1");
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");
524 public void activeIntentInstance() throws IOException {
525 CCVPNInstance instance = new CCVPNInstance();
526 instance.setInstanceId("1");
527 instance.setJobId("1");
528 instance.setStatus("1");
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);
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);
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();
548 intentInstanceService.activeIntentInstance("1");
553 public void queryAccessNodeInfo() throws IOException {
555 Call mockCall = PowerMockito.mock(Call.class);
556 JSONObject body = JSONObject.parseObject("{\n" +
557 " \"network-route\": [\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" +
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" +
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" +
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" +
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);
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);
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);
625 JSONObject instanceStatus = intentInstanceService.getInstanceStatus(new JSONArray());
626 assertEquals(instanceStatus.getJSONArray("IntentInstances").getJSONObject(0).getString("id"), "id1");
629 public void formatBandwidthTest() {
631 String bandwidth = intentInstanceService.formatBandwidth("2Gbps");
632 assertEquals(bandwidth, "2000");
635 public void formatCloudPointTest() {
637 String bandwidth = intentInstanceService.formatCloudPoint("Cloud one");
638 assertEquals(bandwidth, "tranportEp_dst_ID_212_1");
641 public void formatAccessPointOneTest() {
642 String bandwidth = intentInstanceService.formatAccessPoint("Access one");
643 assertEquals(bandwidth, "tranportEp_src_ID_111_1");
646 public void formatAccessPointTwoTest() {
647 String bandwidth = intentInstanceService.formatAccessPoint("Access two");
648 assertEquals(bandwidth, "tranportEp_src_ID_111_2");
651 public void formatAccessPointThreeTest() {
652 String bandwidth = intentInstanceService.formatAccessPoint("Access three");
653 assertEquals(bandwidth, "tranportEp_src_ID_113_1");
657 public void addCustomerTest() throws IOException {
659 Call mockCall = PowerMockito.mock(Call.class);
660 Response<Object> response = Response.error(404, new ResponseBody() {
663 public MediaType contentType() {
668 public long contentLength() {
673 public BufferedSource source() {
678 when(intentAaiClient.queryCustomer(anyString())).thenReturn(mockCall);
679 when(intentAaiClient.addCustomer(anyString(), any())).thenReturn(mockCall);
680 when(mockCall.execute()).thenReturn(response);
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();
691 Mockito.verify(intentAaiClient,Mockito.times(1)).addCustomer(anyString(),any());
696 public void addSubscriptionTest() throws IOException {
698 Call mockCall = PowerMockito.mock(Call.class);
699 Response<Object> response = Response.error(404, new ResponseBody() {
702 public MediaType contentType() {
707 public long contentLength() {
712 public BufferedSource source() {
716 when(intentProperties.getServiceType()).thenReturn("someServiceType");
717 when(intentAaiClient.querySubscription(anyString(),anyString())).thenReturn(mockCall);
718 when(mockCall.execute()).thenReturn(response);
720 IntentInstanceServiceImpl spy = spy(intentInstanceService);
722 Call mockCall2 = PowerMockito.mock(Call.class);
723 when(intentAaiClient.addSubscription(anyString(),anyString(),any())).thenReturn(mockCall2);
725 spy.addSubscription();
726 Mockito.verify(intentAaiClient,Mockito.times(1)).addSubscription(anyString(),anyString(),any());
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();
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);
743 CCVPNInstance instance = new CCVPNInstance();
744 instance.setName("name");
745 instance.setInstanceId("id");
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);
752 spy.saveIntentInstanceToAAI("CCVPN-id",instance);
753 Mockito.verify(intentAaiClient, Mockito.times(1)).saveServiceInstance(anyString(),anyString(),anyString(),any());
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();
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);
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);
775 spy.deleteIntentInstanceToAAI("CCVPN-id");
776 Mockito.verify(intentAaiClient, Mockito.times(1)).deleteServiceInstance(anyString(),anyString(),anyString(),any());
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);
787 Serializable save = Mockito.mock(Serializable.class);
788 when(session.save(any(IntentInstance.class))).thenReturn(save);
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");
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");
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);
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_5GS);
812 assertEquals(instance.getBusinessInstanceId(), "id");
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");
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 when(session.save(any(IntentInstance.class))).thenThrow(new RuntimeException());
827 doNothing().when(session).close();
828 IntentInstance instance = intentInstanceService.createIntentInstance(body, "id", "name", IntentConstant.MODEL_TYPE_5GS);
829 assertNull(instance);
833 public void deleteIntentWithDeleteCCVPNInstanceTest() {
835 IntentInstanceServiceImpl spy = spy(intentInstanceService);
837 IntentInstance instance = new IntentInstance();
839 instance.setIntentSource(IntentConstant.MODEL_TYPE_CCVPN);
840 instance.setBusinessInstanceId("1");
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);
847 doNothing().when(spy).deleteIntentInstance(anyString());
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();
857 Mockito.verify(spy, Mockito.times(1)).deleteIntentInstance("1");
861 public void deleteIntentWithDeleteSlicingInstanceTest() {
864 IntentInstance instance = new IntentInstance();
866 instance.setIntentSource(IntentConstant.MODEL_TYPE_5GS);
867 instance.setBusinessInstanceId("1");
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);
874 ServiceResult serviceResult = new ServiceResult();
875 when(resourceMgtService.terminateSlicingService(anyString())).thenReturn(serviceResult);
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();
883 intentInstanceService.deleteIntent(1);
885 Mockito.verify(resourceMgtService, Mockito.times(1)).terminateSlicingService(anyString());
888 public void deleteIntentWithThrowErrorTest() {
891 IntentInstance instance = new IntentInstance();
893 instance.setIntentSource(IntentConstant.MODEL_TYPE_5GS);
894 instance.setBusinessInstanceId("1");
896 when(session.createQuery(anyString())).thenThrow(new RuntimeException());
898 doNothing().when(session).close();
900 intentInstanceService.deleteIntent(1);
902 Mockito.verify(resourceMgtService, Mockito.times(0)).terminateSlicingService(anyString());
906 public void getIntentInstanceListTest() {
907 IntentInstanceServiceImpl spy = spy(intentInstanceService);
908 doReturn(2).when(spy).getIntentInstanceAllCount();
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);
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);
925 public void getIntentInstanceListThrowErrorTest() {
926 IntentInstanceServiceImpl spy = spy(intentInstanceService);
927 doReturn(2).when(spy).getIntentInstanceAllCount();
929 when(session.createQuery("from IntentInstance order by id")).thenThrow(new RuntimeException());
930 doNothing().when(session).close();
931 assertEquals(spy.getIntentInstanceList(1, 10),null);
935 public void createSlicingServiceWithIntent() throws IOException {
936 IntentInstanceServiceImpl spy = spy(intentInstanceService);
938 SlicingOrder slicingOrder = new SlicingOrder();
939 slicingOrder.setSlicing_order_info(new SlicingOrderDetail());
940 slicingOrder.getSlicing_order_info().setName("name");
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));
951 public void getIntentInstanceAllCountTest() {
953 Query query = PowerMockito.mock(Query.class);
954 when(session.createQuery("select count(*) from IntentInstance")).thenReturn(query);
955 when(query.uniqueResult()).thenReturn(2L);
958 assertEquals(intentInstanceService.getIntentInstanceAllCount(),2);
962 public void getIntentInstanceAllCountThrowErrorTest() {
964 when(session.createQuery("select count(*) from IntentInstance")).thenThrow(new RuntimeException());
965 assertEquals(intentInstanceService.getIntentInstanceAllCount(),-1);
969 public void updateCCVPNInstanceTest() throws IOException {
970 CCVPNInstance instance = new CCVPNInstance();
971 instance.setInstanceId("1");
972 instance.setAccessPointOneBandWidth(1);
974 CCVPNInstance ccvpnInstance = new CCVPNInstance();
975 ccvpnInstance.setInstanceId(instance.getInstanceId());
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);
982 IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
983 doNothing().when(spy).saveIntentInstanceToAAI(anyString(),any(CCVPNInstance.class));
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();
990 assertEquals(spy.updateCCVPNInstance(instance), 1);