package com.atlassian.bitbucket.internal.mirroring.mirror;

import static java.util.Optional.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class MirroringTestHelper {
    public static final String UPSTREAM_BASE_URL = "http://bitbucket.server/bitbucket";

    public static ExternalProject mockExternalProject(String id, String key) {
        return mockExternalProject(id, key, key + "-name");
    }

    public static ExternalProject mockExternalProject(String id, String key, String name) {
        ExternalProject project = mock(ExternalProject.class);
        when(project.getId()).thenReturn(id);
        when(project.getKey()).thenReturn(key);
        when(project.getName()).thenReturn(name);
        return project;
    }

    public static ExternalRepository mockExternalRepository(String id, String name, ExternalProject project, String contentHash) {
        return mockExternalRepository(id, name, project, contentHash, null);
    }

    public static ExternalRepository mockExternalRepository(String id, String name, ExternalProject project,
                                                            String contentHash, ExternalRepository origin) {
        ExternalRepository repository = mock(ExternalRepository.class);
        String projectKey = project.getKey();
        when(repository.getId()).thenReturn(id);
        when(repository.getName()).thenReturn(name);
        when(repository.getSlug()).thenReturn(name);
        when(repository.getScmId()).thenReturn("git");
        when(repository.getOrigin()).thenReturn(ofNullable(origin));
        when(repository.getProject()).thenReturn(project);
        when(repository.getCloneUrl()).thenReturn(
                of(cloneUrlFor(projectKey, name)));
        when(repository.getContentHash()).thenReturn(of(contentHash));
        when(repository.getOwner()).thenReturn(empty());

        return repository;
    }

    public static String cloneUrlFor(String projectKey, String repoSlug) {
        return UPSTREAM_BASE_URL + "/scm/" + projectKey + "/" + repoSlug + ".git";
    }
}