package io.enmasse.systemtest.marathon;

import io.enmasse.systemtest.*;
import io.enmasse.systemtest.amqp.AmqpClient;
import io.enmasse.systemtest.amqp.AmqpClientFactory;
import io.enmasse.systemtest.standard.TopicTest;
import org.junit.Test;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.Assert.assertFalse;

public class AuthenticationTest extends MarathonTestBase {

//    @Test disabled due to issue: #520
    public void testCreateDeleteUsersLong() throws Exception {
        Logging.log.info("testCreateDeleteUsersLong start");
        AddressSpace addressSpace = new AddressSpace("test-create-delete-users-brokered",
                AddressSpaceType.BROKERED);
        createAddressSpace(addressSpace, "standard");
        Logging.log.info("Address space '{}'created", addressSpace);

        Destination queue = Destination.queue("test-create-delete-users-queue");
        Destination topic = Destination.topic("test-create-delete-users-topic");
        setAddresses(addressSpace, queue, topic);
        Logging.log.info("Addresses '{}', '{}' created", queue.getAddress(), topic.getAddress());

        final String prefixUser = "test-user";
        final String prefixPswd = "test-user";

        AtomicInteger from = new AtomicInteger(0);
        AtomicInteger to = new AtomicInteger(100);
        int iteration = 100;
        int step = 10;

        runTestInLoop(30, () -> {
            createUsers(addressSpace, prefixUser, prefixPswd, from.get(), to.get());
            Logging.log.info("Users <{};{}> successfully created", from.get(), to.get());
            for (int i = from.get(); i < to.get(); i += step) {
                doBasicAuthQueueTopicTest(addressSpace, queue, topic, prefixUser + i, prefixPswd + i);
            }
            removeUsers(addressSpace, prefixUser, from.get(), to.get() - step);
            Logging.log.info("Users <{};{}> successfully removed", from.get(), to.get() - step);
            from.set(from.get() + iteration);
            to.set(to.get() + iteration);
            Thread.sleep(5000);
        });
        Logging.log.info("testCreateDeleteUsersLong finished");
    }

    @Test
    public void testAuthSendReceiveLong() throws Exception{
        Logging.log.info("testAuthSendReceiveLong start");
        AddressSpace addressSpace = new AddressSpace("test-auth-send-receive-brokered",
                AddressSpaceType.BROKERED);
        createAddressSpace(addressSpace, "standard");
        Logging.log.info("Address space '{}'created", addressSpace);

        Destination queue = Destination.queue("test-auth-send-receive-queue");
        Destination topic = Destination.topic("test-auth-send-receive-topic");
        setAddresses(addressSpace, queue, topic);
        Logging.log.info("Addresses '{}', '{}' created", queue.getAddress(), topic.getAddress());

        final String username = "test-user";
        final String password = "test-user";

        createUser(addressSpace, username, password);

        runTestInLoop(30, () -> {
            Logging.log.info("Start test loop basic auth tests");
            doBasicAuthQueueTopicTest(addressSpace, queue, topic, username, password);
            assertCannotConnect(addressSpace, "nobody", "nobody", Arrays.asList(queue, topic));
            Thread.sleep(5000);
        });
        Logging.log.info("testAuthSendReceiveLong finished");
    }

    @Test
    public void testCreateDeleteUsersRestartKeyCloakLong() throws Exception {
        Logging.log.info("testCreateDeleteUsersRestartKeyCloakLong start");
        AddressSpace addressSpace = new AddressSpace("test-create-delete-users-restart-brokered",
                AddressSpaceType.BROKERED);
        createAddressSpace(addressSpace, "standard");
        Logging.log.info("Address space '{}'created", addressSpace);

        Destination queue = Destination.queue("test-create-delete-users-restart-queue");
        Destination topic = Destination.topic("test-create-delete-users-restart-topic");
        setAddresses(addressSpace, queue, topic);
        Logging.log.info("Addresses '{}', '{}' created", queue.getAddress(), topic.getAddress());

        final String username = "test-user";
        final String password = "test-user";

        runTestInLoop(30, () -> {
            createUser(addressSpace, username, password);
            doBasicAuthQueueTopicTest(addressSpace, queue, topic, username, password);
            scaleInGlobal("keycloak", 0);
            scaleInGlobal("keycloak", 1);
            doBasicAuthQueueTopicTest(addressSpace, queue, topic, username, password);
            removeUser(addressSpace, username);
        });
        Logging.log.info("testCreateDeleteUsersRestartKeyCloakLong finished");
    }



    protected void createUser(AddressSpace addressSpace, String username, String password) throws Exception{
        getKeycloakClient().createUser(addressSpace.getName(), username, password);
    }

    protected void removeUser(AddressSpace addressSpace, String username) throws Exception{
        getKeycloakClient().deleteUser(addressSpace.getName(), username);
    }

    protected void createUsers(AddressSpace addressSpace, String prefixName, String prefixPswd, int from, int to)
            throws Exception {
        for (int i = from; i < to; i++) {
            createUser(addressSpace, prefixName + i, prefixPswd + i);
        }
    }

    protected void removeUsers(AddressSpace addressSpace, String prefixName, int from, int to) throws Exception {
        for (int i = from; i < to; i++) {
            removeUser(addressSpace, prefixName + i);
        }
    }

    private void doBasicAuthQueueTopicTest(AddressSpace addressSpace, Destination queue, Destination topic,
                                           String uname, String password) throws Exception {
        int messageCount = 100;
        AmqpClientFactory factory = createAmqpClientFactory(addressSpace);
        AmqpClient queueClient = factory.createQueueClient(addressSpace);
        queueClient.getConnectOptions().setUsername(uname).setPassword(password);
        io.enmasse.systemtest.standard.QueueTest.runQueueTest(queueClient, queue, messageCount);
        Logging.log.info("User: '{}'; Message count:'{}'; destination:'{}' - done",
                uname, messageCount, queue.getAddress());

        AmqpClient topicClient = factory.createTopicClient(addressSpace);
        topicClient.getConnectOptions().setUsername(uname).setPassword(password);
        TopicTest.runTopicTest(topicClient, topic, messageCount);
        Logging.log.info("User: '{}'; Message count:'{}'; destination:'{}' - done",
                uname, messageCount, topic.getAddress());
    }
}

@@ -4280,32 +4280,86 @@
 oop(30, () -%3E %7B%0A
+            Logging.log.info(%22Start test iteration%22);%0A
             crea
@@ -4479,32 +4479,82 @@
 ame, password);%0A
+            Logging.log.info(%22Restart keycloak%22);%0A
             scal

Add logging information