feat:add Intent Instance Management 96/127896/1
author’zhaoyh6‘ <zhaoyh6@asiainfo.com>
Mon, 21 Mar 2022 06:18:47 +0000 (14:18 +0800)
committer’zhaoyh6‘ <zhaoyh6@asiainfo.com>
Mon, 21 Mar 2022 06:18:54 +0000 (14:18 +0800)
Issue-ID: REQ-1075
Signed-off-by: ’zhaoyh6‘ <zhaoyh6@asiainfo.com>
Change-Id: Ia725c68f3f5cb5fbbd1ba8105b947c7f869e5609

server/src/main/java/org/onap/usecaseui/server/bean/intent/IntentInstance.java [new file with mode: 0644]
server/src/main/java/org/onap/usecaseui/server/constant/IntentConstant.java
server/src/main/java/org/onap/usecaseui/server/controller/IntentController.java
server/src/main/java/org/onap/usecaseui/server/service/intent/IntentInstanceService.java
server/src/main/java/org/onap/usecaseui/server/service/intent/impl/IntentInstanceServiceImpl.java
server/src/test/java/org/onap/usecaseui/server/controller/IntentControllerTest.java
server/src/test/java/org/onap/usecaseui/server/service/intent/impl/IntentInstanceServiceImplTest.java
standalone/src/main/assembly/resources/dbscripts/postgres/uui_create_table.sql

