@Test public void failsToRecordVoteIfUserDoesNotHaveWriteAccess() throws Exception { User user = user().withWriteAccess(false).build(); Ern recordVote = Ern.valueOf("E05001221-PD-123-1"); Try<RecordVoteResponse> recordVoteResponse = underTest.recordVote(user, recordVote); assertThat(recordVoteResponse, isFailure(instanceOf(NotAuthorizedFailure.class))); }
public Try<RecordVoteResponse> recordVote(User user, Ern ern) { return user .ensureWriteAccess() .flatMap(resolvedUser -> wardService.getByCode(ern.getWardCode(), user) .flatMap(ward -> { Call<RecordVotedResponse> call = pafClient.recordVote(ern.longForm()); return pafRequestExecutor.execute(call); }) .map(success -> { log.debug("user={} recorded vote for ern={}", user, ern.longForm()); auditor.audit(recordVoted(ern.longForm(), user.getUsername())); return ImmutableRecordVoteResponse.builder() .withErn(ern.longForm()) .withWardCode(ern.getWardCode()) .build(); })); }
RecordVotedService { public Try<RecordVoteResponse> recordVote(User user, Ern ern) { return user .ensureWriteAccess() .flatMap(resolvedUser -> wardService.getByCode(ern.getWardCode(), user) .flatMap(ward -> { Call<RecordVotedResponse> call = pafClient.recordVote(ern.longForm()); return pafRequestExecutor.execute(call); }) .map(success -> { log.debug("user={} recorded vote for ern={}", user, ern.longForm()); auditor.audit(recordVoted(ern.longForm(), user.getUsername())); return ImmutableRecordVoteResponse.builder() .withErn(ern.longForm()) .withWardCode(ern.getWardCode()) .build(); })); } }
RecordVotedService { public Try<RecordVoteResponse> recordVote(User user, Ern ern) { return user .ensureWriteAccess() .flatMap(resolvedUser -> wardService.getByCode(ern.getWardCode(), user) .flatMap(ward -> { Call<RecordVotedResponse> call = pafClient.recordVote(ern.longForm()); return pafRequestExecutor.execute(call); }) .map(success -> { log.debug("user={} recorded vote for ern={}", user, ern.longForm()); auditor.audit(recordVoted(ern.longForm(), user.getUsername())); return ImmutableRecordVoteResponse.builder() .withErn(ern.longForm()) .withWardCode(ern.getWardCode()) .build(); })); } @Autowired  RecordVotedService(WardService wardService,
                              PafClient pafClient,
                              PafRequestExecutor pafRequestExecutor,
                              Auditor auditor); }
RecordVotedService { public Try<RecordVoteResponse> recordVote(User user, Ern ern) { return user .ensureWriteAccess() .flatMap(resolvedUser -> wardService.getByCode(ern.getWardCode(), user) .flatMap(ward -> { Call<RecordVotedResponse> call = pafClient.recordVote(ern.longForm()); return pafRequestExecutor.execute(call); }) .map(success -> { log.debug("user={} recorded vote for ern={}", user, ern.longForm()); auditor.audit(recordVoted(ern.longForm(), user.getUsername())); return ImmutableRecordVoteResponse.builder() .withErn(ern.longForm()) .withWardCode(ern.getWardCode()) .build(); })); } @Autowired  RecordVotedService(WardService wardService,
                              PafClient pafClient,
                              PafRequestExecutor pafRequestExecutor,
                              Auditor auditor); Try<RecordVoteResponse> recordVote(User user, Ern ern); Try<RecordVoteResponse> wontVote(User user, Ern ern); Try<RecordVoteResponse> undoVote(User user, Ern ern); Try<DeleteContactResponse> undoWontVote(User user, Ern ern, UUID contactId); }
RecordVotedService { public Try<RecordVoteResponse> recordVote(User user, Ern ern) { return user .ensureWriteAccess() .flatMap(resolvedUser -> wardService.getByCode(ern.getWardCode(), user) .flatMap(ward -> { Call<RecordVotedResponse> call = pafClient.recordVote(ern.longForm()); return pafRequestExecutor.execute(call); }) .map(success -> { log.debug("user={} recorded vote for ern={}", user, ern.longForm()); auditor.audit(recordVoted(ern.longForm(), user.getUsername())); return ImmutableRecordVoteResponse.builder() .withErn(ern.longForm()) .withWardCode(ern.getWardCode()) .build(); })); } @Autowired  RecordVotedService(WardService wardService,
                              PafClient pafClient,
                              PafRequestExecutor pafRequestExecutor,
                              Auditor auditor); Try<RecordVoteResponse> recordVote(User user, Ern ern); Try<RecordVoteResponse> wontVote(User user, Ern ern); Try<RecordVoteResponse> undoVote(User user, Ern ern); Try<DeleteContactResponse> undoWontVote(User user, Ern ern, UUID contactId);  }