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());
}
Aggregations