@Test public void listMailsShouldApplyLimitAndOffset() throws Exception { when(mailRepositoryStore.getByPath(FIRST_REPOSITORY_PATH)).thenReturn(Stream.of(repository)); repository.store(FakeMail.builder() .name(NAME_1.asString()) .build()); repository.store(FakeMail.builder() .name(NAME_2.asString()) .build()); repository.store(FakeMail.builder() .name("name3") .build()); assertThat(testee.listMails(FIRST_REPOSITORY_PATH, Offset.from(1), Limit.from(1)).get()) .containsOnly(new MailKeyDTO(NAME_2)); }
public Optional<List<MailKeyDTO>> listMails(MailRepositoryPath path, Offset offset, Limit limit) throws MailRepositoryStore.MailRepositoryStoreException, MessagingException { Optional<Stream<MailKeyDTO>> maybeMails = Optional.of(getRepositories(path) .flatMap(Throwing.function((MailRepository repository) -> Iterators.toStream(repository.list())).sneakyThrow()) .map(MailKeyDTO::new) .skip(offset.getOffset())); return maybeMails.map(limit::applyOnStream) .map(stream -> stream.collect(ImmutableList.toImmutableList())); }
MailRepositoryStoreService { public Optional<List<MailKeyDTO>> listMails(MailRepositoryPath path, Offset offset, Limit limit) throws MailRepositoryStore.MailRepositoryStoreException, MessagingException { Optional<Stream<MailKeyDTO>> maybeMails = Optional.of(getRepositories(path) .flatMap(Throwing.function((MailRepository repository) -> Iterators.toStream(repository.list())).sneakyThrow()) .map(MailKeyDTO::new) .skip(offset.getOffset())); return maybeMails.map(limit::applyOnStream) .map(stream -> stream.collect(ImmutableList.toImmutableList())); } }
MailRepositoryStoreService { public Optional<List<MailKeyDTO>> listMails(MailRepositoryPath path, Offset offset, Limit limit) throws MailRepositoryStore.MailRepositoryStoreException, MessagingException { Optional<Stream<MailKeyDTO>> maybeMails = Optional.of(getRepositories(path) .flatMap(Throwing.function((MailRepository repository) -> Iterators.toStream(repository.list())).sneakyThrow()) .map(MailKeyDTO::new) .skip(offset.getOffset())); return maybeMails.map(limit::applyOnStream) .map(stream -> stream.collect(ImmutableList.toImmutableList())); } @Inject  MailRepositoryStoreService(MailRepositoryStore mailRepositoryStore); }
MailRepositoryStoreService { public Optional<List<MailKeyDTO>> listMails(MailRepositoryPath path, Offset offset, Limit limit) throws MailRepositoryStore.MailRepositoryStoreException, MessagingException { Optional<Stream<MailKeyDTO>> maybeMails = Optional.of(getRepositories(path) .flatMap(Throwing.function((MailRepository repository) -> Iterators.toStream(repository.list())).sneakyThrow()) .map(MailKeyDTO::new) .skip(offset.getOffset())); return maybeMails.map(limit::applyOnStream) .map(stream -> stream.collect(ImmutableList.toImmutableList())); } @Inject  MailRepositoryStoreService(MailRepositoryStore mailRepositoryStore); Stream<SingleMailRepositoryResponse> listMailRepositories(); MailRepository createMailRepository(MailRepositoryPath repositoryPath, String protocol); Optional<List<MailKeyDTO>> listMails(MailRepositoryPath path, Offset offset, Limit limit); Optional<Long> size(MailRepositoryPath path); Optional<MailDto> retrieveMail(MailRepositoryPath path, MailKey mailKey, Set<AdditionalField> additionalAttributes); Optional<MimeMessage> retrieveMessage(MailRepositoryPath path, MailKey mailKey); void deleteMail(MailRepositoryPath path, MailKey mailKey); Task createClearMailRepositoryTask(MailRepositoryPath path); Stream<MailRepository> getRepositories(MailRepositoryPath path); }
MailRepositoryStoreService { public Optional<List<MailKeyDTO>> listMails(MailRepositoryPath path, Offset offset, Limit limit) throws MailRepositoryStore.MailRepositoryStoreException, MessagingException { Optional<Stream<MailKeyDTO>> maybeMails = Optional.of(getRepositories(path) .flatMap(Throwing.function((MailRepository repository) -> Iterators.toStream(repository.list())).sneakyThrow()) .map(MailKeyDTO::new) .skip(offset.getOffset())); return maybeMails.map(limit::applyOnStream) .map(stream -> stream.collect(ImmutableList.toImmutableList())); } @Inject  MailRepositoryStoreService(MailRepositoryStore mailRepositoryStore); Stream<SingleMailRepositoryResponse> listMailRepositories(); MailRepository createMailRepository(MailRepositoryPath repositoryPath, String protocol); Optional<List<MailKeyDTO>> listMails(MailRepositoryPath path, Offset offset, Limit limit); Optional<Long> size(MailRepositoryPath path); Optional<MailDto> retrieveMail(MailRepositoryPath path, MailKey mailKey, Set<AdditionalField> additionalAttributes); Optional<MimeMessage> retrieveMessage(MailRepositoryPath path, MailKey mailKey); void deleteMail(MailRepositoryPath path, MailKey mailKey); Task createClearMailRepositoryTask(MailRepositoryPath path); Stream<MailRepository> getRepositories(MailRepositoryPath path);  }