package com.atlassian.stash.internal.comment;

import com.atlassian.bitbucket.comment.DiffCommentAnchor;
import com.atlassian.fugue.Iterables;
import com.atlassian.stash.internal.AbstractDaoTest;
import com.atlassian.stash.internal.content.InternalChangeLocation;
import com.google.common.collect.ImmutableList;

import java.util.Map;

/**
 * For using the {@code &#064;DataSet} annotation it is simpler to keep the testing for different
 * {@link DiffCommentAnchor} types separated. This serves as a base
 * class to provide common aspects for testing all anchor types.
 */
public abstract class AbstractDiffCommentAnchorDaoTest extends AbstractDaoTest {

    protected static void assertAnchorIds(Iterable<? extends InternalDiffCommentAnchor> anchors, long... expectedIds) {
        if (expectedIds.length == 0) {
            assertTrue(Iterables.isEmpty().apply(anchors));
        } else {
            ImmutableList<InternalDiffCommentAnchor> list = ImmutableList.copyOf(anchors);
            assertEquals("Found [" + list.size() + "] values where [" + expectedIds.length + "] were expected",
                    expectedIds.length, list.size());

            for (int i = 0; i < expectedIds.length; ++i) {
                assertEquals("Value [" + (i + 1) + "] did not have the expected ID",
                        expectedIds[i], list.get(i).getId());
            }
        }
    }

    protected static void assertCounts(Map<InternalChangeLocation, CommentCounts> map,
                                       String path, int active, int orphaned) {
        assertCounts(map, path, null, active, orphaned);
    }

    protected static void assertCounts(Map<InternalChangeLocation, CommentCounts> map,
                                       String path, String srcPath, int active, int orphaned) {
        InternalChangeLocation location = new InternalChangeLocation.Builder().path(path).srcPath(srcPath).build();

        CommentCounts counts = map.get(location);
        assertNotNull(counts);
        assertEquals("Incorrect active count for " + location, active, counts.getActive());
        assertEquals("Incorrect orphaned count for " + location, orphaned, counts.getOrphaned());
    }

    protected InternalComment loadComment(long id) {
        return (InternalComment) getCurrentSession().load(InternalComment.class, id);
    }
}
