feat:Add file transfer function
[usecase-ui/server.git] / server / src / test / java / org / onap / usecaseui / server / controller / IntentControllerTest.java
index a80574d..7727420 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 CTC, Inc. and others. All rights reserved.
+ * 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.
  */
 package org.onap.usecaseui.server.controller;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.jupiter.api.Assertions.*;
-
-import java.io.File;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.text.ParseException;
-import java.util.HashMap;
-import java.util.Map;
-
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import org.junit.Before;
 import org.junit.Test;
 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.IntentInstance;
 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.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 com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONObject;
+import javax.annotation.Resource;
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
+import static org.junit.Assert.assertEquals;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.powermock.api.mockito.PowerMockito.when;
 
 @RunWith(PowerMockRunner.class)
 @PrepareForTest({HttpUtil.class})
-class IntentControllerTest {
+public class IntentControllerTest {
 
     public IntentControllerTest(){}
 
@@ -55,8 +60,19 @@ class IntentControllerTest {
     private IntentController intentController;
 
     @Mock
+    @Resource(name = "IntentService")
     private IntentService intentService;
 
+    @Mock
+    private IntentInstanceService intentInstanceService;
+
+    @Before
+    public void before() throws IllegalAccessException {
+        MemberModifier.field(IntentController.class, "intentService").set(intentController , intentService);
+        MemberModifier.field(IntentController.class, "intentInstanceService").set(intentController , intentInstanceService);
+
+    }
+
     @Test
     public void activeModelTest() {
         IntentModel model = new IntentModel();
@@ -110,8 +126,78 @@ class IntentControllerTest {
     public void tranlateFieldNameTest() throws InvocationTargetException, IllegalAccessException {
         String key = "Region";
         IntentController spy = PowerMockito.spy(intentController);
-        Method method = PowerMockito.method(IntentController.class, "tranlateFieldName", String.class);//如果多个参数,逗号分隔,然后写参数类型.class
+        Method method = PowerMockito.method(IntentController.class, "tranlateFieldName", String.class);
         Object result = method.invoke(spy, key);
         assertEquals(result, "coverageArea");
     }
+    @Test
+    public void getInstanceId() {
+        assertEquals(intentController.getInstanceId().containsKey("instanceId"), true);
+    }
+    @Test
+    public void getInstanceList() {
+        Map<String, Object> body = new HashMap<>();
+
+        body.put("currentPage",1);
+        body.put("pageSize",2);
+        Mockito.when(intentInstanceService.queryIntentInstance(null,1,2)).thenReturn(null);
+        assertEquals(intentController.getInstanceList(body), null);
+    }
+    @Test
+    public void createIntentInstance() throws IOException {
+        Map<String, Object> body = new HashMap<>();
+        body.put("instanceId","instanceId");
+        body.put("name","name");
+        body.put("lineNum","lineNum");
+        body.put("cloudPointName","cloudPointName");
+        Map<String, Object> accessPointOne = new HashMap<>();
+        accessPointOne.put("name","name");
+        accessPointOne.put("bandwidth","1");
+        body.put("accessPointOne",accessPointOne);
+        Mockito.when(intentInstanceService.createIntentInstance(any())).thenReturn(1);
+        assertEquals(intentController.createIntentInstance(body), "OK");
+    }
+    @Test
+    public void getFinishedInstanceInfo() {
+        List<IntentInstance> instanceList = new ArrayList<>();
+        IntentInstance instance = new IntentInstance();
+        instance.setInstanceId("instanceId");
+        instance.setName("name");
+        instanceList.add(instance);
+        Mockito.when(intentInstanceService.getFinishedInstanceInfo()).thenReturn(instanceList);
+        assertEquals(((List)intentController.getFinishedInstanceInfo()).size(), 1);
+    }
+    @Test
+    public void deleteIntentInstance() {
+        Map<String, Object> body = new HashMap<>();
+        body.put("instanceId", "instanceId");
+        Mockito.doNothing().when(intentInstanceService).deleteIntentInstance(anyString());
+        assertEquals(intentController.deleteIntentInstance(body), "ok");
+    }
+    @Test
+    public void activeIntentInstance() {
+        Map<String, Object> body = new HashMap<>();
+        body.put("instanceId", "instanceId");
+        Mockito.doNothing().when(intentInstanceService).activeIntentInstance(anyString());
+        assertEquals(intentController.activeIntentInstance(body), "ok");
+    }
+    @Test
+    public void invalidIntentInstance() {
+        Map<String, Object> body = new HashMap<>();
+        body.put("instanceId", "instanceId");
+        Mockito.doNothing().when(intentInstanceService).invalidIntentInstance(anyString());
+        assertEquals(intentController.invalidIntentInstance(body), "ok");
+    }
+    @Test
+    public void queryInstancePerformanceData() {
+        Map<String, Object> body = new HashMap<>();
+        body.put("instanceId", "instanceId");
+        Mockito.when(intentInstanceService.queryInstancePerformanceData(anyString())).thenReturn(body);
+        assertEquals(intentController.queryInstancePerformanceData(body), body);
+    }
+    @Test
+    public void queryAccessNodeInfoTest() throws IOException {
+        Mockito.when(intentInstanceService.queryAccessNodeInfo()).thenReturn("ok");
+        assertEquals(intentController.queryAccessNodeInfo(), "ok");
+    }
 }
\ No newline at end of file