package com.atlassian.bitbucket.internal.branch.model.version;

import com.atlassian.bitbucket.repository.Branch;
import com.atlassian.bitbucket.repository.SimpleBranch;
import org.junit.Test;

import java.util.Optional;
import java.util.function.Function;

import static com.atlassian.bitbucket.internal.branch.model.DefaultBranchModelService.DEFAULT_VERSION_COMPONENT_SEPARATOR;
import static org.junit.Assert.*;

public class VersionedBranchTest {

    private static final Function<Branch, Optional<VersionedBranch>> TO_VERSIONED_BRANCH =
            VersionedBranch.fromBranch("release/", DEFAULT_VERSION_COMPONENT_SEPARATOR);

    @Test
    public void testFromBranchWithVersion() {
        Branch branch = new SimpleBranch.Builder()
                .id("refs/heads/release/branch-3.5")
                .displayId("release/branch-3.5")
                .build();

        Optional<VersionedBranch> versioned = TO_VERSIONED_BRANCH.apply(branch);

        assertTrue(versioned.isPresent());
        assertSame(branch, versioned.get().getBranch());
        assertSame(0, versioned.get().getVersion().compareTo(BranchVersion.parse("branch-3.5", DEFAULT_VERSION_COMPONENT_SEPARATOR)));
    }

    @Test
    public void testFromBranchWithoutVersion() {
        Optional<VersionedBranch> branch = TO_VERSIONED_BRANCH.apply(new SimpleBranch.Builder()
                .id("refs/heads/release/branch.one")
                .displayId("release/branch.one")
                .build());

        assertFalse(branch.isPresent());
    }

}