package org.gitlab4j.api;

import java.util.Collections;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class PagerSpliteratorTest {

	PagerSpliterator<Integer> pagerSpliterator;

	@Mock
	Pager pager;

	@Before
	public void setUp() {
		pagerSpliterator = new PagerSpliterator<>(pager);
	}

	@Test
	public void shouldAcceptElementFromSource() {
		when(pager.hasNext()).thenReturn(true);
		when(pager.next()).thenReturn(Collections.singletonList(1));

		boolean success = pagerSpliterator.tryAdvance(System.out::println);

		assertTrue(success);
	}

	@Test
	public void shouldDoNotFailOnEmptySource() {
		boolean success = pagerSpliterator.tryAdvance(System.out::println);

		assertFalse(success);
	}

	@Test
	public void shouldThrowNullPointerExceptionWhenActionIsMissing() {
		try {
			pagerSpliterator.tryAdvance(null);
			Assert.fail("Missing NullPointerException");
		} catch (Throwable e) {
			assertEquals(NullPointerException.class, e.getClass());
		}
	}
}
@@ -49,16 +49,55 @@
 ections;
+%0Aimport java.util.stream.StreamSupport;
 %0A%0Aimport
@@ -267,16 +267,105 @@
 unner;%0A%0A
+import static java.util.Arrays.asList;%0Aimport static org.junit.Assert.assertArrayEquals;%0A
 import s
@@ -1427,10 +1427,581 @@
 ;%0A%09%09%7D%0A%09%7D
+%0A%0A%09@Test%0A%09public void shouldAllowToGatherDataPageByPage() %7B%0A%09%09when(pager.hasNext()).thenReturn(true);%0A%09%09when(pager.getTotalItems()).thenReturn(12);%0A%09%09when(pager.next())%0A%09%09%09%09.thenReturn(asList(1, 2, 3))%0A%09%09%09%09.thenReturn(asList(4, 5, 6))%0A%09%09%09%09.thenReturn(asList(7, 8, 9))%0A%09%09%09%09.thenReturn(asList(0, 1, 2));%0A%0A%09%09Integer%5B%5D elements = StreamSupport.stream(new PagerSpliterator%3CInteger%3E(pager), false)%0A%09%09%09%09.parallel() // should be ignored%0A%09%09%09%09.skip(2) // should be ignored%0A%09%09%09%09.limit(5)%0A%09%09%09%09.toArray(Integer%5B%5D::new);%0A%0A%09%09assertArrayEquals(new Integer%5B%5D%7B1, 2, 3, 4, 5%7D, elements);%0A%09%7D
 %0A%7D

Verify that PagerSplitter can collect data page by page (#299)