package com.atlassian.bitbucket.web.conditions;

import com.atlassian.plugin.PluginParseException;
import com.atlassian.bitbucket.permission.Permission;
import com.atlassian.bitbucket.permission.PermissionService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.Collections;

import static com.atlassian.bitbucket.web.conditions.HasGlobalPermissionCondition.PERMISSION;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.class)
public class HasGlobalPermissionConditionTest {

    @Mock
    private PermissionService permissionService;
    @InjectMocks
    private HasGlobalPermissionCondition condition;

    @Test(expected = PluginParseException.class)
    public void testInitWithEmptyContext() throws Exception {
        condition.init(Collections.<String, String>emptyMap());
    }

    @Test(expected = PluginParseException.class)
    public void testInitWithProjectPermission() throws Exception {
        condition.init(Collections.singletonMap(PERMISSION, Permission.PROJECT_ADMIN.name()));
    }

    @Test(expected = PluginParseException.class)
    public void testInitWithFakePermission() throws Exception {
        condition.init(Collections.singletonMap(PERMISSION, "FAKE_PERM"));
    }

    @Test
    public void testInitWithGlobalPermission() throws Exception {
        condition.init(Collections.singletonMap(PERMISSION, Permission.SYS_ADMIN.name()));
    }

    @Test
    public void testShouldDisplay() throws Exception {
        condition.init(Collections.singletonMap(PERMISSION, Permission.SYS_ADMIN.name()));
        condition.shouldDisplay(Collections.<String, Object>emptyMap());
        verify(permissionService, times(1)).hasGlobalPermission(Permission.SYS_ADMIN);
    }
}
