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

import com.atlassian.bitbucket.commit.Commit;
import com.atlassian.bitbucket.commit.MinimalCommit;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class RevListCommandUtils {

    private RevListCommandUtils() {
        throw new UnsupportedOperationException(RevListCommandUtils.class.getName() + " cannot be instantiated");
    }

    public static void assertCommit(Commit commit, String author, String email, int time, String hash, String message,
                                    Map<String, Object> changeMap, String... parentHashes) {
        assertNotNull(commit);
        assertEquals(author, commit.getAuthor().getName());
        assertEquals(email, commit.getAuthor().getEmailAddress());
        assertEquals(new Date(TimeUnit.SECONDS.toMillis(time)), commit.getAuthorTimestamp());
        assertEquals(hash.substring(0, 11), commit.getDisplayId());
        assertEquals(hash, commit.getId());
        assertEquals(message, commit.getMessage());

        assertEquals(changeMap, commit.getProperties().getAs(AbstractCommitRevListOutputHandler.NAME_STATUS_CHANGE, Map.class));

        assertEquals(commit.getParents().size(), parentHashes.length);

        int i = 0;
        for (MinimalCommit parent : commit.getParents()) {
            assertParent(parentHashes[i], parentHashes[i].substring(0, 11), parent);
            i++;
        }
    }

    public static void assertParent(String id, String displayId, MinimalCommit parent) {
        assertEquals(displayId, parent.getDisplayId());
        assertEquals(id, parent.getId());
    }
}
