package com.atlassian.bitbucket.internal.mirroring.mirror.rest.cloud;

import com.atlassian.bitbucket.internal.mirroring.mirror.rest.MirrorJsonEntity;
import com.google.common.collect.Iterables;
import org.junit.Rule;
import org.junit.Test;

import static org.junit.Assert.*;

public class RestCloudPageTest {
    @Rule
    public MirrorJsonEntity fullPage = new MirrorJsonEntity("cloud/json/page-of-repositories.json");
    @Rule
    public MirrorJsonEntity emptyPage = new MirrorJsonEntity("cloud/json/empty-page.json");

    @Test
    public void testFullPage() throws Exception {
        RestCloudPage<RestCloudExternalRepository> page =
                new RestCloudPage<>(fullPage.getValue(), RestCloudExternalRepository::new);

        assertEquals(5, Iterables.size(page.getValues()));
        assertEquals("https://api.bitbucket.org/2.0/repositories/atlassian?pagelen=5&page=26", page.getNextUrl());

        RestCloudExternalRepository first = Iterables.get(page.getValues(), 0);
        assertNotNull(first);
        assertEquals("functest-plugin", first.getName());

        RestCloudExternalRepository last = Iterables.get(page.getValues(), 4);
        assertNotNull(last);
        assertEquals("git-client-debug", last.getName());
    }

    @Test
    public void testEmptyPage() throws Exception {
        RestCloudPage<RestCloudExternalRepository> page =
                new RestCloudPage<>(emptyPage.getValue(), RestCloudExternalRepository::new);

        assertEquals(0, Iterables.size(page.getValues()));
        assertNull(page.getNextUrl());
    }
}