package com.atlassian.stash.internal.notification.commit.handlers;

import com.atlassian.bitbucket.commit.CommitDiscussionCommentAnchor;
import com.atlassian.bitbucket.notification.commit.CommitDiscussionCommentAddedNotification;
import com.google.common.collect.Maps;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.Map;

import static com.atlassian.stash.internal.notification.commit.handlers.AbstractCommitDiscussionNotificationHandler.summarize;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class CommitDiscussionCommentAddedNotificationHandlerTest {

    private static final String TEST_FILEPATH = "src/main/java/org/awesome/test/";
    private static final String TEST_FILENAME = "pom.xml";
    private static final int TEST_LINE = 42;

    @InjectMocks
    private CommitDiscussionCommentAddedNotificationHandler notificationHandler;

    @Test
    public void testContextValuesNoAnchor() {
        CommitDiscussionCommentAddedNotification notification = mock(CommitDiscussionCommentAddedNotification.class);
        CommitDiscussionNotificationTestUtils.setupNotificationComment(notification);

        Map<String, Object> context = notificationHandler.getContext(notification);

        CommitDiscussionNotificationTestUtils.assertNotificationComment(notification, context);
    }

    @Test
    public void testContextValuesAnchor() {
        CommitDiscussionCommentAddedNotification notification = mock(CommitDiscussionCommentAddedNotification.class);
        CommitDiscussionNotificationTestUtils.setupNotificationComment(notification);

        CommitDiscussionCommentAnchor anchor = mock(CommitDiscussionCommentAnchor.class);
        when(anchor.getLine()).thenReturn(TEST_LINE);
        when(anchor.getPath()).thenReturn(TEST_FILEPATH + TEST_FILENAME);
        when(notification.getAnchor()).thenReturn(anchor);

        Map<String, Object> context = notificationHandler.getContext(notification);

        CommitDiscussionNotificationTestUtils.assertNotificationComment(notification, context);
        assertEquals(TEST_FILEPATH, context.get("filePath"));
        assertEquals(TEST_FILENAME, context.get("fileName"));
        assertEquals(TEST_LINE, context.get("line"));
    }

    @Test
    public void testSummarizeCommitMessage() {
        Map<String, String> testCommits = Maps.newHashMap();
        testCommits.put("STASH-1234: removed  `.hidden` css selectors on elements. Fixes the issue",
                        "STASH-1234: removed  `.hidden` css selectors on elements.");
        testCommits.put("STASH-1234: Fixed in 2.11.4. No regressions found",
                        "STASH-1234: Fixed in 2.11.4.");
        testCommits.put("This fixes the following issues\n\nSTASH-1234: bad alignment of toolbar buttons\n\nSTASH-2345: incorrect icon is shown\n\nSTASH-3456: heading text is too big",
                        "This fixes the following issues");
        testCommits.put("STASH-1234: import the com.google.common.collect.Maps class. No more errors thrown.",
                        "STASH-1234: import the com.google.common.collect.Maps class.");
        testCommits.put("STASH-9999 Fix rescope activity initialization.\n\n- Override initialize() in InternalPullRequestRescopeActivity and correctly initialize the commits set\n- Override equals/hashCode on InternalPullRequestRescopeCommit to ensure two commits with the same ID are equivalent",
                        "STASH-9999 Fix rescope activity initialization.");

        for (String key : testCommits.keySet()) {
            assertEquals(testCommits.get(key), summarize(key));
        }
    }
}
