package io.enmasse.systemtest.marathon;

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

import java.util.concurrent.atomic.AtomicInteger;

public class AuthenticationTest extends MarathonTestBase {
    @Test
    public void testCreateDeleteUsersLong() throws Exception {
        AddressSpace addressSpace = new AddressSpace("test-create-delete-users-brokered",
                AddressSpaceType.BROKERED);
        createAddressSpace(addressSpace, "none");
        Logging.log.info("Address space created");

        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");

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

        AtomicInteger from = new AtomicInteger(0);
        AtomicInteger to = new AtomicInteger(0);
        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());
            from.set(from.get() + iteration);
            to.set(to.get() + iteration);
        });
    }

    private void createUsers(AddressSpace addressSpace, String prefixName, String prefixPswd, int from, int to)
            throws Exception {
        KeycloakCredentials user;
        for (int i = from; i < to; i++) {
            user = new KeycloakCredentials(prefixName + i, prefixPswd + i);
            getKeycloakClient().createUser(addressSpace.getName(), user.getUsername(), user.getPassword());
        }
    }

    private void removeUsers(AddressSpace addressSpace, String prefixName, int from, int to) throws Exception {
        for (int i = from; i < to; i++) {
            getKeycloakClient().deleteUser(addressSpace.getName(), prefixName + i);
        }
    }

    private void doBasicAuthQueueTopicTest(AddressSpace addressSpace, Destination queue, Destination topic,
                                           String uname, String password) throws Exception {
        AmqpClient queueClient = amqpClientFactory.createQueueClient(addressSpace);
        queueClient.getConnectOptions().setUsername(uname).setPassword(password);
        io.enmasse.systemtest.standard.QueueTest.runQueueTest(queueClient, queue, 100);

        AmqpClient topicClient = amqpClientFactory.createTopicClient(addressSpace);
        topicClient.getConnectOptions().setUsername(uname).setPassword(password);
        TopicTest.runTopicTest(topicClient, topic, 100);
    }
}

@@ -360,32 +360,93 @@
 ows Exception %7B%0A
+        Logging.log.info(%22testCreateDeleteUsersLong start%22);%0A
         AddressS
@@ -609,12 +609,16 @@
 e, %22
-none
+standard
 %22);%0A
@@ -657,24 +657,28 @@
 s space 
+'%7B%7D'
 created%22
 );%0A%0A    
@@ -661,32 +661,46 @@
 ace '%7B%7D'created%22
+, addressSpace
 );%0A%0A        Dest
@@ -935,24 +935,75 @@
 dresses 
-created%22
+'%7B%7D', '%7B%7D' created%22, queue.getAddress(), topic.getAddress()
 );%0A%0A    
@@ -1906,16 +1906,80 @@
     %7D);%0A
+        Logging.log.info(%22testCreateDeleteUsersLong finished%22);%0A
     %7D%0A%0A 

FIX marathon AuthTests - fixed authService from none to standard