package com.atlassian.stash.internal.header.navlinks;

import com.atlassian.plugins.navlink.spi.Project;
import com.atlassian.plugins.navlink.spi.ProjectNotFoundException;
import com.atlassian.bitbucket.project.ProjectService;
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.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class NavlinksProjectManagerTest {

    @Mock
    private ProjectService projectService;

    @InjectMocks
    private NavlinksProjectManager projectManager;

    @Test
    public void testGetProjectByKey() throws Exception {
        final com.atlassian.bitbucket.project.Project project = mockStashProject("key");
        when(projectService.getByKey("key")).thenReturn(project);

        Project result = projectManager.getProjectByKey("key");
        assertEquals("key", result.getKey());
    }

    @Test(expected = ProjectNotFoundException.class)
    public void testGetNonExistentProject() throws Exception {
        projectManager.getProjectByKey("key");
    }

    private com.atlassian.bitbucket.project.Project mockStashProject(String key) {
        com.atlassian.bitbucket.project.Project project = mock(com.atlassian.bitbucket.project.Project.class);
        when(project.getKey()).thenReturn(key);
        return project;
    }

}
