package com.intland.codebeamer.functionaltest.test;

import com.codeborne.selenide.CollectionCondition;
import com.codeborne.selenide.Condition;
import com.intland.codebeamer.functionaltest.helper.Helper;
import com.intland.codebeamer.functionaltest.page.*;
import com.intland.codebeamer.functionaltest.page.mystart.MyStartPage;
import com.intland.codebeamer.functionaltest.page.projects.ProjectsPage;
import com.intland.codebeamer.functionaltest.page.trackers.TrackersPage;
import com.intland.codebeamer.functionaltest.page.wiki.ProjectWikiPage;
import org.testng.annotations.*;

import java.lang.reflect.Method;
import java.util.Date;

import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.Selenide.page;
import static com.codeborne.selenide.WebDriverRunner.url;
import static org.testng.Assert.assertEquals;

/**
 * Copyright by Intland Software
 * <p>
 * All rights reserved.
 * <p>
 * This software is the confidential and proprietary information
 * of Intland Software. ("Confidential Information"). You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the productLicense agreement
 * you entered into with Intland.
 */
public class ProjectCreationTests extends BaseTest {

    private CreateProjectPage createProjectPage;
    private MyStartPage myStartPage;
    private static String SIMPLE_PROJECT_NAME = "testNewProject" + new Date().getTime();
    private static String TEMPLATE_PROJECT_NAME = "template project" + new Date().getTime();
    private boolean isRunProjectDelete = false;
    private String smartTemplateGeneratedProjectName = "";
    private final String GIT_REPO_URL = "http://172.20.41.16:8080/cb/git/GIT";
    private final String HG_REPO_URL = "http://172.20.41.16:8080/cb/hg/HG-repo";
    //@BeforeClass
    //public void beforeClass() {
//        holdBrowserOpen = true;
//    }

    @BeforeMethod
    public void beforeMethod() {
        isRunProjectDelete = false;
        myStartPage = Helper.loginWithSystemAdmin();
        createProjectPage = open("/createProject.spr", CreateProjectPage.class);

    }

    @Test
    public void testNewProject() {
        createProjectPage.tickProjectType(CreateProjectPage.ProjectTypeOptions.NEW_PROJECT);
        createProjectPage.clickOnNext();
        createProjectPage = page(CreateProjectPage.class);
        createProjectPage.setName(SIMPLE_PROJECT_NAME);
        createProjectPage.clickOnNext();
        ProjectWikiPage projectWikiPage = createProjectPage.clickOnFinish();
        isRunProjectDelete = true;
        assertEquals(projectWikiPage.leftPane.getWikipagesCount(), 3, "Wiki pages count does not match");
        TrackersPage trackersPage = projectWikiPage.clickOnTrackers();
        assertEquals(trackersPage.getWorkItemsCount(), 9, "Work items count does not match");
        assertEquals(trackersPage.getConfigItemsCount(), 6, "Config items count does not match");
    }

    @Test
    public void newProjectFromBuiltInSmartTemplateWhenEverythingStaysDefaultShouldFitTheTemplate() {
        createProjectPage.tickProjectType(CreateProjectPage.ProjectTypeOptions.DEMO_PROJECT);
        createProjectPage.selectSmartTemplate(CreateProjectPage.SmartTemplateOptions.AGILE_SCRUM);
        createProjectPage.clickOnNext();
        ProjectTemplatePage projectTemplatePage = page(ProjectTemplatePage.class);
        smartTemplateGeneratedProjectName = projectTemplatePage.getName();
        ProjectWikiPage projectWikiPage = projectTemplatePage.clickOnFinish();
        isRunProjectDelete = false;
        assertEquals(projectWikiPage.leftPane.getWikipagesCount(), 12, "Wiki pages count does not match");
        assertEquals(projectWikiPage.leftPane.getDashboardCount(), 1, "Dashboard count does not match");
        TrackersPage trackersPage = projectWikiPage.clickOnTrackers();
        assertEquals(trackersPage.getOpenWorkItemsCountSum(), 71, "Sum of open work items count does not match");
        assertEquals(trackersPage.getAllWorkItemsCountSum(), 77, "Sum of all work items count does not match");
        assertEquals(trackersPage.getOpenConfigItemsCountSum(), 40, "Sum of open config items count does not match");
        assertEquals(trackersPage.getAllConfigItemsCountSum(), 52, "Sum of all config items count does not match");
    }

