Search in sources :

Example 6 with Problem

use of cn.edu.zjnu.acm.judge.domain.Problem in project judge by zjnu-acm.

the class ProblemStatusControllerTest method testStatus.

/**
 * Test of status method, of class ProblemStatusController.
 *
 * {@link ProblemStatusController#status(HttpServletRequest, long, Integer, Pageable)}
 */
@Test
public void testStatus() throws Exception {
    log.info("status");
    Problem problem = Problem.builder().contests(new long[] { 0 }).timeLimit(1000L).memoryLimit(65536L).build();
    problemService.save(problem);
    long problemId = problem.getId();
    MvcResult result = mvc.perform(get("/problemstatus").param("problem_id", Long.toString(problemId))).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML)).andReturn();
}
Also used : Problem(cn.edu.zjnu.acm.judge.domain.Problem) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 7 with Problem

use of cn.edu.zjnu.acm.judge.domain.Problem in project judge by zjnu-acm.

the class ProblemControllerTest method test.

/**
 * {@link ProblemController#save(Problem)}
 * {@link ProblemController#findOne(long, String)}
 * {@link ProblemController#update(long, Problem, String)}
 * {@link ProblemController#delete(long)}
 */
@Test
public void test() throws Exception {
    Problem problem = mockDataService.problem(false);
    Long id = objectMapper.readValue(mvc.perform(post("/api/problems?_format=json").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsBytes(problem))).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andReturn().getResponse().getContentAsString(), Problem.class).getId();
    assertThat(id).describedAs("problem id").isNotNull();
    assertFalse(findOne(id).getDisabled());
    mvc.perform(patch("/api/problems/{id}?_format=json", id).content("{\"disabled\":true}").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isNoContent());
    assertTrue(findOne(id).getDisabled());
    mvc.perform(patch("/api/problems/{id}?_format=json", id).content("{\"disabled\":false}").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isNoContent());
    assertFalse(findOne(id).getDisabled());
    mvc.perform(delete("/api/problems/{id}?_format=json", id)).andExpect(status().isNoContent());
    BusinessCode code = assertThrows(BusinessException.class, () -> problemService.findOne(id)).getCode();
    assertThat(code).isEqualTo(BusinessCode.PROBLEM_NOT_FOUND);
    mvc.perform(get("/api/problems/{id}?_format=json", id)).andExpect(status().isNotFound());
    mvc.perform(delete("/api/problems/{id}?_format=json", id)).andExpect(status().isNotFound());
    mvc.perform(patch("/api/problems/{id}?_format=json", id).content("{}").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isNotFound());
}
Also used : BusinessException(cn.edu.zjnu.acm.judge.exception.BusinessException) BusinessCode(cn.edu.zjnu.acm.judge.exception.BusinessCode) Problem(cn.edu.zjnu.acm.judge.domain.Problem) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 8 with Problem

use of cn.edu.zjnu.acm.judge.domain.Problem in project judge by zjnu-acm.

the class SubmitControllerTest method testContestSubmitPage.

/**
 * Test of contestSubmitPage method, of class SubmitController.
 *
 * {@link SubmitController#contestSubmitPage(Model, Long, long)}
 */
@Test
public void testContestSubmitPage() throws Exception {
    log.info("contestSubmitPage");
    User user = mockDataService.user();
    Problem problem = mockDataService.problem();
    {
        mvc.perform(get("/problems/{problemId}/submit", problem.getId()).with(anonymous())).andExpect(forwardedUrl("/unauthorized"));
        mvc.perform(get("/problems/{problemId}/submit", problem.getId()).with(user(user.getId()))).andExpect(status().isOk()).andReturn();
    }
    {
        Contest contest = mockDataService.contest();
        long problemId = problem.getId();
        long contestId = contest.getId();
        mvc.perform(get("/contests/{contestId}/problems/{problemId}/submit", contestId, problemId).with(user(user.getId()))).andExpect(status().isOk()).andReturn();
    }
}
Also used : User(cn.edu.zjnu.acm.judge.domain.User) Problem(cn.edu.zjnu.acm.judge.domain.Problem) Contest(cn.edu.zjnu.acm.judge.domain.Contest) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 9 with Problem

