package com.atlassian.stash.internal.project;

import com.atlassian.bitbucket.AbstractServiceTest;
import com.atlassian.bitbucket.project.ProjectSupplier;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Matchers.isNull;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
@SuppressWarnings("ConstantConditions")
public class DefaultProjectSupplierTest extends AbstractServiceTest {

    @Mock
    private InternalProject project;
    @Mock
    private ProjectDao projectDao;
    @InjectMocks
    private DefaultProjectSupplier supplier;

    public DefaultProjectSupplierTest() {
        super(DefaultProjectSupplier.class, ProjectSupplier.class);
    }

    @Test
    public void testGetById() {
        when(projectDao.getById(eq(5))).thenReturn(project);

        assertSame(project, supplier.getById(5));
    }

    @Test
    public void testGetByKey() {
        //Note that the case is not changed in the service; the DAO handles the case-insensitive matching
        when(projectDao.getByKey(isNull(String.class), eq("key"))).thenReturn(project);

        assertSame(project, supplier.getByKey("key"));
    }

    @Test
    public void testGetByKeyWithNamespace() {
        when(projectDao.getByKey(eq("namespace"), eq("key"))).thenReturn(project);

        assertSame(project, supplier.getByKey("namespace", "key"));
    }
    
    @Test(expected = NullPointerException.class)
    public void testGetByKeyWithNullKey() {
        supplier.getByKey(null);
    }

    @Test
    public void testGetByName() {
        //Note that the case is not changed in the service; the DAO handles the case-insensitive matching
        when(projectDao.getByName(isNull(String.class), eq("Name"))).thenReturn(project);

        assertSame(project, supplier.getByName("Name"));
    }

    @Test(expected = NullPointerException.class)
    public void testGetByNameWithNullKey() {
        supplier.getByName(null);
    }
}