### Question:
RecordingDataHandler implements DataHandler { @Override public Optional<byte[]> handle(final byte[] data) { return delegateDataHandler.handle(data) .map(response -> RecordingEntity.builder() .withRequest(data) .withReply(response) .build()) .map(recordingsRepository::save) .map(RecordingEntity::getReply); } RecordingDataHandler(final RecordingsRepository recordingsRepository,
                                final DataHandler delegateDataHandler); @Override Optional<byte[]> handle(final byte[] data);  }### Answer:
@Test public void handleSavesRecordingIfDelegateSuccessfullyHandlesGivenData() { final byte[] givenRequest = RandomUtils.nextBytes(100); final byte[] givenReply = RandomUtils.nextBytes(50); final RecordingEntity givenRecordingEntity = RecordingEntity.builder() .withRequest(givenRequest) .withReply(givenReply) .build(); given(delegateDataHandler.handle(any())) .willReturn(Optional.of(givenReply)); given(recordingsRepository.save(any())) .willReturn(givenRecordingEntity); final Optional<byte[]> reply = victim.handle(givenRequest); assertThat(reply).hasValue(givenReply); assertRecordingEntitySaved(givenRequest, givenReply); }
@Test public void handleDoesntSaveRecordingIfDelegateDataHandlerFailsToHandleGivenData() { final byte[] givenRequest = RandomUtils.nextBytes(100); given(delegateDataHandler.handle(any())) .willReturn(Optional.empty()); final Optional<byte[]> reply = victim.handle(givenRequest); assertThat(reply).isEmpty(); then(recordingsRepository).shouldHaveZeroInteractions(); }