package com.atlassian.bitbucket.web.conditions;

import com.atlassian.plugin.PluginParseException;
import com.atlassian.bitbucket.repository.Repository;
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.HasRepositoryPermissionCondition.PERMISSION;
import static com.atlassian.bitbucket.web.conditions.HasRepositoryPermissionCondition.REPOSITORY;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class HasRepositoryPermissionConditionTest {

    @Mock
    private Repository accessibleRepository;
    @InjectMocks
    private HasRepositoryPermissionCondition condition;
    @Mock
    private Repository inaccessibleRepository;
    @Mock
    private PermissionService permissionService;

    @Before
    public void setup() throws Exception {
        when(permissionService.hasRepositoryPermission(accessibleRepository, Permission.REPO_READ)).thenReturn(true);
        when(permissionService.hasRepositoryPermission(inaccessibleRepository, Permission.REPO_READ)).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.REPO_READ.name()));
    }

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