use of cn.edu.zjnu.acm.judge.domain.Problem in project judge by zjnu-acm.

the class ProblemStatusController method status.

@GetMapping("problemstatus")
@SuppressWarnings("AssignmentToMethodParameter")
public String status(HttpServletRequest request, @RequestParam("problem_id") long id, @RequestParam(value = "language", required = false) Integer language, @PageableDefault(size = 20, sort = { "time", "memory", "code_length" }) Pageable pageable) {
    log.debug("{}", pageable);
    if (pageable.getPageSize() > 500) {
        pageable = PageRequest.of(pageable.getPageNumber(), 500, pageable.getSort());
    }
    Problem problem = problemService.findOneNoI18n(id);
    List<ScoreCount> list = submissionMapper.groupByScore(null, id);
    ArrayList<String> scores = new ArrayList<>(list.size());
    ArrayList<Long> counts = new ArrayList<>(list.size());
    ArrayList<String> urls = new ArrayList<>(list.size());
    for (ScoreCount scoreCount : list) {
        int score = scoreCount.getScore();
        scores.add(ResultType.getShowSourceString(score));
        counts.add(scoreCount.getCount());
        urls.add(request.getContextPath() + "/status?problem_id=" + id + "&score=" + score);
    }
    BestSubmissionForm form = BestSubmissionForm.builder().problemId(id).language(language).build();
    Page<Submission> page = submissionService.bestSubmission(form, pageable, problem.getSubmitUser());
    boolean isAdmin = UserDetailsServiceImpl.isAdministrator(request);
    boolean isSourceBrowser = UserDetailsServiceImpl.isSourceBrowser(request);
    boolean canView = isAdmin || isSourceBrowser;
    request.setAttribute("page", page);
    request.setAttribute("sa", Arrays.asList(counts, scores, urls));
    request.setAttribute("problem", problem);
    request.setAttribute("url", URIBuilder.fromRequest(request).replaceQueryParam("page").toString());
    request.setAttribute("canView", canView);
    request.setAttribute("currentUserId", SecurityUtils.getUserId());
    return "problems/status";
}
Also used : Submission(cn.edu.zjnu.acm.judge.domain.Submission) ArrayList(java.util.ArrayList) ScoreCount(cn.edu.zjnu.acm.judge.data.dto.ScoreCount) Problem(cn.edu.zjnu.acm.judge.domain.Problem) BestSubmissionForm(cn.edu.zjnu.acm.judge.data.form.BestSubmissionForm) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 10 with Problem

use of cn.edu.zjnu.acm.judge.domain.Problem in project judge by zjnu-acm.

the class JudgeServiceImpl method execute.

