package it.com.atlassian.stash;

import com.atlassian.webdriver.bitbucket.page.ProjectOverviewPage;
import com.atlassian.webdriver.bitbucket.page.RepositoryDeleteConfirmationPage;
import com.atlassian.webdriver.bitbucket.page.RepositoryEditSettingsPage;
import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public class RepositoryDeleteTest extends BaseBrowserFuncTest {
    private static final String REPO_SLUG_TO_CREATE = "functestrepo";

    private static final String PROJECT_NAME_TO_CREATE = "Func test project";
    private static final String PROJECT_KEY_TO_CREATE = "FUNC_TEST_PROJECT";
    private static final String PROJECT_DESC_TO_CREATE = "Func test project description";

    @Before
    public void createProject() {
        testContext.project(PROJECT_KEY_TO_CREATE, PROJECT_NAME_TO_CREATE, PROJECT_DESC_TO_CREATE)
                   .repository(PROJECT_KEY_TO_CREATE, REPO_SLUG_TO_CREATE);
    }

    @Test
    public void testCanCancelDelete() {
        RepositoryEditSettingsPage settingsPage = loginAsAdmin(RepositoryEditSettingsPage.class, PROJECT_KEY_TO_CREATE, REPO_SLUG_TO_CREATE);

        RepositoryDeleteConfirmationPage confirmationPage = settingsPage.clickDelete();
        assertTrue(confirmationPage.getDialog().isVisible());

        confirmationPage.getDialog().clickCancel();
    }

    @Test
    public void testCanPerformDelete() {
        RepositoryEditSettingsPage settingsPage = loginAsAdmin(RepositoryEditSettingsPage.class, PROJECT_KEY_TO_CREATE, REPO_SLUG_TO_CREATE);

        RepositoryDeleteConfirmationPage confirmationPage = settingsPage.clickDelete();
        assertTrue(confirmationPage.getDialog().isVisible());

        ProjectOverviewPage projectOverviewPage = confirmationPage.clickConfirmSuccess();
        assertThat(projectOverviewPage.getAuiSuccessMessages(), hasItem(String.format("The repository %s has been deleted.", REPO_SLUG_TO_CREATE)));
    }

}
