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.mockito.junit.MockitoJUnitRunner;
 
  38 import org.onap.usecaseui.server.bean.csmf.ServiceCreateResult;
 
  39 import org.onap.usecaseui.server.bean.csmf.SlicingOrder;
 
  40 import org.onap.usecaseui.server.bean.csmf.SlicingOrderDetail;
 
  41 import org.onap.usecaseui.server.bean.intent.CCVPNInstance;
 
  42 import org.onap.usecaseui.server.bean.intent.InstancePerformance;
 
  43 import org.onap.usecaseui.server.bean.intent.IntentInstance;
 
  44 import org.onap.usecaseui.server.bean.intent.IntentModel;
 
  45 import org.onap.usecaseui.server.bean.nsmf.common.ServiceResult;
 
  46 import org.onap.usecaseui.server.constant.IntentConstant;
 
  47 import org.onap.usecaseui.server.service.csmf.SlicingService;
 
  48 import org.onap.usecaseui.server.service.intent.IntentApiService;
 
  49 import org.onap.usecaseui.server.service.lcm.domain.so.SOService;
 
  50 import org.onap.usecaseui.server.service.lcm.domain.so.bean.OperationProgress;
 
  51 import org.onap.usecaseui.server.service.lcm.domain.so.bean.OperationProgressInformation;
 
  52 import org.onap.usecaseui.server.service.nsmf.ResourceMgtService;
 
  53 import org.powermock.api.mockito.PowerMockito;
 
  54 import org.powermock.api.support.membermodification.MemberModifier;
 
  55 import org.powermock.modules.junit4.PowerMockRunner;
 
  57 import static org.junit.Assert.*;
 
  58 import static org.mockito.ArgumentMatchers.*;
 
  59 import static org.powermock.api.mockito.PowerMockito.*;
 
  61 import retrofit2.Call;
 
  62 import retrofit2.Response;
 
  64 import jakarta.annotation.Nullable;
 
  65 import jakarta.annotation.Resource;
 
  67 @RunWith(MockitoJUnitRunner.class)
 
  68 public class IntentInstanceServiceImplTest {
 
  70     public IntentInstanceServiceImplTest() {
 
  74     private IntentInstanceServiceImpl intentInstanceService;
 
  77     private IntentApiService intentApiService;
 
  80     private SOService soService;
 
  83     @Resource(name = "ResourceMgtService")
 
  84     private ResourceMgtService resourceMgtService;
 
  87     @Resource(name = "SlicingService")
 
  88     private SlicingService slicingService;
 
  91     private SessionFactory sessionFactory;
 
  94     private Session session;
 
  97     public void before() throws Exception {
 
  98         MemberModifier.field(IntentInstanceServiceImpl.class, "sessionFactory").set(intentInstanceService , sessionFactory);
 
  99         MemberModifier.field(IntentInstanceServiceImpl.class, "resourceMgtService").set(intentInstanceService , resourceMgtService);
 
 100         MemberModifier.field(IntentInstanceServiceImpl.class, "slicingService").set(intentInstanceService , slicingService);
 
 101         when(sessionFactory.openSession()).thenReturn(session);
 
 105     public void queryIntentInstanceTest() {
 
 106         CCVPNInstance instance = new CCVPNInstance();
 
 107         instance.setInstanceId("1");
 
 108         instance.setJobId("1");
 
 109         instance.setStatus("1");
 
 111         Query query = Mockito.mock(Query.class);
 
 112         when(session.createQuery(anyString())).thenReturn(query);
 
 113         List<IntentModel> list = new ArrayList<>();
 
 114         when(query.list()).thenReturn(list);
 
 115         when(query.uniqueResult()).thenReturn(10L);
 
 116         assertTrue(intentInstanceService.queryIntentInstance(instance,1,2).getList().isEmpty());
 
 120     public void queryIntentInstanceGetCountErrorTest() {
 
 121         CCVPNInstance instance = new CCVPNInstance();
 
 122         instance.setInstanceId("1");
 
 123         instance.setJobId("1");
 
 124         instance.setStatus("1");
 
 126         Query query = Mockito.mock(Query.class);
 
 127         when(session.createQuery(anyString())).thenReturn(query);
 
 128         List<IntentModel> list = new ArrayList<>();
 
 129         when(query.list()).thenReturn(list);
 
 130         when(query.uniqueResult()).thenReturn(10);
 
 131         assertTrue(intentInstanceService.queryIntentInstance(instance,1,2).getList().isEmpty());
 
 135     public void queryIntentInstanceThrowErrorTest() {
 
 136         CCVPNInstance instance = new CCVPNInstance();
 
 137         instance.setInstanceId("1");
 
 138         instance.setJobId("1");
 
 139         instance.setStatus("1");
 
 141         when(session.createQuery(anyString())).thenThrow(new RuntimeException());
 
 143         assertEquals(intentInstanceService.queryIntentInstance(instance,1,2), null);
 
 146     public void createCCVPNInstanceTest() throws IOException {
 
 147         CCVPNInstance instance = new CCVPNInstance();
 
 148         instance.setInstanceId("1");
 
 149         instance.setJobId("1");
 
 150         instance.setStatus("1");
 
 152         Call mockCall = PowerMockito.mock(Call.class);
 
 153         JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}");
 
 154         Response<JSONObject> response = Response.success(body);
 
 155         Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall);
 
 156         Mockito.when(mockCall.execute()).thenReturn(response);
 
 158         IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
 
 159         doNothing().when(spy).saveIntentInstanceToAAI(isNull(),any(CCVPNInstance.class));
 
 161         Transaction tx = Mockito.mock(Transaction.class);
 
 162         Mockito.when(session.beginTransaction()).thenReturn(tx);
 
 163         Serializable save = Mockito.mock(Serializable.class);
 
 164         Mockito.when(session.save(any())).thenReturn(save);
 
 165         Mockito.doNothing().when(tx).commit();
 
 167         assertEquals(spy.createCCVPNInstance(instance), 1);
 
 171     public void createCCVPNInstanceThrowErrorTest() throws IOException {
 
 172         CCVPNInstance instance = new CCVPNInstance();
 
 173         instance.setInstanceId("1");
 
 174         instance.setJobId("1");
 
 175         instance.setStatus("1");
 
 177         Call mockCall = PowerMockito.mock(Call.class);
 
 178         JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}");
 
 179         Response<JSONObject> response = Response.success(body);
 
 180         Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall);
 
 181         Mockito.when(mockCall.execute()).thenReturn(response);
 
 183         IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
 
 184         doThrow(new RuntimeException()).when(spy).saveIntentInstanceToAAI(isNull(),any(CCVPNInstance.class));
 
 186         Transaction tx = Mockito.mock(Transaction.class);
 
 187         Mockito.when(session.beginTransaction()).thenReturn(tx);
 
 188         Serializable save = Mockito.mock(Serializable.class);
 
 189         Mockito.when(session.save(any())).thenReturn(save);
 
 190         Mockito.doNothing().when(tx).commit();
 
 192         assertEquals(spy.createCCVPNInstance(instance), 0);
 
 196     public void createCCVPNInstanceInstanceIsNullTest() throws IOException {
 
 197         assertEquals(intentInstanceService.createCCVPNInstance(null), 0);
 
 200     public void createCCVPNInstanceInstanceJobIdIsNullTest() throws IOException {
 
 201         CCVPNInstance instance = new CCVPNInstance();
 
 202         instance.setInstanceId("1");
 
 203         instance.setStatus("1");
 
 204         assertEquals(intentInstanceService.createCCVPNInstance(instance), 0);
 
 208     public void getIntentInstanceProgressTest() throws IOException {
 
 210         Query query1 = Mockito.mock(Query.class);
 
 211         when(session.createQuery("from CCVPNInstance where deleteState = 0 and status = '0'")).thenReturn(query1);
 
 212         List<CCVPNInstance> q = new ArrayList<>();
 
 213         CCVPNInstance instance = new CCVPNInstance();
 
 214         instance.setInstanceId("1");
 
 215         instance.setResourceInstanceId("1");
 
 216         instance.setJobId("1");
 
 218         when(query1.list()).thenReturn(q);
 
 220         OperationProgressInformation operationProgressInformation = new OperationProgressInformation();
 
 221         OperationProgress operationProgress = new OperationProgress();
 
 222         operationProgress.setProgress(100);
 
 223         operationProgressInformation.setOperationStatus(operationProgress);
 
 225         JSONObject jsonObject = new JSONObject();
 
 226         JSONObject operation = new JSONObject();
 
 227         operation.put("progress", 100);
 
 228         jsonObject.put("operation", operation);
 
 229         Call mockCall = PowerMockito.mock(Call.class);
 
 230         Response<JSONObject> response = Response.success(jsonObject);
 
 231         Mockito.when(intentApiService.queryOperationProgress(anyString(),anyString())).thenReturn(mockCall);
 
 232         Mockito.when(mockCall.execute()).thenReturn(response);
 
 234         IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
 
 235         doNothing().when(spy).saveIntentInstanceToAAI(anyString(),any(CCVPNInstance.class));
 
 237         Transaction tx = Mockito.mock(Transaction.class);
 
 238         Mockito.when(session.beginTransaction()).thenReturn(tx);
 
 239         Serializable save = Mockito.mock(Serializable.class);
 
 240         Mockito.when(session.save(any())).thenReturn(save);
 
 241         Mockito.doNothing().when(tx).commit();
 
 243         spy.getIntentInstanceProgress();
 
 244         assertEquals(operation.getString("progress"),"100");
 
 247     public void getIntentInstanceCreateStatusTest() throws IOException {
 
 249         Query query1 = Mockito.mock(Query.class);
 
 250         when(session.createQuery("from CCVPNInstance where deleteState = 0 and status = '0'")).thenReturn(query1);
 
 251         List<CCVPNInstance> q = new ArrayList<>();
 
 252         CCVPNInstance instance = new CCVPNInstance();
 
 253         instance.setInstanceId("1");
 
 254         instance.setResourceInstanceId("1");
 
 255         instance.setJobId("1");
 
 257         when(query1.list()).thenReturn(q);
 
 259         OperationProgressInformation operationProgressInformation = new OperationProgressInformation();
 
 260         OperationProgress operationProgress = new OperationProgress();
 
 261         operationProgress.setProgress(100);
 
 262         operationProgressInformation.setOperationStatus(operationProgress);
 
 264         JSONObject jsonObject = new JSONObject();
 
 265         jsonObject.put("orchestration-status", "created");
 
 266         Call mockCall = PowerMockito.mock(Call.class);
 
 267         Response<JSONObject> response = Response.success(jsonObject);
 
 268         Mockito.when(intentApiService.getInstanceInfo(anyString())).thenReturn(mockCall);
 
 269         Mockito.when(mockCall.execute()).thenReturn(response);
 
 271         IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
 
 272         doNothing().when(spy).saveIntentInstanceToAAI(anyString(),any(CCVPNInstance.class));
 
 274         Transaction tx = Mockito.mock(Transaction.class);
 
 275         Mockito.when(session.beginTransaction()).thenReturn(tx);
 
 276         Serializable save = Mockito.mock(Serializable.class);
 
 277         Mockito.when(session.save(any())).thenReturn(save);
 
 278         Mockito.doNothing().when(tx).commit();
 
 280         spy.getIntentInstanceCreateStatus();
 
 281         assertEquals(jsonObject.getString("orchestration-status"),"created");
 
 285     public void getFinishedInstanceInfo() {
 
 286         Query query = Mockito.mock(Query.class);
 
 287         when(session.createQuery(anyString())).thenReturn(query);
 
 288         when(query.list()).thenReturn(new ArrayList());
 
 289         assertTrue(intentInstanceService.getFinishedInstanceInfo().isEmpty());
 
 293     public void getIntentInstanceBandwidth() throws IOException {
 
 294         Query query1 = Mockito.mock(Query.class);
 
 295         when(session.createQuery("from CCVPNInstance where deleteState = 0 and status = '1'")).thenReturn(query1);
 
 296         List<CCVPNInstance> q = new ArrayList<>();
 
 297         CCVPNInstance instance = new CCVPNInstance();
 
 298         instance.setInstanceId("1");
 
 299         instance.setResourceInstanceId("1");
 
 301         when(query1.list()).thenReturn(q);
 
 303         Call mockCall = PowerMockito.mock(Call.class);
 
 304         JSONObject jsonObject = JSONObject.parseObject("{\n" +
 
 305                 "    \"service-instance-id\":\"cll-101\",\n" +
 
 306                 "    \"service-instance-name\":\"cloud-leased-line-101\",\n" +
 
 307                 "    \"service-type\":\"CLL\",\n" +
 
 308                 "    \"service-role\":\"cll\",\n" +
 
 309                 "    \"environment-context\":\"cll\",\n" +
 
 310                 "    \"model-invariant-id\":\"6790ab0e-034f-11eb-adc1-0242ac120002\",\n" +
 
 311                 "    \"model-version-id\":\"6790ab0e-034f-11eb-adc1-0242ac120002\",\n" +
 
 312                 "    \"resource-version\":\"1628714665927\",\n" +
 
 313                 "    \"orchestration-status\":\"created\",\n" +
 
 314                 "    \"allotted-resources\":{\n" +
 
 315                 "        \"allotted-resource\":[\n" +
 
 317                 "                \"id\":\"cll-101-network-001\",\n" +
 
 318                 "                \"resource-version\":\"1628714665798\",\n" +
 
 319                 "                \"type\":\"TsciNetwork\",\n" +
 
 320                 "                \"allotted-resource-name\":\"network_cll-101-network-001\",\n" +
 
 321                 "                \"relationship-list\":{\n" +
 
 322                 "                    \"relationship\":[\n" +
 
 324                 "                            \"related-to\":\"logical-link\",\n" +
 
 325                 "                            \"relationship-label\":\"org.onap.relationships.inventory.ComposedOf\",\n" +
 
 326                 "                            \"related-link\":\"/aai/v24/network/logical-links/logical-link/tranportEp_UNI_ID_311_1\",\n" +
 
 327                 "                            \"relationship-data\":[\n" +
 
 329                 "                                    \"relationship-key\":\"logical-link.link-name\",\n" +
 
 330                 "                                    \"relationship-value\":\"tranportEp_UNI_ID_311_1\"\n" +
 
 335                 "                            \"related-to\":\"network-policy\",\n" +
 
 336                 "                            \"relationship-label\":\"org.onap.relationships.inventory.Uses\",\n" +
 
 337                 "                            \"related-link\":\"/aai/v24/network/network-policies/network-policy/de00a0a0-be2e-4d19-974a-80a2bca6bdf9\",\n" +
 
 338                 "                            \"relationship-data\":[\n" +
 
 340                 "                                    \"relationship-key\":\"network-policy.network-policy-id\",\n" +
 
 341                 "                                    \"relationship-value\":\"de00a0a0-be2e-4d19-974a-80a2bca6bdf9\"\n" +
 
 344                 "                            \"related-to-property\":[\n" +
 
 346                 "                                    \"property-key\":\"network-policy.network-policy-fqdn\",\n" +
 
 347                 "                                    \"property-value\":\"cll-101\"\n" +
 
 357         Response<JSONObject> response = Response.success(jsonObject);
 
 358         Mockito.when(intentApiService.getInstanceNetworkInfo(any())).thenReturn(mockCall);
 
 359         Mockito.when(mockCall.execute()).thenReturn(response);
 
 361         Call mockCall1 = PowerMockito.mock(Call.class);
 
 362         JSONObject jsonObject1 = JSONObject.parseObject("{\n" +
 
 363                 "    \"network-policy-id\":\"de00a0a0-be2e-4d19-974a-80a2bca6bdf9\",\n" +
 
 364                 "    \"network-policy-fqdn\":\"cll-101\",\n" +
 
 365                 "    \"resource-version\":\"1628714665619\",\n" +
 
 366                 "    \"name\":\"TSCi policy\",\n" +
 
 367                 "    \"type\":\"SLA\",\n" +
 
 368                 "    \"latency\":2,\n" +
 
 369                 "    \"max-bandwidth\":3000,\n" +
 
 370                 "    \"relationship-list\":{\n" +
 
 371                 "        \"relationship\":[\n" +
 
 373                 "                \"related-to\":\"allotted-resource\",\n" +
 
 374                 "                \"relationship-label\":\"org.onap.relationships.inventory.Uses\",\n" +
 
 375                 "                \"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" +
 
 376                 "                \"relationship-data\":[\n" +
 
 378                 "                        \"relationship-key\":\"customer.global-customer-id\",\n" +
 
 379                 "                        \"relationship-value\":\"IBNCustomer\"\n" +
 
 382                 "                        \"relationship-key\":\"service-subscription.service-type\",\n" +
 
 383                 "                        \"relationship-value\":\"IBN\"\n" +
 
 386                 "                        \"relationship-key\":\"service-instance.service-instance-id\",\n" +
 
 387                 "                        \"relationship-value\":\"cll-101\"\n" +
 
 390                 "                        \"relationship-key\":\"allotted-resource.id\",\n" +
 
 391                 "                        \"relationship-value\":\"cll-101-network-001\"\n" +
 
 394                 "                \"related-to-property\":[\n" +
 
 396                 "                        \"property-key\":\"allotted-resource.description\"\n" +
 
 399                 "                        \"property-key\":\"allotted-resource.allotted-resource-name\",\n" +
 
 400                 "                        \"property-value\":\"network_cll-101-network-001\"\n" +
 
 407         Response<JSONObject> response1 = Response.success(jsonObject1);
 
 408         Mockito.when(intentApiService.getInstanceNetworkPolicyInfo(any())).thenReturn(mockCall1);
 
 409         Mockito.when(mockCall1.execute()).thenReturn(response1);
 
 411         Call mockCall2 = PowerMockito.mock(Call.class);
 
 412         JSONObject jsonObject2 = JSONObject.parseObject("{\n" +
 
 413                 "    \"metadatum\":[\n" +
 
 415                 "            \"metaname\":\"ethernet-uni-id-1\",\n" +
 
 416                 "            \"metaval\":\"1234\",\n" +
 
 417                 "            \"resource-version\":\"1629409084707\"\n" +
 
 420                 "            \"metaname\":\"ethernet-uni-id-2\",\n" +
 
 421                 "            \"metaval\":\"5678\",\n" +
 
 422                 "            \"resource-version\":\"1629409204904\"\n" +
 
 426         Response<JSONObject> response2 = Response.success(jsonObject2);
 
 427         Mockito.when(intentApiService.getInstanceBandwidth(any())).thenReturn(mockCall2);
 
 428         Mockito.when(mockCall2.execute()).thenReturn(response2);
 
 430         Transaction tx = Mockito.mock(Transaction.class);
 
 431         Mockito.when(session.beginTransaction()).thenReturn(tx);
 
 432         Serializable save = Mockito.mock(Serializable.class);
 
 433         Mockito.when(session.save(any())).thenReturn(save);
 
 434         Mockito.doNothing().when(tx).commit();
 
 436         intentInstanceService.getIntentInstanceBandwidth();
 
 440     public void deleteIntentInstance() throws IOException {
 
 441         CCVPNInstance instance = new CCVPNInstance();
 
 442         instance.setResourceInstanceId("1");
 
 444         Query query = Mockito.mock(Query.class);
 
 445         when(session.createQuery(anyString())).thenReturn(query);
 
 446         when(query.setParameter(anyString(), anyString())).thenReturn(query);
 
 447         when(query.uniqueResult()).thenReturn(instance);
 
 449         Call mockCall = PowerMockito.mock(Call.class);
 
 450         when(intentApiService.deleteIntentInstance(any())).thenReturn(mockCall);
 
 451         when(mockCall.execute()).thenReturn(null);
 
 453         Transaction tx = PowerMockito.mock(Transaction.class);
 
 454         when(session.beginTransaction()).thenReturn(tx);
 
 455         Serializable save = PowerMockito.mock(Serializable.class);
 
 456         doNothing().when(session).delete(any());
 
 457         doNothing().when(tx).commit();
 
 459         IntentInstanceServiceImpl spy = spy(intentInstanceService);
 
 460         doNothing().when(spy).deleteIntentInstanceToAAI(anyString());
 
 462         spy.deleteIntentInstance("1");
 
 467     public void invalidIntentInstanceTest() throws IOException {
 
 468         CCVPNInstance instance = new CCVPNInstance();
 
 469         instance.setResourceInstanceId("1");
 
 471         Query query = Mockito.mock(Query.class);
 
 472         when(session.createQuery(anyString())).thenReturn(query);
 
 473         when(query.setParameter(anyString(), anyString())).thenReturn(query);
 
 474         when(query.uniqueResult()).thenReturn(instance);
 
 476         Call mockCall = PowerMockito.mock(Call.class);
 
 477         when(intentApiService.deleteIntentInstance(any())).thenReturn(mockCall);
 
 478         when(mockCall.execute()).thenReturn(null);
 
 480         Transaction tx = PowerMockito.mock(Transaction.class);
 
 481         when(session.beginTransaction()).thenReturn(tx);
 
 482         Serializable save = PowerMockito.mock(Serializable.class);
 
 483         doNothing().when(session).delete(any());
 
 484         doNothing().when(tx).commit();
 
 486         intentInstanceService.invalidIntentInstance("1");
 
 489     public void queryInstancePerformanceDataTest() throws IOException {
 
 490         CCVPNInstance instance = new CCVPNInstance();
 
 491         instance.setResourceInstanceId("1");
 
 493         InstancePerformance instancePerformance = new InstancePerformance();
 
 494         instancePerformance.setBandwidth(2000);
 
 495         instancePerformance.setMaxBandwidth(20000);
 
 496         instancePerformance.setDate(new Date());
 
 497         Object[] o = {null,instancePerformance};
 
 498         List<Object[]> queryResult= new ArrayList<>();
 
 501         Query query = Mockito.mock(Query.class);
 
 502         when(session.createQuery(anyString())).thenReturn(query);
 
 503         when(query.setParameter(anyString(), anyString())).thenReturn(query);
 
 504         when(query.list()).thenReturn(queryResult);
 
 506         intentInstanceService.queryInstancePerformanceData("1");
 
 512         Call mockCall = PowerMockito.mock(Call.class);
 
 513         when(intentApiService.deleteIntentInstance(any())).thenReturn(mockCall);
 
 514         when(mockCall.execute()).thenReturn(null);
 
 516         Transaction tx = PowerMockito.mock(Transaction.class);
 
 517         when(session.beginTransaction()).thenReturn(tx);
 
 518         Serializable save = PowerMockito.mock(Serializable.class);
 
 519         doNothing().when(session).delete(any());
 
 520         doNothing().when(tx).commit();
 
 522         intentInstanceService.invalidIntentInstance("1");
 
 526     public void activeIntentInstance() throws IOException {
 
 527         CCVPNInstance instance = new CCVPNInstance();
 
 528         instance.setInstanceId("1");
 
 529         instance.setJobId("1");
 
 530         instance.setStatus("1");
 
 532         Query query = Mockito.mock(Query.class);
 
 533         when(session.createQuery(anyString())).thenReturn(query);
 
 534         when(query.setParameter(anyString(), anyString())).thenReturn(query);
 
 535         when(query.uniqueResult()).thenReturn(instance);
 
 538         Call mockCall = PowerMockito.mock(Call.class);
 
 539         JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}");
 
 540         Response<JSONObject> response = Response.success(body);
 
 541         Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall);
 
 542         Mockito.when(mockCall.execute()).thenReturn(response);
 
 544         Transaction tx = Mockito.mock(Transaction.class);
 
 545         Mockito.when(session.beginTransaction()).thenReturn(tx);
 
 546         Serializable save = Mockito.mock(Serializable.class);
 
 547         Mockito.when(session.save(any())).thenReturn(save);
 
 548         Mockito.doNothing().when(tx).commit();
 
 550         intentInstanceService.activeIntentInstance("1");
 
 555     public void queryAccessNodeInfo() throws IOException {
 
 557         Call mockCall = PowerMockito.mock(Call.class);
 
 558         JSONObject body = JSONObject.parseObject("{\n" +
 
 559                 "    \"network-route\": [\n" +
 
 561                 "            \"route-id\": \"tranportEp_src_ID_111_1\",\n" +
 
 562                 "            \"type\": \"LEAF\",\n" +
 
 563                 "            \"role\": \"3gppTransportEP\",\n" +
 
 564                 "            \"function\": \"3gppTransportEP\",\n" +
 
 565                 "            \"ip-address\": \"10.2.3.4\",\n" +
 
 566                 "            \"prefix-length\": 24,\n" +
 
 567                 "            \"next-hop\": \"networkId-providerId-10-clientId-0-topologyId-2-nodeId-10.1.1.1-ltpId-1000\",\n" +
 
 568                 "            \"address-family\": \"ipv4\",\n" +
 
 569                 "            \"resource-version\": \"1634198223345\"\n" +
 
 572                 "            \"route-id\": \"tranportEp_src_ID_113_1\",\n" +
 
 573                 "            \"type\": \"LEAF\",\n" +
 
 574                 "            \"role\": \"3gppTransportEP\",\n" +
 
 575                 "            \"function\": \"3gppTransportEP\",\n" +
 
 576                 "            \"ip-address\": \"10.2.3.4\",\n" +
 
 577                 "            \"prefix-length\": 24,\n" +
 
 578                 "            \"next-hop\": \"networkId-providerId-10-clientId-0-topologyId-2-nodeId-10.1.1.3-ltpId-1000\",\n" +
 
 579                 "            \"address-family\": \"ipv4\",\n" +
 
 580                 "            \"resource-version\": \"1634198260496\"\n" +
 
 583                 "            \"route-id\": \"tranportEp_src_ID_111_2\",\n" +
 
 584                 "            \"type\": \"LEAF\",\n" +
 
 585                 "            \"role\": \"3gppTransportEP\",\n" +
 
 586                 "            \"function\": \"3gppTransportEP\",\n" +
 
 587                 "            \"ip-address\": \"10.2.3.4\",\n" +
 
 588                 "            \"prefix-length\": 24,\n" +
 
 589                 "            \"next-hop\": \"networkId-providerId-10-clientId-0-topologyId-2-nodeId-10.1.1.1-ltpId-2000\",\n" +
 
 590                 "            \"address-family\": \"ipv4\",\n" +
 
 591                 "            \"resource-version\": \"1634198251534\"\n" +
 
 594                 "            \"route-id\": \"tranportEp_dst_ID_212_1\",\n" +
 
 595                 "            \"type\": \"ROOT\",\n" +
 
 596                 "            \"role\": \"3gppTransportEP\",\n" +
 
 597                 "            \"function\": \"3gppTransportEP\",\n" +
 
 598                 "            \"ip-address\": \"10.2.3.4\",\n" +
 
 599                 "            \"prefix-length\": 24,\n" +
 
 600                 "            \"next-hop\": \"networkId-providerId-20-clientId-0-topologyId-2-nodeId-10.2.1.2-ltpId-512\",\n" +
 
 601                 "            \"address-family\": \"ipv4\",\n" +
 
 602                 "            \"resource-version\": \"1634198274852\"\n" +
 
 606         Response<JSONObject> response = Response.success(body);
 
 607         Mockito.when(intentApiService.queryNetworkRoute()).thenReturn(mockCall);
 
 608         Mockito.when(mockCall.execute()).thenReturn(response);
 
 609         Map<String, Object> result = (Map<String, Object>) intentInstanceService.queryAccessNodeInfo();
 
 610         assertEquals(((List)result.get("accessNodeList")).size(), 3);
 
 614     public void getInstanceStatusTest() {
 
 615         List<CCVPNInstance> queryResult = new ArrayList<>();
 
 616         CCVPNInstance instance = new CCVPNInstance();
 
 617         instance.setInstanceId("id1");
 
 618         instance.setStatus("1");
 
 619         queryResult.add(instance);
 
 621         Query query = Mockito.mock(Query.class);
 
 622         when(session.createQuery(anyString())).thenReturn(query);
 
 623         when(query.setParameter(anyString(), any())).thenReturn(query);
 
 624         when(query.list()).thenReturn(queryResult);
 
 627         JSONObject instanceStatus = intentInstanceService.getInstanceStatus(new JSONArray());
 
 628         assertEquals(instanceStatus.getJSONArray("IntentInstances").getJSONObject(0).getString("id"), "id1");
 
 631     public void formatBandwidthTest() {
 
 633         String bandwidth = intentInstanceService.formatBandwidth("2Gbps");
 
 634         assertEquals(bandwidth, "2000");
 
 637     public void formatCloudPointTest() {
 
 639         String bandwidth = intentInstanceService.formatCloudPoint("Cloud one");
 
 640         assertEquals(bandwidth, "tranportEp_dst_ID_212_1");
 
 643     public void formatAccessPointOneTest() {
 
 644         String bandwidth = intentInstanceService.formatAccessPoint("Access one");
 
 645         assertEquals(bandwidth, "tranportEp_src_ID_111_1");
 
 648     public void formatAccessPointTwoTest() {
 
 649         String bandwidth = intentInstanceService.formatAccessPoint("Access two");
 
 650         assertEquals(bandwidth, "tranportEp_src_ID_111_2");
 
 653     public void formatAccessPointThreeTest() {
 
 654         String bandwidth = intentInstanceService.formatAccessPoint("Access three");
 
 655         assertEquals(bandwidth, "tranportEp_src_ID_113_1");
 
 659     public void addCustomerTest() throws IOException {
 
 661         Call mockCall = PowerMockito.mock(Call.class);
 
 662         Response<Object> response = Response.error(404, new ResponseBody() {
 
 665             public MediaType contentType() {
 
 670             public long contentLength() {
 
 675             public BufferedSource source() {
 
 679         when(intentApiService.queryCustomer(anyString())).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();
 
 690         Call mockCall2 = PowerMockito.mock(Call.class);
 
 691         when(intentApiService.addCustomer(anyString(),any())).thenReturn(mockCall2);
 
 694         Mockito.verify(intentApiService,Mockito.times(1)).addCustomer(anyString(),any());
 
 699     public void addSubscriptionTest() throws IOException {
 
 701         Call mockCall = PowerMockito.mock(Call.class);
 
 702         Response<Object> response = Response.error(404, new ResponseBody() {
 
 705             public MediaType contentType() {
 
 710             public long contentLength() {
 
 715             public BufferedSource source() {
 
 719         when(intentApiService.querySubscription(anyString(),anyString())).thenReturn(mockCall);
 
 720         when(mockCall.execute()).thenReturn(response);
 
 722         Properties properties = new Properties();
 
 723         properties.put("ccvpn.globalCustomerId", "IBNCustomer");
 
 724         properties.put("ccvpn.subscriberName", "IBNCustomer");
 
 725         properties.put("ccvpn.subscriberType", "INFRA");
 
 726         properties.put("ccvpn.serviceType", "IBN");
 
 727         IntentInstanceServiceImpl spy = spy(intentInstanceService);
 
 728         doReturn(properties).when(spy).getProperties();
 
 730         Call mockCall2 = PowerMockito.mock(Call.class);
 
 731         when(intentApiService.addSubscription(anyString(),anyString(),any())).thenReturn(mockCall2);
 
 733         spy.addSubscription();
 
 734         Mockito.verify(intentApiService,Mockito.times(1)).addSubscription(anyString(),anyString(),any());
 
 738     public void saveIntentInstanceToAAITest() throws IOException {
 
 739         IntentInstanceServiceImpl spy = spy(intentInstanceService);
 
 740         doNothing().when(spy).addCustomer();
 
 741         doNothing().when(spy).addSubscription();
 
 743         Properties properties = new Properties();
 
 744         properties.put("ccvpn.globalCustomerId", "IBNCustomer");
 
 745         properties.put("ccvpn.subscriberName", "IBNCustomer");
 
 746         properties.put("ccvpn.subscriberType", "INFRA");
 
 747         properties.put("ccvpn.serviceType", "IBN");
 
 748         doReturn(properties).when(spy).getProperties();
 
 750         JSONObject body = new JSONObject();
 
 751         body.put("resource-version",123);
 
 752         Call mockCall = PowerMockito.mock(Call.class);
 
 753         Response<JSONObject> response = Response.success(body);
 
 754         when(intentApiService.queryServiceInstance(anyString(),anyString(),anyString())).thenReturn(mockCall);
 
 755         when(mockCall.execute()).thenReturn(response);
 
 757         CCVPNInstance instance = new CCVPNInstance();
 
 758         instance.setName("name");
 
 759         instance.setInstanceId("id");
 
 761         Call mockCall2 = PowerMockito.mock(Call.class);
 
 762         Response<JSONObject> response2 = Response.success(body);
 
 763         when(intentApiService.saveServiceInstance(anyString(),anyString(),anyString(),any())).thenReturn(mockCall2);
 
 764         when(mockCall2.execute()).thenReturn(response2);
 
 766         spy.saveIntentInstanceToAAI("CCVPN-id",instance);
 
 767         Mockito.verify(intentApiService, Mockito.times(1)).saveServiceInstance(anyString(),anyString(),anyString(),any());
 
 771     public void deleteIntentInstanceToAAITest() throws IOException {
 
 772         IntentInstanceServiceImpl spy = spy(intentInstanceService);
 
 773         doNothing().when(spy).addCustomer();
 
 774         doNothing().when(spy).addSubscription();
 
 776         Properties properties = new Properties();
 
 777         properties.put("ccvpn.globalCustomerId", "IBNCustomer");
 
 778         properties.put("ccvpn.subscriberName", "IBNCustomer");
 
 779         properties.put("ccvpn.subscriberType", "INFRA");
 
 780         properties.put("ccvpn.serviceType", "IBN");
 
 781         doReturn(properties).when(spy).getProperties();
 
 783         JSONObject body = new JSONObject();
 
 784         body.put("resource-version",123);
 
 785         Call mockCall = PowerMockito.mock(Call.class);
 
 786         Response<JSONObject> response = Response.success(body);
 
 787         when(intentApiService.queryServiceInstance(anyString(),anyString(),anyString())).thenReturn(mockCall);
 
 788         when(mockCall.execute()).thenReturn(response);
 
 790         Call mockCall2 = PowerMockito.mock(Call.class);
 
 791         Response<JSONObject> response2 = Response.success(body);
 
 792         when(intentApiService.deleteServiceInstance(anyString(),anyString(),anyString(),anyString())).thenReturn(mockCall2);
 
 793         when(mockCall2.execute()).thenReturn(response2);
 
 795         spy.deleteIntentInstanceToAAI("CCVPN-id");
 
 796         Mockito.verify(intentApiService, Mockito.times(1)).deleteServiceInstance(anyString(),anyString(),anyString(),any());
 
 800     public void createIntentInstanceWithCCVPNInstanceTest() {
 
 801         Map<String, Object> body = new HashMap<>();
 
 802         body.put("intentContent", "this is intent content");
 
 803         body.put("name", "this is name");
 
 804         Transaction tx = PowerMockito.mock(Transaction.class);
 
 805         when(session.beginTransaction()).thenReturn(tx);
 
 807         Serializable save = Mockito.mock(Serializable.class);
 
 808         when(session.save(any(IntentInstance.class))).thenReturn(save);
 
 810         doNothing().when(tx).commit();
 
 811         doNothing().when(session).close();
 
 812         IntentInstance instance = intentInstanceService.createIntentInstance(body, "id", "name", IntentConstant.MODEL_TYPE_CCVPN);
 
 813         assertEquals(instance.getBusinessInstanceId(), "id");
 
 816     public void createIntentInstanceWithSlicingInstanceTest() {
 
 817         Map<String, Object> slicingOrderInfo = new HashMap<>();
 
 818         slicingOrderInfo.put("intentContent", "this is intent content");
 
 819         slicingOrderInfo.put("name", "this is name");
 
 821         Map<String, Object> body = new HashMap<>();
 
 822         body.put("slicing_order_info", slicingOrderInfo);
 
 823         Transaction tx = PowerMockito.mock(Transaction.class);
 
 824         when(session.beginTransaction()).thenReturn(tx);
 
 826         Serializable save = Mockito.mock(Serializable.class);
 
 827         when(session.save(any(IntentInstance.class))).thenReturn(save);
 
 829         doNothing().when(tx).commit();
 
 830         doNothing().when(session).close();
 
 831         IntentInstance instance = intentInstanceService.createIntentInstance(body, "id", "name", IntentConstant.MODEL_TYPE_5GS);
 
 832         assertEquals(instance.getBusinessInstanceId(), "id");
 
 835     public void createIntentInstanceWithThrowErrorTest() {
 
 836         Map<String, Object> slicingOrderInfo = new HashMap<>();
 
 837         slicingOrderInfo.put("intentContent", "this is intent content");
 
 838         slicingOrderInfo.put("name", "this is name");
 
 840         Map<String, Object> body = new HashMap<>();
 
 841         body.put("slicing_order_info", slicingOrderInfo);
 
 842         Transaction tx = PowerMockito.mock(Transaction.class);
 
 843         when(session.beginTransaction()).thenReturn(tx);
 
 845         when(session.save(any(IntentInstance.class))).thenThrow(new RuntimeException());
 
 847         doNothing().when(session).close();
 
 848         IntentInstance instance = intentInstanceService.createIntentInstance(body, "id", "name", IntentConstant.MODEL_TYPE_5GS);
 
 849         assertNull(instance);
 
 853     public void deleteIntentWithDeleteCCVPNInstanceTest() {
 
 855         IntentInstanceServiceImpl spy = spy(intentInstanceService);
 
 857         IntentInstance instance = new IntentInstance();
 
 859         instance.setIntentSource(IntentConstant.MODEL_TYPE_CCVPN);
 
 860         instance.setBusinessInstanceId("1");
 
 862         Query query = PowerMockito.mock(Query.class);
 
 863         when(session.createQuery(anyString())).thenReturn(query);
 
 864         when(query.setParameter("id", 1)).thenReturn(query);
 
 865         when(query.uniqueResult()).thenReturn(instance);
 
 867         doNothing().when(spy).deleteIntentInstance(anyString());
 
 869         Transaction tx = PowerMockito.mock(Transaction.class);
 
 870         when(session.beginTransaction()).thenReturn(tx);
 
 871         doNothing().when(session).delete(any());
 
 872         doNothing().when(tx).commit();
 
 873         doNothing().when(session).close();
 
 877         Mockito.verify(spy, Mockito.times(1)).deleteIntentInstance("1");
 
 881     public void deleteIntentWithDeleteSlicingInstanceTest() {
 
 884         IntentInstance instance = new IntentInstance();
 
 886         instance.setIntentSource(IntentConstant.MODEL_TYPE_5GS);
 
 887         instance.setBusinessInstanceId("1");
 
 889         Query query = PowerMockito.mock(Query.class);
 
 890         when(session.createQuery(anyString())).thenReturn(query);
 
 891         when(query.setParameter("id", 1)).thenReturn(query);
 
 892         when(query.uniqueResult()).thenReturn(instance);
 
 894         ServiceResult serviceResult = new ServiceResult();
 
 895         when(resourceMgtService.terminateSlicingService(anyString())).thenReturn(serviceResult);
 
 897         Transaction tx = PowerMockito.mock(Transaction.class);
 
 898         when(session.beginTransaction()).thenReturn(tx);
 
 899         doNothing().when(session).delete(any());
 
 900         doNothing().when(tx).commit();
 
 901         doNothing().when(session).close();
 
 903         intentInstanceService.deleteIntent(1);
 
 905         Mockito.verify(resourceMgtService, Mockito.times(1)).terminateSlicingService(anyString());
 
 908     public void deleteIntentWithThrowErrorTest() {
 
 911         IntentInstance instance = new IntentInstance();
 
 913         instance.setIntentSource(IntentConstant.MODEL_TYPE_5GS);
 
 914         instance.setBusinessInstanceId("1");
 
 916         when(session.createQuery(anyString())).thenThrow(new RuntimeException());
 
 918         doNothing().when(session).close();
 
 920         intentInstanceService.deleteIntent(1);
 
 922         Mockito.verify(resourceMgtService, Mockito.times(0)).terminateSlicingService(anyString());
 
 926     public void getIntentInstanceListTest() {
 
 927         IntentInstanceServiceImpl spy = spy(intentInstanceService);
 
 928         doReturn(2).when(spy).getIntentInstanceAllCount();
 
 930         Query query = PowerMockito.mock(Query.class);
 
 931         when(session.createQuery("from IntentInstance order by id")).thenReturn(query);
 
 932         when(query.setFirstResult(anyInt())).thenReturn(query);
 
 933         when(query.setMaxResults(anyInt())).thenReturn(query);
 
 935         List<IntentInstance> list = new ArrayList<>();
 
 936         list.add(new IntentInstance());
 
 937         list.add(new IntentInstance());
 
 938         when(query.list()).thenReturn(list);
 
 939         doNothing().when(session).close();
 
 940         int totalRecords = spy.getIntentInstanceList(1, 10).getTotalRecords();
 
 941         assertEquals(totalRecords,2);
 
 945     public void getIntentInstanceListThrowErrorTest() {
 
 946         IntentInstanceServiceImpl spy = spy(intentInstanceService);
 
 947         doReturn(2).when(spy).getIntentInstanceAllCount();
 
 949         when(session.createQuery("from IntentInstance order by id")).thenThrow(new RuntimeException());
 
 950         doNothing().when(session).close();
 
 951         assertEquals(spy.getIntentInstanceList(1, 10),null);
 
 955     public void createSlicingServiceWithIntent() {
 
 956         IntentInstanceServiceImpl spy = spy(intentInstanceService);
 
 958         SlicingOrder slicingOrder = new SlicingOrder();
 
 959         slicingOrder.setSlicing_order_info(new SlicingOrderDetail());
 
 960         slicingOrder.getSlicing_order_info().setName("name");
 
 962         ServiceResult serviceResult = new ServiceResult();
 
 963         ServiceCreateResult serviceCreateResult = new ServiceCreateResult();
 
 964         serviceCreateResult.setService_id("id");
 
 965         serviceResult.setResult_body(serviceCreateResult);
 
 966         when(slicingService.createSlicingService(any())).thenReturn(serviceResult);
 
 968         IntentInstance instance = new IntentInstance();
 
 969         doReturn(instance).when(spy).createIntentInstance(any(),anyString(),anyString(),anyString());
 
 971         assertEquals(spy.createSlicingServiceWithIntent(slicingOrder), serviceResult);
 
 975     public void getIntentInstanceAllCountTest() {
 
 977         Query query = PowerMockito.mock(Query.class);
 
 978         when(session.createQuery("select count(*) from IntentInstance")).thenReturn(query);
 
 979         when(query.uniqueResult()).thenReturn(2L);
 
 982         assertEquals(intentInstanceService.getIntentInstanceAllCount(),2);
 
 986     public void getIntentInstanceAllCountThrowErrorTest() {
 
 988         when(session.createQuery("select count(*) from IntentInstance")).thenThrow(new RuntimeException());
 
 989         assertEquals(intentInstanceService.getIntentInstanceAllCount(),-1);
 
 993     public void updateCCVPNInstanceTest() throws IOException {
 
 994         CCVPNInstance instance = new CCVPNInstance();
 
 995         instance.setInstanceId("1");
 
 996         instance.setAccessPointOneBandWidth(1);
 
 998         CCVPNInstance ccvpnInstance = new CCVPNInstance();
 
 999         ccvpnInstance.setInstanceId(instance.getInstanceId());
 
1001         Query query = mock(Query.class);
 
1002         when(session.createQuery("from CCVPNInstance where instanceId = :instanceId")).thenReturn(query);
 
1003         when(query.setParameter(anyString(), anyString())).thenReturn(query);
 
1004         when(query.uniqueResult()).thenReturn(ccvpnInstance);
 
1006         IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
 
1007         doNothing().when(spy).saveIntentInstanceToAAI(anyString(),any(CCVPNInstance.class));
 
1009         Transaction tx = Mockito.mock(Transaction.class);
 
1010         Mockito.when(session.beginTransaction()).thenReturn(tx);
 
1011         doNothing().when(session).update(ccvpnInstance);
 
1012         Mockito.doNothing().when(tx).commit();
 
1014         assertEquals(spy.updateCCVPNInstance(instance), 1);