private List<MailQueueItemDTO> listMails(ManageableMailQueue queue, Optional<Boolean> isDelayed, Limit limit) { try { return limit.applyOnStream(Iterators.toStream(queue.browse())) .map(Throwing.function(MailQueueItemDTO::from).sneakyThrow()) .filter(item -> filter(item, isDelayed)) .collect(Guavate.toImmutableList()); } catch (MailQueueException e) { throw ErrorResponder.builder() .statusCode(HttpStatus.BAD_REQUEST_400) .type(ErrorType.INVALID_ARGUMENT) .message("Invalid request for listing the mails from the mail queue " + queue) .cause(e) .haltError(); } }
@Test public void reprocessingShouldNotFailOnConcurrentDeletion() throws Exception { MailRepository repository = mailRepositoryStore.select(MailRepositoryUrl.fromPathAndProtocol(PATH, MEMORY_PROTOCOL)); repository.store(mail1); repository.store(mail2); repository.store(mail3); AtomicBoolean shouldPerformRemove = new AtomicBoolean(true); ConsumerChainer<MailKey> concurrentRemoveConsumer = Throwing.consumer(key -> { if (shouldPerformRemove.get()) { shouldPerformRemove.set(false); MailKey toRemove = ImmutableList.of(NAME_1, NAME_2, NAME_3) .stream() .map(MailKey::new) .filter(candidateForRemoval -> !candidateForRemoval.equals(key)) .findFirst() .get(); repository.remove(toRemove); } }); reprocessingService.reprocessAll(PATH, NO_TARGET_PROCESSOR, SPOOL, concurrentRemoveConsumer); assertThat(queueFactory.getQueue(SPOOL).get() .browse()) .hasSize(2); }
@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()) .extracting(item -> item.getMail().getName()) .containsOnly(NAME_2); }
@Test public void reprocessingShouldEnqueueAllMails() throws Exception { MailRepository repository = mailRepositoryStore.select(MailRepositoryUrl.fromPathAndProtocol(PATH, MEMORY_PROTOCOL)); repository.store(mail1); repository.store(mail2); repository.store(mail3); reprocessingService.reprocessAll(PATH, NO_TARGET_PROCESSOR, SPOOL, NOOP_CONSUMER); assertThat(queueFactory.getQueue(SPOOL).get() .browse()) .extracting(item -> item.getMail().getName()) .containsOnly(NAME_1, NAME_2, NAME_3); }
@Test public void remoteDeliveryShouldNotForwardMailsWithNoRecipients() throws Exception { remoteDelivery.init(FakeMailetConfig.builder() .setProperty(RemoteDeliveryConfiguration.DELIVERY_THREADS, "1") .build()); Mail mail = FakeMail.builder().name(MAIL_NAME).build(); remoteDelivery.service(mail); assertThat(mailQueue.browse()).isEmpty(); }
@Test public void remoteDeliveryShouldNotForwardMailsWithNoRecipientsWithGateway() throws Exception { remoteDelivery.init(FakeMailetConfig.builder() .setProperty(RemoteDeliveryConfiguration.DELIVERY_THREADS, "1") .setProperty(RemoteDeliveryConfiguration.GATEWAY, MailAddressFixture.JAMES_LOCAL) .build()); Mail mail = FakeMail.builder().name(MAIL_NAME).build(); remoteDelivery.service(mail); assertThat(mailQueue.browse()).isEmpty(); } }
@Test public void remoteDeliveryShouldAddEmailToSpool() throws Exception { remoteDelivery.init(FakeMailetConfig.builder() .setProperty(RemoteDeliveryConfiguration.DELIVERY_THREADS, "1") .build()); Mail mail = FakeMail.builder().name(MAIL_NAME).recipients(MailAddressFixture.ANY_AT_JAMES).build(); remoteDelivery.service(mail); assertThat(mailQueue.browse()) .extracting(MailProjection::from) .containsOnly(MailProjection.from( FakeMail.builder() .name(MAIL_NAME + RemoteDelivery.NAME_JUNCTION + MailAddressFixture.JAMES_APACHE_ORG) .recipient(MailAddressFixture.ANY_AT_JAMES) .build())); }
@Test public void remoteDeliveryShouldAddPriorityIfSpecified() throws Exception { remoteDelivery.init(FakeMailetConfig.builder() .setProperty(RemoteDeliveryConfiguration.DELIVERY_THREADS, "1") .setProperty(RemoteDeliveryConfiguration.USE_PRIORITY, "true") .build()); Mail mail = FakeMail.builder().name(MAIL_NAME).recipients(MailAddressFixture.ANY_AT_JAMES).build(); remoteDelivery.service(mail); assertThat(mailQueue.browse()) .extracting(MailProjection::from) .containsOnly(MailProjection.from(FakeMail.builder() .name(MAIL_NAME + RemoteDelivery.NAME_JUNCTION + MailAddressFixture.JAMES_APACHE_ORG) .attribute(MailPrioritySupport.MAIL_PRIORITY, MailPrioritySupport.HIGH_PRIORITY) .recipient(MailAddressFixture.ANY_AT_JAMES) .build())); }
@Test public void remoteDeliveryShouldNotSplitMailsByServerWhenGateway() throws Exception { remoteDelivery.init(FakeMailetConfig.builder() .setProperty(RemoteDeliveryConfiguration.DELIVERY_THREADS, "1") .setProperty(RemoteDeliveryConfiguration.GATEWAY, MailAddressFixture.JAMES_LOCAL) .build()); Mail mail = FakeMail.builder() .name(MAIL_NAME) .recipients(MailAddressFixture.ANY_AT_JAMES, MailAddressFixture.ANY_AT_JAMES2, MailAddressFixture.OTHER_AT_JAMES) .build(); remoteDelivery.service(mail); assertThat(mailQueue.browse()) .extracting(MailProjection::from) .containsOnly( MailProjection.from(FakeMail.builder() .name(MAIL_NAME) .recipients(MailAddressFixture.ANY_AT_JAMES, MailAddressFixture.ANY_AT_JAMES2, MailAddressFixture.OTHER_AT_JAMES) .build())); }
@Test public void reprocessingOneTaskShouldNotEnqueueUnknownMailKey() throws Exception { MailRepository mailRepository = mailRepositoryStore.create(URL_MY_REPO); mailRepository.store(FakeMail.builder() .name(NAME_1) .build()); mailRepository.store(FakeMail.builder() .name(NAME_2) .build()); String taskId = with() .param("action", "reprocess") .param("queue", CUSTOM_QUEUE) .patch(PATH_ESCAPED_MY_REPO + "/mails/" + "unknown") .jsonPath() .get("taskId"); with() .basePath(TasksRoutes.BASE) .get(taskId + "/await"); assertThat(customQueue.browse()) .isEmpty(); }
@Test public void reprocessingAllTaskShouldEnqueueMailsOnSpecifiedQueue() throws Exception { MailRepository mailRepository = mailRepositoryStore.create(URL_MY_REPO); mailRepository.store(FakeMail.builder() .name(NAME_1) .build()); mailRepository.store(FakeMail.builder() .name(NAME_2) .build()); String taskId = with() .param("action", "reprocess") .param("queue", CUSTOM_QUEUE) .patch(PATH_ESCAPED_MY_REPO + "/mails") .jsonPath() .get("taskId"); with() .basePath(TasksRoutes.BASE) .get(taskId + "/await"); assertThat(customQueue.browse()) .extracting(ManageableMailQueue.MailQueueItemView::getMail) .extracting(Mail::getName) .containsOnly(NAME_1, NAME_2); }
@Test public void reprocessingAllTaskShouldEnqueueMailsOnDefaultQueue() throws Exception { MailRepository mailRepository = mailRepositoryStore.create(URL_MY_REPO); mailRepository.store(FakeMail.builder() .name(NAME_1) .build()); mailRepository.store(FakeMail.builder() .name(NAME_2) .build()); String taskId = with() .param("action", "reprocess") .patch(PATH_ESCAPED_MY_REPO + "/mails") .jsonPath() .get("taskId"); with() .basePath(TasksRoutes.BASE) .get(taskId + "/await"); assertThat(spoolQueue.browse()) .extracting(ManageableMailQueue.MailQueueItemView::getMail) .extracting(Mail::getName) .containsOnly(NAME_1, NAME_2); }
@Test public void reprocessingAllTaskShouldEnqueueMailsOfBothRepositoriesOnDefaultQueueWhenSamePath() throws Exception { MailRepository mailRepository1 = mailRepositoryStore.create(URL_MY_REPO); MailRepository mailRepository2 = mailRepositoryStore.create(URL_MY_REPO_OTHER); mailRepository1.store(FakeMail.builder() .name(NAME_1) .build()); mailRepository2.store(FakeMail.builder() .name(NAME_2) .build()); String taskId = with() .param("action", "reprocess") .patch(PATH_ESCAPED_MY_REPO + "/mails") .jsonPath() .get("taskId"); with() .basePath(TasksRoutes.BASE) .get(taskId + "/await"); assertThat(spoolQueue.browse()) .extracting(ManageableMailQueue.MailQueueItemView::getMail) .extracting(Mail::getName) .containsOnly(NAME_1, NAME_2); }
@Test public void remoteDeliveryShouldSplitMailsByServerWhenNoGateway() throws Exception { remoteDelivery.init(FakeMailetConfig.builder() .setProperty(RemoteDeliveryConfiguration.DELIVERY_THREADS, "1") .build()); Mail mail = FakeMail.builder() .name(MAIL_NAME) .recipients(MailAddressFixture.ANY_AT_JAMES, MailAddressFixture.ANY_AT_JAMES2, MailAddressFixture.OTHER_AT_JAMES) .build(); remoteDelivery.service(mail); assertThat(mailQueue.browse()) .extracting(MailProjection::from) .containsOnly( MailProjection.from(FakeMail.builder() .name(MAIL_NAME + RemoteDelivery.NAME_JUNCTION + MailAddressFixture.JAMES_APACHE_ORG) .recipients(MailAddressFixture.ANY_AT_JAMES, MailAddressFixture.OTHER_AT_JAMES) .build()), MailProjection.from(FakeMail.builder() .name(MAIL_NAME + RemoteDelivery.NAME_JUNCTION + MailAddressFixture.JAMES2_APACHE_ORG) .recipients(MailAddressFixture.ANY_AT_JAMES2) .build())); }
@Test public void reprocessingOneTaskShouldEnqueueMailsOnDefaultQueue() throws Exception { MailRepository mailRepository = mailRepositoryStore.create(URL_MY_REPO); mailRepository.store(FakeMail.builder() .name(NAME_1) .build()); mailRepository.store(FakeMail.builder() .name(NAME_2) .build()); String taskId = with() .param("action", "reprocess") .patch(PATH_ESCAPED_MY_REPO + "/mails/" + NAME_1) .jsonPath() .get("taskId"); with() .basePath(TasksRoutes.BASE) .get(taskId + "/await"); assertThat(spoolQueue.browse()) .extracting(ManageableMailQueue.MailQueueItemView::getMail) .extracting(Mail::getName) .containsOnly(NAME_1); }
@Test public void reprocessingOneTaskShouldEnqueueMailsOnSpecifiedQueue() throws Exception { MailRepository mailRepository = mailRepositoryStore.create(URL_MY_REPO); mailRepository.store(FakeMail.builder() .name(NAME_1) .build()); mailRepository.store(FakeMail.builder() .name(NAME_2) .build()); String taskId = with() .param("action", "reprocess") .param("queue", CUSTOM_QUEUE) .patch(PATH_ESCAPED_MY_REPO + "/mails/" + NAME_1) .jsonPath() .get("taskId"); with() .basePath(TasksRoutes.BASE) .get(taskId + "/await"); assertThat(customQueue.browse()) .extracting(ManageableMailQueue.MailQueueItemView::getMail) .extracting(Mail::getName) .containsOnly(NAME_1); }
@Test public void reprocessingAllTaskShouldPreserveStateWhenProcessorIsNotSpecified() throws Exception { MailRepository mailRepository = mailRepositoryStore.create(URL_MY_REPO); String state1 = "state1"; String state2 = "state2"; mailRepository.store(FakeMail.builder() .name(NAME_1) .state(state1) .build()); mailRepository.store(FakeMail.builder() .name(NAME_2) .state(state2) .build()); String taskId = with() .param("action", "reprocess") .patch(PATH_ESCAPED_MY_REPO + "/mails") .jsonPath() .get("taskId"); with() .basePath(TasksRoutes.BASE) .get(taskId + "/await"); assertThat(spoolQueue.browse()) .extracting(ManageableMailQueue.MailQueueItemView::getMail) .extracting(Mail::getState) .containsOnly(state1, state2); }
@Test public void reprocessingOneTaskShouldPreserveStateWhenProcessorIsNotSpecified() throws Exception { MailRepository mailRepository = mailRepositoryStore.create(URL_MY_REPO); String state1 = "state1"; String state2 = "state2"; mailRepository.store(FakeMail.builder() .name(NAME_1) .state(state1) .build()); mailRepository.store(FakeMail.builder() .name(NAME_2) .state(state2) .build()); String taskId = with() .param("action", "reprocess") .patch(PATH_ESCAPED_MY_REPO + "/mails/" + NAME_1) .jsonPath() .get("taskId"); with() .basePath(TasksRoutes.BASE) .get(taskId + "/await"); assertThat(spoolQueue.browse()) .extracting(ManageableMailQueue.MailQueueItemView::getMail) .extracting(Mail::getState) .containsOnly(state1); }
@Test public void reprocessingAllTaskShouldOverWriteStateWhenProcessorSpecified() throws Exception { MailRepository mailRepository = mailRepositoryStore.create(URL_MY_REPO); String state1 = "state1"; String state2 = "state2"; mailRepository.store(FakeMail.builder() .name(NAME_1) .state(state1) .build()); mailRepository.store(FakeMail.builder() .name(NAME_2) .state(state2) .build()); String transport = "transport"; String taskId = with() .param("action", "reprocess") .param("processor", transport) .patch(PATH_ESCAPED_MY_REPO + "/mails") .jsonPath() .get("taskId"); with() .basePath(TasksRoutes.BASE) .get(taskId + "/await"); assertThat(spoolQueue.browse()) .extracting(ManageableMailQueue.MailQueueItemView::getMail) .extracting(Mail::getState) .containsOnly(transport, transport); }
@Test public void reprocessingOneTaskShouldOverWriteStateWhenProcessorSpecified() throws Exception { MailRepository mailRepository = mailRepositoryStore.create(URL_MY_REPO); String state1 = "state1"; String state2 = "state2"; mailRepository.store(FakeMail.builder() .name(NAME_1) .state(state1) .build()); mailRepository.store(FakeMail.builder() .name(NAME_2) .state(state2) .build()); String transport = "transport"; String taskId = with() .param("action", "reprocess") .param("processor", transport) .patch(PATH_ESCAPED_MY_REPO + "/mails/" + NAME_1) .jsonPath() .get("taskId"); with() .basePath(TasksRoutes.BASE) .get(taskId + "/await"); assertThat(spoolQueue.browse()) .extracting(ManageableMailQueue.MailQueueItemView::getMail) .extracting(Mail::getState) .containsOnly(transport); }