Sfoglia il codice sorgente

添加几个Controller测试

LiHaoyu 5 anni fa
parent
commit
6f44489e2c

+ 14 - 0
src/main/java/net/mooctest/www/android_auto_test/controller/AutoTestController.java

@@ -6,6 +6,10 @@ import net.mooctest.www.android_auto_test.services.AutoTestService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
 
 /**
  * @author henrylee
@@ -18,6 +22,16 @@ public class AutoTestController {
 
     @RequestMapping(value = "/api/v1/runTest", method = RequestMethod.POST)
     public TraceStatusResult runTest(@RequestBody TraceMetaInfo traceInfo){
+        List<String> leakParams = new ArrayList<>();
+        if (traceInfo.getTraceId() == null){
+            leakParams.add("traceId");
+        }
+        if (traceInfo.getDownloadUrl() == null){
+            leakParams.add("downloadUrl");
+        }
+        if (leakParams.size() != 0){
+            throw new RuntimeException("缺少参数:" + Arrays.toString(leakParams.toArray()));
+        }
         return autoTestService.executeAutoTestTask(traceInfo);
     }
 

+ 60 - 1
src/test/java/net/mooctest/www/android_auto_test/ctrl/AutoTestControllerTest.java

@@ -1,10 +1,13 @@
 package net.mooctest.www.android_auto_test.ctrl;
 
 
+import com.alibaba.fastjson.JSON;
 import net.mooctest.www.android_auto_test.common.constant.enums.TraceStatus;
 import net.mooctest.www.android_auto_test.controller.AutoTestController;
 import net.mooctest.www.android_auto_test.services.AutoTestService;
+import net.mooctest.www.android_auto_test.vo.TraceMetaInfo;
 import net.mooctest.www.android_auto_test.vo.TraceStatusResult;
+import org.apache.commons.lang.exception.NestableRuntimeException;
 import org.json.JSONObject;
 import org.junit.Assert;
 import org.junit.Before;
@@ -17,6 +20,7 @@ import org.springframework.http.MediaType;
 import org.springframework.test.web.servlet.MockMvc;
 import org.springframework.test.web.servlet.MvcResult;
 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import org.springframework.web.util.NestedServletException;
 
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.when;
@@ -52,10 +56,13 @@ public class AutoTestControllerTest {
         ts.setStatusCode(TraceStatus.INIT.getCode());
         ts.setDescription(TraceStatus.INIT.getDescription());
         when(autoTestService.executeAutoTestTask(any())).thenReturn(ts);
+        JSONObject object = new JSONObject();
+        object.put("traceId", "123");
+        object.put("downloadUrl", "url");
         // action
         MvcResult result = mockMvc.perform(post("/api/v1/runTest")
                 .contentType(MediaType.APPLICATION_JSON)
-                .content("{}"))
+                .content(object.toString()))
                 .andDo(print()).andExpect(status().isOk()).andReturn();
         // assert
         JSONObject re = new JSONObject(result.getResponse().getContentAsString());
@@ -64,6 +71,26 @@ public class AutoTestControllerTest {
     }
 
     @Test
+    public void testStartWithWrongParams() throws Exception{
+        // arrange
+        ts.setStatusCode(TraceStatus.INIT.getCode());
+        ts.setDescription(TraceStatus.INIT.getDescription());
+        when(autoTestService.executeAutoTestTask(any())).thenReturn(ts);
+        JSONObject object = new JSONObject();
+        object.put("traceId", "123");
+        // action
+        try {
+            MvcResult result = mockMvc.perform(post("/api/v1/runTest")
+                    .contentType(MediaType.APPLICATION_JSON)
+                    .content(object.toString()))
+                    .andDo(print()).andExpect(status().is5xxServerError()).andReturn();
+        }catch (NestedServletException e){
+            // assert
+            Assert.assertEquals("缺少参数:[downloadUrl]", e.getRootCause().getMessage());
+        }
+    }
+
+    @Test
     public void testGetStatus() throws Exception{
         // arrange
         ts.setStatusCode(TraceStatus.FINISH.getCode());
@@ -81,6 +108,22 @@ public class AutoTestControllerTest {
     }
 
     @Test
+    public void testGetStatusWithoutParams() throws Exception{
+        // arrange
+        ts.setStatusCode(TraceStatus.FINISH.getCode());
+        ts.setDescription(TraceStatus.FINISH.getDescription());
+        when(autoTestService.getResult(any())).thenReturn(ts);
+        // action
+        try {
+            MvcResult result = mockMvc.perform(get("/api/v1/result")
+                    .contentType(MediaType.APPLICATION_JSON))
+                    .andDo(print()).andExpect(status().is4xxClientError()).andReturn();
+        } catch (NestedServletException e){
+            // assert
+        }
+    }
+
+    @Test
     public void testStop() throws Exception{
         // arrange
         ts.setStatusCode(TraceStatus.FINISH.getCode());
@@ -95,4 +138,20 @@ public class AutoTestControllerTest {
         boolean re = Boolean.parseBoolean(result.getResponse().getContentAsString());
         Assert.assertFalse(re);
     }
+
+    @Test
+    public void testStopWithoutParams() throws Exception{
+        // arrange
+        ts.setStatusCode(TraceStatus.FINISH.getCode());
+        ts.setDescription(TraceStatus.FINISH.getDescription());
+        when(autoTestService.getResult(any())).thenReturn(ts);
+        // action
+        try {
+            MvcResult result = mockMvc.perform(delete("/api/v1/result")
+                    .contentType(MediaType.APPLICATION_JSON))
+                    .andDo(print()).andExpect(status().is4xxClientError()).andReturn();
+        } catch (NestedServletException e){
+            // assert
+        }
+    }
 }

+ 30 - 0
src/test/java/net/mooctest/www/android_auto_test/ctrl/SecondaryControllerTest.java

@@ -15,6 +15,7 @@ import org.springframework.http.MediaType;
 import org.springframework.test.web.servlet.MockMvc;
 import org.springframework.test.web.servlet.MvcResult;
 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import org.springframework.web.util.NestedServletException;
 
 import static org.mockito.Mockito.*;
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@@ -62,6 +63,22 @@ public class SecondaryControllerTest {
     }
 
     @Test
+    public void testStartWithoutParams() throws Exception{
+        //arrange
+        doNothing().when(secondaryService).generateSecondaryReport(traceId, toolName);
+        //action
+        try {
+            MvcResult result = mockMvc.perform(post("/api/v1/secondaryAnalysis")
+                    .param("traceId", traceId)
+                    .contentType(MediaType.APPLICATION_JSON)
+                    .content("{}"))
+                    .andDo(print()).andExpect(status().is4xxClientError()).andReturn();
+        }catch (NestedServletException e){
+            //assert
+        }
+    }
+
+    @Test
     public void testStatus() throws Exception{
         //arrange
         SecondaryResult sr = new SecondaryResult();
@@ -82,4 +99,17 @@ public class SecondaryControllerTest {
         Assert.assertEquals(toolName, re.getString("toolName"));
         Assert.assertEquals("5", re.getString("statusCode"));
     }
+
+    @Test
+    public void testStatusWithoutParams() throws Exception{
+        //arrange
+        //action
+        try {
+            MvcResult result = mockMvc.perform(get("/api/v1/secondaryAnalysis")
+                    .param("toolName", toolName))
+                    .andDo(print()).andExpect(status().is4xxClientError()).andReturn();
+        }catch (NestedServletException e) {
+
+        }
+    }
 }