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.IntentApiService;
50 import org.onap.usecaseui.server.service.lcm.domain.so.SOService;
51 import org.onap.usecaseui.server.service.lcm.domain.so.bean.OperationProgress;
52 import org.onap.usecaseui.server.service.lcm.domain.so.bean.OperationProgressInformation;
53 import org.onap.usecaseui.server.service.nsmf.ResourceMgtService;
54 import org.powermock.api.mockito.PowerMockito;
55 import org.powermock.api.support.membermodification.MemberModifier;
57 import static org.junit.Assert.*;
58 import static org.mockito.ArgumentMatchers.*;
59 import static org.mockito.Mockito.mock;
60 import static org.mockito.Mockito.when;
61 import static org.powermock.api.mockito.PowerMockito.*;
63 import retrofit2.Call;
64 import retrofit2.Response;
66 import jakarta.annotation.Nullable;
67 import jakarta.annotation.Resource;
69 @RunWith(MockitoJUnitRunner.class)
70 public class IntentInstanceServiceImplTest {
72 public IntentInstanceServiceImplTest() {
76 private IntentInstanceServiceImpl intentInstanceService;
79 private IntentApiService intentApiService;
82 private SOService soService;
85 @Resource(name = "ResourceMgtService")
86 private ResourceMgtService resourceMgtService;
89 @Resource(name = "SlicingService")
90 private SlicingService slicingService;
93 private SessionFactory sessionFactory;
96 private Session session;
99 public void before() throws Exception {
100 MemberModifier.field(IntentInstanceServiceImpl.class, "sessionFactory").set(intentInstanceService , sessionFactory);
101 MemberModifier.field(IntentInstanceServiceImpl.class, "resourceMgtService").set(intentInstanceService , resourceMgtService);
102 MemberModifier.field(IntentInstanceServiceImpl.class, "slicingService").set(intentInstanceService , slicingService);
103 MemberModifier.field(IntentInstanceServiceImpl.class, "intentApiService").set(intentInstanceService , intentApiService);
104 when(sessionFactory.openSession()).thenReturn(session);
108 public void queryIntentInstanceTest() {
109 CCVPNInstance instance = new CCVPNInstance();
110 instance.setInstanceId("1");
111 instance.setJobId("1");
112 instance.setStatus("1");
114 Query query = Mockito.mock(Query.class);
115 when(session.createQuery(anyString())).thenReturn(query);
116 List<IntentModel> list = new ArrayList<>();
117 when(query.list()).thenReturn(list);
118 when(query.uniqueResult()).thenReturn(10L);
119 assertTrue(intentInstanceService.queryIntentInstance(instance,1,2).getList().isEmpty());
123 public void queryIntentInstanceGetCountErrorTest() {
124 CCVPNInstance instance = new CCVPNInstance();
125 instance.setInstanceId("1");
126 instance.setJobId("1");
127 instance.setStatus("1");
129 Query query = Mockito.mock(Query.class);
130 when(session.createQuery(anyString())).thenReturn(query);
131 List<IntentModel> list = new ArrayList<>();
132 when(query.list()).thenReturn(list);
133 when(query.uniqueResult()).thenReturn(10);
134 assertTrue(intentInstanceService.queryIntentInstance(instance,1,2).getList().isEmpty());
138 public void queryIntentInstanceThrowErrorTest() {
139 CCVPNInstance instance = new CCVPNInstance();
140 instance.setInstanceId("1");
141 instance.setJobId("1");
142 instance.setStatus("1");
144 when(session.createQuery(anyString())).thenThrow(new RuntimeException());
146 assertEquals(intentInstanceService.queryIntentInstance(instance,1,2), null);
149 public void createCCVPNInstanceTest() throws IOException {
150 CCVPNInstance instance = new CCVPNInstance();
151 instance.setInstanceId("1");
152 instance.setJobId("1");
153 instance.setStatus("1");
155 Call mockCall = PowerMockito.mock(Call.class);
156 JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}");
157 Response<JSONObject> response = Response.success(body);
158 Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall);
159 Mockito.when(mockCall.execute()).thenReturn(response);
161 IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
162 doNothing().when(spy).saveIntentInstanceToAAI(isNull(),any(CCVPNInstance.class));
164 Transaction tx = Mockito.mock(Transaction.class);
165 Mockito.when(session.beginTransaction()).thenReturn(tx);
166 Serializable save = Mockito.mock(Serializable.class);
167 Mockito.when(session.save(any())).thenReturn(save);
168 Mockito.doNothing().when(tx).commit();
170 assertEquals(spy.createCCVPNInstance(instance), 1);
174 public void createCCVPNInstanceThrowErrorTest() throws IOException {
175 CCVPNInstance instance = new CCVPNInstance();
176 instance.setInstanceId("1");
177 instance.setJobId("1");
178 instance.setStatus("1");
180 Call mockCall = PowerMockito.mock(Call.class);
181 JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}");
182 Response<JSONObject> response = Response.success(body);
183 Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall);
184 Mockito.when(mockCall.execute()).thenReturn(response);
186 IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
187 doThrow(new RuntimeException()).when(spy).saveIntentInstanceToAAI(isNull(),any(CCVPNInstance.class));
189 Transaction tx = Mockito.mock(Transaction.class);
190 Serializable save = Mockito.mock(Serializable.class);
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.doNothing().when(tx).commit();
241 spy.getIntentInstanceProgress();
242 assertEquals(operation.getString("progress"),"100");
245 public void getIntentInstanceCreateStatusTest() throws IOException {
247 Query query1 = Mockito.mock(Query.class);
248 when(session.createQuery("from CCVPNInstance where deleteState = 0 and status = '0'")).thenReturn(query1);
249 List<CCVPNInstance> q = new ArrayList<>();
250 CCVPNInstance instance = new CCVPNInstance();
251 instance.setInstanceId("1");
252 instance.setResourceInstanceId("1");
253 instance.setJobId("1");
255 when(query1.list()).thenReturn(q);
257 OperationProgressInformation operationProgressInformation = new OperationProgressInformation();
258 OperationProgress operationProgress = new OperationProgress();
259 operationProgress.setProgress(100);
260 operationProgressInformation.setOperationStatus(operationProgress);
262 JSONObject jsonObject = new JSONObject();
263 jsonObject.put("orchestration-status", "created");
264 Call mockCall = PowerMockito.mock(Call.class);
265 Response<JSONObject> response = Response.success(jsonObject);
266 Mockito.when(intentApiService.getInstanceInfo(anyString())).thenReturn(mockCall);
267 Mockito.when(mockCall.execute()).thenReturn(response);
269 IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
270 doNothing().when(spy).saveIntentInstanceToAAI(anyString(),any(CCVPNInstance.class));
272 Transaction tx = Mockito.mock(Transaction.class);
273 Mockito.when(session.beginTransaction()).thenReturn(tx);
274 Serializable save = Mockito.mock(Serializable.class);
275 Mockito.doNothing().when(tx).commit();
277 spy.getIntentInstanceCreateStatus();
278 assertEquals(jsonObject.getString("orchestration-status"),"created");
282 public void getFinishedInstanceInfo() {
283 Query query = Mockito.mock(Query.class);
284 when(session.createQuery(anyString())).thenReturn(query);
285 when(query.list()).thenReturn(new ArrayList());
286 assertTrue(intentInstanceService.getFinishedInstanceInfo().isEmpty());
290 public void getIntentInstanceBandwidth() throws IOException {
291 Query query1 = Mockito.mock(Query.class);
292 when(session.createQuery("from CCVPNInstance where deleteState = 0 and status = '1'")).thenReturn(query1);
293 List<CCVPNInstance> q = new ArrayList<>();
294 CCVPNInstance instance = new CCVPNInstance();
295 instance.setInstanceId("1");
296 instance.setResourceInstanceId("1");
298 when(query1.list()).thenReturn(q);
300 Call mockCall = PowerMockito.mock(Call.class);
301 JSONObject jsonObject = JSONObject.parseObject("{\n" +
302 " \"service-instance-id\":\"cll-101\",\n" +
303 " \"service-instance-name\":\"cloud-leased-line-101\",\n" +
304 " \"service-type\":\"CLL\",\n" +
305 " \"service-role\":\"cll\",\n" +
306 " \"environment-context\":\"cll\",\n" +
307 " \"model-invariant-id\":\"6790ab0e-034f-11eb-adc1-0242ac120002\",\n" +
308 " \"model-version-id\":\"6790ab0e-034f-11eb-adc1-0242ac120002\",\n" +
309 " \"resource-version\":\"1628714665927\",\n" +
310 " \"orchestration-status\":\"created\",\n" +
311 " \"allotted-resources\":{\n" +
312 " \"allotted-resource\":[\n" +
314 " \"id\":\"cll-101-network-001\",\n" +
315 " \"resource-version\":\"1628714665798\",\n" +
316 " \"type\":\"TsciNetwork\",\n" +
317 " \"allotted-resource-name\":\"network_cll-101-network-001\",\n" +
318 " \"relationship-list\":{\n" +
319 " \"relationship\":[\n" +
321 " \"related-to\":\"logical-link\",\n" +
322 " \"relationship-label\":\"org.onap.relationships.inventory.ComposedOf\",\n" +
323 " \"related-link\":\"/aai/v24/network/logical-links/logical-link/tranportEp_UNI_ID_311_1\",\n" +
324 " \"relationship-data\":[\n" +
326 " \"relationship-key\":\"logical-link.link-name\",\n" +
327 " \"relationship-value\":\"tranportEp_UNI_ID_311_1\"\n" +
332 " \"related-to\":\"network-policy\",\n" +
333 " \"relationship-label\":\"org.onap.relationships.inventory.Uses\",\n" +
334 " \"related-link\":\"/aai/v24/network/network-policies/network-policy/de00a0a0-be2e-4d19-974a-80a2bca6bdf9\",\n" +
335 " \"relationship-data\":[\n" +
337 " \"relationship-key\":\"network-policy.network-policy-id\",\n" +
338 " \"relationship-value\":\"de00a0a0-be2e-4d19-974a-80a2bca6bdf9\"\n" +
341 " \"related-to-property\":[\n" +
343 " \"property-key\":\"network-policy.network-policy-fqdn\",\n" +
344 " \"property-value\":\"cll-101\"\n" +
354 Response<JSONObject> response = Response.success(jsonObject);
355 Mockito.when(intentApiService.getInstanceNetworkInfo(any())).thenReturn(mockCall);
356 Mockito.when(mockCall.execute()).thenReturn(response);
358 Call mockCall1 = PowerMockito.mock(Call.class);
359 JSONObject jsonObject1 = JSONObject.parseObject("{\n" +
360 " \"network-policy-id\":\"de00a0a0-be2e-4d19-974a-80a2bca6bdf9\",\n" +
361 " \"network-policy-fqdn\":\"cll-101\",\n" +
362 " \"resource-version\":\"1628714665619\",\n" +
363 " \"name\":\"TSCi policy\",\n" +
364 " \"type\":\"SLA\",\n" +
365 " \"latency\":2,\n" +
366 " \"max-bandwidth\":3000,\n" +
367 " \"relationship-list\":{\n" +
368 " \"relationship\":[\n" +
370 " \"related-to\":\"allotted-resource\",\n" +
371 " \"relationship-label\":\"org.onap.relationships.inventory.Uses\",\n" +
372 " \"related-link\":\"/aai/v24/business/customers/customer/IBNCustomer/service-subscriptions/service-subscription/IBN/service-instances/service-instance/cll-101/allotted-resources/allotted-resource/cll-101-network-001\",\n" +
373 " \"relationship-data\":[\n" +
375 " \"relationship-key\":\"customer.global-customer-id\",\n" +
376 " \"relationship-value\":\"IBNCustomer\"\n" +
379 " \"relationship-key\":\"service-subscription.service-type\",\n" +
380 " \"relationship-value\":\"IBN\"\n" +
383 " \"relationship-key\":\"service-instance.service-instance-id\",\n" +
384 " \"relationship-value\":\"cll-101\"\n" +
387 " \"relationship-key\":\"allotted-resource.id\",\n" +
388 " \"relationship-value\":\"cll-101-network-001\"\n" +
391 " \"related-to-property\":[\n" +
393 " \"property-key\":\"allotted-resource.description\"\n" +
396 " \"property-key\":\"allotted-resource.allotted-resource-name\",\n" +
397 " \"property-value\":\"network_cll-101-network-001\"\n" +
404 Response<JSONObject> response1 = Response.success(jsonObject1);
405 Mockito.when(intentApiService.getInstanceNetworkPolicyInfo(any())).thenReturn(mockCall1);
406 Mockito.when(mockCall1.execute()).thenReturn(response1);
408 Call mockCall2 = PowerMockito.mock(Call.class);
409 JSONObject jsonObject2 = JSONObject.parseObject("{\n" +
410 " \"metadatum\":[\n" +
412 " \"metaname\":\"ethernet-uni-id-1\",\n" +
413 " \"metaval\":\"1234\",\n" +
414 " \"resource-version\":\"1629409084707\"\n" +
417 " \"metaname\":\"ethernet-uni-id-2\",\n" +
418 " \"metaval\":\"5678\",\n" +
419 " \"resource-version\":\"1629409204904\"\n" +
423 Response<JSONObject> response2 = Response.success(jsonObject2);
424 Mockito.when(intentApiService.getInstanceBandwidth(any())).thenReturn(mockCall2);
425 Mockito.when(mockCall2.execute()).thenReturn(response2);
427 Transaction tx = Mockito.mock(Transaction.class);
428 Mockito.when(session.beginTransaction()).thenReturn(tx);
429 Serializable save = Mockito.mock(Serializable.class);
430 Mockito.when(session.save(any())).thenReturn(save);
431 Mockito.doNothing().when(tx).commit();
433 intentInstanceService.getIntentInstanceBandwidth();
437 public void deleteIntentInstance() throws IOException {
438 CCVPNInstance instance = new CCVPNInstance();
439 instance.setResourceInstanceId("1");
441 Query query = Mockito.mock(Query.class);
442 when(session.createQuery(anyString())).thenReturn(query);
443 when(query.setParameter(anyString(), anyString())).thenReturn(query);
444 when(query.uniqueResult()).thenReturn(instance);
446 Call mockCall = PowerMockito.mock(Call.class);
447 when(intentApiService.deleteIntentInstance(any())).thenReturn(mockCall);
448 when(mockCall.execute()).thenReturn(null);
450 Transaction tx = PowerMockito.mock(Transaction.class);
451 when(session.beginTransaction()).thenReturn(tx);
452 Serializable save = PowerMockito.mock(Serializable.class);
453 doNothing().when(session).delete(any());
454 doNothing().when(tx).commit();
456 IntentInstanceServiceImpl spy = spy(intentInstanceService);
457 doNothing().when(spy).deleteIntentInstanceToAAI(anyString());
459 spy.deleteIntentInstance("1");
464 public void invalidIntentInstanceTest() throws IOException {
465 CCVPNInstance instance = new CCVPNInstance();
466 instance.setResourceInstanceId("1");
468 Query query = Mockito.mock(Query.class);
469 when(session.createQuery(anyString())).thenReturn(query);
470 when(query.setParameter(anyString(), anyString())).thenReturn(query);
471 when(query.uniqueResult()).thenReturn(instance);
473 Call mockCall = PowerMockito.mock(Call.class);
474 when(intentApiService.deleteIntentInstance(any())).thenReturn(mockCall);
475 when(mockCall.execute()).thenReturn(null);
477 Transaction tx = PowerMockito.mock(Transaction.class);
478 when(session.beginTransaction()).thenReturn(tx);
479 doNothing().when(tx).commit();
481 intentInstanceService.invalidIntentInstance("1");
484 public void queryInstancePerformanceDataTest() throws IOException {
485 CCVPNInstance instance = new CCVPNInstance();
486 instance.setResourceInstanceId("1");
488 InstancePerformance instancePerformance = new InstancePerformance();
489 instancePerformance.setBandwidth(2000);
490 instancePerformance.setMaxBandwidth(20000);
491 instancePerformance.setDate(new Date());
492 Object[] o = {null,instancePerformance};
493 List<Object[]> queryResult= new ArrayList<>();
496 Query query = Mockito.mock(Query.class);
497 when(session.createQuery(anyString())).thenReturn(query);
498 when(query.setParameter(anyString(), anyString())).thenReturn(query);
499 when(query.list()).thenReturn(queryResult);
501 intentInstanceService.queryInstancePerformanceData("1");
507 Call mockCall = PowerMockito.mock(Call.class);
508 Transaction tx = PowerMockito.mock(Transaction.class);
509 Serializable save = PowerMockito.mock(Serializable.class);
510 intentInstanceService.invalidIntentInstance("1");
514 public void activeIntentInstance() throws IOException {
515 CCVPNInstance instance = new CCVPNInstance();
516 instance.setInstanceId("1");
517 instance.setJobId("1");
518 instance.setStatus("1");
520 Query query = Mockito.mock(Query.class);
521 when(session.createQuery(anyString())).thenReturn(query);
522 when(query.setParameter(anyString(), anyString())).thenReturn(query);
523 when(query.uniqueResult()).thenReturn(instance);
526 Call mockCall = PowerMockito.mock(Call.class);
527 JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}");
528 Response<JSONObject> response = Response.success(body);
529 Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall);
530 Mockito.when(mockCall.execute()).thenReturn(response);
532 Transaction tx = Mockito.mock(Transaction.class);
533 Mockito.when(session.beginTransaction()).thenReturn(tx);
534 Serializable save = Mockito.mock(Serializable.class);
535 Mockito.when(session.save(any())).thenReturn(save);
536 Mockito.doNothing().when(tx).commit();
538 intentInstanceService.activeIntentInstance("1");
543 public void queryAccessNodeInfo() throws IOException {
545 Call mockCall = PowerMockito.mock(Call.class);
546 JSONObject body = JSONObject.parseObject("{\n" +
547 " \"network-route\": [\n" +
549 " \"route-id\": \"tranportEp_src_ID_111_1\",\n" +
550 " \"type\": \"LEAF\",\n" +
551 " \"role\": \"3gppTransportEP\",\n" +
552 " \"function\": \"3gppTransportEP\",\n" +
553 " \"ip-address\": \"10.2.3.4\",\n" +
554 " \"prefix-length\": 24,\n" +
555 " \"next-hop\": \"networkId-providerId-10-clientId-0-topologyId-2-nodeId-10.1.1.1-ltpId-1000\",\n" +
556 " \"address-family\": \"ipv4\",\n" +
557 " \"resource-version\": \"1634198223345\"\n" +
560 " \"route-id\": \"tranportEp_src_ID_113_1\",\n" +
561 " \"type\": \"LEAF\",\n" +
562 " \"role\": \"3gppTransportEP\",\n" +
563 " \"function\": \"3gppTransportEP\",\n" +
564 " \"ip-address\": \"10.2.3.4\",\n" +
565 " \"prefix-length\": 24,\n" +
566 " \"next-hop\": \"networkId-providerId-10-clientId-0-topologyId-2-nodeId-10.1.1.3-ltpId-1000\",\n" +
567 " \"address-family\": \"ipv4\",\n" +
568 " \"resource-version\": \"1634198260496\"\n" +
571 " \"route-id\": \"tranportEp_src_ID_111_2\",\n" +
572 " \"type\": \"LEAF\",\n" +
573 " \"role\": \"3gppTransportEP\",\n" +
574 " \"function\": \"3gppTransportEP\",\n" +
575 " \"ip-address\": \"10.2.3.4\",\n" +
576 " \"prefix-length\": 24,\n" +
577 " \"next-hop\": \"networkId-providerId-10-clientId-0-topologyId-2-nodeId-10.1.1.1-ltpId-2000\",\n" +
578 " \"address-family\": \"ipv4\",\n" +
579 " \"resource-version\": \"1634198251534\"\n" +
582 " \"route-id\": \"tranportEp_dst_ID_212_1\",\n" +
583 " \"type\": \"ROOT\",\n" +
584 " \"role\": \"3gppTransportEP\",\n" +
585 " \"function\": \"3gppTransportEP\",\n" +
586 " \"ip-address\": \"10.2.3.4\",\n" +
587 " \"prefix-length\": 24,\n" +
588 " \"next-hop\": \"networkId-providerId-20-clientId-0-topologyId-2-nodeId-10.2.1.2-ltpId-512\",\n" +
589 " \"address-family\": \"ipv4\",\n" +
590 " \"resource-version\": \"1634198274852\"\n" +
594 Response<JSONObject> response = Response.success(body);
595 Mockito.when(intentApiService.queryNetworkRoute()).thenReturn(mockCall);
596 Mockito.when(mockCall.execute()).thenReturn(response);
597 Map<String, Object> result = (Map<String, Object>) intentInstanceService.queryAccessNodeInfo();
598 assertEquals(((List)result.get("accessNodeList")).size(), 3);
602 public void getInstanceStatusTest() {
603 List<CCVPNInstance> queryResult = new ArrayList<>();
604 CCVPNInstance instance = new CCVPNInstance();
605 instance.setInstanceId("id1");
606 instance.setStatus("1");
607 queryResult.add(instance);
609 Query query = Mockito.mock(Query.class);
610 when(session.createQuery(anyString())).thenReturn(query);
611 when(query.setParameter(anyString(), any())).thenReturn(query);
612 when(query.list()).thenReturn(queryResult);
615 JSONObject instanceStatus = intentInstanceService.getInstanceStatus(new JSONArray());
616 assertEquals(instanceStatus.getJSONArray("IntentInstances").getJSONObject(0).getString("id"), "id1");
619 public void formatBandwidthTest() {
621 String bandwidth = intentInstanceService.formatBandwidth("2Gbps");
622 assertEquals(bandwidth, "2000");
625 public void formatCloudPointTest() {
627 String bandwidth = intentInstanceService.formatCloudPoint("Cloud one");
628 assertEquals(bandwidth, "tranportEp_dst_ID_212_1");
631 public void formatAccessPointOneTest() {
632 String bandwidth = intentInstanceService.formatAccessPoint("Access one");
633 assertEquals(bandwidth, "tranportEp_src_ID_111_1");
636 public void formatAccessPointTwoTest() {
637 String bandwidth = intentInstanceService.formatAccessPoint("Access two");
638 assertEquals(bandwidth, "tranportEp_src_ID_111_2");
641 public void formatAccessPointThreeTest() {
642 String bandwidth = intentInstanceService.formatAccessPoint("Access three");
643 assertEquals(bandwidth, "tranportEp_src_ID_113_1");
647 public void addCustomerTest() throws IOException {
649 Call mockCall = PowerMockito.mock(Call.class);
650 Response<Object> response = Response.error(404, new ResponseBody() {
653 public MediaType contentType() {
658 public long contentLength() {
663 public BufferedSource source() {
667 when(intentApiService.queryCustomer(anyString())).thenReturn(mockCall);
668 when(mockCall.execute()).thenReturn(response);
670 Properties properties = new Properties();
671 properties.put("ccvpn.globalCustomerId", "IBNCustomer");
672 properties.put("ccvpn.subscriberName", "IBNCustomer");
673 properties.put("ccvpn.subscriberType", "INFRA");
674 properties.put("ccvpn.serviceType", "IBN");
675 IntentInstanceServiceImpl spy = spy(intentInstanceService);
676 doReturn(properties).when(spy).getProperties();
678 Call mockCall2 = PowerMockito.mock(Call.class);
679 when(intentApiService.addCustomer(anyString(),any())).thenReturn(mockCall2);
682 Mockito.verify(intentApiService,Mockito.times(1)).addCustomer(anyString(),any());
687 public void addSubscriptionTest() throws IOException {
689 Call mockCall = PowerMockito.mock(Call.class);
690 Response<Object> response = Response.error(404, new ResponseBody() {
693 public MediaType contentType() {
698 public long contentLength() {
703 public BufferedSource source() {
707 when(intentApiService.querySubscription(anyString(),anyString())).thenReturn(mockCall);
708 when(mockCall.execute()).thenReturn(response);
710 Properties properties = new Properties();
711 properties.put("ccvpn.globalCustomerId", "IBNCustomer");
712 properties.put("ccvpn.subscriberName", "IBNCustomer");
713 properties.put("ccvpn.subscriberType", "INFRA");
714 properties.put("ccvpn.serviceType", "IBN");
715 IntentInstanceServiceImpl spy = spy(intentInstanceService);
716 doReturn(properties).when(spy).getProperties();
718 Call mockCall2 = PowerMockito.mock(Call.class);
719 when(intentApiService.addSubscription(anyString(),anyString(),any())).thenReturn(mockCall2);
721 spy.addSubscription();
722 Mockito.verify(intentApiService,Mockito.times(1)).addSubscription(anyString(),anyString(),any());
726 public void saveIntentInstanceToAAITest() throws IOException {
727 IntentInstanceServiceImpl spy = spy(intentInstanceService);
728 doNothing().when(spy).addCustomer();
729 doNothing().when(spy).addSubscription();
731 Properties properties = new Properties();
732 properties.put("ccvpn.globalCustomerId", "IBNCustomer");
733 properties.put("ccvpn.subscriberName", "IBNCustomer");
734 properties.put("ccvpn.subscriberType", "INFRA");
735 properties.put("ccvpn.serviceType", "IBN");
736 doReturn(properties).when(spy).getProperties();
738 JSONObject body = new JSONObject();
739 body.put("resource-version",123);
740 Call mockCall = PowerMockito.mock(Call.class);
741 Response<JSONObject> response = Response.success(body);
742 when(intentApiService.queryServiceInstance(anyString(),anyString(),anyString())).thenReturn(mockCall);
743 when(mockCall.execute()).thenReturn(response);
745 CCVPNInstance instance = new CCVPNInstance();
746 instance.setName("name");
747 instance.setInstanceId("id");
749 Call mockCall2 = PowerMockito.mock(Call.class);
750 Response<JSONObject> response2 = Response.success(body);
751 when(intentApiService.saveServiceInstance(anyString(),anyString(),anyString(),any())).thenReturn(mockCall2);
752 when(mockCall2.execute()).thenReturn(response2);
754 spy.saveIntentInstanceToAAI("CCVPN-id",instance);
755 Mockito.verify(intentApiService, Mockito.times(1)).saveServiceInstance(anyString(),anyString(),anyString(),any());
759 public void deleteIntentInstanceToAAITest() throws IOException {
760 IntentInstanceServiceImpl spy = spy(intentInstanceService);
761 doNothing().when(spy).addCustomer();
762 doNothing().when(spy).addSubscription();
764 Properties properties = new Properties();
765 properties.put("ccvpn.globalCustomerId", "IBNCustomer");
766 properties.put("ccvpn.subscriberName", "IBNCustomer");
767 properties.put("ccvpn.subscriberType", "INFRA");
768 properties.put("ccvpn.serviceType", "IBN");
769 doReturn(properties).when(spy).getProperties();
771 JSONObject body = new JSONObject();
772 body.put("resource-version",123);
773 Call mockCall = PowerMockito.mock(Call.class);
774 Response<JSONObject> response = Response.success(body);
775 when(intentApiService.queryServiceInstance(anyString(),anyString(),anyString())).thenReturn(mockCall);
776 when(mockCall.execute()).thenReturn(response);
778 Call mockCall2 = PowerMockito.mock(Call.class);
779 Response<JSONObject> response2 = Response.success(body);
780 when(intentApiService.deleteServiceInstance(anyString(),anyString(),anyString(),anyString())).thenReturn(mockCall2);
781 when(mockCall2.execute()).thenReturn(response2);
783 spy.deleteIntentInstanceToAAI("CCVPN-id");
784 Mockito.verify(intentApiService, Mockito.times(1)).deleteServiceInstance(anyString(),anyString(),anyString(),any());
788 public void createIntentInstanceWithCCVPNInstanceTest() {
789 Map<String, Object> body = new HashMap<>();
790 body.put("intentContent", "this is intent content");
791 body.put("name", "this is name");
792 Transaction tx = PowerMockito.mock(Transaction.class);
793 when(session.beginTransaction()).thenReturn(tx);
795 Serializable save = Mockito.mock(Serializable.class);
796 when(session.save(any(IntentInstance.class))).thenReturn(save);
798 doNothing().when(tx).commit();
799 doNothing().when(session).close();
800 IntentInstance instance = intentInstanceService.createIntentInstance(body, "id", "name", IntentConstant.MODEL_TYPE_CCVPN);
801 assertEquals(instance.getBusinessInstanceId(), "id");
804 public void createIntentInstanceWithSlicingInstanceTest() {
805 Map<String, Object> slicingOrderInfo = new HashMap<>();
806 slicingOrderInfo.put("intentContent", "this is intent content");
807 slicingOrderInfo.put("name", "this is name");
809 Map<String, Object> body = new HashMap<>();
810 body.put("slicing_order_info", slicingOrderInfo);
811 Transaction tx = PowerMockito.mock(Transaction.class);
812 when(session.beginTransaction()).thenReturn(tx);
814 Serializable save = Mockito.mock(Serializable.class);
815 when(session.save(any(IntentInstance.class))).thenReturn(save);
817 doNothing().when(tx).commit();
818 doNothing().when(session).close();
819 IntentInstance instance = intentInstanceService.createIntentInstance(body, "id", "name", IntentConstant.MODEL_TYPE_5GS);
820 assertEquals(instance.getBusinessInstanceId(), "id");
823 public void createIntentInstanceWithThrowErrorTest() {
824 Map<String, Object> slicingOrderInfo = new HashMap<>();
825 slicingOrderInfo.put("intentContent", "this is intent content");
826 slicingOrderInfo.put("name", "this is name");
828 Map<String, Object> body = new HashMap<>();
829 body.put("slicing_order_info", slicingOrderInfo);
830 Transaction tx = PowerMockito.mock(Transaction.class);
831 when(session.beginTransaction()).thenReturn(tx);
833 when(session.save(any(IntentInstance.class))).thenThrow(new RuntimeException());
835 doNothing().when(session).close();
836 IntentInstance instance = intentInstanceService.createIntentInstance(body, "id", "name", IntentConstant.MODEL_TYPE_5GS);
837 assertNull(instance);
841 public void deleteIntentWithDeleteCCVPNInstanceTest() {
843 IntentInstanceServiceImpl spy = spy(intentInstanceService);
845 IntentInstance instance = new IntentInstance();
847 instance.setIntentSource(IntentConstant.MODEL_TYPE_CCVPN);
848 instance.setBusinessInstanceId("1");
850 Query query = PowerMockito.mock(Query.class);
851 when(session.createQuery(anyString())).thenReturn(query);
852 when(query.setParameter("id", 1)).thenReturn(query);
853 when(query.uniqueResult()).thenReturn(instance);
855 doNothing().when(spy).deleteIntentInstance(anyString());
857 Transaction tx = PowerMockito.mock(Transaction.class);
858 when(session.beginTransaction()).thenReturn(tx);
859 doNothing().when(session).delete(any());
860 doNothing().when(tx).commit();
861 doNothing().when(session).close();
865 Mockito.verify(spy, Mockito.times(1)).deleteIntentInstance("1");
869 public void deleteIntentWithDeleteSlicingInstanceTest() {
872 IntentInstance instance = new IntentInstance();
874 instance.setIntentSource(IntentConstant.MODEL_TYPE_5GS);
875 instance.setBusinessInstanceId("1");
877 Query query = PowerMockito.mock(Query.class);
878 when(session.createQuery(anyString())).thenReturn(query);
879 when(query.setParameter("id", 1)).thenReturn(query);
880 when(query.uniqueResult()).thenReturn(instance);
882 ServiceResult serviceResult = new ServiceResult();
883 when(resourceMgtService.terminateSlicingService(anyString())).thenReturn(serviceResult);
885 Transaction tx = PowerMockito.mock(Transaction.class);
886 when(session.beginTransaction()).thenReturn(tx);
887 doNothing().when(session).delete(any());
888 doNothing().when(tx).commit();
889 doNothing().when(session).close();
891 intentInstanceService.deleteIntent(1);
893 Mockito.verify(resourceMgtService, Mockito.times(1)).terminateSlicingService(anyString());
896 public void deleteIntentWithThrowErrorTest() {
899 IntentInstance instance = new IntentInstance();
901 instance.setIntentSource(IntentConstant.MODEL_TYPE_5GS);
902 instance.setBusinessInstanceId("1");
904 when(session.createQuery(anyString())).thenThrow(new RuntimeException());
906 doNothing().when(session).close();
908 intentInstanceService.deleteIntent(1);
910 Mockito.verify(resourceMgtService, Mockito.times(0)).terminateSlicingService(anyString());
914 public void getIntentInstanceListTest() {
915 IntentInstanceServiceImpl spy = spy(intentInstanceService);
916 doReturn(2).when(spy).getIntentInstanceAllCount();
918 Query query = PowerMockito.mock(Query.class);
919 when(session.createQuery("from IntentInstance order by id")).thenReturn(query);
920 when(query.setFirstResult(anyInt())).thenReturn(query);
921 when(query.setMaxResults(anyInt())).thenReturn(query);
923 List<IntentInstance> list = new ArrayList<>();
924 list.add(new IntentInstance());
925 list.add(new IntentInstance());
926 when(query.list()).thenReturn(list);
927 doNothing().when(session).close();
928 int totalRecords = spy.getIntentInstanceList(1, 10).getTotalRecords();
929 assertEquals(totalRecords,2);
933 public void getIntentInstanceListThrowErrorTest() {
934 IntentInstanceServiceImpl spy = spy(intentInstanceService);
935 doReturn(2).when(spy).getIntentInstanceAllCount();
937 when(session.createQuery("from IntentInstance order by id")).thenThrow(new RuntimeException());
938 doNothing().when(session).close();
939 assertEquals(spy.getIntentInstanceList(1, 10),null);
943 public void createSlicingServiceWithIntent() throws IOException {
944 IntentInstanceServiceImpl spy = spy(intentInstanceService);
946 SlicingOrder slicingOrder = new SlicingOrder();
947 slicingOrder.setSlicing_order_info(new SlicingOrderDetail());
948 slicingOrder.getSlicing_order_info().setName("name");
950 ServiceResult serviceResult = new ServiceResult();
951 ServiceCreateResult serviceCreateResult = new ServiceCreateResult();
952 serviceCreateResult.setService_id("id");
953 serviceResult.setResult_body(serviceCreateResult);
954 when(slicingService.createSlicingService(any())).thenReturn(serviceResult);
955 Assert.assertThrows(RuntimeException.class,()->intentInstanceService.createSlicingServiceWithIntent(slicingOrder));
959 public void getIntentInstanceAllCountTest() {
961 Query query = PowerMockito.mock(Query.class);
962 when(session.createQuery("select count(*) from IntentInstance")).thenReturn(query);
963 when(query.uniqueResult()).thenReturn(2L);
966 assertEquals(intentInstanceService.getIntentInstanceAllCount(),2);
970 public void getIntentInstanceAllCountThrowErrorTest() {
972 when(session.createQuery("select count(*) from IntentInstance")).thenThrow(new RuntimeException());
973 assertEquals(intentInstanceService.getIntentInstanceAllCount(),-1);
977 public void updateCCVPNInstanceTest() throws IOException {
978 CCVPNInstance instance = new CCVPNInstance();
979 instance.setInstanceId("1");
980 instance.setAccessPointOneBandWidth(1);
982 CCVPNInstance ccvpnInstance = new CCVPNInstance();
983 ccvpnInstance.setInstanceId(instance.getInstanceId());
985 Query query = mock(Query.class);
986 when(session.createQuery("from CCVPNInstance where instanceId = :instanceId")).thenReturn(query);
987 when(query.setParameter(anyString(), anyString())).thenReturn(query);
988 when(query.uniqueResult()).thenReturn(ccvpnInstance);
990 IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
991 doNothing().when(spy).saveIntentInstanceToAAI(anyString(),any(CCVPNInstance.class));
993 Transaction tx = Mockito.mock(Transaction.class);
994 Mockito.when(session.beginTransaction()).thenReturn(tx);
995 doNothing().when(session).update(ccvpnInstance);
996 Mockito.doNothing().when(tx).commit();
998 assertEquals(spy.updateCCVPNInstance(instance), 1);