Search in sources :

Example 1 with BusinessException

use of cn.edu.zjnu.acm.judge.exception.BusinessException in project judge by zjnu-acm.

the class ProblemControllerTest method test.

/**
 * @see ProblemController#save(Problem)
 * @see ProblemController#findOne(long, String)
 * @see ProblemController#update(long, Problem, String)
 * @see ProblemController#delete(long)
 */
@Test
public void test() throws Exception {
    Problem problem = mockDataService.problem(false);
    Long id = objectMapper.readValue(mvc.perform(post("/api/problems.json").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsBytes(problem))).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andReturn().getResponse().getContentAsString(), Problem.class).getId();
    assertNotNull(id);
    assertFalse(findOne(id).getDisabled());
    mvc.perform(patch("/api/problems/{id}.json", id).content("{\"disabled\":true}").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isNoContent());
    assertTrue(findOne(id).getDisabled());
    mvc.perform(patch("/api/problems/{id}.json", id).content("{\"disabled\":false}").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isNoContent());
    assertFalse(findOne(id).getDisabled());
    mvc.perform(delete("/api/problems/{id}.json", id)).andExpect(status().isNoContent());
    try {
        Problem p = problemService.findOne(id);
        fail("should throw a BusinessException");
    } catch (BusinessException ex) {
        assertEquals(BusinessCode.PROBLEM_NOT_FOUND, ex.getCode());
    }
    mvc.perform(get("/api/problems/{id}.json", id)).andExpect(status().isNotFound());
    mvc.perform(delete("/api/problems/{id}.json", id)).andExpect(status().isNotFound());
    mvc.perform(patch("/api/problems/{id}.json", id).content("{}").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isNotFound());
}
Also used : BusinessException(cn.edu.zjnu.acm.judge.exception.BusinessException) Problem(cn.edu.zjnu.acm.judge.domain.Problem) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with BusinessException

use of cn.edu.zjnu.acm.judge.exception.BusinessException in project judge by zjnu-acm.

the class SubmissionService method contestSubmit.

public CompletableFuture<?> contestSubmit(int languageId, String source, String userId, String ip, long contestId, long problemNum) {
    check(languageId, source, userId);
    Contest contest = contestService.findOneByIdAndNotDisabled(contestId);
    // contest not started yet, can't submit the problem.
    if (!contest.isStarted()) {
        throw new BusinessException(BusinessCode.CONTEST_PROBLEM_NOT_FOUND, contestId, problemNum);
    }
    Problem problem = contestService.getProblem(contestId, problemNum, null);
    return submit0(Submission.builder().contest(contest.isEnded() ? null : contestId).problem(problem.getOrigin()).ip(ip), source, userId, languageId);
}
Also used : BusinessException(cn.edu.zjnu.acm.judge.exception.BusinessException) Problem(cn.edu.zjnu.acm.judge.domain.Problem) Contest(cn.edu.zjnu.acm.judge.domain.Contest)

Example 3 with BusinessException

use of cn.edu.zjnu.acm.judge.exception.BusinessException in project judge by zjnu-acm.

the class SearchUserController method searchuser.

@GetMapping("/searchuser")
public String searchuser(Model model, @RequestParam(value = "user_id", defaultValue = "") String keyword, @RequestParam(value = "position", required = false) String position, @PageableDefault(1000) Pageable pageable) {
    if (keyword.replace("%", "").length() < 2) {
        throw new BusinessException(BusinessCode.USER_SEARCH_KEYWORD_SHORT);
    }
    String like = keyword;
    if (position == null) {
        like = "%" + like + "%";
    } else if (position.equalsIgnoreCase("begin")) {
        like += "%";
    } else {
        like = "%" + like;
    }
    Pageable t = pageable;
    if (t.getSort() == null) {
        t = new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), new Sort(new Sort.Order("solved").with(Sort.Direction.DESC), new Sort.Order("submit")));
    }
    Page<User> users = accountService.findAll(AccountForm.builder().disabled(Boolean.FALSE).query(like).build(), t);
    model.addAttribute("query", keyword);
    model.addAttribute("users", users);
    return "users/search";
}
Also used : BusinessException(cn.edu.zjnu.acm.judge.exception.BusinessException) PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) User(cn.edu.zjnu.acm.judge.domain.User) Sort(org.springframework.data.domain.Sort) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 4 with BusinessException

