package com.atlassian.stash.internal.avatar;

import com.atlassian.plugin.webresource.UrlMode;
import com.atlassian.plugin.webresource.WebResourceUrlProvider;
import com.atlassian.bitbucket.avatar.AvatarRequest;
import com.atlassian.bitbucket.nav.NavBuilder;
import com.atlassian.bitbucket.user.Person;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

public class WebResourceAvatarSourceTest {

    @Test
    public void testGetUrlForPerson() throws Exception {
        Person person = mock(Person.class);
        WebResourceUrlProvider urlProvider = mock(WebResourceUrlProvider.class);
        NavBuilder navBuilder = mock(NavBuilder.class);

        WebResourceAvatarSource source = new WebResourceAvatarSource(urlProvider, navBuilder);

        int[] requestSizes = {96, 36, 28, 18, 12};
        int[] expectedSizes = {128, 48, 32, 24, 16};
        for (int i = 0; i < requestSizes.length; ++i) {
            if (i > 0) {
                reset(urlProvider);
            }
            ArgumentCaptor<String> resourceNameCaptor = ArgumentCaptor.forClass(String.class);
            when(urlProvider.getStaticPluginResourceUrl(eq(WebResourceAvatarSource.AVATAR_MODULE_KEY),
                    resourceNameCaptor.capture(), same(UrlMode.ABSOLUTE))).thenReturn("URL");

            String url = source.getUrlForPerson(person, new AvatarRequest(false, requestSizes[i]));
            assertEquals("URL", url);
            assertEquals(String.format(WebResourceAvatarSource.AVATAR_RESOURCE_FORMAT, expectedSizes[i]),
                    resourceNameCaptor.getValue());
        }

        verifyZeroInteractions(person);
    }
}