@Override
public void execute(long submissionId) {
    Submission submission = submissionMapper.findOne(submissionId);
    if (submission == null) {
        throw new BusinessException(BusinessCode.SUBMISSION_NOT_FOUND, submissionId);
    }
    long problemId = submission.getProblem();
    Problem problem = problemService.findOneNoI18n(problemId);
    try {
        RunRecord runRecord = RunRecord.builder().language(languageService.getAvailableLanguage(submission.getLanguage())).source(submissionDetailMapper.findSourceById(submissionId)).memoryLimit(problem.getMemoryLimit()).timeLimit(problem.getTimeLimit()).build();
        Path dataDirectory = systemService.getDataDirectory(problemId);
        JudgeData judgeData = JudgeData.parse(dataDirectory);
        Path specialFile = systemService.getSpecialJudgeExecutable(problemId);
        boolean isSpecial = systemService.isSpecialJudge(problemId);
        // 建立临时文件
        Path work = systemService.getWorkDirectory(submissionId);
        final Validator validator = isSpecial ? new SpecialValidator(specialFile.toString(), work) : SimpleValidator.PE_AS_AC;
        boolean deleteTempFile = systemService.isDeleteTempFile();
        RunResult runResult = judgeRunner.run(runRecord, work, judgeData, validator, deleteTempFile);
        SubmissionDetail detail = SubmissionDetail.builder().id(submissionId).compileInfo(runResult.getCompileInfo()).detail(runResult.getDetail()).systemInfo(runResult.getSystemInfo()).build();
        if (runResult.getType() == Status.COMPILATION_ERROR) {
            submissionMapper.updateResult(submissionId, ResultType.COMPILE_ERROR, 0, 0);
        } else {
            int score = runResult.getScore();
            long time = runResult.getTime();
            long memory = runResult.getMemory();
            submissionMapper.updateResult(submissionId, score, time, memory);
        }
        // TODO return value not handled, we can do nothing for the record not exists in the table now.
        submissionDetailMapper.update(detail);
        updateSubmissionStatus(submission.getUser(), problemId);
    } catch (ThreadDeath | VirtualMachineError error) {
        throw error;
    } catch (JudgeException | IOException | Error ex) {
        log.error("got an exception when judging submission {}", submissionId, ex);
        submissionMapper.updateResult(submissionId, ResultType.SYSTEM_ERROR, 0, 0);
        StringWriter sw = new StringWriter();
        try (PrintWriter pw = new PrintWriter(sw)) {
            ex.printStackTrace(pw);
        }
        submissionDetailMapper.update(SubmissionDetail.builder().id(submissionId).systemInfo(sw.toString()).build());
    }
}
Also used : Path(java.nio.file.Path) Submission(cn.edu.zjnu.acm.judge.domain.Submission) IOException(java.io.IOException) RunRecord(cn.edu.zjnu.acm.judge.support.RunRecord) BusinessException(cn.edu.zjnu.acm.judge.exception.BusinessException) JudgeData(cn.edu.zjnu.acm.judge.support.JudgeData) StringWriter(java.io.StringWriter) SubmissionDetail(cn.edu.zjnu.acm.judge.domain.SubmissionDetail) SpecialValidator(cn.edu.zjnu.acm.judge.sandbox.win32.SpecialValidator) Problem(cn.edu.zjnu.acm.judge.domain.Problem) JudgeException(cn.edu.zjnu.acm.judge.core.JudgeException) RunResult(cn.edu.zjnu.acm.judge.support.RunResult) Validator(cn.edu.zjnu.acm.judge.core.Validator) SpecialValidator(cn.edu.zjnu.acm.judge.sandbox.win32.SpecialValidator) SimpleValidator(cn.edu.zjnu.acm.judge.core.SimpleValidator) PrintWriter(java.io.PrintWriter)

Aggregations

Problem (cn.edu.zjnu.acm.judge.domain.Problem)26 Contest (cn.edu.zjnu.acm.judge.domain.Contest)10 BusinessException (cn.edu.zjnu.acm.judge.exception.BusinessException)10 Nonnull (javax.annotation.Nonnull)7 Submission (cn.edu.zjnu.acm.judge.domain.Submission)6 GetMapping (org.springframework.web.bind.annotation.GetMapping)6 ArrayList (java.util.ArrayList)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 ScoreCount (cn.edu.zjnu.acm.judge.data.dto.ScoreCount)4 Test (org.junit.jupiter.api.Test)4 BusinessCode (cn.edu.zjnu.acm.judge.exception.BusinessCode)3 IOException (java.io.IOException)3 Transactional (org.springframework.transaction.annotation.Transactional)3 BestSubmissionForm (cn.edu.zjnu.acm.judge.data.form.BestSubmissionForm)2 ContestForm (cn.edu.zjnu.acm.judge.data.form.ContestForm)2 ContestStatus (cn.edu.zjnu.acm.judge.data.form.ContestStatus)2 ContestMapper (cn.edu.zjnu.acm.judge.mapper.ContestMapper)2 SubmissionMapper (cn.edu.zjnu.acm.judge.mapper.SubmissionMapper)2 EnumUtils (cn.edu.zjnu.acm.judge.util.EnumUtils)2 SpecialCall (cn.edu.zjnu.acm.judge.util.SpecialCall)2