123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- package edu.nju.service;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.OutputStreamWriter;
- import java.io.Writer;
- import java.text.SimpleDateFormat;
- import java.util.*;
- import edu.nju.dao.*;
- import edu.nju.entities.*;
- import edu.nju.model.*;
- import edu.nju.util.*;
- import org.apache.commons.lang3.EnumUtils;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import org.springframework.web.bind.annotation.ResponseBody;
- @Service
- public class ExtraService {
- @Autowired
- ReportDao reportDao;
-
- @Autowired
- ExamDao examDao;
-
- @Autowired
- TestCaseDao testDao;
-
- @Autowired
- BugDao bugDao;
- @Autowired
- TaskDao taskDao;
- @Autowired
- BugMirrorDao mirrorDao;
- @Autowired
- ReviewPaperService reviewPaperService;
- @Autowired
- CrowdSourceToReviewPaperDao crowdSourceToReviewPaperDao;
- @Value("${sanjiPageUrl}")
- private String pageUrl;
- @Value("${main.site}")
- private String mainSiteUrl;
- //测试用例相关
- public String saveTestCase(String report_id, String name, String front, String behind, String description) {
- TestCase testCase = new TestCase(name, front, behind, description, report_id, Long.toString(System.currentTimeMillis()));
- return testDao.save(testCase);
- }
- // public String saveTestCase(String report_id, String name, String front, String behind, String description,String if_execute,String if_bug) {
- // TestCase testCase = new TestCase(name, front, behind, description, report_id, Long.toString(System.currentTimeMillis()),if_execute,if_bug);
- // return testDao.save(testCase);
- // }
-
- public boolean updateTestCase(String id, String report_id, String name, String front, String behind, String description) {
- try {
- testDao.updateTestCase(id, report_id, name, front, behind, description);
- return true;
- } catch (Exception e) { return false; }
- }
-
- public List<TestCase> getCaseList(String report_id) {
- return testDao.findByReport(report_id);
- }
-
- public TestCase getTestCase(String id) {
- return testDao.findById(id);
- }
-
-
- //测试报告相关
- public synchronized String saveReport(String case_id, String task_id, String case_take_id, String woker_id, String name, String device_model,
- String device_brand, String device_os, String script_location, String report_location, String log_location) {
- Report report=reportDao.findByWoker(case_take_id,woker_id);
- if(report==null) {
- Report report2 = new Report(case_id, task_id, case_take_id, woker_id, name, Long.toString(System.currentTimeMillis()),
- device_model, device_brand, device_os, script_location, report_location, log_location);
- return reportDao.save(report2);
- }
- else{
- return report.getId();
- }
- }
- public boolean updateReport(String report_id, String case_id, String task_id, String case_take_id, String woker_id, String name, String device_model,
- String device_brand, String device_os, String script_location, String report_location, String log_location) {
- Report report = new Report(case_id, task_id, case_take_id, woker_id, name, Long.toString(System.currentTimeMillis()),
- device_model, device_brand, device_os, script_location, report_location, log_location);
- return reportDao.update(report_id, report);
- }
-
- public Report getReport(String report_id) {
- return reportDao.findById(report_id);
- }
-
- public Report findByWorker(String case_take_id, String worker_id) {
- return reportDao.findByWoker(case_take_id, worker_id);
- }
-
- public List<Bug> getBugList(String report_id, String case_take_id) {
- return bugDao.findByReport(report_id, case_take_id);
- }
-
-
- //测试题目相关
- public String saveExam(String case_id, String path, String app_name, String paper_type, String test_type, String description, String collaborative_type) {
- String json = ExcelToJson.ExcelTranse(path).toString();
- Exam exam = new Exam(case_id, json, app_name, paper_type, test_type, description, collaborative_type);
- examDao.save(exam);
- return json;
- }
- // public String saveExam1(String case_id, String path, String app_name, String paper_type, String test_type, String description,
- // String if_test_case,String if_bug) {
- // String json = ExcelToJson.ExcelTranse(path).toString();
- //// Exam exam = new Exam(case_id, json, app_name, paper_type, test_type, description);
- // Exam exam = new Exam(case_id, json, app_name, paper_type, test_type, description,if_test_case,if_bug);
- // examDao.save(exam);
- // return json;
- // }
- public JSONArray getExamList() {
- List<Exam> result = examDao.findAll();
- JSONArray array = new JSONArray();
- // Set<String> ids = bugDao.findAllids();
- Map<String, Exam> exams = new HashMap<>();
- for(Exam exam: result) {
- JSONObject object = new JSONObject();
- exams.put(exam.getId(), exam);
- object.put("name", exam.getName());
- object.put("description", exam.getDescription());
- object.put("testType", exam.getTest_type());
- object.put("case_id", exam.getId());
- object.put("task_id", exam.getId()); //这里是私有云特有的,一次case对应一次task
- array.put(object);
- }
- // for(Exam exam: result) { exams.put(exam.getId(), exam); }
- // for(String id: ids) {
- // if(id == null || id.length() <= 0) { continue; }
- // String[] strs = id.split("-");
- // String case_id = strs[0];
- // String task_id = strs[1];
- // if(exams.containsKey(case_id)) {
- // JSONObject object = new JSONObject();
- // Exam exam = exams.get(case_id);
- // object.put("name", exam.getName());
- // object.put("description", exam.getDescription());
- // object.put("testType", exam.getTest_type());
- // object.put("case_id", case_id);
- // object.put("task_id", task_id);
- // array.put(object);
- // }
- // }
- return array;
- }
- public Exam getExam(String id) {
- return examDao.findById(id);
- }
- public Task saveTask(String id){
- Task task=taskDao.findById(id);
- if(task!=null) {
- long oldTime=task.getEnd_time();
- //考试时间已过
- if(oldTime>System.currentTimeMillis()){
- return getAndSaveTaskInfo(id);
- }else {
- return task;
- }
- }else{
- return getAndSaveTaskInfo(id);
- }
- }
- private Task getAndSaveTaskInfo(String id){
- String result = HTTP.sendGet(mainSiteUrl + "/api/exam/" + id + "/info", "");
- System.out.println("result " + result);
- if (!result.equals("")) {
- JSONObject json = new JSONObject(result);
- long beginTime = json.getLong("beginTime");
- long endTime = json.getLong("endTime");
- String name=json.getString("name");
- double totalMins = (endTime - beginTime) / 1000 / 60.0;
- Task newTask = new Task(id, name,beginTime, endTime, totalMins, totalMins);
- taskDao.save(newTask);
- return newTask;
- }else{
- return null;
- }
- }
- public boolean updateTask(String id,double writeMins){
- try {
- Task task =taskDao.findById(id);
- task.setWrite_time(writeMins);
- taskDao.save(task);
- return true;
- }catch (Exception e){
- return false;
- }
- }
- public String reportsToReviewPaper(String case_take_id){
- int[]options={0,1,2,3,4,5,6,7,8,9,10};
- String url="http://mooctest-site.oss-cn-shanghai.aliyuncs.com/paperjson/"+case_take_id+".json";
- try {
- boolean isExist=OssAliyun.existFile("paperjson/"+case_take_id+".json");
- if(isExist){
- return url;
- }
- JSONObject result=new JSONObject();
- List<Bug>bugs=bugDao.findByCaseid(case_take_id);
- JSONArray reportArray=new JSONArray();
- int[]indexArray=new int[bugs.size()];
- for(int i=0;i<bugs.size();i++) {
- Bug bug=bugs.get(i);
- BugMirror mirror=mirrorDao.findById(bug.getId());
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("index",i);
- indexArray[i]=i;
- jsonObject.put("name",bug.getTitle());
- jsonObject.put("original_id",bug.getId());
- String description="";
- description+=bug.getDescription()+"\n";
- description+="三级页面:"+bug.getBug_page()+"\n";
- description+=bug.getBug_category()+"\n";
- BugSeverity bugSeverity=BugSeverity.getValue(bug.getSeverity());
- description+="严重程度:"+bugSeverity+"\n";
- BugRecurrent bugRecurrent=BugRecurrent.getValue(bug.getRecurrent());
- description+="复现程度:"+bugRecurrent+"\n";
- SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String dateStr = dateformat.format(TimeUtil.StringToDate(bug.getCreate_time_millis()));
- description+=dateStr+"\n";
- if(mirror==null){
- description+="0\n";
- description+="0\n";
- }
- else {
- description += "点赞数: "+mirror.getGood().size() + "\n";
- description += "点踩数: "+mirror.getBad().size();
- }
- jsonObject.put("description",description);
- String[]urlArray=new String[1];
- urlArray[0]=bug.getImg_url();
- jsonObject.put("img_url",urlArray);
- JSONArray fileArray=new JSONArray();
- jsonObject.put("file_url",fileArray);
- reportArray.put(jsonObject);
- }
- result.put("report_list",reportArray);
- result.put("description","");
- result.put("name","");
- result.put("create_time",String.valueOf(System.currentTimeMillis()));
- result.put("type","众包测试");
- result.put("application_url","");
- result.put("requirement_url","");
- JSONArray itemGroupArray=new JSONArray();
- JSONObject itemGroupObject=new JSONObject();
- JSONArray itemArray=new JSONArray();
- JSONObject itemObject=new JSONObject();
- itemObject.put("index",0);
- itemObject.put("description","请评分");
- itemObject.put("is_required",true);
- itemObject.put("type","Single");
- itemObject.put("options",options);
- itemArray.put(itemObject);
- itemGroupObject.put("report_list",indexArray);
- itemGroupObject.put("item_list",itemArray);
- itemGroupArray.put(itemGroupObject);
- result.put("item_group_list",itemGroupArray);
- String fileName=case_take_id+".json";
- File file=new File(fileName);
- Writer write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
- write.write(result.toString());
- write.flush();
- write.close();
- OssAliyun.uploadFile("paperjson/"+case_take_id+".json",file);
- return url;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- public String getPageUrl(String caseId){
- return pageUrl+caseId+".json";
- }
- public PageExamVO findPageAndExam(long examId, long caseId){
- PageExamVO pageExamVO=new PageExamVO(examId,caseId);
- Exam examCase=getExam(String.valueOf(caseId));
- pageExamVO.setPageContent(examCase.getJson());
- String json=examCase.getPaper_type();
- int caseTypeId=getPaperType(json);
- pageExamVO.setCaseTypeId(caseTypeId);
- Task task=taskDao.findById(String.valueOf(examId));
- Date startTime=null;
- Date endTime=null;
- if(task!=null){
- startTime=new Date(task.getStart_time());
- endTime=new Date(task.getEnd_time());
- }
- ExamVO examVO=new ExamVO(examId,caseId,0,caseTypeId,startTime,endTime);
- pageExamVO.setExamVO(examVO);
- return pageExamVO;
- }
- private int getPaperType(String json){
- //json=json.substring(1,json.length()-1);
- JSONObject jsonObject = new JSONObject(json);
- JSONArray jsonArray=jsonObject.getJSONArray("subTitles");
- int caseTypeId=1;//这里只对WEB和APP进行了判断
- for(int i=0;i<jsonArray.length();i++) {
- JSONObject item = jsonArray.getJSONObject(i);
- String label=item.getString("name");
- if(label.equals("操作系统")){
- JSONArray brandList=item.getJSONArray("value");
- String brand=String.valueOf(brandList.get(0));
- if(EnumUtils.isValidEnum(WebBrand.class, brand)){
- caseTypeId=0;
- }
- break;
- }
- }
- return caseTypeId;
- }
- }
|