Search in sources :

Example 1 with BusinessCode

use of cn.edu.zjnu.acm.judge.exception.BusinessCode 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)

Aggregations

Problem (cn.edu.zjnu.acm.judge.domain.Problem)1 BusinessCode (cn.edu.zjnu.acm.judge.exception.BusinessCode)1 BusinessException (cn.edu.zjnu.acm.judge.exception.BusinessException)1 Test (org.junit.jupiter.api.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1