package it.com.atlassian.stash;

import com.atlassian.pageobjects.TestedProductFactory;
import com.atlassian.pageobjects.page.LoginPage;
import com.atlassian.bitbucket.test.BaseFuncTest;
import com.atlassian.bitbucket.test.DatabaseTestHelper;
import com.atlassian.bitbucket.test.LicenseTestHelper;
import com.atlassian.bitbucket.test.BitbucketTestLicense;
import com.atlassian.webdriver.bitbucket.BitbucketTestedProduct;
import com.atlassian.webdriver.bitbucket.page.admin.ConfigureMigrationPage;
import com.atlassian.webdriver.bitbucket.page.admin.ViewDatabasePage;
import com.atlassian.webdriver.testing.rule.SessionCleanupRule;
import com.atlassian.webdriver.testing.rule.WebDriverScreenshotRule;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.rules.TestRule;

import java.util.List;

import static com.atlassian.bitbucket.test.TestRuleOrderingSyntax.outerRules;
import static org.junit.Assert.assertEquals;

public class DatabaseMigrationTest extends BaseFuncTest {

    protected static final BitbucketTestedProduct BITBUCKET = TestedProductFactory.create(BitbucketTestedProduct.class);

    private static final BitbucketTestLicense STANDALONE_LICENSE = BitbucketTestLicense.builder()
            .whenNonClustered(
                    "AAABAw0ODAoPeNptkN1Kw0AQhe/7FAtep+Sn0FJYMDbBFE0aaazX4zq1C8kmzGyCfXvTuC0o3i3n7\n" +
                            "Hdm5ty94YfIgYS/FEGwXizWYSTSfSVCPwhnbIFP83SAugerWyOPUDM6OUFWpLtJr5CteNYKzc3eE\n" +
                            "E5MAhblJc3zI89fXt3WWFC2gAbl+G7J6TloY9GAUZh+dZrOE15GmfNHRg8oLfX4OykdyVqqS9Q92\n" +
                            "BqYNZi5ahv3zS1XnTuchlbjkdvicbZHGpC2iXw4bDIvXr2svDCOMu/pUOYOLYGsQZowJ+3oE4zmn\n" +
                            "1Li6zhnFn3zjrQ7vjISy1uNboN/zyp7Uidg/FvWNzfnjvQwLAIUTVb2KvELK9HtYl4MOc1u/Z8yx\n" +
                            "20CFDHaIjT/tVAlYnXGmXy3SlkcnixtX02d9")
            .build();

    private static String license;

    @AfterClass
    public static void revertLicense() {
        LicenseTestHelper.setLicense(license);
    }

    @BeforeClass
    public static void setLicense() {
        license = LicenseTestHelper.getLicense();
        LicenseTestHelper.setLicense(STANDALONE_LICENSE.get());
    }

    @Override
    protected List<TestRule> getRuleChain() {
        return outerRules(new WebDriverScreenshotRule(), new SessionCleanupRule())
                .around(super.getRuleChain());
    }

    @Test
    public void testDatabaseMigration() throws Exception {
        ViewDatabasePage databasePage = BITBUCKET.visit(LoginPage.class).loginAsSysAdmin(ViewDatabasePage.class);
        assertEquals("Internal", databasePage.getDatabaseType());

        ConfigureMigrationPage migrationPage = databasePage.clickMigrationDatabase();
        databasePage = migrationPage.selectMySQL()
                .enterHost(DatabaseTestHelper.getTestDatabaseHost())
                .enterPort(DatabaseTestHelper.getTestDatabasePort())
                .enterDatabaseName(DatabaseTestHelper.getTestDatabaseName())
                .enterUsername(DatabaseTestHelper.getTestDatabaseUser())
                .enterPassword(DatabaseTestHelper.getTestDatabasePassword())
                .clickStartMigration()
                .waitUntilCompleted(ViewDatabasePage.class);

        assertEquals("MySQL", databasePage.getDatabaseType());
    }
}
