package com.atlassian.stash.internal.rest.inject;

import com.atlassian.bitbucket.rest.util.ResourcePatterns;
import com.atlassian.bitbucket.project.NoSuchProjectException;
import com.atlassian.bitbucket.repository.NoSuchRepositoryException;
import com.atlassian.bitbucket.i18n.I18nService;
import com.atlassian.bitbucket.i18n.KeyedMessage;
import com.atlassian.bitbucket.project.Project;
import com.atlassian.bitbucket.project.ProjectService;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.repository.RepositoryService;
import com.sun.jersey.api.core.ExtendedUriInfo;
import com.sun.jersey.api.core.HttpContext;
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 javax.ws.rs.core.PathSegment;
import java.util.Collections;

import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class RepositoryInjectableTest {

    private static final String PROJECT_KEY = "projectKey";
    private static final String REPO_SLUG = "repoSlug";

    @Mock
    private I18nService i18nService;
    @Mock
    private ProjectService projectService;
    @Mock
    private RepositoryService repositoryService;
    @InjectMocks
    private RepositoryInjectable repositoryInjectable;

    @Before
    public void setUp() throws Exception {
        when(i18nService.createKeyedMessage(anyString(), anyVararg())).thenReturn(new KeyedMessage("an", "err", "or"));
    }

    @Test
    public void testWhenRepoExists() throws Exception {
        HttpContext context = createContext();
        Repository repository = mock(Repository.class);
        when(repositoryService.getBySlug(PROJECT_KEY, REPO_SLUG)).thenReturn(repository);
        assertSame(repository, repositoryInjectable.doGetValue(context));
    }

    @Test(expected = NoSuchRepositoryException.class)
    public void testWhenRepoDoesNotExist() throws Exception {
        HttpContext context = createContext();
        when(repositoryService.getBySlug(PROJECT_KEY, REPO_SLUG)).thenReturn(null);
        when(projectService.getByKey(PROJECT_KEY)).thenReturn(mock(Project.class));
        assertNull(repositoryInjectable.doGetValue(context));
    }

    @Test(expected = NoSuchProjectException.class)
    public void testWhenRepoAndProjectDoesNotExist() throws Exception {
        HttpContext context = createContext();
        when(repositoryService.getBySlug(PROJECT_KEY, REPO_SLUG)).thenReturn(null);
        when(projectService.getByKey(PROJECT_KEY)).thenReturn(null);
        assertNull(repositoryInjectable.doGetValue(context));
    }

    private HttpContext createContext() {
        PathSegment projectSegment = mock(PathSegment.class);
        when(projectSegment.getPath()).thenReturn(PROJECT_KEY);
        PathSegment repoSegment = mock(PathSegment.class);
        when(repoSegment.getPath()).thenReturn(REPO_SLUG);

        ExtendedUriInfo uriInfo = mock(ExtendedUriInfo.class);
        when(uriInfo.getPathSegments(eq(ResourcePatterns.PROJECT_KEY))).thenReturn(Collections.singletonList(projectSegment));
        when(uriInfo.getPathSegments(eq(ResourcePatterns.REPOSITORY_SLUG))).thenReturn(Collections.singletonList(repoSegment));

        HttpContext context = mock(HttpContext.class);
        when(context.getUriInfo()).thenReturn(uriInfo);
        return context;
    }
}
