RoleResource { @ResponseBody @ApiOperation(value = "Save a new Role") @PostMapping @PreAuthorize(ConstantsPrivilege.PRIVILEGE_CREATE_ROLE) public ResponseEntity<?> save(@RequestBody @Valid RoleDTO roleDTO) { Role role = roleService.save(roleDTO); return ResponseEntity.created(URI.create(String.format("/%s/%s", "roles", role.getId().toString()))).build(); }  @ResponseBody @ApiOperation(value = "Save a new Role") @PostMapping @PreAuthorize(ConstantsPrivilege.PRIVILEGE_CREATE_ROLE) ResponseEntity<?> save(@RequestBody @Valid RoleDTO roleDTO); @ResponseBody @ApiOperation(value = "Find Role by id", response = Role.class) @GetMapping(value = "/{roleId}") @PreAuthorize(ConstantsPrivilege.PRIVILEGE_READ_ROLE) ResponseEntity<?> findById(@PathVariable("roleId") Long roleId); @ResponseBody @ApiOperation(value = "Find all Resources", responseContainer = "List", response = Role.class) @GetMapping @PreAuthorize(ConstantsPrivilege.PRIVILEGE_READ_ROLE) ResponseEntity<?> findAll(@ModelAttribute RoleDTO roleDTO, @ModelAttribute PageableDTO pageableDTO); @ResponseBody @ApiOperation(value = "Update Role") @PutMapping(value = "/{roleId}") @PreAuthorize(ConstantsPrivilege.PRIVILEGE_UPDATE_ROLE) ResponseEntity<?> update(@PathVariable("roleId") Long roleId, @RequestBody RoleDTO roleDTO); @ResponseBody @ApiOperation(value = "Delete Role") @DeleteMapping(value = "/{roleId}") @PreAuthorize(ConstantsPrivilege.PRIVILEGE_DELETE_ROLE) ResponseEntity<?> delete( @PathVariable("roleId") Long roleId);  }
@Test @WithMockUser(username="tester", authorities={"READ_ROLE", "CREATE_ROLE", "DELETE_ROLE", "UPDATE_ROLE"}) public void notPermitToPersistRoleWithPrivilegesMissing() throws Exception { Role role = new Role(); role.setId(100L); Mockito.when(service.save(Mockito.any(RoleDTO.class))).thenReturn(role); mockMvc.perform(MockMvcRequestBuilders.post(ConstantsPath.PATH_ROLES) .content("{\"name\":\"\",\"privileges\":[{\"id\":5},{\"id\":2},{\"id\":10}]}") .contentType(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().is4xxClientError()) .andExpect(MockMvcResultMatchers.content().json("{\"status\":400,\"exception\":\"MethodArgumentNotValidException\",\"erros\":[{\"defaultMessage\":\"size must be between 6 and 80\",\"objectName\":\"roleDTO\",\"field\":\"name\",\"code\":\"Size\"}]}")); }