package com.atlassian.bitbucket.web.conditions;

import com.atlassian.bitbucket.nav.NavBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.Collections;
import java.util.Locale;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class BaseUrlMismatchConditionTest {

    private static final String BASE_URL = "http://example.com/bitbucket";

    @Mock
    private NavBuilder navBuilder;
    @InjectMocks
    private BaseUrlMismatchCondition condition;

    @Test
    public void testNoRequestInformation() throws Exception {
        assertFalse(condition.shouldDisplay(Collections.<String, Object>emptyMap()));
    }

    @Test
    public void testHasMatchingRequest() throws Exception {
        when(navBuilder.buildAbsolute()).thenReturn(BASE_URL);
        when(navBuilder.buildConfigured()).thenReturn(BASE_URL);
        assertFalse(condition.shouldDisplay(Collections.<String, Object>emptyMap()));
    }

    @Test
    public void testHasMismatchedRequest() throws Exception {
        when(navBuilder.buildAbsolute()).thenReturn("http://localhost:7990/bitbucket");
        when(navBuilder.buildConfigured()).thenReturn(BASE_URL);
        assertTrue(condition.shouldDisplay(Collections.<String, Object>emptyMap()));
    }

    @Test
    public void testMatchesCaseInsensitvely() throws Exception {
        when(navBuilder.buildAbsolute()).thenReturn(BASE_URL.toUpperCase(Locale.ROOT));
        when(navBuilder.buildConfigured()).thenReturn(BASE_URL);
        assertFalse(condition.shouldDisplay(Collections.<String, Object>emptyMap()));
    }
}