package com.atlassian.stash.internal.scm.git.protocol.http;

import com.atlassian.stash.internal.scm.git.protocol.RepositoryPath;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;

import static com.atlassian.stash.internal.scm.git.protocol.http.PathInfoTestUtils.assertPathInfo;
import static java.util.Optional.of;

@RunWith(Parameterized.class)
public class PathInfoMatchingTest {

    private final String pathSuffix;
    private final String postRepositoryAddressPath;
    private final boolean isReceivePack;
    private final boolean isUploadPack;
    private final boolean isDumb;

    @Parameterized.Parameters(name = "{0}")
    public static Collection<Object[]> data() throws Exception {
        return Arrays.asList(
                new Object[][] {
                        {"/git-receive-pack", "/git-receive-pack", true, false, false},
                        {"/git-upload-pack", "/git-upload-pack", false, true, false},
                        {"/HEAD", "/HEAD", false, false, true},
                        {"/info/refs", "/info/refs", false, false, true},
                        {"/objects/ab/ababababababababababababababababababab", "/objects/ab/ababababababababababababababababababab", false, false, true},
                        {"/objects/info/alternates", "/objects/info/alternates", false, false, true},
                        {"/objects/info/http-alternates", "/objects/info/http-alternates", false, false, true},
                        {"/objects/info/packs", "/objects/info/packs", false, false, true},
                        {"/objects/pack/pack-abababababababababababababababababababab.idx", "/objects/pack/pack-abababababababababababababababababababab.idx", false, false, true},
                        {"/objects/pack/pack-abababababababababababababababababababab.pack", "/objects/pack/pack-abababababababababababababababababababab.pack", false, false, true},
                        {"", "", false, false, false},
                        {"/", "", false, false, false},
                });
    }

    public PathInfoMatchingTest(String pathSuffix, String postRepositoryAddressPath, boolean isReceivePack, boolean isUploadPack, boolean isDumb) {
        this.pathSuffix = pathSuffix;
        this.postRepositoryAddressPath = postRepositoryAddressPath;
        this.isReceivePack = isReceivePack;
        this.isUploadPack = isUploadPack;
        this.isDumb = isDumb;
    }

    @Test
    public void testUnqualifiedRepository() throws Exception {
        assertPathInfo("/foo/bar" + pathSuffix,
                of(new RepositoryPath(null, "foo", "bar")), postRepositoryAddressPath, isReceivePack, isUploadPack, isDumb);
    }

    @Test
    public void testQualifiedRepository() throws Exception {
        assertPathInfo("/qualification/foo/bar" + postRepositoryAddressPath,
                of(new RepositoryPath("qualification", "foo", "bar")), postRepositoryAddressPath, isReceivePack, isUploadPack, isDumb);
    }
}