package com.atlassian.stash.internal.web.fragments;

import com.atlassian.bitbucket.auth.AuthenticationContext;
import com.atlassian.plugin.web.api.DynamicWebInterfaceManager;
import com.atlassian.sal.api.web.context.HttpContext;
import org.junit.Before;
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.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class WebFragmentSupportTest {

    private static final String KEY = "KEY";

    @Mock
    private AuthenticationContext authenticationContext;

    @Mock
    private DynamicWebInterfaceManager dynamicWebInterfaceManager;

    @Mock
    private HttpContext httpContext;

    @Mock
    private WebFragmentData fragment;

    @InjectMocks
    private WebFragmentSupport webFragmentSupport;

    @Before
    public void setUp() throws Exception {
        when(fragment.getKey()).thenReturn(KEY);
    }

    @Test
    public void testFormatLocation() {
        assertEquals("KEY", webFragmentSupport.formatLocation("$(key)", fragment));
        assertEquals("prefix-KEY", webFragmentSupport.formatLocation("prefix-$(key)", fragment));
        assertEquals("KEY-suffix", webFragmentSupport.formatLocation("$(key)-suffix", fragment));
        assertEquals("prefix-KEY-suffix", webFragmentSupport.formatLocation("prefix-$(key)-suffix", fragment));
        assertEquals("prefix-KEY-KEY-suffix", webFragmentSupport.formatLocation("prefix-$(key)-$(key)-suffix", fragment));
        assertEquals("KEY-KEY", webFragmentSupport.formatLocation("$(key)-$(key)", fragment));
        assertEquals("$(other)", webFragmentSupport.formatLocation("$(other)", fragment));
        assertEquals("prefix-suffix", webFragmentSupport.formatLocation("prefix-suffix", fragment));
    }
}
