Procházet zdrojové kódy

fix mutation analyze

chenxz před 8 roky
rodič
revize
bdb260dec6

+ 3 - 5
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/rpc/dev/DevService.java

@@ -38,13 +38,11 @@ public interface DevService {
 
     /**
      * get single mutation analysis result
-     * @param examID
-     * @param workerID
-     * @param caseName
-     * @param endTime
+     * @param url
+     * @param mutators
      * @return mutation result
      */
-    public MutationDTO mutationAnalyze(long examID, long workerID, String caseName, long endTime);
+    public MutationDTO mutationAnalyze(String url,List<String> mutators);
 
     /**
      * start analyze all the workers' case in one exam

+ 7 - 2
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/rpc/dev/impl/DevServiceImpl.java

@@ -2,6 +2,7 @@ package cn.iselab.mooctest.site.rpc.dev.impl;
 
 import cn.iselab.mooctest.site.rpc.dev.DevService;
 import cn.iselab.mooctest.site.rpc.dev.api.AnalyzeService;
+import cn.iselab.mooctest.site.rpc.dev.api.MutationService;
 import cn.iselab.mooctest.site.rpc.dev.data.ApbcDTO;
 import cn.iselab.mooctest.site.rpc.dev.data.CoverageDTO;
 import cn.iselab.mooctest.site.rpc.dev.data.CoverageInfoDTO;
@@ -18,6 +19,10 @@ import java.util.List;
 public class DevServiceImpl implements DevService {
     @Reference(version = "1.0.0")
     AnalyzeService analyzeService;
+
+    @Reference(version = "1.0.0")
+    MutationService mutationService;
+
     @Override
     public ApbcDTO apbcAnalyze(long examID, long caseID, long workerID, String examName, long startTime, long endTime) {
         return analyzeService.apbcAnalyze(examID, caseID, workerID, examName, startTime, endTime);
@@ -29,8 +34,8 @@ public class DevServiceImpl implements DevService {
     }
 
     @Override
-    public MutationDTO mutationAnalyze(long examID, long workerID, String caseName, long endTime) {
-        return analyzeService.mutationAnalyze(examID, workerID, caseName, endTime);
+    public MutationDTO mutationAnalyze(String url,List<String> mutators) {
+        return mutationService.mutationAnalyze(url,mutators);
     }
 
     @Override

+ 16 - 0
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/service/fromDev/AnalysisService.java

@@ -0,0 +1,16 @@
+package cn.iselab.mooctest.site.service.fromDev;
+
+import cn.iselab.mooctest.site.rpc.dev.data.MutationDTO;
+
+import java.util.List;
+
+/**
+ * Created by ROGK on 2017/8/10.
+ */
+public interface AnalysisService {
+
+    MutationDTO mutationAnalyze(long examId, long workerId, long caseId);
+
+    List<MutationDTO> mutationAllAnalyze(long examId, List<Long> workerIds, long caseId);
+
+}

+ 54 - 0
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/service/fromDev/impl/AnalysisServiceImpl.java

@@ -0,0 +1,54 @@
+package cn.iselab.mooctest.site.service.fromDev.impl;
+
+import cn.iselab.mooctest.site.dao.WeightDao;
+import cn.iselab.mooctest.site.models.Case;
+import cn.iselab.mooctest.site.models.CaseExtends;
+import cn.iselab.mooctest.site.models.Weight;
+import cn.iselab.mooctest.site.rpc.dev.DevService;
+import cn.iselab.mooctest.site.rpc.dev.data.MutationDTO;
+import cn.iselab.mooctest.site.service.CaseService;
+import cn.iselab.mooctest.site.service.fromDev.AnalysisService;
+import cn.iselab.mooctest.site.web.logic.fromDev.PluginLogic;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Created by ROGK on 2017/8/10.
+ */
+@Service
+public class AnalysisServiceImpl implements AnalysisService{
+
+    @Autowired
+    PluginLogic pluginLogic;
+    @Autowired
+    DevService devService;
+    @Autowired
+    CaseService caseService;
+
+    @Override
+    public MutationDTO mutationAnalyze(long examId, long workerId, long caseId){
+        Case devCase=caseService.getCaseById(caseId);
+        String url = pluginLogic.getAnalysisSignature(examId, workerId, devCase.getName());
+        List<String> defaultList = new ArrayList<>();
+        defaultList.add("CONDITIONALS_BOUNDARY");
+        defaultList.add("NEGATE_CONDITIONALS");
+        defaultList.add("MATH");
+        defaultList.add("INCREMENTS");
+        defaultList.add("INVERT_NEGS");
+        defaultList.add("RETURN_VALS");
+        defaultList.add("VOID_METHOD_CALLS");
+        return devService.mutationAnalyze(url, defaultList);
+    }
+
+    @Override
+    public List<MutationDTO> mutationAllAnalyze(long examId, List<Long> workerIds, long caseId){
+        List<MutationDTO> list=new ArrayList<>();
+        for(Long workerId:workerIds){
+            list.add(mutationAnalyze(examId,workerId,caseId));
+        }
+        return list;
+    }
+}

+ 20 - 0
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/service/impl/WeightServiceImpl.java

@@ -0,0 +1,20 @@
+package cn.iselab.mooctest.site.service.impl;
+
+import cn.iselab.mooctest.site.dao.WeightDao;
+import cn.iselab.mooctest.site.models.Weight;
+import cn.iselab.mooctest.site.service.worker.WeightService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+/**
+ * Created by ROGK on 2017/8/10.
+ */
+@Service
+public class WeightServiceImpl implements WeightService {
+
+    @Autowired
+    WeightDao weightDao;
+    public Weight getWeightByTidAndCid(long taskId, long caseId){
+        return weightDao.findByTidAndCid(taskId,caseId);
+    }
+}

+ 14 - 0
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/service/worker/WeightService.java

@@ -0,0 +1,14 @@
+package cn.iselab.mooctest.site.service.worker;
+
+import cn.iselab.mooctest.site.dao.WeightDao;
+import cn.iselab.mooctest.site.models.Weight;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+/**
+ * Created by ROGK on 2017/8/10.
+ */
+public interface WeightService {
+
+    Weight getWeightByTidAndCid(long taskId, long caseId);
+}

+ 21 - 5
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/web/ctrl/fromDev/AnalysisController.java

@@ -1,11 +1,14 @@
 package cn.iselab.mooctest.site.web.ctrl.fromDev;
 
 import cn.iselab.mooctest.site.common.constant.UrlConstants;
+import cn.iselab.mooctest.site.models.Weight;
 import cn.iselab.mooctest.site.rpc.dev.DevService;
 import cn.iselab.mooctest.site.rpc.dev.data.ApbcDTO;
 import cn.iselab.mooctest.site.rpc.dev.data.CoverageDTO;
 import cn.iselab.mooctest.site.rpc.dev.data.CoverageInfoDTO;
 import cn.iselab.mooctest.site.rpc.dev.data.MutationDTO;
+import cn.iselab.mooctest.site.service.fromDev.AnalysisService;
+import cn.iselab.mooctest.site.service.worker.WeightService;
 import cn.iselab.mooctest.site.web.ctrl.BaseController;
 import cn.iselab.mooctest.site.web.data.fromDev.ApfdAllVO;
 import cn.iselab.mooctest.site.web.data.fromDev.MutationAllVO;
@@ -24,6 +27,10 @@ public class AnalysisController extends BaseController{
 
     @Autowired
     DevService devService;
+    @Autowired
+    AnalysisService analysisService;
+    @Autowired
+    WeightService weightService;
 
     @RequestMapping(value = UrlConstants.API_DEV + "/apbc", method = RequestMethod.GET)
     public String apbcAnalyze(@RequestParam(name = "examID") long examID,
@@ -47,10 +54,12 @@ public class AnalysisController extends BaseController{
 
     @RequestMapping(value = UrlConstants.API_DEV + "mutation", method = RequestMethod.GET)
     public String mutationAnalyze(@RequestParam(name = "examID") long examID,
-                                  @RequestParam(name = "stuID") long stuID,
-                                  @RequestParam(name = "examName") String examName,
-                                  @RequestParam(name = "endTime") long endTime) {
-        MutationDTO mutationDTO = devService.mutationAnalyze(examID, stuID, examName, endTime);
+                                  @RequestParam(name = "workerID") long stuID,
+                                  @RequestParam(name = "caseID") long caseID) {
+        Weight weight=weightService.getWeightByTidAndCid(examID,caseID);
+        if(weight.getMutation()==0)
+            return StResponse.failure("该题无需变异分析");
+        MutationDTO mutationDTO = analysisService.mutationAnalyze(examID, stuID, caseID);
         if (mutationDTO == null) {
             return StResponse.failure("获取变异结果失败");
         } else {
@@ -60,7 +69,14 @@ public class AnalysisController extends BaseController{
 
     @RequestMapping(value = UrlConstants.API_DEV + "mutation/all", method = RequestMethod.POST)
     public String mutationAllAnalyze(@RequestBody MutationAllVO mutationAllVO) {
-        return StResponse.success(devService.mutationAllAnalyze(mutationAllVO.getTaskID(), mutationAllVO.getCaseID(), mutationAllVO.getWorkerID(), mutationAllVO.getEndTime()));
+        Weight weight=weightService.getWeightByTidAndCid(mutationAllVO.getTaskID(),mutationAllVO.getCaseID());
+        if(weight.getMutation()==0)
+            return StResponse.failure("该题无需变异分析");
+        List<MutationDTO> list= analysisService.mutationAllAnalyze(mutationAllVO.getTaskID(),mutationAllVO.getWorkerID(),mutationAllVO.getCaseID());
+        if (list.size()!=0&&!list.isEmpty())
+            return StResponse.success(list);
+        else
+            return StResponse.failure("获取编译结果失败");
     }
 
     @RequestMapping(value = UrlConstants.API_DEV + "coverage", method = RequestMethod.GET)

+ 2 - 0
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/web/logic/fromDev/PluginLogic.java

@@ -48,4 +48,6 @@ public interface PluginLogic {
     PluginResultVO loginFromPlugin(String userEmail, String userToken, String taskId);
 
     PluginResultVO getCaseList(String userEmail, String userToken, String taskId) throws Exception;
+
+    String getAnalysisSignature(long taskID,long stuID,String caseName);
 }

+ 28 - 0
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/web/logic/fromDev/impl/PluginLogicImpl.java

@@ -20,6 +20,8 @@ import cn.iselab.mooctest.site.web.logic.fromDev.PluginLogic;
 import com.aliyun.oss.HttpMethod;
 import com.aliyun.oss.OSSClient;
 import com.aliyun.oss.model.GeneratePresignedUrlRequest;
+import com.aliyun.oss.model.OSSObjectSummary;
+import com.aliyun.oss.model.ObjectListing;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
@@ -226,4 +228,30 @@ public class PluginLogicImpl extends BaseLogic implements PluginLogic{
         loginResultVO.setWorkerId(user.getId().toString());
         return loginResultVO;
     }
+
+    @Override
+    public String getAnalysisSignature(long examID,long wokerID,String caseName){
+        //生成路径
+        String ossDir = "data/answers/" + examID + "/" + wokerID + "/";
+        OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);
+
+        ObjectListing objectListing = client.listObjects(bucketName, ossDir);
+        List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
+        long latestTimestamp = 0;
+        for (OSSObjectSummary s : sums) {
+            String zipFullName = s.getKey();
+            if (!zipFullName.endsWith("/") && zipFullName.contains(caseName)) {
+                String zipName = zipFullName.split("/")[zipFullName.split("/").length - 1];
+                if (zipName.startsWith(caseName)) {
+                    String zipNameWithoutPostfix = zipName.split("\\.")[0];
+                    long currentTimestamp = Long.parseLong(zipNameWithoutPostfix.split("_")[1]);
+                    if (currentTimestamp > latestTimestamp) {
+                        latestTimestamp = currentTimestamp;
+                    }
+                }
+            }
+        }
+        String ossUrl = ossDir + caseName + "_" + latestTimestamp + ".zip";
+        return ossUrl;
+    }
 }

+ 83 - 0
mooctest-site-server/src/test/java/cn/iselab/mooctest/site/service/fromDev/impl/AnalysisServiceImplTest.java

@@ -0,0 +1,83 @@
+package cn.iselab.mooctest.site.service.fromDev.impl;
+
+import cn.iselab.mooctest.site.Application;
+import cn.iselab.mooctest.site.models.Case;
+import cn.iselab.mooctest.site.rpc.dev.DevService;
+import cn.iselab.mooctest.site.rpc.dev.data.MutationDTO;
+import cn.iselab.mooctest.site.service.CaseService;
+import cn.iselab.mooctest.site.service.fromDev.AnalysisService;
+import cn.iselab.mooctest.site.web.logic.fromDev.PluginLogic;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.springframework.boot.test.SpringApplicationConfiguration;
+import org.springframework.test.context.web.WebAppConfiguration;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.Assert.*;
+import static org.mockito.Matchers.*;
+import static org.mockito.Mockito.when;
+
+/**
+ * Created by ROGK on 2017/8/10.
+ */
+public class AnalysisServiceImplTest {
+
+    @InjectMocks
+    private AnalysisService analysisService=new AnalysisServiceImpl();
+
+    @Mock
+    private CaseService caseService;
+    @Mock
+    private PluginLogic pluginLogic;
+    @Mock
+    private DevService devService;
+
+    private MutationDTO mutationDTO=new MutationDTO();
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mutationDTO.setTotal(10);
+        mutationDTO.setKilled(100);
+    }
+
+    @Test
+    public void should_return_MutationDTO_when_given_examIdAndworkerIdAndcaseId() throws Exception{
+        Case devcase=new Case();
+        devcase.setId(1L);
+        devcase.setName("test");
+        when(caseService.getCaseById(anyLong())).thenReturn(devcase);
+        when(pluginLogic.getAnalysisSignature(anyLong(),anyLong(),anyString())).thenReturn("test");
+        when(devService.mutationAnalyze(anyString(),anyList())).thenReturn(mutationDTO);
+
+        MutationDTO result=analysisService.mutationAnalyze(1L,1L,1L);
+
+        Assert.assertEquals(mutationDTO,result);
+    }
+
+    @Test
+    public void should_return_MutationDTOList_when_given_workerIdList() throws Exception{
+        List<MutationDTO> expect=new ArrayList<>();
+        expect.add(mutationDTO);
+        Case devcase=new Case();
+        devcase.setId(1L);
+        devcase.setName("test");
+        when(caseService.getCaseById(anyLong())).thenReturn(devcase);
+        when(pluginLogic.getAnalysisSignature(anyLong(),anyLong(),anyString())).thenReturn("test");
+        when(devService.mutationAnalyze(anyString(),anyList())).thenReturn(mutationDTO);
+
+        List<Long> longs=new ArrayList<>();
+        longs.add(1L);
+        List<MutationDTO> result=analysisService.mutationAllAnalyze(1L,longs,1L);
+
+        Assert.assertEquals(expect,result);
+    }
+
+}

+ 0 - 37
mooctest-site-server/src/test/java/cn/iselab/mooctest/site/web/ctrl/fromDev/AnalysisControllerTest.java

@@ -117,43 +117,6 @@ public class AnalysisControllerTest {
     }
 
     @Test
-    public void should_return_MutationDTO_when_MutationDTOExists() throws Exception{
-        MutationDTO mutationDTO=new MutationDTO();
-        String expect=StResponse.success(mutationDTO);
-        when(devService.mutationAnalyze(anyLong(),anyLong(),anyString(),anyLong())).thenReturn(mutationDTO);
-
-        MvcResult result=mockMvc.perform(
-                get("/api/dev/mutation")
-                        .param("examID","1")
-                        .param("stuID","1")
-                        .param("examName","test")
-                        .param("endTime","1")
-        ).andDo(print()).andExpect(status().isOk()).andReturn();
-
-        Assert.assertEquals(expect,result.getResponse().getContentAsString());
-    }
-
-    @Test
-    public void should_return_String_when_FailMutationAnalysis() throws Exception{
-        String expect= StResponse.failure("获取变异分析结果失败");
-
-        when(devService.mutationAnalyze(anyLong(),anyLong(),anyString(),anyLong())).thenReturn(null);
-
-        MvcResult result=mockMvc.perform(
-                get("/api/dev/mutation")
-                        .param("examID","1")
-                        .param("stuID","1")
-                        .param("examName","test")
-                        .param("endTime","1")
-        ).andDo(print()).andExpect(status().isOk()).andReturn();
-
-        JSONObject jsonObject=new JSONObject(result.getResponse().getContentAsString());
-        JSONObject object=new JSONObject(expect);
-
-        Assert.assertEquals(object.getString("condition"),jsonObject.getString("condition"));
-    }
-
-    @Test
     public void should_return_True_when_MutationAllAnalyzeSuccess() throws Exception{
         JSONObject jsonObject=new JSONObject();
         List<Long> list=new ArrayList<>();