Search in sources :

Example 1 with ReturnsElementsOf

use of org.mockito.internal.stubbing.answers.ReturnsElementsOf in project nem2-sdk-java by nemtech.

the class PaginationStreamerTester method runSearch.

private void runSearch(int pageSize, int totalEntries, Integer limit) {
    try {
        criteria.setPageSize(pageSize);
        List<E> infos = IntStream.range(0, totalEntries).mapToObj((i) -> Mockito.mock(entityClass)).collect(Collectors.toList());
        Assertions.assertEquals(totalEntries, infos.size());
        List<Observable<Page<E>>> pages = toPages(infos, criteria.getPageSize());
        Mockito.when(repository.search(Mockito.eq(criteria))).thenAnswer(new ReturnsElementsOf(pages));
        Observable<E> search = streamer.search(criteria);
        if (limit != null) {
            search = search.take(limit);
        }
        List<E> returnedInfos = search.toList().toFuture().get();
        Assertions.assertEquals(infos.subList(0, limit == null ? infos.size() : limit), returnedInfos);
        int totalPagesRead = limit == null ? pages.size() : (int) Math.ceil(limit.doubleValue() / pageSize);
        Mockito.verify(repository, Mockito.times(totalPagesRead)).search(Mockito.eq(criteria));
    } catch (InterruptedException | ExecutionException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) IntStream(java.util.stream.IntStream) Mockito(org.mockito.Mockito) List(java.util.List) ReturnsElementsOf(org.mockito.internal.stubbing.answers.ReturnsElementsOf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions(org.junit.jupiter.api.Assertions) Observable(io.reactivex.Observable) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) Observable(io.reactivex.Observable) ReturnsElementsOf(org.mockito.internal.stubbing.answers.ReturnsElementsOf)

Aggregations

Observable (io.reactivex.Observable)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Assertions (org.junit.jupiter.api.Assertions)1 Mockito (org.mockito.Mockito)1 ReturnsElementsOf (org.mockito.internal.stubbing.answers.ReturnsElementsOf)1