use of cn.edu.zjnu.acm.judge.exception.BusinessException in project judge by zjnu-acm.

the class AccountService method prepare.

private void prepare(Collection<Account> accounts) {
    for (Account account : accounts) {
        if (!StringUtils.hasText(account.getId())) {
            throw new BusinessException(BusinessCode.EMPTY_USER_ID);
        }
        String password = account.getPassword();
        if (StringUtils.isEmpty(password)) {
            throw new BusinessException(BusinessCode.EMPTY_PASSWORD);
        }
        if (password.length() <= PasswordConfiguration.MAX_PASSWORD_LENGTH) {
            ValueCheck.checkPassword(password);
            account.setPassword(passwordEncoder.encode(password));
        }
        if (!StringUtils.hasText(account.getEmail())) {
            account.setEmail(null);
        }
        if (!StringUtils.hasText(account.getNick())) {
            account.setNick(account.getId());
        }
        if (account.getSchool() == null) {
            account.setSchool("");
        }
    }
}
Also used : Account(cn.edu.zjnu.acm.judge.data.excel.Account) BusinessException(cn.edu.zjnu.acm.judge.exception.BusinessException)

Example 5 with BusinessException

use of cn.edu.zjnu.acm.judge.exception.BusinessException in project judge by zjnu-acm.

the class JudgeService method toCompletableFuture.

CompletableFuture<?> toCompletableFuture(Executor executor, long submissionId) {
    return CompletableFuture.runAsync(() -> {
        Submission submission = submissionMapper.findOne(submissionId);
        if (submission == null) {
            throw new BusinessException(BusinessCode.SUBMISSION_NOT_FOUND);
        }
        Problem problem = problemService.findOneNoI18n(submission.getProblem());
        RunRecord runRecord = RunRecord.builder().submissionId(submission.getId()).language(languageService.getAvailableLanguage(submission.getLanguage())).problemId(submission.getProblem()).userId(submission.getUser()).source(submissionMapper.findSourceById(submissionId)).memoryLimit(problem.getMemoryLimit()).timeLimit(problem.getTimeLimit()).build();
        judgeInternal(runRecord);
    }, executor);
}
Also used : RunRecord(cn.edu.zjnu.acm.judge.data.dto.RunRecord) BusinessException(cn.edu.zjnu.acm.judge.exception.BusinessException) Submission(cn.edu.zjnu.acm.judge.domain.Submission) Problem(cn.edu.zjnu.acm.judge.domain.Problem)

Aggregations

BusinessException (cn.edu.zjnu.acm.judge.exception.BusinessException)10 Problem (cn.edu.zjnu.acm.judge.domain.Problem)5 GetMapping (org.springframework.web.bind.annotation.GetMapping)4 Contest (cn.edu.zjnu.acm.judge.domain.Contest)3 Submission (cn.edu.zjnu.acm.judge.domain.Submission)3 MessageException (cn.edu.zjnu.acm.judge.exception.MessageException)2 RunRecord (cn.edu.zjnu.acm.judge.data.dto.RunRecord)1 SubmissionDetail (cn.edu.zjnu.acm.judge.data.dto.SubmissionDetail)1 Account (cn.edu.zjnu.acm.judge.data.excel.Account)1 User (cn.edu.zjnu.acm.judge.domain.User)1 Path (java.nio.file.Path)1 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)1 FormulaEvaluator (org.apache.poi.ss.usermodel.FormulaEvaluator)1 Workbook (org.apache.poi.ss.usermodel.Workbook)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 PageRequest (org.springframework.data.domain.PageRequest)1 Pageable (org.springframework.data.domain.Pageable)1 Sort (org.springframework.data.domain.Sort)1 Secured (org.springframework.security.access.annotation.Secured)1