package org.jboss.hal.testsuite.test.configuration.messaging.server.destinations;

import java.io.IOException;

import org.jboss.arquillian.junit.Arquillian;
import org.jboss.hal.testsuite.Random;
import org.jboss.hal.testsuite.fragment.FormFragment;
import org.jboss.hal.testsuite.fragment.TableFragment;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.extras.creaper.core.online.operations.OperationException;
import org.wildfly.extras.creaper.core.online.operations.Values;

import static org.jboss.hal.dmr.ModelDescriptionConstants.ENTRIES;
import static org.jboss.hal.dmr.ModelDescriptionConstants.NAME;
import static org.jboss.hal.dmr.ModelDescriptionConstants.SERVER;
import static org.jboss.hal.resources.Ids.ITEM;
import static org.jboss.hal.resources.Ids.MESSAGING_JMS_TOPIC;
import static org.jboss.hal.testsuite.fixtures.MessagingFixtures.JMSTOPIC_CREATE;
import static org.jboss.hal.testsuite.fixtures.MessagingFixtures.JMSTOPIC_DELETE;
import static org.jboss.hal.testsuite.fixtures.MessagingFixtures.JMSTOPIC_UPDATE;
import static org.jboss.hal.testsuite.fixtures.MessagingFixtures.SRV_UPDATE;
import static org.jboss.hal.testsuite.fixtures.MessagingFixtures.jmsTopicAddress;
import static org.jboss.hal.testsuite.fixtures.MessagingFixtures.serverAddress;

@RunWith(Arquillian.class)
public class JMSTopicTest extends AbstractServerDestinationsTest {

    @BeforeClass
    public static void createResources() throws IOException {
        createServer(SRV_UPDATE);
        operations.add(jmsTopicAddress(SRV_UPDATE, JMSTOPIC_UPDATE), Values.ofList(ENTRIES, Random.name())).assertSuccess();
        operations.add(jmsTopicAddress(SRV_UPDATE, JMSTOPIC_DELETE), Values.ofList(ENTRIES, Random.name())).assertSuccess();
    }

    @AfterClass
    public static void removeResources() throws IOException, OperationException {
        operations.removeIfExists(serverAddress(SRV_UPDATE));
    }

    @Before
    public void navigate() {
        page.navigate(SERVER, SRV_UPDATE);
    }

    @Test
    public void create() throws Exception {
        console.verticalNavigation().selectPrimary(MESSAGING_JMS_TOPIC + ID_DELIMITER + ITEM);
        TableFragment table = page.getJmsTopicTable();
        FormFragment form = page.getJmsTopicForm();
        table.bind(form);

        crudOperations.create(jmsTopicAddress(SRV_UPDATE, JMSTOPIC_CREATE), table,
            formFragment -> {
                formFragment.text(NAME, JMSTOPIC_CREATE);
                formFragment.properties(ENTRIES).add(Random.name());
            }
        );
    }

    @Test
    public void editEntries() throws Exception {
        console.verticalNavigation().selectPrimary(MESSAGING_JMS_TOPIC + ID_DELIMITER + ITEM);
        TableFragment table = page.getJmsTopicTable();
        FormFragment form = page.getJmsTopicForm();
        table.bind(form);
        String val = Random.name();

        table.select(JMSTOPIC_UPDATE);
        crudOperations.update(jmsTopicAddress(SRV_UPDATE, JMSTOPIC_UPDATE), form,
            formFragment -> formFragment.list(ENTRIES).add(val),
            resourceVerifier -> resourceVerifier.verifyListAttributeContainsValue(ENTRIES, val));
    }

    @Test
    public void remove() throws Exception {
        console.verticalNavigation().selectPrimary(MESSAGING_JMS_TOPIC + ID_DELIMITER + ITEM);
        TableFragment table = page.getJmsTopicTable();
        FormFragment form = page.getJmsTopicForm();
        table.bind(form);

        crudOperations.delete(jmsTopicAddress(SRV_UPDATE, JMSTOPIC_DELETE), table, JMSTOPIC_DELETE);
    }

}

@@ -176,133 +176,452 @@
 hal.
-testsuite.Random;%0Aimport org.jboss.hal.testsuite.fragment.FormFragment;%0Aimport org.jboss.hal.testsuite.fragment.TableFragment
+meta.token.NameTokens;%0Aimport org.jboss.hal.resources.Ids;%0Aimport org.jboss.hal.testsuite.Random;%0Aimport org.jboss.hal.testsuite.creaper.ResourceVerifier;%0Aimport org.jboss.hal.testsuite.fragment.FormFragment;%0Aimport org.jboss.hal.testsuite.fragment.TableFragment;%0Aimport org.jboss.hal.testsuite.fragment.finder.ColumnFragment;%0Aimport org.jboss.hal.testsuite.fragment.finder.FinderPath;%0Aimport org.jboss.hal.testsuite.util.ServerEnvironmentUtils
 ;%0Aim
@@ -1026,20 +1026,164 @@
 nstants.
-NA
 ME
+SSAGING_ACTIVEMQ;%0Aimport static org.jboss.hal.dmr.ModelDescriptionConstants.NAME;%0Aimport static org.jboss.hal.dmr.ModelDescriptionConstants.PAUSED
 ;%0Aimport
@@ -1837,16 +1837,107 @@
 Address;
+%0Aimport static org.jboss.hal.testsuite.fragment.finder.FinderFragment.runtimeSubsystemPath;
 %0A%0A@RunWi
@@ -2025,16 +2025,126 @@
 Test %7B%0A%0A
+    private static final ServerEnvironmentUtils serverEnvironmentUtils = new ServerEnvironmentUtils(client);%0A%0A
     @Bef
@@ -4324,10 +4324,747 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void pause() throws Exception %7B%0A        FinderPath path = runtimeSubsystemPath(serverEnvironmentUtils.getServerHostName(), MESSAGING_ACTIVEMQ)%0A                .append(Ids.MESSAGING_SERVER_RUNTIME, Ids.messagingServer(SRV_UPDATE));%0A        String itemId = Ids.destination(null, null, SRV_UPDATE, %22jms-topic%22,%0A                JMSTOPIC_UPDATE);%0A%0A        ColumnFragment column = console.finder(NameTokens.RUNTIME, path)%0A                .column(Ids.MESSAGING_SERVER_DESTINATION_RUNTIME);%0A        column.selectItem(itemId)%0A                .dropdown()%0A                .click(%22Pause%22);%0A%0A        new ResourceVerifier(jmsTopicAddress(SRV_UPDATE, JMSTOPIC_UPDATE), client)%0A                .verifyAttribute(PAUSED, true);%0A    %7D%0A
 %7D%0A

Add test to pause a JMS topic