package com.atlassian.stash.internal.scm.git.command.notes;

import com.atlassian.bitbucket.content.ChangeType;
import com.atlassian.bitbucket.content.Conflict;
import com.atlassian.bitbucket.content.ConflictChange;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;

import java.util.Map;

import static org.junit.Assert.*;

public class ConflictMapNotesOutputHandlerTest {

    @Test
    public void testWithMultipleContentConflicts() throws Exception {
        ConflictMapNotesOutputHandler handler = new ConflictMapNotesOutputHandler();
        handler.process(new ClassPathResource("git-notes-multiple-content-conflicts.out", getClass()).getInputStream());

        Map<String, Conflict> map = handler.getOutput();
        assertNotNull(map);
        assertEquals(2, map.size());

        assertContentConflict(map, "service-impl/src/main/java/com/atlassian/stash/internal/notification/DefaultNotificationManager.java");
        assertContentConflict(map, "service-impl/src/main/java/com/atlassian/stash/internal/notification/PullRequestOpenedNotificationHandler.java");
    }

    private static void assertContentConflict(Map<String, Conflict> map, String path) {
        Conflict conflict = map.get(path);
        assertNotNull("Missing expected conflict for " + path, conflict);
        assertChange(conflict.getOurChange(), ChangeType.MODIFY, path);
        assertChange(conflict.getTheirChange(), ChangeType.MODIFY, path);
    }

    private static void assertChange(ConflictChange change, ChangeType type, String path) {
        assertChange(change, type, path, null);
    }

    private static void assertChange(ConflictChange change, ChangeType type, String path, String srcPath) {
        assertSame(type, change.getType());
        assertEquals(path, change.getPath().toString());
        if (srcPath == null) {
            assertNull(change.getSrcPath());
        } else {
            assertNotNull("Missing expected srcPath " + srcPath, change.getSrcPath());
            assertEquals(srcPath, change.getSrcPath().toString());
        }
    }
}
