package it.com.atlassian.stash.admin;

import com.atlassian.webdriver.bitbucket.element.UserGroupsTable;
import com.atlassian.webdriver.bitbucket.page.admin.UserEditPage;
import com.atlassian.webdriver.bitbucket.page.admin.UserListPage;
import it.com.atlassian.stash.BaseBrowserFuncTest;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.List;

import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

/**
 * @see GlobalPermissionsPagingTest
 */
public class UserGroupsPagingTest extends BaseBrowserFuncTest {

    private static final int PAGE_SIZE = 50;
    private static final int NUM_ITEMS = PAGE_SIZE * 3 / 2;
    private static final String GROUP_PATTERN = "group-for-paging-test-%03d";
    private static final String USER = "user-for-paging-test";

    @BeforeClass
    public static void setUp() {
        getClassTestContext().user(USER, false);
        for (int i = 0; i < NUM_ITEMS; i++) {
            getClassTestContext().group(groupName(i), USER);
        }
    }

    @Test
    public void testPaging() throws Exception {
        UserEditPage page = loginAsAdmin(UserListPage.class).setFilter(USER).getUser(USER).clickView();
        UserGroupsTable groups = page.getGroups();
        groups.loadNextPage();
        List<UserGroupsTable.Row> rows = groups.list();
        assertThat(rows, hasSize(NUM_ITEMS));
        for (int i = 0; i < NUM_ITEMS; i++) {
            assertEquals(groupName(i), rows.get(i).getName());
        }
    }

    private static String groupName(int i) {
        return String.format(GROUP_PATTERN, i);
    }

}