    @Test(dependsOnMethods = {"newProjectFromBuiltInSmartTemplateWhenEverythingStaysDefaultShouldFitTheTemplate"})
    public void newProjectCreatedFromTemplateWhenAllComponentSelectedShouldBeTheSameAsOriginal() {
        makeProjectAvailableAsTemplate(smartTemplateGeneratedProjectName);
        createProjectPage.tickProjectType(CreateProjectPage.ProjectTypeOptions.NEW_PROJECT);
        createProjectPage.selectProjectTemplate(smartTemplateGeneratedProjectName);
        createProjectPage.clickOnNext();

        ProjectComponentsPage projectComponentsPage = page(ProjectComponentsPage.class);
        projectComponentsPage.tickAllCheckBoxes(true);

        projectComponentsPage.clickOnNext();

        createProjectPage.setName(TEMPLATE_PROJECT_NAME);
        createProjectPage.setKey("template_key" + new Date().getTime());
        createProjectPage.setCategory("TemplateProjects");
        createProjectPage.clickOnNext();

        ScmProjectSettingsPage scmProjectSettingsPage = page(ScmProjectSettingsPage.class);
        scmProjectSettingsPage.selectGit();
        RepositoryManagementPage repositoryManagementPage = scmProjectSettingsPage.clickOnNext();

        repositoryManagementPage.selectRepositoryOption(RepositoryManagementPage.RepositoryManagementOptions.NEW_MANAGED_REPO);
        repositoryManagementPage.clickOnNext();
        NewManagedRepositoryPage newManagedRepositoryPage = page(NewManagedRepositoryPage.class);
        ProjectWikiPage projectWikiPage = newManagedRepositoryPage.clickOnFinish();
        isRunProjectDelete = true;
        //we do not copy trackers in this case !
        assertEquals(projectWikiPage.leftPane.getWikipagesCount(), 12, "Wiki pages count does not match");
        assertEquals(projectWikiPage.leftPane.getDashboardCount(), 1, "Dashboard count does not match");

        DocumentsPage documentsPage = projectWikiPage.clickOnDocuments();
        documentsPage.getDocumentTreeItems().shouldHave(CollectionCondition.texts("Documents", ".codeBeamer", "Project Icons", "Templates", "Traceability", "Trackers", "Test Parameters"));
    }

    @Test
    public void newProjectFromBuiltInSmartTemplateWhenEverythingFilledShouldFitTheTemplate() {
        createProjectPage.tickProjectType(CreateProjectPage.ProjectTypeOptions.DEMO_PROJECT);
        createProjectPage.selectSmartTemplate(CreateProjectPage.SmartTemplateOptions.AGILE_WATERFALL_HYBRID);
        createProjectPage.clickOnNext();
        ProjectTemplatePage projectTemplatePage = page(ProjectTemplatePage.class);
        projectTemplatePage.setCategory("FromSmartTemplate");
        ProjectWikiPage projectWikiPage = projectTemplatePage.clickOnFinish();
        isRunProjectDelete = true;
        assertEquals(projectWikiPage.leftPane.getWikipagesCount(), 5, "Wiki pages count does not match");
        assertEquals(projectWikiPage.leftPane.getDashboardCount(), 1, "Dashboard count does not match");
        TrackersPage trackersPage = projectWikiPage.clickOnTrackers();
        assertEquals(trackersPage.getOpenWorkItemsCountSum(), 80, "Sum of open work items count does not match");
        assertEquals(trackersPage.getAllWorkItemsCountSum(), 110, "Sum of all work items count does not match");
        assertEquals(trackersPage.getOpenConfigItemsCountSum(), 41, "Sum of open config items count does not match");
        assertEquals(trackersPage.getAllConfigItemsCountSum(), 52, "Sum of all config items count does not match");
    }

