package org.jboss.hal.testsuite.test.rbac;

import org.jboss.arquillian.drone.api.annotation.Drone;
import org.jboss.arquillian.graphene.page.Page;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.hal.testsuite.category.Domain;
import org.jboss.hal.testsuite.finder.Application;
import org.jboss.hal.testsuite.finder.FinderNames;
import org.jboss.hal.testsuite.finder.FinderNavigation;
import org.jboss.hal.testsuite.page.domain.ServerGroupDetailPage;
import org.jboss.hal.testsuite.page.runtime.DomainRuntimeEntryPoint;
import org.jboss.hal.testsuite.util.Authentication;
import org.jboss.hal.testsuite.util.RbacRole;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.openqa.selenium.WebDriver;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

/**
 * Tests behaviour of server group detail page when accessed with a role with several different
 * privileges to multiple server groups.
 *
 * @author msmerek
 */
@RunWith(Arquillian.class)
@Category(Domain.class)
public class ServerGroupDetailTestCase {

    private FinderNavigation runtimePageNavigation;

    @Drone
    private WebDriver browser;

    @Page
    private ServerGroupDetailPage serverGroupDetailPage;

    @Test
    public void modificationButtonsNotVisibleForMonitor() {
        Authentication.with(browser).authenticate(RbacRole.MAIN_ADMINISTRATOR_OTHER_MONITOR);

        startOnRuntimePage();
        goToServerGroupDetailPage("other-server-group");

        checkVisibilityOfButtons(false);
    }

    @Test
    public void modificationButtonsVisibleForAdministrator() {
        Authentication.with(browser).authenticate(RbacRole.MAIN_ADMINISTRATOR_OTHER_MONITOR);

        startOnRuntimePage();
        goToServerGroupDetailPage("other-server-group");
        goBackToRuntimePage();
        goToServerGroupDetailPage("main-server-group");

        checkVisibilityOfButtons(true);
    }

    // test utils

    private void startOnRuntimePage() {
        runtimePageNavigation = new FinderNavigation(browser, DomainRuntimeEntryPoint.class);
    }

    private void goToServerGroupDetailPage(String group) {
        runtimePageNavigation.step(FinderNames.BROWSE_DOMAIN_BY, FinderNames.SERVER_GROUPS)
                .step(FinderNames.SERVER_GROUP, group);

        runtimePageNavigation.selectRow().invoke("View");
        Application.waitUntilVisible();
    }

    private void goBackToRuntimePage() {
        serverGroupDetailPage.clickOnBackLink();
        runtimePageNavigation.resetNavigation();
    }

    private void checkVisibilityOfButtons(boolean expectVisible) {
        serverGroupDetailPage.switchToSystemProperties();
        checkButtonVisibility("Add", expectVisible);
        checkButtonVisibility("Remove", expectVisible);

        serverGroupDetailPage.switchToJVMConfiguration();
        checkButtonVisibility("Clear", expectVisible);
    }

    private void checkButtonVisibility(String buttonLabel, boolean expectVisible) {
        boolean actuallyVisible = serverGroupDetailPage.isButtonVisible(buttonLabel);
        assertThat("Incorrectly set visibility of button \'" + buttonLabel + "\'.",
                actuallyVisible,
                is(expectVisible));
    }

}




@@ -1319,47 +1319,56 @@
 oid 
-modificationB
+b
 uttons
-Not
 Visib
-leForMonitor
+ilityIsChangedForDifferentPrivileges
 () %7B
@@ -1596,269 +1596,8 @@
 e);%0A
-    %7D%0A%0A    @Test%0A    public void modificationButtonsVisibleForAdministrator() %7B%0A        Authentication.with(browser).authenticate(RbacRole.MAIN_ADMINISTRATOR_OTHER_MONITOR);%0A%0A        startOnRuntimePage();%0A        goToServerGroupDetailPage(%22other-server-group%22);
 %0A   
@@ -2919,18 +2919,19 @@
 ibility 
-o
 f
+or
  button 

Test for HAL-1125: Merge two test cases into one