/*
 * Copyright 2018 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kie.wb.selenium.ui;

import org.assertj.core.api.Assertions;
import org.guvnor.rest.client.CloneProjectRequest;
import org.guvnor.rest.client.Space;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.kie.wb.selenium.model.KieSeleniumTest;
import org.kie.wb.selenium.util.Waits;
import org.kie.wb.test.rest.client.RestWorkbenchClient;
import org.kie.wb.test.rest.client.WorkbenchClient;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriverException;

import static org.assertj.core.api.Assertions.assertThat;

public class StandalonePerspectivesIntegrationTest extends KieSeleniumTest {

    private final static String
            PROJECT_URL = "https://github.com/Rikkola/single-project-test-repository.git",
            SPACE_NAME = "Standalone_space",
            PROJECT_NAME = "Standalone_project";

    private final static String
            BASE_URL = System.getProperty("kie.wb.url"),
            APP_CONTEXT = "kie-wb.jsp",
            STANDALONE_PARAMETER = "?standalone",
            // URL of a standalone perspective:
            PERSPECTIVE_PARAMETER = "&perspective=",
            HEADER_PARAMETER = "&header=UberfireBreadcrumbsContainer",
            PERSPECTIVE_FULL_URL = BASE_URL + "/" + APP_CONTEXT + STANDALONE_PARAMETER + PERSPECTIVE_PARAMETER,
            // URL of a standalone editor:
            PROJECT_ASSET_PATH = "&path=default://master@" + SPACE_NAME + "/" + PROJECT_NAME + "/src/main/java/mortgages/mortgages/Applicant.java",
            EDITOR_FULL_URL = BASE_URL + "/" + APP_CONTEXT + STANDALONE_PARAMETER + PROJECT_ASSET_PATH;

    private static final By
            CONTENT_MANAGEMENT = By.cssSelector("[title='Content Manager']"),
            DATA_OBJECT = By.cssSelector("[title='Applicant.java - Data Objects']"),
            HEADER = By.id("workbenchHeaderPanel"),
            LIBRARY = By.className("toolbar-data-title-kie");

    @BeforeClass
    public static void cloneTestingProject() {
        WorkbenchClient workbenchClient = RestWorkbenchClient
                // REST credentials from Cargo configuration in business-central-tests/pom.xml:
                .createWorkbenchClient(BASE_URL, "restAll", "restAll1234;");

        // Create a space:
        final Space space = new Space();
        space.setName(SPACE_NAME);
        space.setOwner("donald@duck.gov");
        space.setDefaultGroupId("gov.duck");
        workbenchClient.createSpace(space);

        // Clone a project:
        CloneProjectRequest cloneReq = new CloneProjectRequest();
        cloneReq.setGitURL(PROJECT_URL);
        cloneReq.setName(PROJECT_NAME);
        workbenchClient.cloneRepository(SPACE_NAME, cloneReq);
    }

    @Before
    public void setUp() {
        login.get();
        if (login.isDisplayed()) {
            login.loginDefaultUser();
        }
    }

    @Test
    public void testLibraryPerspectiveWithoutHeader() {
        String perspective = "LibraryPerspective";
        driver.get(PERSPECTIVE_FULL_URL + perspective);

        verifyPerspectiveIsLoaded(LIBRARY);
        verifyPresenceOfHeader(false);
    }

    @Test
    public void testLibraryPerspectiveWithHeader() {
        String perspective = "LibraryPerspective";
        driver.get(PERSPECTIVE_FULL_URL + perspective + HEADER_PARAMETER);

        verifyPerspectiveIsLoaded(LIBRARY);
        verifyPresenceOfHeader(true);
    }

    @Test
    public void testStandaloneEditorPerspective() {
        driver.get(EDITOR_FULL_URL);

        verifyPerspectiveIsLoaded(DATA_OBJECT);
    }

    @Test
    public void testContentManagerPerspective() {
        String perspective = "ContentManagerPerspective";
        driver.get(PERSPECTIVE_FULL_URL + perspective);

        verifyPerspectiveIsLoaded(CONTENT_MANAGEMENT);
    }

    private void verifyPerspectiveIsLoaded(By verifier) {
        try {
            Waits.elementPresent(verifier, 15);
        } catch (WebDriverException exception) {
            Assertions.fail("The standalone perspective was not loaded.", exception);
        }
    }

    private void verifyPresenceOfHeader(boolean headerIncluded) {
        int
                actualHeaderCount = driver.findElements(HEADER).size(),
                expectedHeaderCount = headerIncluded ? 1 : 0;

        String headerAssertionMessage = String.format("There should %s be UberfireBreadcrumbsContainer on the page.", headerIncluded ? "" : "NOT");

        assertThat(actualHeaderCount)
                .as(headerAssertionMessage)
                .isEqualTo(expectedHeaderCount);
    }
}

@@ -775,16 +775,45 @@
 .Space;%0A
+import org.junit.AfterClass;%0A
 import o
@@ -829,16 +829,16 @@
 Before;%0A
-
 import o
@@ -1422,53 +1422,8 @@
 t%22,%0A
-            SPACE_NAME = %22Standalone_space%22,%0A
     
@@ -1933,303 +1933,8 @@
 ETER
-,%0A            // URL of a standalone editor:%0A            PROJECT_ASSET_PATH = %22&path=default://master@%22 + SPACE_NAME + %22/%22 + PROJECT_NAME + %22/src/main/java/mortgages/mortgages/Applicant.java%22,%0A            EDITOR_FULL_URL = BASE_URL + %22/%22 + APP_CONTEXT + STANDALONE_PARAMETER + PROJECT_ASSET_PATH
 ;%0A%0A 
@@ -2234,24 +2234,148 @@
 tle-kie%22);%0A%0A
+    private static WorkbenchClient workbenchClient;%0A%0A    private static String spaceName, projectAssetPath, editorFullUrl;%0A%0A
     @BeforeC
@@ -2434,32 +2434,16 @@
         
-WorkbenchClient 
 workbenc
@@ -2642,24 +2642,368 @@
 ll1234;%22);%0A%0A
+        // Create unique space name%0A        spaceName = %22Standalone_space_%22 + System.currentTimeMillis();%0A        projectAssetPath = %22&path=default://master@%22 + spaceName + %22/%22 + PROJECT_NAME + %22/src/main/java/mortgages/mortgages/Applicant.java%22;%0A        editorFullUrl = BASE_URL + %22/%22 + APP_CONTEXT + STANDALONE_PARAMETER + projectAssetPath;%0A%0A
         // C
@@ -3080,26 +3080,25 @@
 setName(
-SPACE_NAME
+spaceName
 );%0A     
@@ -3444,18 +3444,17 @@
 ory(
-SPACE_NAME
+spaceName
 , cl
@@ -3465,24 +3465,137 @@
 eq);%0A    %7D%0A%0A
+    @AfterClass%0A    public static void cleanWorkbench() %7B%0A        workbenchClient.deleteSpace(spaceName);%0A    %7D%0A%0A
     @Before%0A
@@ -4359,23 +4359,21 @@
 get(
-EDITOR_FULL_URL
+editorFullUrl
 );%0A%0A

Make StandalonePerspectivesIntegrationTest independent of other runs (#998)