package com.atlassian.bitbucket.mesh.logback;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.io.File;

import static com.atlassian.bitbucket.mesh.logback.LogDirPropertyDefiner.ENV_KEY;
import static com.atlassian.bitbucket.mesh.logback.LogDirPropertyDefiner.PROP_KEY;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class LogDirPropertyDefinerTest {

    @InjectMocks
    private LogDirPropertyDefiner definer;
    @Mock
    private LogDirPropertyDefiner.PropertyResolver resolver;

    @Test
    public void testGetPropertyValueReturnsEnvironmentVariable() {
        when(resolver.getEnvironmentVariable(eq(ENV_KEY))).thenReturn("foo");

        String logDir = definer.getPropertyValue();
        assertEquals("foo" + File.separator + "log", logDir);

        InOrder ordered = inOrder(resolver);
        ordered.verify(resolver).getSystemProperty(eq(PROP_KEY)); //Should try the system property first
        ordered.verify(resolver).getEnvironmentVariable(eq(ENV_KEY)); //Then fall back to the environment
    }

    @Test
    public void testGetPropertyValueReturnsNullIfNeitherKeyIsSet() {
        assertNull(definer.getPropertyValue());

        verify(resolver).getEnvironmentVariable(eq(ENV_KEY));
        verify(resolver).getSystemProperty(eq(PROP_KEY));
    }

    @Test
    public void testGetPropertyValueReturnsSystemProperty() {
        when(resolver.getSystemProperty(eq(PROP_KEY))).thenReturn("foo");

        String logDir = definer.getPropertyValue();
        assertEquals("foo" + File.separator + "log", logDir);

        verify(resolver).getSystemProperty(eq(PROP_KEY));
        verifyNoMoreInteractions(resolver); //Should not check environment
    }
}