ReprocessingService { public void reprocess(MailRepositoryPath path, MailKey key, Optional<String> targetProcessor, MailQueueName targetQueue) throws MailRepositoryStore.MailRepositoryStoreException, MessagingException { try (Reprocessor reprocessor = new Reprocessor(getMailQueue(targetQueue), targetProcessor)) { Pair<MailRepository, Mail> mailPair = mailRepositoryStoreService .getRepositories(path) .map(Throwing.function(repository -> Pair.of(repository, Optional.ofNullable(repository.retrieve(key))))) .filter(pair -> pair.getRight().isPresent()) .map(pair -> Pair.of(pair.getLeft(), pair.getRight().get())) .findFirst() .orElseThrow(() -> new MissingKeyException(key)); reprocessor.reprocess(mailPair.getKey(), mailPair.getValue()); } } @Inject  ReprocessingService(MailQueueFactory<?> mailQueueFactory,
                               MailRepositoryStoreService mailRepositoryStoreService); void reprocessAll(MailRepositoryPath path, Optional<String> targetProcessor, MailQueueName targetQueue, Consumer<MailKey> keyListener); void reprocess(MailRepositoryPath path, MailKey key, Optional<String> targetProcessor, MailQueueName targetQueue);  }
@Test public void reprocessingOneShouldEnqueueMail() throws Exception { MailRepository repository = mailRepositoryStore.select(MailRepositoryUrl.fromPathAndProtocol(PATH, MEMORY_PROTOCOL)); repository.store(mail1); repository.store(mail2); repository.store(mail3); reprocessingService.reprocess(PATH, KEY_2, NO_TARGET_PROCESSOR, SPOOL); assertThat(queueFactory.getQueue(SPOOL).get().browse()) .toIterable() .extracting(item -> item.getMail().getName()) .containsOnly(NAME_2); }
@Test public void reprocessingOneShouldRemoveMailFromRepository() throws Exception { MailRepository repository = mailRepositoryStore.select(MailRepositoryUrl.fromPathAndProtocol(PATH, MEMORY_PROTOCOL)); repository.store(mail1); repository.store(mail2); repository.store(mail3); reprocessingService.reprocess(PATH, KEY_2, NO_TARGET_PROCESSOR, SPOOL); assertThat(repository.list()).toIterable() .containsOnly(KEY_1, KEY_3); }