package org.jboss.hal.testsuite.test.configuration.messaging.providerSettings;

import org.apache.commons.lang.RandomStringUtils;
import org.jboss.arquillian.graphene.page.Page;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.hal.testsuite.category.Shared;
import org.jboss.hal.testsuite.page.config.MessagingPage;
import org.jboss.hal.testsuite.test.configuration.messaging.AbstractMessagingTestCase;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.wildfly.extras.creaper.core.online.operations.Address;
import org.wildfly.extras.creaper.core.online.operations.OperationException;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

@RunWith(Arquillian.class)
@Category(Shared.class)
public class JournalTestCase extends AbstractMessagingTestCase {
    private static final String SERVER_NAME = "test-provider_" + RandomStringUtils.randomAlphanumeric(5);

    private static final Address SERVER_ADDRESS = MESSAGING_SUBSYSTEM.and("server", SERVER_NAME);

    @BeforeClass
    public static void setUp() throws IOException {
        operations.add(SERVER_ADDRESS);
    }

    @AfterClass
    public static void tearDown() throws IOException, OperationException {
        operations.removeIfExists(SERVER_ADDRESS);
    }

    @Page
    private MessagingPage page;

    @Before
    public void before() {
        page.selectProvider(SERVER_NAME);
        page.InvokeProviderSettings();
        page.switchToJournalTab();
    }

    @After
    public void after() throws InterruptedException, TimeoutException, IOException {
        administration.reloadIfRequired();
    }

    @Test
    public void updateJournalType() throws Exception {
        selectOptionAndVerify(SERVER_ADDRESS, "journal-type", "NIO");
    }

    @Test
    public void updateCreateJournalDir() throws Exception {
        editCheckboxAndVerify(SERVER_ADDRESS, "create-journal-dir", false);
    }

    @Test
    public void updateJournalSyncNonTransactional() throws Exception {
        editCheckboxAndVerify(SERVER_ADDRESS, "journal-sync-non-transactional", false);
    }

    @Test
    public void updateJournalSyncTransactional() throws Exception {
        editCheckboxAndVerify(SERVER_ADDRESS, "journal-sync-transactional", false);
    }

    @Test
    public void updateJournalMinFilesInvalid() throws Exception {
        verifyIfErrorAppears("journal-min-files", "0");
    }

    @Test
    public void updateJournalMaxFilesWrongValue() {
        verifyIfErrorAppears("journal-max-io", "-1");
    }

    @Test
    public void updateJournalBufferSize() throws Exception {
        editTextAndVerify(SERVER_ADDRESS, "journal-buffer-size", 1024L);
        undefineAndVerify(SERVER_ADDRESS, "journal-buffer-size");
    }

    @Test
    public void updateJournalBufferTimeout() throws Exception {
        editTextAndVerify(SERVER_ADDRESS, "journal-buffer-timeout", 1L);
    }

    @Test
    public void updateJournalCompactMinFiles() throws Exception {
        editTextAndVerify(SERVER_ADDRESS, "journal-compact-min-files", 1);
    }

    @Test
    public void updateJournalCompactPercentage() throws Exception {
        editTextAndVerify(SERVER_ADDRESS, "journal-compact-percentage", 99);
    }

    @Test
    public void updateJournalFileSize() throws Exception {
        editTextAndVerify(SERVER_ADDRESS, "journal-file-size", 4296L);
    }
}

@@ -767,16 +767,81 @@
 ception;
+%0Aimport org.wildfly.extras.creaper.core.online.operations.Values;
 %0A%0Aimport
@@ -1332,32 +1332,241 @@
 ERVER_ADDRESS);%0A
+        Address journalPathAddress = SERVER_ADDRESS.and(%22path%22, %22journal-directory%22);%0A        operations.add(journalPathAddress, Values.of(%22path%22, %22test-journal%22).and(%22relative-to%22, %22jboss.server.data.dir%22));%0A
     %7D%0A%0A    @Afte
@@ -3782,17 +3782,17 @@
 size%22, 4
-2
+0
 96L);%0A  

Fix harm of default messaging server journal files by JournalTestCase.