package com.atlassian.bitbucket.installer.scripts;

import com.atlassian.bitbucket.installer.validation.SoftValidationException;
import org.junit.Test;

import java.io.File;

import static com.atlassian.bitbucket.installer.InstallerConstants.*;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;

public class HomeLocationScreenTest extends BaseInstallerTest {

    private static final String NEW_INSTALL_TEXT = "new.install.text";
    private static final String UPGRADE_TEXT = "upgrade.text";

    private HomeLocationScreen homeLocationScreen = new HomeLocationScreen(getContext(), getUtils());

    @Test
    public void testShouldSetNewInstallTextWhenInNewInstallWorkflow() throws Exception {
        setDefaultInstall();
        setI18nMessageReturn(I18N_CONFIGURE_INSTALLATION_HOME_DIRECTORY_INFO_NEW_INSTALL_TEXT, NEW_INSTALL_TEXT);

        homeLocationScreen.initHomeDirectoryScreen();

        verify(getContext()).setVariable(KEY_HOME_DIRECTORY_INFO_TEXT, NEW_INSTALL_TEXT);
    }

    @Test
    public void testShouldSetUpgradeTextWhenInUpgradeWorkflow() throws Exception {
        setUpgrade();
        setI18nMessageReturn(I18N_CONFIGURE_INSTALLATION_HOME_DIRECTORY_INFO_UPGRADE_TEXT, UPGRADE_TEXT);
        File homeDir = mock(File.class);
        when(getInstallationDetails().getHomeDirectory()).thenReturn(homeDir);
        when(homeDir.exists()).thenReturn(true);

        homeLocationScreen.initHomeDirectoryScreen();

        verify(getContext()).setVariable(KEY_HOME_DIRECTORY_INFO_TEXT, UPGRADE_TEXT);
    }

    @Test
    public void testShouldSetHomeDirectoryAfterSkippingSoftWarning() throws Exception {
        String homeDirectoryPath = "path/to/home";
        when(getContext().getVariable(KEY_APP_BITBUCKET_HOME)).thenReturn(homeDirectoryPath);

        when(getUtils().showConfirmDialog(any(String.class), any(String.class), any(String.class), eq(false)))
                .thenReturn(true);

        doThrow(new SoftValidationException(SoftValidationException.ERROR_DIRECTORY_USED))
                .when(getInstallationDetails()).setHomeDirectory(eq(new File(homeDirectoryPath)), eq(false));

        assertTrue(homeLocationScreen.validateHomeDirectory());
    }

}