package com.atlassian.bitbucket.project;

import com.atlassian.bitbucket.permission.Permission;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class ProjectSearchRequestTest {

    @Test(expected = IllegalArgumentException.class)
    public void testWithGlobalPermission() throws Exception {
        new ProjectSearchRequest.Builder().name("abc").permission(Permission.ADMIN).build();
    }

    @Test(expected = IllegalArgumentException.class)
    public void testWithRepositoryPermission() throws Exception {
        new ProjectSearchRequest.Builder().name("abc").permission(Permission.REPO_ADMIN).build();
    }

    @Test
    public void testTrimName() throws Exception {
        ProjectSearchRequest request = new ProjectSearchRequest.Builder()
                .name(" abc ").permission(Permission.PROJECT_READ).build();
        assertCriteria(request, "abc", true, Permission.PROJECT_READ, true);
    }

    @Test
    public void testTrimNameToNull() throws Exception {
        ProjectSearchRequest request = new ProjectSearchRequest.Builder()
                .name("  ").permission(Permission.PROJECT_WRITE).build();
        assertCriteria(request, null, false, Permission.PROJECT_WRITE, true);
    }

    private static void assertCriteria(ProjectSearchRequest request, String name, boolean hasName,
                                       Permission permission, boolean hasPermission) {
        assertEquals(name, request.getName());
        assertEquals(hasName, request.hasName());
        assertEquals(permission, request.getPermission());
        assertEquals(hasPermission, request.hasPermission());
    }

}
