package com.atlassian.bitbucket.web.conditions;

import com.atlassian.plugin.PluginParseException;
import com.atlassian.bitbucket.project.Project;
import com.atlassian.bitbucket.permission.Permission;
import com.atlassian.bitbucket.permission.PermissionService;
import org.junit.Before;
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 java.util.Date;

import static com.atlassian.bitbucket.web.conditions.HasProjectPermissionCondition.PERMISSION;
import static com.atlassian.bitbucket.web.conditions.HasProjectPermissionCondition.PROJECT;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class HasProjectPermissionConditionTest {

    @Mock
    private Project accessibleProject;
    @InjectMocks
    private HasProjectPermissionCondition condition;
    @Mock
    private Project inaccessibleProject;
    @Mock
    private PermissionService permissionService;

    @Before
    public void setup() throws Exception {
        when(permissionService.hasProjectPermission(accessibleProject, Permission.PROJECT_ADMIN)).thenReturn(true);
        when(permissionService.hasProjectPermission(inaccessibleProject, Permission.PROJECT_ADMIN)).thenReturn(false);
    }

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

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

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

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

    @Test
    public void testShouldDisplay() throws Exception {
        condition.init(Collections.singletonMap(PERMISSION, Permission.PROJECT_ADMIN.name()));
        assertFalse("Should return false when no project present", condition.shouldDisplay(Collections.<String, Object>emptyMap()));
        assertFalse("Should return false when given rubbish",
                condition.shouldDisplay(Collections.<String, Object>singletonMap(PROJECT, new Date())));
        assertTrue("Should return true when given accessible project",
                condition.shouldDisplay(Collections.<String, Object>singletonMap(PROJECT, accessibleProject)));
        assertFalse("Should return false when given inaccessible project",
                condition.shouldDisplay(Collections.<String, Object>singletonMap(PROJECT, inaccessibleProject)));
    }
}