    @Test
    public void newProjectFromBuiltInSmartTemplateWhenIncludeWorkItemsSelectedShouldFitTheTemplate() {
        createProjectPage.tickProjectType(CreateProjectPage.ProjectTypeOptions.DEMO_PROJECT);
        createProjectPage.selectSmartTemplate(CreateProjectPage.SmartTemplateOptions.AGILE_WATERFALL_HYBRID);
        createProjectPage.clickOnNext();
        ProjectTemplatePage projectTemplatePage = page(ProjectTemplatePage.class);
        projectTemplatePage.setCategory("FromSmartTemplate");
        projectTemplatePage.checkIncludeAllWorkItems(true);
        projectTemplatePage.checkIncludeWikiPages(false);
        ProjectWikiPage projectWikiPage = projectTemplatePage.clickOnFinish();
        isRunProjectDelete = true;
        assertEquals(projectWikiPage.leftPane.getWikipagesCount(), 0, "Wiki pages count does not match");
        assertEquals(projectWikiPage.leftPane.getDashboardCount(), 1, "Dashboard count does not match");

        DocumentsPage documentsPage = projectWikiPage.clickOnDocuments();
        documentsPage.getDocumentTreeItems().shouldHave(CollectionCondition.texts("Documents", ".codeBeamer", "Project Icons", "Templates", "Trackers", "Test Parameters"));

        TrackersPage trackersPage = projectWikiPage.clickOnTrackers();
        assertEquals(trackersPage.getOpenWorkItemsCountSum(), 80, "Sum of open work items count does not match");
        assertEquals(trackersPage.getAllWorkItemsCountSum(), 110, "Sum of all work items count does not match");
        assertEquals(trackersPage.getOpenConfigItemsCountSum(), 41, "Sum of open config items count does not match");
        assertEquals(trackersPage.getAllConfigItemsCountSum(), 52, "Sum of all config items count does not match");
    }

    @Test
    public void newProjectFromBuiltInSmartTemplateWhenIncludeWikiPagesShouldFitTheTemplate() {
        createProjectPage.tickProjectType(CreateProjectPage.ProjectTypeOptions.DEMO_PROJECT);
        createProjectPage.selectSmartTemplate(CreateProjectPage.SmartTemplateOptions.AGILE_WATERFALL_HYBRID);
        createProjectPage.clickOnNext();
        ProjectTemplatePage projectTemplatePage = page(ProjectTemplatePage.class);
        projectTemplatePage.setCategory("FromSmartTemplate");
        projectTemplatePage.checkIncludeAllWorkItems(false);
        projectTemplatePage.checkIncludeWikiPages(true);
        ProjectWikiPage projectWikiPage = projectTemplatePage.clickOnFinish();
        isRunProjectDelete = true;
        assertEquals(projectWikiPage.leftPane.getWikipagesCount(), 5, "Wiki pages count does not match");
        assertEquals(projectWikiPage.leftPane.getDashboardCount(), 1, "Dashboard count does not match");

        DocumentsPage documentsPage = projectWikiPage.clickOnDocuments();
        documentsPage.getDocumentTreeItems().shouldHave(CollectionCondition.texts("Documents"));

        TrackersPage trackersPage = projectWikiPage.clickOnTrackers();
        assertEquals(trackersPage.getAllWorkItemsCountSum(), 0, "Sum of all work items count does not match");
        assertEquals(trackersPage.getAllConfigItemsCountSum(), 0, "Sum of all config items count does not match");
    }