diff --git a/server/src/main/java/org/onap/usecaseui/server/bean/intent/IntentInstance.java b/server/src/main/java/org/onap/usecaseui/server/bean/intent/IntentInstance.java
new file mode 100644 (file)
index 0000000..d877103
--- /dev/null
@@ -0,0 +1,156 @@
+/*
+ * Copyright (C) 2021 CTC, Inc. and others. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onap.usecaseui.server.bean.intent;
+import org.apache.commons.collections.MapUtils;
+
+import javax.persistence.*;
+import java.io.Serializable;
+import java.util.HashMap;
+import java.util.Map;
+
+@Entity
+@Table(name="intent_instance")
+public class IntentInstance implements Serializable {
+
+    @Id
+    @GeneratedValue(strategy=GenerationType.IDENTITY)
+    @Column(name = "id")
+    private int id;
+
+    @Column(name = "intent_name")
+    private String intentName;
+
+    @Column(name = "intent_source")
+    private int intentSource;
+
+    @Column(name = "customer")
+    private String customer;
+
+    @Column(name = "intent_content")
+    private String intentContent;
+
+    @Column(name = "intent_config")
+    private String intentConfig;
+
+    @Column(name = "business_instance_id")
+    private String businessInstanceId;
+
+    @Column(name = "business_instance")
+    private String businessInstance;
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public String getIntentName() {
+        return intentName;
+    }
+
+    public void setIntentName(String intentName) {
+        this.intentName = intentName;
+    }
+
+    public String getIntentSource() {
+        if (this.intentSource == 1) {
+            return "ccvpn";
+        }
+        else {
+            return "5gs";
+        }
+    }
+
+    public void setIntentSource(String intentSource) {
+        if ("ccvpn".equals(intentSource)) {
+            this.intentSource = 1;
+        }
+        else {
+            this.intentSource = 0;
+        }
+    }
+
+    public String getCustomer() {
+        return customer;
+    }
+
+    public void setCustomer(String customer) {
+        this.customer = customer;
+    }
+
+    public String getIntentContent() {
+        return intentContent;
+    }
+
+    public void setIntentContent(String intentContent) {
+        this.intentContent = intentContent;
+    }
+
+    public String getIntentConfig() {
+        return intentConfig;
+    }
+
+    public void setIntentConfig(String intentConfig) {
+        this.intentConfig = intentConfig;
+    }
+
+    public String getBusinessInstanceId() {
+        return businessInstanceId;
+    }
+
+    public void setBusinessInstanceId(String businessInstanceId) {
+        this.businessInstanceId = businessInstanceId;
+    }
+
+    public String getBusinessInstance() {
+        return businessInstance;
+    }
+
+    public void setBusinessInstance(String businessInstance) {
+        this.businessInstance = businessInstance;
+    }
+
+    public static IntentInstance map2Object(Map map) {
+        IntentInstance intentInstance = new IntentInstance();
+        if (MapUtils.getIntValue(map, "id", -1) > -1) {
+            intentInstance.setId(MapUtils.getIntValue(map, "id", -1));
+        }
+        intentInstance.setIntentName(MapUtils.getString(map, "intent_name", ""));
+        intentInstance.setIntentSource(MapUtils.getString(map, "intent_source", ""));
+        intentInstance.setCustomer(MapUtils.getString(map, "customer", ""));
+        intentInstance.setIntentContent(MapUtils.getString(map, "intent_content", ""));
+        intentInstance.setIntentConfig(MapUtils.getString(map, "intent_config", ""));
+        intentInstance.setBusinessInstanceId(MapUtils.getString(map, "business_instance_id", ""));
+        intentInstance.setBusinessInstance(MapUtils.getString(map, "business_instance", ""));
+        return intentInstance;
+    }
+
+    public static Map object2Map(IntentInstance intentInstance) {
+        Map<String, Object> map = new HashMap<>();
+        map.put("id", intentInstance.getId());
+        map.put("intent_name", intentInstance.getIntentName());
+        map.put("intent_source", intentInstance.getIntentSource());
+        map.put("customer", intentInstance.getCustomer());
+        map.put("intent_content", intentInstance.getIntentContent());
+        map.put("intent_config", intentInstance.getIntentConfig());
+        map.put("business_instance_id", intentInstance.getBusinessInstanceId());
+        map.put("business_instance", intentInstance.getBusinessInstance());
+
+        return map;
+    }
+}
\ No newline at end of file
index 9164176..4262fda 100644 (file)
@@ -27,8 +27,7 @@ public final class IntentConstant {
     public final static String INTENT_INSTANCE_ID_PREFIX = "IBN";
     public final static String INTENT_INSTANCE_DATA_OWNER = "UUI";
 
-    public final static String NLP_HOST = "http://10.21.19.55";
-    //    public final static String NLP_HOST = "http://uui-nlp";
+    public final static String NLP_HOST = "http://uui-nlp";
     public final static String NLP_ONLINE_URL_BASE = NLP_HOST+":33011";
     public final static String NLP_OFFLINE_URL_BASE = NLP_HOST+":33012";
     public final static String NLP_FILE_URL_BASE = NLP_HOST+":33013";
index ef1ebba..f63f9d9 100644 (file)
@@ -29,6 +29,7 @@ import org.onap.usecaseui.server.bean.HttpResponseResult;
 import org.onap.usecaseui.server.bean.intent.CCVPNInstance;
 import org.onap.usecaseui.server.bean.intent.IntentModel;
 import org.onap.usecaseui.server.bean.intent.IntentResponseBody;
+import org.onap.usecaseui.server.bean.nsmf.common.ServiceResult;
 import org.onap.usecaseui.server.constant.IntentConstant;
 import org.onap.usecaseui.server.service.csmf.SlicingService;
 import org.onap.usecaseui.server.service.intent.IntentApiService;
@@ -415,7 +416,7 @@ public class IntentController {
     @ResponseBody
     @PostMapping(value = {"/createIntentInstance"}, consumes = MediaType.APPLICATION_JSON_VALUE,
             produces = "application/json; charset=utf-8")
-    public Object createIntentInstance(@RequestBody Object body) throws IOException {
+    public Object createCCVPNInstance(@RequestBody Object body) throws IOException {
         String intentInstanceId = (String) ((Map)body).get("instanceId");
         String name = (String) ((Map)body).get("name");
         String lineNum = (String) ((Map)body).get("lineNum");
@@ -435,9 +436,12 @@ public class IntentController {
         instance.setStatus("0");
         instance.setProtectStatus(protectStatus?1:0);
 
-        int flag = intentInstanceService.createIntentInstance(instance);
+        int flag = intentInstanceService.createCCVPNInstance(instance);
 
         if(flag == 1) {
+            if (((Map) body).containsKey("intentContent")) {
+                intentInstanceService.createIntentInstance(body, instance.getInstanceId() + "", instance.getName(), IntentConstant.MODEL_TYPE_CCVPN);
+            }
             return "OK";
         }
         else {
@@ -514,4 +518,42 @@ public class IntentController {
     public File newFile(String filePath) {
         return new File(filePath);
     }
+
+
+    @ResponseBody
+    @PostMapping(value = {"/csmf/5gSlicing"}, consumes = MediaType.APPLICATION_JSON_VALUE,
+            produces = "application/json; charset=utf-8")
+    public ServiceResult createSlicingServiceWithIntent(@RequestBody Object slicingOrderBody) {
+        return intentInstanceService.createSlicingServiceWithIntent(slicingOrderBody);
+    }
+
+    @IntentResponseBody
+    @DeleteMapping(value = {"/deleteIntent"}, produces = "application/json; charset=utf-8")
+    public Object deleteIntent(@RequestParam int id) {
+        intentInstanceService.deleteIntent(id);
+        return "ok";
+    }
+
+    @IntentResponseBody
+    @ResponseBody
+    @PostMapping(value = {"/verifyIntentInstance"}, consumes = MediaType.APPLICATION_JSON_VALUE,
+            produces = "application/json; charset=utf-8")
+    public Object verifyIntentInstance(@RequestBody Object body) {
+        int id = MapUtils.getIntValue((Map) body, "id");
+        intentInstanceService.verifyIntent(id);
+        return "Intent verification passed, Recommended implementation!";
+    }
+
+    @IntentResponseBody
+    @ResponseBody
+    @PostMapping(value = {"/getIntentList"}, consumes = MediaType.APPLICATION_JSON_VALUE,
+            produces = "application/json; charset=utf-8")
+    public Object getIntentList(@RequestBody Object body) {
+
+        int currentPage = (int) ((Map)body).get("currentPage");
+        int pageSize = (int) ((Map)body).get("pageSize");
+        logger.error("getInstanceList --> currentPage:" + currentPage + ",pageSize:" + pageSize);
+        return intentInstanceService.getIntentInstanceList(currentPage, pageSize);
+    }
+
 }
index 13362c1..cb922e7 100644 (file)
@@ -18,6 +18,8 @@ package org.onap.usecaseui.server.service.intent;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import org.onap.usecaseui.server.bean.intent.CCVPNInstance;
+import org.onap.usecaseui.server.bean.intent.IntentInstance;
+import org.onap.usecaseui.server.bean.nsmf.common.ServiceResult;
 import org.onap.usecaseui.server.util.Page;
 
 import java.io.IOException;
@@ -26,7 +28,7 @@ import java.util.Map;
 
 public interface IntentInstanceService {
     Page<CCVPNInstance> queryIntentInstance(CCVPNInstance instance, int currentPage, int pageSize);
-    int createIntentInstance(CCVPNInstance instance);
+    int createCCVPNInstance(CCVPNInstance instance);
     void getIntentInstanceProgress();
     void getIntentInstanceCreateStatus();
     List<CCVPNInstance> getFinishedInstanceInfo();
@@ -51,4 +53,14 @@ public interface IntentInstanceService {
     String formatAccessPoint(String accessPoint);
 
     void addCustomer() throws IOException;
+
+    IntentInstance createIntentInstance(Object body,String businessInstanceId, String businessInstance,  String type);
+
+    void deleteIntent(int id);
+
+    void verifyIntent(int id);
+
+    Page<IntentInstance> getIntentInstanceList(int currentPage, int pageSize);
+
+    ServiceResult createSlicingServiceWithIntent(Object slicingOrderBody);
 }
index 3c04ec2..c54e591 100644 (file)
@@ -22,12 +22,18 @@ import org.hibernate.Query;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.Transaction;
+import org.onap.usecaseui.server.bean.csmf.ServiceCreateResult;
+import org.onap.usecaseui.server.bean.csmf.SlicingOrder;
 import org.onap.usecaseui.server.bean.intent.InstancePerformance;
 import org.onap.usecaseui.server.bean.intent.CCVPNInstance;
+import org.onap.usecaseui.server.bean.intent.IntentInstance;
+import org.onap.usecaseui.server.bean.nsmf.common.ServiceResult;
 import org.onap.usecaseui.server.constant.IntentConstant;
+import org.onap.usecaseui.server.service.csmf.SlicingService;
 import org.onap.usecaseui.server.service.intent.IntentApiService;
 import org.onap.usecaseui.server.service.intent.IntentInstanceService;
 import org.onap.usecaseui.server.service.lcm.domain.so.SOService;
+import org.onap.usecaseui.server.service.nsmf.ResourceMgtService;
 import org.onap.usecaseui.server.util.Page;
 import org.onap.usecaseui.server.util.RestfulServices;
 import org.onap.usecaseui.server.util.UuiCommonUtil;
@@ -39,6 +45,7 @@ import org.springframework.stereotype.Service;
 import retrofit2.Call;
 import retrofit2.Response;
 
+import javax.annotation.Resource;
 import javax.transaction.Transactional;
 import java.io.*;
 import java.text.SimpleDateFormat;
@@ -56,6 +63,11 @@ public class IntentInstanceServiceImpl implements IntentInstanceService {
     @Autowired
     private SessionFactory sessionFactory;
 
+    @Resource(name = "ResourceMgtService")
+    private ResourceMgtService resourceMgtService;
+
+    @Resource(name = "SlicingService")
+    private SlicingService slicingService;
 
     private IntentApiService intentApiService;
 
@@ -151,7 +163,7 @@ public class IntentInstanceServiceImpl implements IntentInstanceService {
     }
 
     @Override
-    public int createIntentInstance(CCVPNInstance instance) {
+    public int createCCVPNInstance(CCVPNInstance instance) {
         Session session = getSession();
         Transaction tx = null;
         try{
@@ -711,6 +723,154 @@ public class IntentInstanceServiceImpl implements IntentInstanceService {
         intentApiService.addCustomer(globalCustomerId, requestBody).execute();
     }
 
+    @Override
+    public IntentInstance createIntentInstance(Object body,String businessInstanceId, String businessInstance, String type) {
+        IntentInstance instance = new IntentInstance();
+        if (IntentConstant.MODEL_TYPE_CCVPN.equals(type)) {
+            assembleIntentInstanceFormCCVPNInfo(instance, body);
+        }
+        else if (IntentConstant.MODEL_TYPE_5GS.equals(type)) {
+            assembleIntentInstanceFormSliceInfo(instance, body);
+        }
+        instance.setIntentSource(type);
+        instance.setBusinessInstanceId(businessInstanceId);
+        instance.setBusinessInstance(businessInstance);
+        Session session = getSession();
+        Transaction tx = null;
+        try{
+            tx = session.beginTransaction();
+            session.save(instance);
+            tx.commit();
+            return instance;
+        } catch (Exception e) {
+            if (tx != null) {
+                tx.rollback();
+            }
+            logger.error("createIntentInstance Details:" + e.getMessage());
+            return null;
+        } finally {
+            session.close();
+        }
+    }
+
+    private IntentInstance assembleIntentInstanceFormCCVPNInfo(IntentInstance instance, Object body) {
+        JSONObject jsonObject = new JSONObject((Map) body);
+        String intent_content = jsonObject.getString("intentContent");
+        jsonObject.remove("intentContent");
+        instance.setIntentConfig(jsonObject.toJSONString());
+        instance.setIntentContent(intent_content);
+        instance.setIntentName(jsonObject.getString("name"));
+        return instance;
+    }
+
+    private IntentInstance assembleIntentInstanceFormSliceInfo(IntentInstance instance, Object body) {
+        JSONObject jsonObject = new JSONObject((Map) body);
+        JSONObject slicingOrderInfo = jsonObject.getJSONObject("slicing_order_info");
+        String intent_content = slicingOrderInfo.getString("intentContent");
+        slicingOrderInfo.remove("intentContent");
+        instance.setIntentConfig(slicingOrderInfo.toJSONString());
+        instance.setIntentContent(intent_content);
+        instance.setIntentName(slicingOrderInfo.getString("name"));
+        return instance;
+    }
+
+
+    @Override
+    public void deleteIntent(int id) {
+        Transaction tx = null;
+        Session session = getSession();
+        try {
+            IntentInstance intentInstance = (IntentInstance)session.createQuery("from IntentInstance where id = :id")
+                    .setParameter("id", id).uniqueResult();
+            if (IntentConstant.MODEL_TYPE_CCVPN.equals(intentInstance.getIntentSource())) {
+                deleteIntentInstance(intentInstance.getBusinessInstanceId());
+            } else {
+                resourceMgtService.terminateSlicingService(intentInstance.getBusinessInstanceId());
+            }
+
+
+            tx = session.beginTransaction();
+            session.delete(intentInstance);
+            tx.commit();
+            logger.info("delete IntentInstance OK, id=" + intentInstance.getId());
+        } catch (Exception e) {
+            if(tx!=null){
+                tx.rollback();
+            }
+            logger.error("delete IntentInstance occur exception:"+e);
+
+        } finally {
+            session.close();
+        }
+    }
+
+    @Override
+    public void verifyIntent(int id) {
+        Session session = getSession();
+        IntentInstance instance = new IntentInstance();
+        try {
+            String hql = "from IntentInstance where id = :id";
+            Query query = session.createQuery(hql).setParameter("id", id);
+            instance = (IntentInstance) query.uniqueResult();
+
+        } catch (Exception e) {
+            logger.error("verifyIntentInstance error. Details:" + e.getMessage());
+        } finally {
+            session.close();
+        }
+    }
+
+    @Override
+    public Page<IntentInstance> getIntentInstanceList(int currentPage, int pageSize) {
+        Page<IntentInstance> page = new Page<IntentInstance>();
+        int allRow = getIntentInstanceAllCount();
+        int offset = page.countOffset(currentPage, pageSize);
+        Session session = getSession();
+        try{
+            String hql = "from IntentInstance order by id";
+            Query query = session.createQuery(hql);
+            query.setFirstResult(offset);
+            query.setMaxResults(pageSize);
+            List<IntentInstance> list= query.list();
+            page.setPageNo(currentPage);
+            page.setPageSize(pageSize);
+            page.setTotalRecords(allRow);
+            page.setList(list);
+            return page;
+        } catch (Exception e) {
+            logger.error("exception occurred while performing IntentInstanceServiceImpl getIntentInstanceList. Details:" + e.getMessage());
+            return null;
+        } finally {
+            session.close();
+        }
+    }
+
+    @Override
+    public ServiceResult createSlicingServiceWithIntent(Object slicingOrderBody) {
+
+        SlicingOrder slicingOrder = JSONObject.parseObject(JSONObject.toJSONString(slicingOrderBody), SlicingOrder.class);
+        ServiceResult serviceResult = slicingService.createSlicingService(slicingOrder);
+        ServiceCreateResult createResult = (ServiceCreateResult) serviceResult.getResult_body();
+
+        createIntentInstance(slicingOrderBody,createResult.getService_id(), slicingOrder.getSlicing_order_info().getName(), IntentConstant.MODEL_TYPE_5GS);
+        return serviceResult;
+    }
+
+    public int getIntentInstanceAllCount() {
+        Session session = getSession();
+        try{
+            String count="select count(*) from IntentInstance";
+            Query query = session.createQuery(count);
+            long q=(long)query.uniqueResult();
+            return (int)q;
+        } catch (Exception e) {
+            logger.error("exception occurred while performing IntentInstanceServiceImpl getAllCount. Details:" + e.getMessage());
+            return -1;
+        } finally {
+            session.close();
+        }
+    }
+
     public void addSubscription() throws IOException {
         Properties environment = getProperties();
         String globalCustomerId = environment.getProperty("ccvpn.globalCustomerId");
index 2300e8e..1aa92c9 100644 (file)
@@ -243,8 +243,8 @@ public class IntentControllerTest {
         accessPointOne.put("name","name");
         accessPointOne.put("bandwidth","1");
         body.put("accessPointOne",accessPointOne);
-        Mockito.when(intentInstanceService.createIntentInstance(any())).thenReturn(1);
-        assertEquals(intentController.createIntentInstance(body), "OK");
+        Mockito.when(intentInstanceService.createCCVPNInstance(any())).thenReturn(1);
+        assertEquals(intentController.createCCVPNInstance(body), "OK");
     }
     @Test
     public void getFinishedInstanceInfo() {
index de7a58c..90353d4 100644 (file)
@@ -34,13 +34,21 @@ import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.Mockito;
+import org.onap.usecaseui.server.bean.csmf.ServiceCreateResult;
+import org.onap.usecaseui.server.bean.csmf.SlicingOrder;
+import org.onap.usecaseui.server.bean.csmf.SlicingOrderDetail;
 import org.onap.usecaseui.server.bean.intent.CCVPNInstance;
 import org.onap.usecaseui.server.bean.intent.InstancePerformance;
+import org.onap.usecaseui.server.bean.intent.IntentInstance;
 import org.onap.usecaseui.server.bean.intent.IntentModel;
+import org.onap.usecaseui.server.bean.nsmf.common.ServiceResult;
+import org.onap.usecaseui.server.constant.IntentConstant;
+import org.onap.usecaseui.server.service.csmf.SlicingService;
 import org.onap.usecaseui.server.service.intent.IntentApiService;
 import org.onap.usecaseui.server.service.lcm.domain.so.SOService;
 import org.onap.usecaseui.server.service.lcm.domain.so.bean.OperationProgress;
 import org.onap.usecaseui.server.service.lcm.domain.so.bean.OperationProgressInformation;
+import org.onap.usecaseui.server.service.nsmf.ResourceMgtService;
 import org.powermock.api.mockito.PowerMockito;
 import org.powermock.api.support.membermodification.MemberModifier;
 import org.powermock.modules.junit4.PowerMockRunner;
@@ -53,6 +61,7 @@ import retrofit2.Call;
 import retrofit2.Response;
 
 import javax.annotation.Nullable;
+import javax.annotation.Resource;
 
 @RunWith(PowerMockRunner.class)
 public class IntentInstanceServiceImplTest {
@@ -69,6 +78,13 @@ public class IntentInstanceServiceImplTest {
     @Mock
     private SOService soService;
 
+    @Mock
+    @Resource(name = "ResourceMgtService")
+    private ResourceMgtService resourceMgtService;
+
+    @Mock
+    @Resource(name = "SlicingService")
+    private SlicingService slicingService;
 
     @Mock
     private SessionFactory sessionFactory;
@@ -79,6 +95,8 @@ public class IntentInstanceServiceImplTest {
     @Before
     public void before() throws Exception {
         MemberModifier.field(IntentInstanceServiceImpl.class, "sessionFactory").set(intentInstanceService , sessionFactory);
+        MemberModifier.field(IntentInstanceServiceImpl.class, "resourceMgtService").set(intentInstanceService , resourceMgtService);
+        MemberModifier.field(IntentInstanceServiceImpl.class, "slicingService").set(intentInstanceService , slicingService);
         doReturn(session).when(sessionFactory,"openSession");
     }
 
@@ -124,7 +142,7 @@ public class IntentInstanceServiceImplTest {
         assertEquals(intentInstanceService.queryIntentInstance(instance,1,2), null);
     }
     @Test
-    public void createIntentInstanceTest() throws IOException {
+    public void createCCVPNInstanceTest() throws IOException {
         CCVPNInstance instance = new CCVPNInstance();
         instance.setInstanceId("1");
         instance.setJobId("1");
@@ -145,11 +163,11 @@ public class IntentInstanceServiceImplTest {
         Mockito.when(session.save(any())).thenReturn(save);
         Mockito.doNothing().when(tx).commit();
 
-        assertEquals(spy.createIntentInstance(instance), 1);
+        assertEquals(spy.createCCVPNInstance(instance), 1);
     }
 
     @Test
-    public void createIntentInstanceThrowErrorTest() throws IOException {
+    public void createCCVPNInstanceThrowErrorTest() throws IOException {
         CCVPNInstance instance = new CCVPNInstance();
         instance.setInstanceId("1");
         instance.setJobId("1");
@@ -170,19 +188,19 @@ public class IntentInstanceServiceImplTest {
         Mockito.when(session.save(any())).thenReturn(save);
         Mockito.doNothing().when(tx).commit();
 
-        assertEquals(spy.createIntentInstance(instance), 0);
+        assertEquals(spy.createCCVPNInstance(instance), 0);
     }
 
     @Test
-    public void createIntentInstanceInstanceIsNullTest() throws IOException {
-        assertEquals(intentInstanceService.createIntentInstance(null), 0);
+    public void createCCVPNInstanceInstanceIsNullTest() throws IOException {
+        assertEquals(intentInstanceService.createCCVPNInstance(null), 0);
     }
     @Test
-    public void createIntentInstanceInstanceJobIdIsNullTest() throws IOException {
+    public void createCCVPNInstanceInstanceJobIdIsNullTest() throws IOException {
         CCVPNInstance instance = new CCVPNInstance();
         instance.setInstanceId("1");
         instance.setStatus("1");
-        assertEquals(intentInstanceService.createIntentInstance(instance), 0);
+        assertEquals(intentInstanceService.createCCVPNInstance(instance), 0);
     }
 
     @Test
@@ -777,4 +795,196 @@ public class IntentInstanceServiceImplTest {
         Mockito.verify(intentApiService, Mockito.times(1)).deleteServiceInstance(anyString(),anyString(),anyString(),any());
 
     }
+    @Test
+    public void createIntentInstanceWithCCVPNInstanceTest() {
+        Map<String, Object> body = new HashMap<>();
+        body.put("intentContent", "this is intent content");
+        body.put("name", "this is name");
+        Transaction tx = PowerMockito.mock(Transaction.class);
+        when(session.beginTransaction()).thenReturn(tx);
+
+        Serializable save = Mockito.mock(Serializable.class);
+        when(session.save(any(IntentInstance.class))).thenReturn(save);
+
+        doNothing().when(tx).commit();
+        doNothing().when(session).close();
+        IntentInstance instance = intentInstanceService.createIntentInstance(body, "id", "name", IntentConstant.MODEL_TYPE_CCVPN);
+        assertEquals(instance.getBusinessInstanceId(), "id");
+    }
+    @Test
+    public void createIntentInstanceWithSlicingInstanceTest() {
+        Map<String, Object> slicingOrderInfo = new HashMap<>();
+        slicingOrderInfo.put("intentContent", "this is intent content");
+        slicingOrderInfo.put("name", "this is name");
+
+        Map<String, Object> body = new HashMap<>();
+        body.put("slicing_order_info", slicingOrderInfo);
+        Transaction tx = PowerMockito.mock(Transaction.class);
+        when(session.beginTransaction()).thenReturn(tx);
+
+        Serializable save = Mockito.mock(Serializable.class);
+        when(session.save(any(IntentInstance.class))).thenReturn(save);
+
+        doNothing().when(tx).commit();
+        doNothing().when(session).close();
+        IntentInstance instance = intentInstanceService.createIntentInstance(body, "id", "name", IntentConstant.MODEL_TYPE_5GS);
+        assertEquals(instance.getBusinessInstanceId(), "id");
+    }
+    @Test
+    public void createIntentInstanceWithThrowErrorTest() {
+        Map<String, Object> slicingOrderInfo = new HashMap<>();
+        slicingOrderInfo.put("intentContent", "this is intent content");
+        slicingOrderInfo.put("name", "this is name");
+
+        Map<String, Object> body = new HashMap<>();
+        body.put("slicing_order_info", slicingOrderInfo);
+        Transaction tx = PowerMockito.mock(Transaction.class);
+        when(session.beginTransaction()).thenReturn(tx);
+
+        when(session.save(any(IntentInstance.class))).thenThrow(new RuntimeException());
+
+        doNothing().when(session).close();
+        IntentInstance instance = intentInstanceService.createIntentInstance(body, "id", "name", IntentConstant.MODEL_TYPE_5GS);
+        assertNull(instance);
+    }
+
+    @Test
+    public void deleteIntentWithDeleteCCVPNInstanceTest() {
+
+        IntentInstanceServiceImpl spy = spy(intentInstanceService);
+
+        IntentInstance instance = new IntentInstance();
+        instance.setId(1);
+        instance.setIntentSource(IntentConstant.MODEL_TYPE_CCVPN);
+        instance.setBusinessInstanceId("1");
+
+        Query query = PowerMockito.mock(Query.class);
+        when(session.createQuery(anyString())).thenReturn(query);
+        when(query.setParameter("id", 1)).thenReturn(query);
+        when(query.uniqueResult()).thenReturn(instance);
+
+        doNothing().when(spy).deleteIntentInstance(anyString());
+
+        Transaction tx = PowerMockito.mock(Transaction.class);
+        when(session.beginTransaction()).thenReturn(tx);
+        doNothing().when(session).delete(any());
+        doNothing().when(tx).commit();
+        doNothing().when(session).close();
+
+        spy.deleteIntent(1);
+
+        Mockito.verify(spy, Mockito.times(1)).deleteIntentInstance("1");
+    }
+
+    @Test
+    public void deleteIntentWithDeleteSlicingInstanceTest() {
+
+
+        IntentInstance instance = new IntentInstance();
+        instance.setId(1);
+        instance.setIntentSource(IntentConstant.MODEL_TYPE_5GS);
+        instance.setBusinessInstanceId("1");
+
+        Query query = PowerMockito.mock(Query.class);
+        when(session.createQuery(anyString())).thenReturn(query);
+        when(query.setParameter("id", 1)).thenReturn(query);
+        when(query.uniqueResult()).thenReturn(instance);
+
+        ServiceResult serviceResult = new ServiceResult();
+        when(resourceMgtService.terminateSlicingService(anyString())).thenReturn(serviceResult);
+
+        Transaction tx = PowerMockito.mock(Transaction.class);
+        when(session.beginTransaction()).thenReturn(tx);
+        doNothing().when(session).delete(any());
+        doNothing().when(tx).commit();
+        doNothing().when(session).close();
+
+        intentInstanceService.deleteIntent(1);
+
+        Mockito.verify(resourceMgtService, Mockito.times(1)).terminateSlicingService(anyString());
+    }
+    @Test
+    public void deleteIntentWithThrowErrorTest() {
+
+
+        IntentInstance instance = new IntentInstance();
+        instance.setId(1);
+        instance.setIntentSource(IntentConstant.MODEL_TYPE_5GS);
+        instance.setBusinessInstanceId("1");
+
+        when(session.createQuery(anyString())).thenThrow(new RuntimeException());
+
+        doNothing().when(session).close();
+
+        intentInstanceService.deleteIntent(1);
+
+        Mockito.verify(resourceMgtService, Mockito.times(0)).terminateSlicingService(anyString());
+    }
+
+    @Test
+    public void getIntentInstanceListTest() {
+        IntentInstanceServiceImpl spy = spy(intentInstanceService);
+        doReturn(2).when(spy).getIntentInstanceAllCount();
+
+        Query query = PowerMockito.mock(Query.class);
+        when(session.createQuery("from IntentInstance order by id")).thenReturn(query);
+        when(query.setFirstResult(anyInt())).thenReturn(query);
+        when(query.setMaxResults(anyInt())).thenReturn(query);
+
+        List<IntentInstance> list = new ArrayList<>();
+        list.add(new IntentInstance());
+        list.add(new IntentInstance());
+        when(query.list()).thenReturn(list);
+        doNothing().when(session).close();
+        int totalRecords = spy.getIntentInstanceList(1, 10).getTotalRecords();
+        assertEquals(totalRecords,2);
+    }
+
+    @Test
+    public void getIntentInstanceListThrowErrorTest() {
+        IntentInstanceServiceImpl spy = spy(intentInstanceService);
+        doReturn(2).when(spy).getIntentInstanceAllCount();
+
+        when(session.createQuery("from IntentInstance order by id")).thenThrow(new RuntimeException());
+        doNothing().when(session).close();
+        assertEquals(spy.getIntentInstanceList(1, 10),null);
+    }
+
+    @Test
+    public void createSlicingServiceWithIntent() {
+        IntentInstanceServiceImpl spy = spy(intentInstanceService);
+
+        SlicingOrder slicingOrder = new SlicingOrder();
+        slicingOrder.setSlicing_order_info(new SlicingOrderDetail());
+        slicingOrder.getSlicing_order_info().setName("name");
+
+        ServiceResult serviceResult = new ServiceResult();
+        ServiceCreateResult serviceCreateResult = new ServiceCreateResult();
+        serviceCreateResult.setService_id("id");
+        serviceResult.setResult_body(serviceCreateResult);
+        when(slicingService.createSlicingService(any())).thenReturn(serviceResult);
+
+        IntentInstance instance = new IntentInstance();
+        doReturn(instance).when(spy).createIntentInstance(any(),anyString(),anyString(),anyString());
+
+        assertEquals(spy.createSlicingServiceWithIntent(slicingOrder), serviceResult);
+    }
+
+    @Test
+    public void getIntentInstanceAllCountTest() {
+
+        Query query = PowerMockito.mock(Query.class);
+        when(session.createQuery("select count(*) from IntentInstance")).thenReturn(query);
+        when(query.uniqueResult()).thenReturn(2L);
+
+
+        assertEquals(intentInstanceService.getIntentInstanceAllCount(),2);
+    }
+
+    @Test
+    public void getIntentInstanceAllCountThrowErrorTest() {
+
+        when(session.createQuery("select count(*) from IntentInstance")).thenThrow(new RuntimeException());
+        assertEquals(intentInstanceService.getIntentInstanceAllCount(),-1);
+    }
 }
\ No newline at end of file
index d7201f5..9e45d5b 100644 (file)
@@ -202,3 +202,21 @@ create table intent_model
     active      integer,
     model_type integer      default 0
 );
+
+-- ----------------------------
+-- Table structure for intent_instance
+-- ----------------------------
+DROP TABLE IF EXISTS intent_instance;
+create table intent_instance
+(
+    id                   serial not null
+        constraint intent_instance_pk
+            primary key,
+    intent_name          varchar(50),
+    intent_source        integer,
+    customer             varchar(50),
+    intent_content       text,
+    intent_config        text,
+    business_instance_id varchar(50),
+    business_instance    varchar(255)
+);