package com.atlassian.bitbucket.util;

import com.google.common.collect.ImmutableList;
import org.junit.Test;

import java.util.Collections;
import java.util.List;

import static org.junit.Assert.*;

public class ListPageProviderTest {

    private static final PageRequest REQ_0_2 = PageUtils.newRequest(0, 2);

    @Test
    public void testEmptyList() {
        ListPageProvider<String> provider = new ListPageProvider<String>(Collections.<String>emptyList());

        Page<String> page = provider.get(REQ_0_2);

        assertNotNull(page);
        assertTrue(page.getIsLastPage());
        assertEquals(0, page.getSize());
    }

    @Test
    public void testPaging() {
        ListPageProvider<String> provider = new ListPageProvider<String>(ImmutableList.of("1", "2", "3"));

        Page<String> page = provider.get(REQ_0_2);

        // verify page 1
        assertNotNull(page);
        assertFalse(page.getIsLastPage());
        assertEquals(2, page.getSize());

        List<String> values = ImmutableList.copyOf(page.getValues());
        assertEquals("1", values.get(0));
        assertEquals("2", values.get(1));

        // get page 2
        PageRequest secondPageRequest = page.getNextPageRequest();
        assertEquals(2, secondPageRequest.getStart());
        assertEquals(2, secondPageRequest.getLimit());

        page = provider.get(secondPageRequest);

        // verify page 2
        assertNotNull(page);
        assertTrue(page.getIsLastPage());
        assertEquals(1, page.getSize());

        values = ImmutableList.copyOf(page.getValues());
        assertEquals("3", values.get(0));
    }
}