    @DataProvider(name = "templates")
    public static Object[][] templates() {
        return new Object[][]{
                {CreateProjectPage.SmartTemplateOptions.NONE, "no Smart Template selected"},
                {CreateProjectPage.SmartTemplateOptions.AUTOMOTIVE, "To get access to the Automotive template, please get in touch with our Sales department"},
                {CreateProjectPage.SmartTemplateOptions.AVIONICS, "To get access to the Avionics template, please get in touch with our Sales department"},
                {CreateProjectPage.SmartTemplateOptions.MEDICAL, "To get access to the Medical template, please get in touch with our Sales department"}
        };
    }

    @Test(dataProvider = "templates")
    public void newProjectFromOptionalSmartTemplateWhenXTemplateSelectedShouldShowErrorMessage(CreateProjectPage.SmartTemplateOptions smartTemplateOption, String errorMsg) {
        createProjectPage.tickProjectType(CreateProjectPage.ProjectTypeOptions.DEMO_PROJECT);
        createProjectPage.selectSmartTemplate(smartTemplateOption);
        createProjectPage.clickOnNext();
        createProjectPage.errorMessages().shouldHave(CollectionCondition.texts(errorMsg));
    }

    //https://codebeamer.com/cb/item/557189
    @Test
    public void newProjectFromValidExternalFileWhenJsonSelectedShouldCreateProjectAsTheFileDescribed() {
        createProjectPage.tickProjectType(CreateProjectPage.ProjectTypeOptions.NEW_PROJECT);
        createProjectPage.uploadProject("Test_Project_Valid.json");
        createProjectPage.clickOnNext();
        createProjectPage.clickOnNext();
        ScmProjectSettingsPage scmProjectSettingsPage = page(ScmProjectSettingsPage.class);
        scmProjectSettingsPage.selectGit();
        scmProjectSettingsPage.clickOnNext();
        RepositoryManagementPage repositoryManagementPage = page(RepositoryManagementPage.class);
        repositoryManagementPage.selectRepositoryOption(RepositoryManagementPage.RepositoryManagementOptions.NEW_MANAGED_REPO);
        repositoryManagementPage.clickOnNext();
        NewManagedRepositoryPage newManagedRepositoryPage = page(NewManagedRepositoryPage.class);
        ProjectWikiPage projectWikiPage = newManagedRepositoryPage.clickOnFinish();
        isRunProjectDelete = true;
        assertEquals(projectWikiPage.leftPane.getWikipagesCount(), 3, "Wiki pages count does not match");
        assertEquals(projectWikiPage.leftPane.getDashboardCount(), 3, "Dashboard count does not match");

        DocumentsPage documentsPage = projectWikiPage.clickOnDocuments();
        documentsPage.getDocumentTreeItems().shouldHave(CollectionCondition.texts("Documents", ".codeBeamer", "Project Icons", "Test Parameters"));

        TrackersPage trackersPage = documentsPage.clickOnTrackers();
        assertEquals(trackersPage.getWorkItemsCount(), 9, "Work items count does not match");
        assertEquals(trackersPage.getConfigItemsCount(), 6, "Config items count does not match");
        assertEquals(trackersPage.getAllConfigItemsCountSum(), 3, "Sum of all config items does not match");
        assertEquals(trackersPage.getOpenConfigItemsCountSum(), 3, "Sum of all open config items does not match");
    }

