package org.jboss.hal.testsuite.test.configuration.undertow.server.hosts;

import java.util.Collections;

import org.jboss.arquillian.core.api.annotation.Inject;
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.resources.Ids;
import org.jboss.hal.testsuite.Console;
import org.jboss.hal.testsuite.CrudOperations;
import org.jboss.hal.testsuite.Random;
import org.jboss.hal.testsuite.creaper.ManagementClientProvider;
import org.jboss.hal.testsuite.creaper.command.BackupAndRestoreAttributes;
import org.jboss.hal.testsuite.page.configuration.UndertowServerPage;
import org.jboss.hal.testsuite.test.configuration.undertow.UndertowFixtures;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.WebDriver;
import org.wildfly.extras.creaper.core.CommandFailedException;
import org.wildfly.extras.creaper.core.online.OnlineManagementClient;
import org.wildfly.extras.creaper.core.online.operations.Address;
import org.wildfly.extras.creaper.core.online.operations.Operations;

@RunWith(Arquillian.class)
public class AttributesTest {

    @Inject
    private Console console;

    @Inject
    private CrudOperations crudOperations;

    @Drone
    private WebDriver browser;

    @Page
    private UndertowServerPage page;

    private static final OnlineManagementClient client = ManagementClientProvider.createOnlineManagementClient();

    private static final Operations operations = new Operations(client);

    private static final Address UNDERTOW_DEFAULT_HOST_ADDRESS =
        UndertowFixtures.serverAddress(UndertowFixtures.DEFAULT_SERVER)
            .and("host", UndertowFixtures.DEFAULT_HOST);

    private static BackupAndRestoreAttributes backup;

    @BeforeClass
    public static void setUp() throws CommandFailedException {
        backup = new BackupAndRestoreAttributes.Builder(UNDERTOW_DEFAULT_HOST_ADDRESS).build();
        client.apply(backup.backup());
    }

    @AfterClass
    public static void tearDown() throws CommandFailedException {
        client.apply(backup.restore());
    }

    @Before
    public void initPage() {
        page.navigate("name", UndertowFixtures.DEFAULT_SERVER);
        console.verticalNavigation().selectPrimary(Ids.UNDERTOW_HOST_ITEM);
        page.getHostsTable().select(UndertowFixtures.DEFAULT_HOST);
    }

    @Test
    public void updateAlias() throws Exception {
        crudOperations.update(
            UNDERTOW_DEFAULT_HOST_ADDRESS,
            page.getHostsForm(), "alias", Collections.singletonList(Random.name()));
    }

    @Test
    public void updateDefaultWebModule() throws Exception {
        crudOperations.update(UNDERTOW_DEFAULT_HOST_ADDRESS, page.getHostsForm(), "default-web-module");
    }

    @Test
    public void updateDefaultResponseCode() throws Exception {
        crudOperations.update(UNDERTOW_DEFAULT_HOST_ADDRESS, page.getHostsForm(), "default-response-code",
            Random.number(400, 599));
    }

    @Test
    public void toggleDisableConsoleRedirect() throws Exception {
        boolean disableConsoleRedirect =
            operations.readAttribute(UNDERTOW_DEFAULT_HOST_ADDRESS, "disable-console-redirect").booleanValue();
        crudOperations.update(UNDERTOW_DEFAULT_HOST_ADDRESS, page.getHostsForm(), "disable-console-redirect",
            !disableConsoleRedirect);
    }
}

@@ -84,24 +84,98 @@
 ava.
-util.Collections
+io.IOException;%0Aimport java.util.Collections;%0Aimport java.util.concurrent.TimeoutException
 ;%0A%0Ai
@@ -677,24 +677,78 @@
 Attributes;%0A
+import org.jboss.hal.testsuite.fragment.FormFragment;%0A
 import org.j
@@ -1327,16 +1327,141 @@
 rations;
+%0Aimport org.wildfly.extras.creaper.core.online.operations.admin.Administration;%0A%0Aimport static org.junit.Assert.assertEquals;
 %0A%0A@RunWi
@@ -1886,24 +1886,111 @@
 s(client);%0A%0A
+    private static final Administration adminOperations = new Administration(client);%0A%0A
     private 
@@ -2520,32 +2520,99 @@
 dFailedException
+, IOException, InterruptedException, TimeoutException %7B%0A        try
  %7B%0A        clien
@@ -2598,32 +2598,36 @@
   try %7B%0A        
+    
 client.apply(bac
@@ -2638,24 +2638,130 @@
 restore());%0A
+            adminOperations.reloadIfRequired();%0A        %7D finally %7B%0A            client.close();%0A        %7D%0A
     %7D%0A%0A    @
@@ -3982,16 +3982,16 @@
 irect%22,%0A
-
         
@@ -4025,11 +4025,579 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void toggleQueueRequestsOnStart() throws Exception %7B%0A        String attrName = %22queue-requests-on-start%22;%0A        FormFragment form = page.getHostsForm();%0A        boolean originalValue = operations.readAttribute(UNDERTOW_DEFAULT_HOST_ADDRESS, attrName).booleanValue();%0A        assertEquals(%22The '%22 + attrName + %22' value presented in the form differs from the one in model.%22,%0A                originalValue, Boolean.valueOf(form.value(attrName)));%0A        crudOperations.update(UNDERTOW_DEFAULT_HOST_ADDRESS, form, attrName, !originalValue);%0A    %7D
 %0A%7D%0A

Test for Undertow host's queue-requests-on-start attribute configuration.