diff options
author | 2022-03-10 17:22:54 +0800 | |
---|---|---|
committer | 2022-03-11 06:02:40 +0000 | |
commit | 608d55685d920851c25523c2418b249811dbf0b7 (patch) | |
tree | 476585dff19b4b9ac7d9b5a8c0ae970cab50e0e7 /server/src/test/java/org/onap/usecaseui | |
parent | aa8f3448bab8a457ca96a43c3f0b0d615bdd871b (diff) |
feat:Create an entry for the unified intent instance, save the instance information to AAI, and add test code.
Issue-ID: REQ-1075
Signed-off-by: ’zhaoyh6‘ <zhaoyh6@asiainfo.com>
Change-Id: Iae5d246441d06b8ba30cdde5b14ae2202d46b85a
Diffstat (limited to 'server/src/test/java/org/onap/usecaseui')
3 files changed, 667 insertions, 42 deletions
diff --git a/server/src/test/java/org/onap/usecaseui/server/controller/IntentControllerTest.java b/server/src/test/java/org/onap/usecaseui/server/controller/IntentControllerTest.java index e82ed0af..2300e8e6 100644 --- a/server/src/test/java/org/onap/usecaseui/server/controller/IntentControllerTest.java +++ b/server/src/test/java/org/onap/usecaseui/server/controller/IntentControllerTest.java @@ -16,6 +16,7 @@ package org.onap.usecaseui.server.controller; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.junit.Before; import org.junit.Test; @@ -29,10 +30,12 @@ import org.onap.usecaseui.server.bean.intent.IntentModel; import org.onap.usecaseui.server.service.intent.IntentInstanceService; import org.onap.usecaseui.server.service.intent.IntentService; import org.onap.usecaseui.server.util.HttpUtil; +import org.onap.usecaseui.server.util.UploadFileUtil; import org.powermock.api.mockito.PowerMockito; import org.powermock.api.support.membermodification.MemberModifier; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.io.File; @@ -46,12 +49,14 @@ import java.util.List; import java.util.Map; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; -import static org.powermock.api.mockito.PowerMockito.when; +import static org.powermock.api.mockito.PowerMockito.*; +import static org.powermock.api.support.membermodification.MemberMatcher.method; @RunWith(PowerMockRunner.class) -@PrepareForTest({HttpUtil.class}) +@PrepareForTest({HttpUtil.class,UploadFileUtil.class}) public class IntentControllerTest { public IntentControllerTest(){} @@ -74,6 +79,40 @@ public class IntentControllerTest { } @Test + public void uploadModelTest() throws Exception { + MultipartFile file=PowerMockito.mock(MultipartFile.class); + PowerMockito.when(file.getOriginalFilename()).thenReturn("filename.zip"); + IntentController spy = PowerMockito.spy(intentController); + File dest=PowerMockito.mock(File.class); + when(spy.newFile(anyString())).thenReturn(dest); + File parent=PowerMockito.mock(File.class); + when(dest.getParentFile()).thenReturn(parent); + when(parent.mkdirs()).thenReturn(true); + doNothing().when(file).transferTo(dest); + when(dest.length()).thenReturn(1024L); + PowerMockito.mockStatic(UploadFileUtil.class); + when(UploadFileUtil.formUpload(anyString(), any(Map.class), any(Map.class),anyString())).thenReturn("ok"); + when(intentService.addModel(any(IntentModel.class))).thenReturn("1"); + assertEquals(spy.uploadModel(file, "5gs"), "1"); + } + @Test + public void uploadModelTestThrowError() throws Exception { + MultipartFile file=PowerMockito.mock(MultipartFile.class); + PowerMockito.when(file.getOriginalFilename()).thenReturn("filename.zip"); + IntentController spy = PowerMockito.spy(intentController); + File dest=PowerMockito.mock(File.class); + when(spy.newFile(anyString())).thenReturn(dest); + File parent=PowerMockito.mock(File.class); + when(dest.getParentFile()).thenReturn(parent); + when(parent.mkdirs()).thenReturn(true); + doThrow(new RuntimeException()).when(file).transferTo(dest); + + assertEquals(spy.uploadModel(file, "5gs"), "0"); + + } + + + @Test public void activeModelTest() { IntentModel model = new IntentModel(); String path = "path"; @@ -98,11 +137,17 @@ public class IntentControllerTest { when(intentService.deleteModel(anyString())).thenReturn("1"); File file=PowerMockito.mock(File.class); - PowerMockito.whenNew(File.class).withArguments(Mockito.anyString()).thenReturn(file); + IntentController spy = PowerMockito.spy(intentController); + when(spy.newFile(anyString())).thenReturn(file); PowerMockito.when(file.exists()).thenReturn(true); PowerMockito.when(file.delete()).thenReturn(true); - assertEquals(intentController.deleteModel(modelId), "1"); + HttpResponseResult mock = PowerMockito.mock(HttpResponseResult.class); + PowerMockito.mockStatic(HttpUtil.class); + when(HttpUtil.sendGetRequest(anyString(), any(Map.class))).thenReturn(mock); + when(mock.getResultContent()).thenReturn("{}"); + + assertEquals(spy.deleteModel(modelId), "1"); } @@ -110,23 +155,67 @@ public class IntentControllerTest { public void predictTest() throws ParseException { Map<String,Object> body = new HashMap<>(); body.put("text", "text"); + body.put("modelType", "5gs"); String respContent = ""; HttpResponseResult mock = PowerMockito.mock(HttpResponseResult.class); PowerMockito.mockStatic(HttpUtil.class); Mockito.when(HttpUtil.sendPostRequestByJson(anyString(), any(Map.class), anyString())).thenReturn(mock); - when(mock.getResultContent()).thenReturn("{'Region':'chengnan'}"); + when(mock.getResultContent()).thenReturn("{'Area':'chengnan'}"); when(intentService.calcFieldValue(anyString(), anyString())).thenReturn("Beijing Changping District Chengnan Street"); - String predict = intentController.predict(body); - JSONObject jsonObject = JSON.parseObject(predict); + when(intentService.getActiveModelType()).thenReturn("5gs"); + Map<String, Object> predict = intentController.predict(body); + JSONObject jsonObject = new JSONObject(predict); assertEquals(jsonObject.getString("coverageArea"), "Beijing Changping District Chengnan Street"); } @Test + public void unifyPredict_5gs_Test() throws ParseException { + Map<String,Object> body = new HashMap<>(); + body.put("text", "Service"); + String respContent = ""; + when(intentService.getModelTypeByIntentText(anyString())).thenReturn("5gs"); + when(intentService.getActiveModelType()).thenReturn("5gs"); + + HttpResponseResult mock = PowerMockito.mock(HttpResponseResult.class); + PowerMockito.mockStatic(HttpUtil.class); + Mockito.when(HttpUtil.sendPostRequestByJson(anyString(), any(Map.class), anyString())).thenReturn(mock); + when(mock.getResultContent()).thenReturn("{'Area':'chengnan'}"); + when(intentService.calcFieldValue(anyString(), anyString())).thenReturn("Beijing Changping District Chengnan Street"); + Map<String, Object> predict = intentController.unifyPredict(body); + JSONObject jsonObject = new JSONObject(predict); + + assertEquals(jsonObject.getString("type"), "5gs"); + assertEquals(jsonObject.getJSONObject("formData").getString("coverageArea"), "Beijing Changping District Chengnan Street"); + } + @Test + public void unifyPredict_ccvpn_Test() throws ParseException { + Map<String,Object> body = new HashMap<>(); + body.put("text", "I need create a Cloud Leased Line, I need a line from Access two to Cloud one, 20Gbps"); + String respContent = ""; + when(intentService.getModelTypeByIntentText(anyString())).thenReturn("ccvpn"); + when(intentService.getActiveModelType()).thenReturn("ccvpn"); + + HttpResponseResult mock = PowerMockito.mock(HttpResponseResult.class); + PowerMockito.mockStatic(HttpUtil.class); + Mockito.when(HttpUtil.sendPostRequestByJson(anyString(), any(Map.class), anyString())).thenReturn(mock); + when(mock.getResultContent()).thenReturn("{'access point':'','cloud point':'','bandwidth':''}"); + when(intentInstanceService.formatAccessPoint(anyString())).thenReturn(""); + when(intentInstanceService.formatCloudPoint(anyString())).thenReturn(""); + Map<String, Object> predict = intentController.unifyPredict(body); + JSONObject jsonObject = new JSONObject(predict); + + + assertEquals(jsonObject.getString("type"), "ccvpn"); + assertEquals(jsonObject.getJSONObject("formData").getJSONObject("accessPointOne").getString("name"), "tranportEp_src_ID_111_2"); + assertEquals(jsonObject.getJSONObject("formData").getString("cloudPointName"), "tranportEp_dst_ID_212_1"); + } + + @Test public void tranlateFieldNameTest() throws InvocationTargetException, IllegalAccessException { - String key = "Region"; + String key = "Area"; IntentController spy = PowerMockito.spy(intentController); - Method method = PowerMockito.method(IntentController.class, "tranlateFieldName", String.class); + Method method = method(IntentController.class, "tranlateFieldName", String.class); Object result = method.invoke(spy, key); assertEquals(result, "coverageArea"); } @@ -200,4 +289,16 @@ public class IntentControllerTest { Mockito.when(intentInstanceService.queryAccessNodeInfo()).thenReturn("ok"); assertEquals(intentController.queryAccessNodeInfo(), "ok"); } + + @Test + public void getInstanceStatusTest() { + Map<String, Object> body = new HashMap<>(); + List<String> ids = new ArrayList<>(); + ids.add("1"); + ids.add("2"); + ids.add("3"); + body.put("ids", ids); + when(intentInstanceService.getInstanceStatus(any(JSONArray.class))).thenReturn(new JSONObject()); + assertTrue(intentController.getInstanceStatus(body) instanceof JSONObject); + } }
\ No newline at end of file diff --git a/server/src/test/java/org/onap/usecaseui/server/service/intent/impl/IntentInstanceServiceImplTest.java b/server/src/test/java/org/onap/usecaseui/server/service/intent/impl/IntentInstanceServiceImplTest.java index 8e591ea6..de7a58c8 100644 --- a/server/src/test/java/org/onap/usecaseui/server/service/intent/impl/IntentInstanceServiceImplTest.java +++ b/server/src/test/java/org/onap/usecaseui/server/service/intent/impl/IntentInstanceServiceImplTest.java @@ -17,11 +17,13 @@ package org.onap.usecaseui.server.service.intent.impl; import java.io.IOException; import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; +import java.util.*; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import okhttp3.MediaType; +import okhttp3.ResponseBody; +import okio.BufferedSource; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; @@ -33,6 +35,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; import org.onap.usecaseui.server.bean.intent.CCVPNInstance; +import org.onap.usecaseui.server.bean.intent.InstancePerformance; import org.onap.usecaseui.server.bean.intent.IntentModel; import org.onap.usecaseui.server.service.intent.IntentApiService; import org.onap.usecaseui.server.service.lcm.domain.so.SOService; @@ -43,14 +46,14 @@ import org.powermock.api.support.membermodification.MemberModifier; import org.powermock.modules.junit4.PowerMockRunner; import static org.junit.Assert.*; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.powermock.api.mockito.PowerMockito.doReturn; -import static org.powermock.api.mockito.PowerMockito.when; +import static org.mockito.ArgumentMatchers.*; +import static org.powermock.api.mockito.PowerMockito.*; import retrofit2.Call; import retrofit2.Response; +import javax.annotation.Nullable; + @RunWith(PowerMockRunner.class) public class IntentInstanceServiceImplTest { @@ -80,7 +83,22 @@ public class IntentInstanceServiceImplTest { } @Test - public void queryIntentInstance() { + public void queryIntentInstanceTest() { + CCVPNInstance instance = new CCVPNInstance(); + instance.setInstanceId("1"); + instance.setJobId("1"); + instance.setStatus("1"); + + Query query = Mockito.mock(Query.class); + when(session.createQuery(anyString())).thenReturn(query); + List<IntentModel> list = new ArrayList<>(); + when(query.list()).thenReturn(list); + when(query.uniqueResult()).thenReturn(10L); + assertTrue(intentInstanceService.queryIntentInstance(instance,1,2).getList().isEmpty()); + } + + @Test + public void queryIntentInstanceGetCountErrorTest() { CCVPNInstance instance = new CCVPNInstance(); instance.setInstanceId("1"); instance.setJobId("1"); @@ -93,8 +111,20 @@ public class IntentInstanceServiceImplTest { when(query.uniqueResult()).thenReturn(10); assertTrue(intentInstanceService.queryIntentInstance(instance,1,2).getList().isEmpty()); } + + @Test + public void queryIntentInstanceThrowErrorTest() { + CCVPNInstance instance = new CCVPNInstance(); + instance.setInstanceId("1"); + instance.setJobId("1"); + instance.setStatus("1"); + + when(session.createQuery(anyString())).thenThrow(new RuntimeException()); + + assertEquals(intentInstanceService.queryIntentInstance(instance,1,2), null); + } @Test - public void createIntentInstance() throws IOException { + public void createIntentInstanceTest() throws IOException { CCVPNInstance instance = new CCVPNInstance(); instance.setInstanceId("1"); instance.setJobId("1"); @@ -106,6 +136,84 @@ public class IntentInstanceServiceImplTest { Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall); Mockito.when(mockCall.execute()).thenReturn(response); + IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService); + doNothing().when(spy).saveIntentInstanceToAAI(isNull(),any(CCVPNInstance.class)); + + Transaction tx = Mockito.mock(Transaction.class); + Mockito.when(session.beginTransaction()).thenReturn(tx); + Serializable save = Mockito.mock(Serializable.class); + Mockito.when(session.save(any())).thenReturn(save); + Mockito.doNothing().when(tx).commit(); + + assertEquals(spy.createIntentInstance(instance), 1); + } + + @Test + public void createIntentInstanceThrowErrorTest() throws IOException { + CCVPNInstance instance = new CCVPNInstance(); + instance.setInstanceId("1"); + instance.setJobId("1"); + instance.setStatus("1"); + + Call mockCall = PowerMockito.mock(Call.class); + JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}"); + Response<JSONObject> response = Response.success(body); + Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall); + Mockito.when(mockCall.execute()).thenReturn(response); + + IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService); + doThrow(new RuntimeException()).when(spy).saveIntentInstanceToAAI(isNull(),any(CCVPNInstance.class)); + + Transaction tx = Mockito.mock(Transaction.class); + Mockito.when(session.beginTransaction()).thenReturn(tx); + Serializable save = Mockito.mock(Serializable.class); + Mockito.when(session.save(any())).thenReturn(save); + Mockito.doNothing().when(tx).commit(); + + assertEquals(spy.createIntentInstance(instance), 0); + } + + @Test + public void createIntentInstanceInstanceIsNullTest() throws IOException { + assertEquals(intentInstanceService.createIntentInstance(null), 0); + } + @Test + public void createIntentInstanceInstanceJobIdIsNullTest() throws IOException { + CCVPNInstance instance = new CCVPNInstance(); + instance.setInstanceId("1"); + instance.setStatus("1"); + assertEquals(intentInstanceService.createIntentInstance(instance), 0); + } + + @Test + public void getIntentInstanceProgressTest() throws IOException { + + Query query1 = Mockito.mock(Query.class); + when(session.createQuery("from CCVPNInstance where deleteState = 0 and status = '0'")).thenReturn(query1); + List<CCVPNInstance> q = new ArrayList<>(); + CCVPNInstance instance = new CCVPNInstance(); + instance.setInstanceId("1"); + instance.setResourceInstanceId("1"); + instance.setJobId("1"); + q.add(instance); + when(query1.list()).thenReturn(q); + + OperationProgressInformation operationProgressInformation = new OperationProgressInformation(); + OperationProgress operationProgress = new OperationProgress(); + operationProgress.setProgress(100); + operationProgressInformation.setOperationStatus(operationProgress); + + JSONObject jsonObject = new JSONObject(); + JSONObject operation = new JSONObject(); + operation.put("progress", 100); + jsonObject.put("operation", operation); + Call mockCall = PowerMockito.mock(Call.class); + Response<JSONObject> response = Response.success(jsonObject); + Mockito.when(intentApiService.queryOperationProgress(anyString(),anyString())).thenReturn(mockCall); + Mockito.when(mockCall.execute()).thenReturn(response); + + IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService); + doNothing().when(spy).saveIntentInstanceToAAI(anyString(),any(CCVPNInstance.class)); Transaction tx = Mockito.mock(Transaction.class); Mockito.when(session.beginTransaction()).thenReturn(tx); @@ -113,16 +221,19 @@ public class IntentInstanceServiceImplTest { Mockito.when(session.save(any())).thenReturn(save); Mockito.doNothing().when(tx).commit(); - assertEquals(intentInstanceService.createIntentInstance(instance), 1); + spy.getIntentInstanceProgress(); + assertEquals(operation.getString("progress"),"100"); } @Test - public void getIntentInstanceProgress() throws IOException { + public void getIntentInstanceCreateStatusTest() throws IOException { Query query1 = Mockito.mock(Query.class); when(session.createQuery("from CCVPNInstance where deleteState = 0 and status = '0'")).thenReturn(query1); List<CCVPNInstance> q = new ArrayList<>(); CCVPNInstance instance = new CCVPNInstance(); instance.setInstanceId("1"); + instance.setResourceInstanceId("1"); + instance.setJobId("1"); q.add(instance); when(query1.list()).thenReturn(q); @@ -130,18 +241,25 @@ public class IntentInstanceServiceImplTest { OperationProgress operationProgress = new OperationProgress(); operationProgress.setProgress(100); operationProgressInformation.setOperationStatus(operationProgress); + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("orchestration-status", "created"); Call mockCall = PowerMockito.mock(Call.class); - Response<OperationProgressInformation> response = Response.success(operationProgressInformation); - Mockito.when(soService.queryOperationProgress(any(),any())).thenReturn(mockCall); + Response<JSONObject> response = Response.success(jsonObject); + Mockito.when(intentApiService.getInstanceInfo(anyString())).thenReturn(mockCall); Mockito.when(mockCall.execute()).thenReturn(response); + IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService); + doNothing().when(spy).saveIntentInstanceToAAI(anyString(),any(CCVPNInstance.class)); + Transaction tx = Mockito.mock(Transaction.class); Mockito.when(session.beginTransaction()).thenReturn(tx); Serializable save = Mockito.mock(Serializable.class); Mockito.when(session.save(any())).thenReturn(save); Mockito.doNothing().when(tx).commit(); - intentInstanceService.getIntentInstanceProgress(); + spy.getIntentInstanceCreateStatus(); + assertEquals(jsonObject.getString("orchestration-status"),"created"); } @Test @@ -311,15 +429,78 @@ public class IntentInstanceServiceImplTest { Call mockCall = PowerMockito.mock(Call.class); when(intentApiService.deleteIntentInstance(any())).thenReturn(mockCall); - Mockito.when(mockCall.execute()).thenReturn(null); + when(mockCall.execute()).thenReturn(null); - Transaction tx = Mockito.mock(Transaction.class); - Mockito.when(session.beginTransaction()).thenReturn(tx); - Serializable save = Mockito.mock(Serializable.class); - Mockito.doNothing().when(session).delete(any()); - Mockito.doNothing().when(tx).commit(); + Transaction tx = PowerMockito.mock(Transaction.class); + when(session.beginTransaction()).thenReturn(tx); + Serializable save = PowerMockito.mock(Serializable.class); + doNothing().when(session).delete(any()); + doNothing().when(tx).commit(); + + IntentInstanceServiceImpl spy = spy(intentInstanceService); + doNothing().when(spy).deleteIntentInstanceToAAI(anyString()); - intentInstanceService.deleteIntentInstance("1"); + spy.deleteIntentInstance("1"); + } + + + @Test + public void invalidIntentInstanceTest() throws IOException { + CCVPNInstance instance = new CCVPNInstance(); + instance.setResourceInstanceId("1"); + + Query query = Mockito.mock(Query.class); + when(session.createQuery(anyString())).thenReturn(query); + when(query.setParameter(anyString(), anyString())).thenReturn(query); + when(query.uniqueResult()).thenReturn(instance); + + Call mockCall = PowerMockito.mock(Call.class); + when(intentApiService.deleteIntentInstance(any())).thenReturn(mockCall); + when(mockCall.execute()).thenReturn(null); + + Transaction tx = PowerMockito.mock(Transaction.class); + when(session.beginTransaction()).thenReturn(tx); + Serializable save = PowerMockito.mock(Serializable.class); + doNothing().when(session).delete(any()); + doNothing().when(tx).commit(); + + intentInstanceService.invalidIntentInstance("1"); + } + @Test + public void queryInstancePerformanceDataTest() throws IOException { + CCVPNInstance instance = new CCVPNInstance(); + instance.setResourceInstanceId("1"); + + InstancePerformance instancePerformance = new InstancePerformance(); + instancePerformance.setBandwidth(2000); + instancePerformance.setMaxBandwidth(20000); + instancePerformance.setDate(new Date()); + Object[] o = {null,instancePerformance}; + List<Object[]> queryResult= new ArrayList<>(); + queryResult.add(o); + + Query query = Mockito.mock(Query.class); + when(session.createQuery(anyString())).thenReturn(query); + when(query.setParameter(anyString(), anyString())).thenReturn(query); + when(query.list()).thenReturn(queryResult); + + intentInstanceService.queryInstancePerformanceData("1"); + + + + + + Call mockCall = PowerMockito.mock(Call.class); + when(intentApiService.deleteIntentInstance(any())).thenReturn(mockCall); + when(mockCall.execute()).thenReturn(null); + + Transaction tx = PowerMockito.mock(Transaction.class); + when(session.beginTransaction()).thenReturn(tx); + Serializable save = PowerMockito.mock(Serializable.class); + doNothing().when(session).delete(any()); + doNothing().when(tx).commit(); + + intentInstanceService.invalidIntentInstance("1"); } @Test @@ -355,11 +536,245 @@ public class IntentInstanceServiceImplTest { public void queryAccessNodeInfo() throws IOException { Call mockCall = PowerMockito.mock(Call.class); - JSONObject body = JSONObject.parseObject("{\"data\":[{\"type\":\"ROOT\",\"route-id\":\"route1\"},{\"type\":\"route\",\"route-id\":\"route2\"}]}"); + JSONObject body = JSONObject.parseObject("{\n" + + " \"network-route\": [\n" + + " {\n" + + " \"route-id\": \"tranportEp_src_ID_111_1\",\n" + + " \"type\": \"LEAF\",\n" + + " \"role\": \"3gppTransportEP\",\n" + + " \"function\": \"3gppTransportEP\",\n" + + " \"ip-address\": \"10.2.3.4\",\n" + + " \"prefix-length\": 24,\n" + + " \"next-hop\": \"networkId-providerId-10-clientId-0-topologyId-2-nodeId-10.1.1.1-ltpId-1000\",\n" + + " \"address-family\": \"ipv4\",\n" + + " \"resource-version\": \"1634198223345\"\n" + + " },\n" + + " {\n" + + " \"route-id\": \"tranportEp_src_ID_113_1\",\n" + + " \"type\": \"LEAF\",\n" + + " \"role\": \"3gppTransportEP\",\n" + + " \"function\": \"3gppTransportEP\",\n" + + " \"ip-address\": \"10.2.3.4\",\n" + + " \"prefix-length\": 24,\n" + + " \"next-hop\": \"networkId-providerId-10-clientId-0-topologyId-2-nodeId-10.1.1.3-ltpId-1000\",\n" + + " \"address-family\": \"ipv4\",\n" + + " \"resource-version\": \"1634198260496\"\n" + + " },\n" + + " {\n" + + " \"route-id\": \"tranportEp_src_ID_111_2\",\n" + + " \"type\": \"LEAF\",\n" + + " \"role\": \"3gppTransportEP\",\n" + + " \"function\": \"3gppTransportEP\",\n" + + " \"ip-address\": \"10.2.3.4\",\n" + + " \"prefix-length\": 24,\n" + + " \"next-hop\": \"networkId-providerId-10-clientId-0-topologyId-2-nodeId-10.1.1.1-ltpId-2000\",\n" + + " \"address-family\": \"ipv4\",\n" + + " \"resource-version\": \"1634198251534\"\n" + + " },\n" + + " {\n" + + " \"route-id\": \"tranportEp_dst_ID_212_1\",\n" + + " \"type\": \"ROOT\",\n" + + " \"role\": \"3gppTransportEP\",\n" + + " \"function\": \"3gppTransportEP\",\n" + + " \"ip-address\": \"10.2.3.4\",\n" + + " \"prefix-length\": 24,\n" + + " \"next-hop\": \"networkId-providerId-20-clientId-0-topologyId-2-nodeId-10.2.1.2-ltpId-512\",\n" + + " \"address-family\": \"ipv4\",\n" + + " \"resource-version\": \"1634198274852\"\n" + + " }\n" + + " ]\n" + + "}"); Response<JSONObject> response = Response.success(body); Mockito.when(intentApiService.queryNetworkRoute()).thenReturn(mockCall); Mockito.when(mockCall.execute()).thenReturn(response); Map<String, Object> result = (Map<String, Object>) intentInstanceService.queryAccessNodeInfo(); - assertEquals(((List)result.get("accessNodeList")).size(), 1); + assertEquals(((List)result.get("accessNodeList")).size(), 3); + } + + @Test + public void getInstanceStatusTest() { + List<CCVPNInstance> queryResult = new ArrayList<>(); + CCVPNInstance instance = new CCVPNInstance(); + instance.setInstanceId("id1"); + instance.setStatus("1"); + queryResult.add(instance); + + Query query = Mockito.mock(Query.class); + when(session.createQuery(anyString())).thenReturn(query); + when(query.setParameter(anyString(), any())).thenReturn(query); + when(query.list()).thenReturn(queryResult); + + + JSONObject instanceStatus = intentInstanceService.getInstanceStatus(new JSONArray()); + assertEquals(instanceStatus.getJSONArray("IntentInstances").getJSONObject(0).getString("id"), "id1"); + } + @Test + public void formatBandwidthTest() { + + String bandwidth = intentInstanceService.formatBandwidth("2Gbps"); + assertEquals(bandwidth, "2000"); + } + @Test + public void formatCloudPointTest() { + + String bandwidth = intentInstanceService.formatCloudPoint("Cloud one"); + assertEquals(bandwidth, "tranportEp_dst_ID_212_1"); + } + @Test + public void formatAccessPointOneTest() { + String bandwidth = intentInstanceService.formatAccessPoint("Access one"); + assertEquals(bandwidth, "tranportEp_src_ID_111_1"); + } + @Test + public void formatAccessPointTwoTest() { + String bandwidth = intentInstanceService.formatAccessPoint("Access two"); + assertEquals(bandwidth, "tranportEp_src_ID_111_2"); + } + @Test + public void formatAccessPointThreeTest() { + String bandwidth = intentInstanceService.formatAccessPoint("Access three"); + assertEquals(bandwidth, "tranportEp_src_ID_113_1"); + } + + @Test + public void addCustomerTest() throws IOException { + + Call mockCall = PowerMockito.mock(Call.class); + Response<Object> response = Response.error(404, new ResponseBody() { + @Nullable + @Override + public MediaType contentType() { + return null; + } + + @Override + public long contentLength() { + return 0; + } + + @Override + public BufferedSource source() { + return null; + } + }); + when(intentApiService.queryCustomer(anyString())).thenReturn(mockCall); + when(mockCall.execute()).thenReturn(response); + + Properties properties = new Properties(); + properties.put("ccvpn.globalCustomerId", "IBNCustomer"); + properties.put("ccvpn.subscriberName", "IBNCustomer"); + properties.put("ccvpn.subscriberType", "INFRA"); + properties.put("ccvpn.serviceType", "IBN"); + IntentInstanceServiceImpl spy = spy(intentInstanceService); + doReturn(properties).when(spy).getProperties(); + + Call mockCall2 = PowerMockito.mock(Call.class); + when(intentApiService.addCustomer(anyString(),any())).thenReturn(mockCall2); + + spy.addCustomer(); + Mockito.verify(intentApiService,Mockito.times(1)).addCustomer(anyString(),any()); + } + + + @Test + public void addSubscriptionTest() throws IOException { + + Call mockCall = PowerMockito.mock(Call.class); + Response<Object> response = Response.error(404, new ResponseBody() { + @Nullable + @Override + public MediaType contentType() { + return null; + } + + @Override + public long contentLength() { + return 0; + } + + @Override + public BufferedSource source() { + return null; + } + }); + when(intentApiService.querySubscription(anyString(),anyString())).thenReturn(mockCall); + when(mockCall.execute()).thenReturn(response); + + Properties properties = new Properties(); + properties.put("ccvpn.globalCustomerId", "IBNCustomer"); + properties.put("ccvpn.subscriberName", "IBNCustomer"); + properties.put("ccvpn.subscriberType", "INFRA"); + properties.put("ccvpn.serviceType", "IBN"); + IntentInstanceServiceImpl spy = spy(intentInstanceService); + doReturn(properties).when(spy).getProperties(); + + Call mockCall2 = PowerMockito.mock(Call.class); + when(intentApiService.addSubscription(anyString(),anyString(),any())).thenReturn(mockCall2); + + spy.addSubscription(); + Mockito.verify(intentApiService,Mockito.times(1)).addSubscription(anyString(),anyString(),any()); + } + + @Test + public void saveIntentInstanceToAAITest() throws IOException { + IntentInstanceServiceImpl spy = spy(intentInstanceService); + doNothing().when(spy).addCustomer(); + doNothing().when(spy).addSubscription(); + + Properties properties = new Properties(); + properties.put("ccvpn.globalCustomerId", "IBNCustomer"); + properties.put("ccvpn.subscriberName", "IBNCustomer"); + properties.put("ccvpn.subscriberType", "INFRA"); + properties.put("ccvpn.serviceType", "IBN"); + doReturn(properties).when(spy).getProperties(); + + JSONObject body = new JSONObject(); + body.put("resource-version",123); + Call mockCall = PowerMockito.mock(Call.class); + Response<JSONObject> response = Response.success(body); + when(intentApiService.queryServiceInstance(anyString(),anyString(),anyString())).thenReturn(mockCall); + when(mockCall.execute()).thenReturn(response); + + CCVPNInstance instance = new CCVPNInstance(); + instance.setName("name"); + instance.setInstanceId("id"); + + Call mockCall2 = PowerMockito.mock(Call.class); + Response<JSONObject> response2 = Response.success(body); + when(intentApiService.saveServiceInstance(anyString(),anyString(),anyString(),any())).thenReturn(mockCall2); + when(mockCall2.execute()).thenReturn(response2); + + spy.saveIntentInstanceToAAI("CCVPN-id",instance); + Mockito.verify(intentApiService, Mockito.times(1)).saveServiceInstance(anyString(),anyString(),anyString(),any()); + + } + @Test + public void deleteIntentInstanceToAAITest() throws IOException { + IntentInstanceServiceImpl spy = spy(intentInstanceService); + doNothing().when(spy).addCustomer(); + doNothing().when(spy).addSubscription(); + + Properties properties = new Properties(); + properties.put("ccvpn.globalCustomerId", "IBNCustomer"); + properties.put("ccvpn.subscriberName", "IBNCustomer"); + properties.put("ccvpn.subscriberType", "INFRA"); + properties.put("ccvpn.serviceType", "IBN"); + doReturn(properties).when(spy).getProperties(); + + JSONObject body = new JSONObject(); + body.put("resource-version",123); + Call mockCall = PowerMockito.mock(Call.class); + Response<JSONObject> response = Response.success(body); + when(intentApiService.queryServiceInstance(anyString(),anyString(),anyString())).thenReturn(mockCall); + when(mockCall.execute()).thenReturn(response); + + Call mockCall2 = PowerMockito.mock(Call.class); + Response<JSONObject> response2 = Response.success(body); + when(intentApiService.deleteServiceInstance(anyString(),anyString(),anyString(),anyString())).thenReturn(mockCall2); + when(mockCall2.execute()).thenReturn(response2); + + spy.deleteIntentInstanceToAAI("CCVPN-id"); + Mockito.verify(intentApiService, Mockito.times(1)).deleteServiceInstance(anyString(),anyString(),anyString(),any()); + } }
\ No newline at end of file diff --git a/server/src/test/java/org/onap/usecaseui/server/service/intent/impl/IntentServiceImplTest.java b/server/src/test/java/org/onap/usecaseui/server/service/intent/impl/IntentServiceImplTest.java index a228b769..991cae64 100644 --- a/server/src/test/java/org/onap/usecaseui/server/service/intent/impl/IntentServiceImplTest.java +++ b/server/src/test/java/org/onap/usecaseui/server/service/intent/impl/IntentServiceImplTest.java @@ -20,6 +20,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; +import java.util.Map; import org.hibernate.query.Query; import org.hibernate.Session; @@ -31,7 +32,10 @@ import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; +import org.onap.usecaseui.server.bean.HttpResponseResult; import org.onap.usecaseui.server.bean.intent.IntentModel; +import org.onap.usecaseui.server.constant.IntentConstant; +import org.onap.usecaseui.server.util.HttpUtil; import org.onap.usecaseui.server.util.ZipUtil; import org.powermock.api.mockito.PowerMockito; import org.powermock.api.support.membermodification.MemberModifier; @@ -39,15 +43,13 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import static org.junit.Assert.*; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.powermock.api.mockito.PowerMockito.doReturn; -import static org.powermock.api.mockito.PowerMockito.when; +import static org.mockito.ArgumentMatchers.*; +import static org.powermock.api.mockito.PowerMockito.*; @RunWith(PowerMockRunner.class) -@PrepareForTest({ZipUtil.class}) -class IntentServiceImplTest { +@PrepareForTest({ZipUtil.class, HttpUtil.class}) +public class IntentServiceImplTest { public IntentServiceImplTest(){} @@ -94,7 +96,7 @@ class IntentServiceImplTest { public void getModel() { Query query = Mockito.mock(Query.class); when(session.createQuery(anyString())).thenReturn(query); - when(query.setParameter("modelId", "1")).thenReturn(query); + when(query.setParameter("modelId", 1)).thenReturn(query); when(query.uniqueResult()).thenReturn(null); assertEquals(intentService.getModel("1"), null); @@ -115,17 +117,24 @@ class IntentServiceImplTest { doReturn(tx).when(session,"beginTransaction"); Query query = Mockito.mock(Query.class); - when(session.createQuery(anyString())).thenReturn(query); + when(session.createQuery("from IntentModel where active=1")).thenReturn(query); List<IntentModel> list = new ArrayList<>(); IntentModel intentModel = new IntentModel(); intentModel.setActive(1); list.add(intentModel); when(query.list()).thenReturn(list); + + Query query2 = Mockito.mock(Query.class); + when(session.createQuery("from IntentModel where id = :modelId")).thenReturn(query2); + when(query2.setParameter("modelId",1)).thenReturn(query2); + IntentModel intentModel2 = new IntentModel(); + intentModel2.setActive(0); + when(query2.uniqueResult()).thenReturn(intentModel2); Serializable save = Mockito.mock(Serializable.class); Mockito.when(session.save(any())).thenReturn(save); Mockito.doNothing().when(tx).commit(); - assertEquals(intentService.activeModel("1"), null); + assertEquals(intentService.activeModel("1"), intentModel2); } @Test @@ -137,6 +146,18 @@ class IntentServiceImplTest { IntentModel model = new IntentModel(); assertEquals(intentService.activeModelFile(model), null); } + @Test + public void activeModelFileFilePathIsZIPTest() { + IntentModel model = new IntentModel(); + model.setModelName("fileName.zip"); + + PowerMockito.mockStatic(HttpUtil.class); + HttpResponseResult mock = PowerMockito.mock(HttpResponseResult.class); + when(HttpUtil.sendGetRequest(anyString(),any(Map.class))).thenReturn(mock); + when(mock.getResultContent()).thenReturn("OK"); + + assertEquals(intentService.activeModelFile(model), "fileName"); + } @Test @@ -161,7 +182,7 @@ class IntentServiceImplTest { } @Test public void calcFieldValueKeyIsCoverageAreaTest() { - assertEquals(intentService.calcFieldValue("coverageArea", "zhongguancun"), "Beijing Haidian District Zhongguancun"); + assertEquals(intentService.calcFieldValue("coverageArea", "zhongguancun"), "Beijing;Beijing;Haidian District;Zhongguancun Street"); } @Test public void calcFieldValueKeyIsMaxNumberofUEsTest() { @@ -172,9 +193,33 @@ class IntentServiceImplTest { assertEquals(intentService.calcFieldValue("expDataRateDL", "1gb"), "1000"); } @Test + public void calcFieldValueKeyIsExpDataRateDLMBTest() { + assertEquals(intentService.calcFieldValue("expDataRateDL", "1mbpss"), "100"); + } + @Test + public void calcFieldValueKeyIsExpDataRateULTest() { + assertEquals(intentService.calcFieldValue("expDataRateUL", "1gb"), "1000"); + } + @Test + public void calcFieldValueKeyIsExpDataRateULMBTest() { + assertEquals(intentService.calcFieldValue("expDataRateUL", "1mbpss"), "100"); + } + @Test public void calcFieldValueKeyIsLatencyTest() { assertEquals(intentService.calcFieldValue("latency", "1s"), "200"); } + @Test + public void calcFieldValueKeyIsLatencyDefaultTest() { + assertEquals(intentService.calcFieldValue("latency", "default"), "200"); + } + @Test + public void calcFieldValueKeyIsLatencyLowTest() { + assertEquals(intentService.calcFieldValue("latency", "low"), "10"); + } + @Test + public void calcFieldValueKeyIsLatencyOtherTest() { + assertEquals(intentService.calcFieldValue("latency", "1min"), "200"); + } @Test @@ -185,4 +230,68 @@ class IntentServiceImplTest { Object result = method.invoke(spy, value); assertEquals(result, "shared"); } + + @Test + public void getActiveModelTypeTest() { + IntentModel intentModel = new IntentModel(); + intentModel.setModelType("ccvpn"); + Query query = PowerMockito.mock(Query.class); + when(session.createQuery(anyString())).thenReturn(query); + when(query.uniqueResult()).thenReturn(intentModel); + assertEquals(intentService.getActiveModelType(), "ccvpn"); + } + @Test + public void getActiveModelTypeThrowErrorTest() { + assertEquals(intentService.getActiveModelType(), null); + } + @Test + public void getModelTypeByIntentTextCCVPNTest() { + assertEquals(intentService.getModelTypeByIntentText("Cloud"), IntentConstant.MODEL_TYPE_CCVPN); + } + @Test + public void getModelTypeByIntentText5GSTest() { + assertEquals(intentService.getModelTypeByIntentText("5gs"), IntentConstant.MODEL_TYPE_5GS); + } + @Test + public void activeModelByTypeTest() { + Transaction tx = PowerMockito.mock(Transaction.class); + when(session.beginTransaction()).thenReturn(tx); + Query query = PowerMockito.mock(Query.class); + when(session.createQuery("from IntentModel where active=1")).thenReturn(query); + IntentModel intentModel = new IntentModel(); + intentModel.setActive(1); + List<IntentModel> list = new ArrayList<>(); + list.add(intentModel); + when(query.list()).thenReturn(list); + Serializable save = PowerMockito.mock(Serializable.class); + when(session.save(intentModel)).thenReturn(save); + + Query query1 = PowerMockito.mock(Query.class); + when(session.createQuery("from IntentModel where modelType = :modelType order by createTime desc")).thenReturn(query1); + when(query1.setParameter("modelType", 1)).thenReturn(query1); + List<IntentModel> list1 = new ArrayList<>(); + IntentModel intentModel1 = new IntentModel(); + intentModel1.setActive(0); + list1.add(intentModel1); + when(query1.list()).thenReturn(list1); + when(session.save(intentModel1)).thenReturn(save); + doNothing().when(tx).commit(); + + IntentServiceImpl spy = spy(intentService); + doReturn("fileName").when(spy).activeModelFile(intentModel1); + doReturn("OK").when(spy).load(anyString()); + + + assertEquals(spy.activeModelByType(IntentConstant.MODEL_TYPE_CCVPN), intentModel1); + } + @Test + public void loadTest() { + PowerMockito.mockStatic(HttpUtil.class); + HttpResponseResult result = PowerMockito.mock(HttpResponseResult.class); + when(HttpUtil.sendPostRequestByJson(anyString(), any(), anyString())).thenReturn(result); + when(result.getResultContent()).thenReturn("{\"Status\":\"OK\"}"); + assertEquals(intentService.load("filename"), "OK"); + + } + }
\ No newline at end of file |