package com.atlassian.stash.internal.notification.util;

import com.atlassian.bitbucket.comment.DiffCommentAnchor;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class AnchorUtilsTest {

    @Test
    public void testFileInDirectory() throws Exception {
        String filePath = "src/main/java/org/awesome/test/";
        String fileName = "pom.xml";
        testExtractPathAndName(filePath, fileName);
    }

    @Test
    public void testTopLevelFile() throws Exception {
        String filePath = "";
        String fileName = "pom.xml";
        testExtractPathAndName(filePath, fileName);
    }

    @Test
    public void testSingleCharacterFileInDirectory() throws Exception {
        String filePath = "src/main/java/org/awesome/test/";
        String fileName = "a";
        testExtractPathAndName(filePath, fileName);
    }

    private void testExtractPathAndName(String filePath, String fileName) throws Exception {
        DiffCommentAnchor commentAnchor = Mockito.mock(DiffCommentAnchor.class);

        Mockito.when(commentAnchor.getPath()).thenReturn(filePath + fileName);

        Assert.assertEquals("Parsing of file path failed", filePath, AnchorUtils.getFilePath(commentAnchor));
        Assert.assertEquals("Parsing of file name failed", fileName, AnchorUtils.getFileName(commentAnchor));
    }

}
