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

import com.atlassian.bitbucket.user.TestApplicationUser;
import com.atlassian.soy.renderer.SoyTemplateRenderer;
import com.atlassian.bitbucket.comment.Comment;
import com.atlassian.bitbucket.mail.HtmlCssInliner;
import com.atlassian.bitbucket.user.DummySecurityService;
import com.atlassian.bitbucket.user.SecurityService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.Date;
import java.util.Map;

import static com.atlassian.stash.internal.comment.like.notification.CommentLikeNotificationRenderer.EMAIL_CSS_MODULE_KEY;
import static com.atlassian.stash.internal.comment.like.notification.CommentLikeNotificationRenderer.EMAIL_TEMPLATE_MODULE_KEY;
import static com.atlassian.stash.internal.comment.like.notification.CommentLikeNotificationRenderer.TEMPLATE;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.hasEntry;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class CommentLikeNotificationRendererTest {

    @Mock
    private HtmlCssInliner htmlCssInliner;
    @Spy
    private SecurityService securityService = new DummySecurityService();
    @Mock
    private SoyTemplateRenderer soyTemplateRenderer;

    @InjectMocks
    private CommentLikeNotificationRenderer renderer;

    @SuppressWarnings("unchecked")
    @Test
    public void testRender() throws Exception {
        Comment comment = mock(Comment.class);
        TestApplicationUser recipient = new TestApplicationUser("foo");
        TestApplicationUser liker = new TestApplicationUser("bar");
        CommentLikeData likeData = new CommentLikeData(comment);
        likeData.addLike(liker, new Date());

        when(soyTemplateRenderer.render(eq(EMAIL_TEMPLATE_MODULE_KEY), eq(TEMPLATE),
                (Map) argThat(allOf(hasEntry("comment", (Object) comment),
                        hasEntry("recipient", (Object) recipient),
                        hasEntry("likers", (Object) likeData.getLikers())))))
                .thenReturn("rendered-template");
        when(htmlCssInliner.inlineCss("rendered-template", EMAIL_CSS_MODULE_KEY))
                .thenReturn("rendered-template-with-inline-css");

        assertEquals("rendered-template-with-inline-css", renderer.render(recipient, likeData));
    }

}