package com.atlassian.stash.internal.comment.like.property;

import com.atlassian.bitbucket.comment.Comment;
import com.atlassian.bitbucket.comment.CommentPropertyContext;
import com.atlassian.bitbucket.user.TestApplicationUser;
import com.atlassian.stash.internal.comment.like.InternalCommentLikeService;
import com.atlassian.stash.internal.comment.like.LikedBy;
import com.atlassian.bitbucket.user.ApplicationUser;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterators;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class CommentLikePropertyProviderTest {

    @Mock
    private InternalCommentLikeService likeService;

    @InjectMocks
    private CommentLikePropertyProvider propertyProvider;

    @Test
    public void testDoProvideProperties() throws Exception {
        Comment comment1 = createComment(42L);
        Comment comment2 = createComment(43L);
        CommentPropertyContext context = createContext(comment1, comment2);

        LikedBy like = new LikedBy(1, ImmutableList.<ApplicationUser>of(new TestApplicationUser("foo")));
        when(likeService.findByComments(eq(context))).thenReturn(ImmutableMap.of(43L, like));

        propertyProvider.doProvideProperties(context);

        verify(context, times(1)).setProperty(any(Comment.class), anyString(), anyObject());
        verify(context).setProperty(eq(comment2), eq("likedBy"), eq(like));
    }

    private static Comment createComment(long commentId) {
        Comment comment = mock(Comment.class);
        when(comment.getId()).thenReturn(commentId);
        return comment;
    }

    private static CommentPropertyContext createContext(Comment comment1, Comment comment2) {
        CommentPropertyContext context = mock(CommentPropertyContext.class);
        when(context.iterator()).thenReturn(Iterators.forArray(comment1, comment2));
        return context;
    }

}