@Test @WithMockUser(roles = "HR") void testApprove() { LeaveRequest saved = repository.save(new LeaveRequest("Alice", of(2019, 11, 30), of(2019, 12, 03), PENDING)); Optional<LeaveRequest> approved = service.approve(saved.getId()); verify(repository).findById(saved.getId()); assertThat(approved).isPresent(); assertThat(approved).get().isSameAs(saved); assertThat(approved.get().getStatus()).isSameAs(APPROVED); }
@RolesAllowed("HR") public Optional<LeaveRequest> approve(UUID id) { Optional<LeaveRequest> found = repo.findById(id); found.ifPresent(lr -> lr.setStatus(Status.APPROVED)); return found; }
LeaveRequestService { @RolesAllowed("HR") public Optional<LeaveRequest> approve(UUID id) { Optional<LeaveRequest> found = repo.findById(id); found.ifPresent(lr -> lr.setStatus(Status.APPROVED)); return found; } }
LeaveRequestService { @RolesAllowed("HR") public Optional<LeaveRequest> approve(UUID id) { Optional<LeaveRequest> found = repo.findById(id); found.ifPresent(lr -> lr.setStatus(Status.APPROVED)); return found; }  }
LeaveRequestService { @RolesAllowed("HR") public Optional<LeaveRequest> approve(UUID id) { Optional<LeaveRequest> found = repo.findById(id); found.ifPresent(lr -> lr.setStatus(Status.APPROVED)); return found; }  @PreAuthorize("#employee == authentication.name or hasRole('HR')") LeaveRequest request(String employee, LocalDate from, LocalDate to); @RolesAllowed("HR") Optional<LeaveRequest> approve(UUID id); @RolesAllowed("HR") Optional<LeaveRequest> deny(UUID id); @PostAuthorize("returnObject.orElse(null)?.employee == authentication.name or hasRole('HR')") Optional<LeaveRequest> retrieve(UUID id); @PreAuthorize("#employee == authentication.name or hasRole('HR')") List<LeaveRequest> retrieveFor(String employee); @RolesAllowed("HR") List<LeaveRequest> retrieveAll(); }
LeaveRequestService { @RolesAllowed("HR") public Optional<LeaveRequest> approve(UUID id) { Optional<LeaveRequest> found = repo.findById(id); found.ifPresent(lr -> lr.setStatus(Status.APPROVED)); return found; }  @PreAuthorize("#employee == authentication.name or hasRole('HR')") LeaveRequest request(String employee, LocalDate from, LocalDate to); @RolesAllowed("HR") Optional<LeaveRequest> approve(UUID id); @RolesAllowed("HR") Optional<LeaveRequest> deny(UUID id); @PostAuthorize("returnObject.orElse(null)?.employee == authentication.name or hasRole('HR')") Optional<LeaveRequest> retrieve(UUID id); @PreAuthorize("#employee == authentication.name or hasRole('HR')") List<LeaveRequest> retrieveFor(String employee); @RolesAllowed("HR") List<LeaveRequest> retrieveAll();  }