    //https://codebeamer.com/cb/item/557189
    @Test
    public void newProjectFromValidExternalFileWhenZipSelectedShouldCreateProjectAsTheFileDescribed() {
        createProjectPage.tickProjectType(CreateProjectPage.ProjectTypeOptions.NEW_PROJECT);
        createProjectPage.uploadProject("Test_Project_Valid.zip");
        createProjectPage.clickOnNext();
        createProjectPage.setCategory("FromZip");
        createProjectPage.clickOnNext();
        ScmProjectSettingsPage scmProjectSettingsPage = page(ScmProjectSettingsPage.class);
        scmProjectSettingsPage.selectMercurial();
        scmProjectSettingsPage.clickOnNext();
        RepositoryManagementPage repositoryManagementPage = page(RepositoryManagementPage.class);
        repositoryManagementPage.selectRepositoryOption(RepositoryManagementPage.RepositoryManagementOptions.USE_EXISTING_EXTERNAL_REPO);
        repositoryManagementPage.clickOnNext();
        RepositorySettingsPage repositorySettingsPage = page(RepositorySettingsPage.class);
        repositorySettingsPage.fillUrl(HG_REPO_URL);
        repositorySettingsPage.fillPassword(Helper.password);
        ProjectWikiPage projectWikiPage = repositorySettingsPage.clickOnFinish();
        isRunProjectDelete = true;
        assertEquals(projectWikiPage.leftPane.getWikipagesCount(), 3, "Wiki pages count does not match");
        assertEquals(projectWikiPage.leftPane.getDashboardCount(), 3, "Dashboard count does not match");

        DocumentsPage documentsPage = projectWikiPage.clickOnDocuments();
        documentsPage.getDocumentTreeItems().shouldHave(CollectionCondition.texts("Documents", ".codeBeamer", "Project Icons", "Test Parameters"));

        TrackersPage trackersPage = documentsPage.clickOnTrackers();
        assertEquals(trackersPage.getWorkItemsCount(), 9, "Work items count does not match");
        assertEquals(trackersPage.getConfigItemsCount(), 6, "Config items count does not match");

        assertEquals(trackersPage.getAllConfigItemsCountSum(), 3, "Sum of all config items does not match");
        assertEquals(trackersPage.getOpenConfigItemsCountSum(), 3, "Sum of all open config items does not match");
    }

    //https://codebeamer.com/cb/item/557189
    @Test
    public void newProjectFromValidExternalFileWhenZipSelectedShouldShowExternalReferencesWarning() {
        createProjectPage.tickProjectType(CreateProjectPage.ProjectTypeOptions.NEW_PROJECT);
        createProjectPage.uploadProject("088_Master_Platform.zip");
        createProjectPage.clickOnNext();
        createProjectPage.setCategory("FromZip");
        createProjectPage.clickOnNext();
        ScmProjectSettingsPage scmProjectSettingsPage = page(ScmProjectSettingsPage.class);
        scmProjectSettingsPage.selectMercurial();
        scmProjectSettingsPage.clickOnNext();
        RepositoryManagementPage repositoryManagementPage = page(RepositoryManagementPage.class);
        repositoryManagementPage.selectRepositoryOption(RepositoryManagementPage.RepositoryManagementOptions.USE_EXISTING_EXTERNAL_REPO);
        repositoryManagementPage.clickOnNext();
        RepositorySettingsPage repositorySettingsPage = page(RepositorySettingsPage.class);
        repositorySettingsPage.fillUrl(HG_REPO_URL);
        repositorySettingsPage.fillPassword(Helper.password);
        ProjectWikiPage projectWikiPage =repositorySettingsPage.clickOnFinish();
        isRunProjectDelete = true;
        projectWikiPage.centerPane.errorMessages().filter(Condition.matchesText("NullPointerException")).shouldBe(CollectionCondition.empty);
        projectWikiPage.centerPane.errorMessages().shouldHave(CollectionCondition.sizeGreaterThan(0)).get(0).shouldHave(Condition.text("contained external references and will not work"));
    }

