package com.atlassian.stash.sal.api.help;

import com.atlassian.bitbucket.help.HelpPathService;
import com.atlassian.sal.api.message.HelpPath;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class HelpPathResolverImplTest {

    @Mock
    private HelpPathService helpPathService;

    private HelpPathResolverImpl helpPathResolver;

    @Before
    public void setUp() throws Exception {
        helpPathResolver = new HelpPathResolverImpl(helpPathService);
        when(helpPathService.getPageTitle("key")).thenReturn("page-title");
        when(helpPathService.getPageAlt("key")).thenReturn("page-alt-text");
    }

    @Test
    public void testGetHelpPath() throws Exception {
        pageKeyAndUrl("page-key", "page-url");

        final HelpPath helpPath = helpPathResolver.getHelpPath("key");
        assertEquals("page-key", helpPath.getKey());
        assertEquals("page-url", helpPath.getUrl());
        assertEquals("page-title", helpPath.getTitle());
        assertEquals("page-alt-text", helpPath.getAlt());
    }

    @Test
    public void testNoPageKey() throws Exception {
        pageKeyAndUrl(null, "page-url");
        assertNull(helpPathResolver.getHelpPath("key"));
    }

    @Test
    public void testNoPageUrl() throws Exception {
        pageKeyAndUrl("page-key", null);
        assertNull(helpPathResolver.getHelpPath("key"));
    }

    private void pageKeyAndUrl(String pageKey, String pageUrl) {
        when(helpPathService.getPageKey("key")).thenReturn(pageKey);
        when(helpPathService.getPageUrl("key")).thenReturn(pageUrl);
    }

}