    //https://codebeamer.com/cb/item/557189
    @Test
    public void newProjectFromValidExternalFileWhenReqifSelectedShouldCreateProjectAsTheFileDescribed() {
        createProjectPage.tickProjectType(CreateProjectPage.ProjectTypeOptions.NEW_PROJECT);
        createProjectPage.uploadProject("BMW_RAM_Test_export.reqif");
        createProjectPage.clickOnNext();
        createProjectPage.clickOnNext();
        ScmProjectSettingsPage scmProjectSettingsPage = page(ScmProjectSettingsPage.class);
        scmProjectSettingsPage.selectSvn();
        scmProjectSettingsPage.clickOnNext();
        RepositoryManagementPage repositoryManagementPage = page(RepositoryManagementPage.class);
        repositoryManagementPage.selectRepositoryOption(RepositoryManagementPage.RepositoryManagementOptions.NEW_MANAGED_REPO);
        repositoryManagementPage.clickOnNext();
        RepositorySettingsPage repositorySettingsPage = page(RepositorySettingsPage.class);
        ProjectWikiPage projectWikiPage = repositorySettingsPage.clickOnFinish();
        isRunProjectDelete = true;
        assertEquals(projectWikiPage.leftPane.getWikipagesCount(), 5, "Wiki pages count does not match");
        assertEquals(projectWikiPage.leftPane.getDashboardCount(), 1, "Dashboard count does not match");

        DocumentsPage documentsPage = projectWikiPage.clickOnDocuments();
        documentsPage.getDocumentTreeItems().shouldHave(CollectionCondition.texts("Documents", "ReqIF Import"));

        TrackersPage trackersPage = documentsPage.clickOnTrackers();
        assertEquals(trackersPage.getWorkItemsCount(), 1, "Work items count does not match");
        assertEquals(trackersPage.getOpenWorkItemsCountSum(), 155, "Sum of all open work items does not match");
        assertEquals(trackersPage.getAllWorkItemsCountSum(), 155, "Sum of all work items does not match");
    }

    //https://codebeamer.com/cb/item/557189
    @Test
    public void newProjectFromValidExternalFileWhenReqifzSelectedShouldCreateProjectAsTheFileDescribed() {
        createProjectPage.tickProjectType(CreateProjectPage.ProjectTypeOptions.NEW_PROJECT);
        createProjectPage.uploadProject("korean_project.reqifz");
        createProjectPage.clickOnNext();
        createProjectPage.tickIncludingEmpty(true);
        createProjectPage.getHiddenCollapsedImportConfigRows().shouldHave(CollectionCondition.size(0));
        createProjectPage.clickOnNext();
        ScmProjectSettingsPage scmProjectSettingsPage = page(ScmProjectSettingsPage.class);
        scmProjectSettingsPage.selectGit();
        scmProjectSettingsPage.clickOnNext();
        RepositoryManagementPage repositoryManagementPage = page(RepositoryManagementPage.class);
        repositoryManagementPage.selectRepositoryOption(RepositoryManagementPage.RepositoryManagementOptions.USE_EXISTING_EXTERNAL_REPO);
        repositoryManagementPage.clickOnNext();
        RepositorySettingsPage repositorySettingsPage = page(RepositorySettingsPage.class);
        repositorySettingsPage.fillRepositoryName("koreanproject");
        repositorySettingsPage.fillUrl(GIT_REPO_URL);
        repositorySettingsPage.fillPassword(Helper.password);
        ProjectWikiPage projectWikiPage = repositorySettingsPage.clickOnFinish();
        isRunProjectDelete = true;
        assertEquals(projectWikiPage.leftPane.getWikipagesCount(), 5, "Wiki pages count does not match");
        assertEquals(projectWikiPage.leftPane.getDashboardCount(), 1, "Dashboard count does not match");

        DocumentsPage documentsPage = projectWikiPage.clickOnDocuments();
        documentsPage.getDocumentTreeItems().shouldHave(CollectionCondition.texts("Documents", "ReqIF Import"));

        TrackersPage trackersPage = documentsPage.clickOnTrackers();
        assertEquals(trackersPage.getWorkItemsCount(), 1, "Work items count does not match");
        assertEquals(trackersPage.getOpenWorkItemsCountSum(), 0, "Sum of all open work items does not match");
        assertEquals(trackersPage.getAllWorkItemsCountSum(), 0, "Sum of all work items does not match");
    }

    //https://codebeamer.com/cb/item/557189
    @Test
    public void newProjectFromValidExternalFileWhenBsonSelectedShouldCreateProjectAsTheFileDescribed() {
        createProjectPage.tickProjectType(CreateProjectPage.ProjectTypeOptions.NEW_PROJECT);
        createProjectPage.uploadProject("Test_Project_Valid.bson");
        createProjectPage.clickOnNext();
        createProjectPage.clickOnNext();
        ProjectWikiPage projectWikiPage = createProjectPage.clickOnFinish();
        isRunProjectDelete = true;
        assertEquals(projectWikiPage.leftPane.getWikipagesCount(), 12, "Wiki pages count does not match");
        assertEquals(projectWikiPage.leftPane.getDashboardCount(), 1, "Dashboard count does not match");

        DocumentsPage documentsPage = projectWikiPage.clickOnDocuments();
        documentsPage.getDocumentTreeItems().shouldHave(CollectionCondition.texts("Documents", ".CodeBeamer Templates",
                "Traceability",
                "Trackers",
                "Wiki",
                "Project Icons",
                "Test Parameters"));

        TrackersPage trackersPage = documentsPage.clickOnTrackers();
        assertEquals(trackersPage.getWorkItemsCount(), 7, "Work items count does not match");
        assertEquals(trackersPage.getConfigItemsCount(), 5);
        assertEquals(trackersPage.getOpenWorkItemsCountSum(), 80, "Sum of all open work items does not match");
        assertEquals(trackersPage.getAllWorkItemsCountSum(), 86, "Sum of all work items does not match");
        assertEquals(trackersPage.getOpenConfigItemsCountSum(), 40, "Sum of all open config items does not match");
        assertEquals(trackersPage.getAllConfigItemsCountSum(), 52, "Sum of all config items does not match");
    }

    @DataProvider(name = "fileNames")
    public static Object[][] fileNames() {
        return new Object[][]{
                {"Test_Project_Invalid.json"},
                {"Test_Project_Invalid.zip"},
                {"Test_Project_Invalid.bson"},
                {"Test_Project_Invalid.reqif"},
                {"Test_Project_Invalid.reqifz"}
        };
    }

    @Test(dataProvider = "fileNames")
    public void newProjectFromInvalidExternalFileWhenExtSelectedShouldCreateProjectAsTheFileDescribed(String fileName) {
        createProjectPage.tickProjectType(CreateProjectPage.ProjectTypeOptions.NEW_PROJECT);
        createProjectPage.uploadProject(fileName);
        createProjectPage.clickOnNext();
        createProjectPage.errorMessages().shouldHave(CollectionCondition.texts("Can not find or import data from the selected template"));
    }


    public void deleteCurrentProject() {
        ProjectAdminSettingsPage projectAdminSettingsPage = createProjectPage.clickOnAdmin();
        projectAdminSettingsPage.clickOnRemoveProject();
        projectAdminSettingsPage.confirmRemoveProject();
        projectAdminSettingsPage.clickOnDeleteProject();
        projectAdminSettingsPage.confirmDeleteProject();
    }

    private void makeProjectAvailableAsTemplate(String projectName) {
        String currentUrl = url();
        ProjectsPage projectsPage = myStartPage.clickOnProjects();
        projectsPage.clickOnCompactList();
        ProjectWikiPage projectWikiPage = projectsPage.clickOnProject(projectName);

        ProjectAdminSettingsPage projectAdminSettingsPage = projectWikiPage.clickOnAdmin();
        projectAdminSettingsPage.tickAvailableAsTemplate(true);
        projectAdminSettingsPage.clickOnSave();
        open(currentUrl);
    }

    @AfterMethod
    public void afterMethod(Method method) {
        if (isRunProjectDelete) {
            deleteCurrentProject();
        }
        if (method.getName().equals("newProjectCreatedFromTemplateWhenAllComponentSelectedShouldBeTheSameAsOriginal")) {
            deleteProject(smartTemplateGeneratedProjectName);
        }

    }

}
