<commit_before>/*
 * Copyright 2017-2018, EnMasse authors.
 * License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
 */
package io.enmasse.systemtest.bases;

import io.enmasse.systemtest.*;
import io.enmasse.systemtest.amqp.AmqpClientFactory;
import io.enmasse.systemtest.clients.AbstractClient;
import io.enmasse.systemtest.mqtt.MqttClientFactory;
import org.junit.Before;
import org.junit.Rule;
import org.junit.experimental.categories.Category;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.slf4j.Logger;

import java.util.*;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

@Category(SharedAddressSpace.class)
public abstract class TestBaseWithShared extends TestBase {
    private static Logger log = CustomLogger.getLogger();
    private static final String defaultAddressTemplate = "-shared-";
    protected static AddressSpace sharedAddressSpace;
    protected static HashMap<String, AddressSpace> sharedAddressSpaces = new HashMap<>();
    private static Map<AddressSpaceType, Integer> spaceCountMap = new HashMap<>();
    private static final Destination dummyAddress = Destination.queue("dummy-address", "pooled-queue");
    private ArrayList<Destination> allSharedAddresses = new ArrayList<>();

    @Rule
    public TestWatcher watcher = new TestWatcher() {
        @Override
        protected void failed(Throwable e, Description description) {
            log.info("test failed:" + description);
            log.info("shared address space '{}' will be removed", sharedAddressSpace);
            try {
                deleteSharedAddressSpace(sharedAddressSpace);
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                spaceCountMap.put(sharedAddressSpace.getType(), spaceCountMap.get(sharedAddressSpace.getType()) + 1);
            }
        }

        @Override
        protected void succeeded(Description description) {
            try {
                deleteAddresses(sharedAddressSpace, allSharedAddresses.toArray(new Destination[0]));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    private void appendSharedAddresses(Destination... destinations) {
        for (Destination dest : destinations) {
            if (!dest.equals(dummyAddress)) {
                allSharedAddresses.add(dest);
            }
        }
    }

    protected static void deleteSharedAddressSpace(AddressSpace addressSpace) throws Exception {
        sharedAddressSpaces.remove(addressSpace.getName());
        TestBase.deleteAddressSpace(addressSpace);
    }

    protected abstract AddressSpaceType getAddressSpaceType();

    protected abstract boolean skipDummyAddress();

    public AddressSpace getSharedAddressSpace() {
        return sharedAddressSpace;
    }

    @Before
    public void setupShared() throws Exception {
        spaceCountMap.putIfAbsent(getAddressSpaceType(), 0);
        sharedAddressSpace = new AddressSpace(
                getAddressSpaceType().name().toLowerCase() + defaultAddressTemplate + spaceCountMap.get(getAddressSpaceType()),
                getAddressSpaceType(),
                AuthService.STANDARD);
        log.info("Test is running in multitenant mode");
        createSharedAddressSpace(sharedAddressSpace);
        createSharedAddressSpace(sharedAddressSpace, "standard");
        if (environment.useDummyAddress() && !skipDummyAddress()) {
            if (!dummyExists()) {
                log.info("'{}' address doesn't exist and will be created", dummyAddress);
                super.setAddresses(sharedAddressSpace, dummyAddress);
            }
        }

        this.username = "test";
        this.password = "<PASSWORD>";
        getKeycloakClient().createUser(sharedAddressSpace.getName(), username, password, 1, TimeUnit.MINUTES);

        this.managementCredentials = new KeycloakCredentials("artemis-admin", "artemis-admin");
        getKeycloakClient().createUser(sharedAddressSpace.getName(),
                managementCredentials.getUsername(),
                managementCredentials.getPassword(),
                Group.ADMIN.toString(),
                Group.SEND_ALL_BROKERED.toString(),
                Group.RECV_ALL_BROKERED.toString(),
                Group.VIEW_ALL_BROKERED.toString(),
                Group.MANAGE_ALL_BROKERED.toString());

        amqpClientFactory = new AmqpClientFactory(kubernetes, environment, sharedAddressSpace, username, password);
        mqttClientFactory = new MqttClientFactory(kubernetes, environment, sharedAddressSpace, username, password);
    }

    protected void createSharedAddressSpace(AddressSpace addressSpace) throws Exception {
        sharedAddressSpaces.put(addressSpace.getName(), addressSpace);
        super.createAddressSpace(addressSpace);
    }

    protected void scale(Destination destination, int numReplicas) throws Exception {
        scale(sharedAddressSpace, destination, numReplicas);
    }

    /**
     * get all addresses except 'dummy-address'
     */
    protected Future<List<String>> getAddresses(Optional<String> addressName) throws Exception {
        return TestUtils.getAddresses(addressApiClient, sharedAddressSpace, addressName, Arrays.asList(dummyAddress.getAddress()));
    }

    /**
     * check if dummy address exists
     */
    private boolean dummyExists() throws Exception {
        Future<List<String>> addresses = TestUtils.getAddresses(addressApiClient, sharedAddressSpace, Optional.empty(),
                new ArrayList<>());
        List<String> address = addresses.get(20, TimeUnit.SECONDS);
        log.info("found addresses");
        address.stream().forEach(addr -> log.info("- address '{}'", addr));
        log.info("looking for '{}' address", dummyAddress.getAddress());
        return address.contains(dummyAddress.getAddress());
    }

    protected Future<List<Address>> getAddressesObjects(Optional<String> addressName) throws Exception {
        return TestUtils.getAddressesObjects(addressApiClient, sharedAddressSpace, addressName, Arrays.asList(dummyAddress.getAddress()));
    }


    /**
     * delete all addresses except 'dummy-address' and append new addresses
     *
     * @param destinations
     * @throws Exception
     */
    protected void setAddresses(Destination... destinations) throws Exception {
        if (isBrokered(sharedAddressSpace) || !environment.useDummyAddress()) {
            setAddresses(sharedAddressSpace);
        } else {
            deleteAddresses(sharedAddressSpace, allSharedAddresses.toArray(new Destination[0]));
            allSharedAddresses.clear();
            if (destinations.length > 0) {
                appendAddresses(destinations);
            }
        }
    }

    /**
     * append new addresses into address-space and sharedAddresses list
     *
     * @param destinations
     * @throws Exception
     */
    protected void appendAddresses(Destination... destinations) throws Exception {
        appendAddresses(sharedAddressSpace, destinations);
        appendSharedAddresses(destinations);
    }

    /**
     * use DELETE html method for delete specific addresses<commit_msg>FIX SharedBase - set destinations instead of delete all addresses<commit_after>/*
 * Copyright 2017-2018, EnMasse authors.
 * License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
 */
package io.enmasse.systemtest.bases;

import io.enmasse.systemtest.*;
import io.enmasse.systemtest.amqp.AmqpClientFactory;
import io.enmasse.systemtest.clients.AbstractClient;
import io.enmasse.systemtest.mqtt.MqttClientFactory;
import org.junit.Before;
import org.junit.Rule;
import org.junit.experimental.categories.Category;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.slf4j.Logger;

import java.util.*;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

@Category(SharedAddressSpace.class)
public abstract class TestBaseWithShared extends TestBase {
    private static Logger log = CustomLogger.getLogger();
    private static final String defaultAddressTemplate = "-shared-";
    protected static AddressSpace sharedAddressSpace;
    protected static HashMap<String, AddressSpace> sharedAddressSpaces = new HashMap<>();
    private static Map<AddressSpaceType, Integer> spaceCountMap = new HashMap<>();
    private static final Destination dummyAddress = Destination.queue("dummy-address", "pooled-queue");
    private ArrayList<Destination> allSharedAddresses = new ArrayList<>();

    @Rule
    public TestWatcher watcher = new TestWatcher() {
        @Override
        protected void failed(Throwable e, Description description) {
            log.info("test failed:" + description);
            log.info("shared address space '{}' will be removed", sharedAddressSpace);
            try {
                deleteSharedAddressSpace(sharedAddressSpace);
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                spaceCountMap.put(sharedAddressSpace.getType(), spaceCountMap.get(sharedAddressSpace.getType()) + 1);
            }
        }

        @Override
        protected void succeeded(Description description) {
            try {
                deleteAddresses(sharedAddressSpace, allSharedAddresses.toArray(new Destination[0]));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    private void appendSharedAddresses(Destination... destinations) {
        for (Destination dest : destinations) {
            if (!dest.equals(dummyAddress)) {
                allSharedAddresses.add(dest);
            }
        }
    }

    protected static void deleteSharedAddressSpace(AddressSpace addressSpace) throws Exception {
        sharedAddressSpaces.remove(addressSpace.getName());
        TestBase.deleteAddressSpace(addressSpace);
    }

    protected abstract AddressSpaceType getAddressSpaceType();

    protected abstract boolean skipDummyAddress();

    public AddressSpace getSharedAddressSpace() {
        return sharedAddressSpace;
    }

    @Before
    public void setupShared() throws Exception {
        spaceCountMap.putIfAbsent(getAddressSpaceType(), 0);
        sharedAddressSpace = new AddressSpace(
                getAddressSpaceType().name().toLowerCase() + defaultAddressTemplate + spaceCountMap.get(getAddressSpaceType()),
                getAddressSpaceType(),
                AuthService.STANDARD);
        log.info("Test is running in multitenant mode");
        createSharedAddressSpace(sharedAddressSpace);
        createSharedAddressSpace(sharedAddressSpace, "standard");
        if (environment.useDummyAddress() && !skipDummyAddress()) {
            if (!dummyExists()) {
                log.info("'{}' address doesn't exist and will be created", dummyAddress);
                super.setAddresses(sharedAddressSpace, dummyAddress);
            }
        }

        this.username = "test";
        this.password = "<PASSWORD>";
        getKeycloakClient().createUser(sharedAddressSpace.getName(), username, password, 1, TimeUnit.MINUTES);

        this.managementCredentials = new KeycloakCredentials("artemis-admin", "artemis-admin");
        getKeycloakClient().createUser(sharedAddressSpace.getName(),
                managementCredentials.getUsername(),
                managementCredentials.getPassword(),
                Group.ADMIN.toString(),
                Group.SEND_ALL_BROKERED.toString(),
                Group.RECV_ALL_BROKERED.toString(),
                Group.VIEW_ALL_BROKERED.toString(),
                Group.MANAGE_ALL_BROKERED.toString());

        amqpClientFactory = new AmqpClientFactory(kubernetes, environment, sharedAddressSpace, username, password);
        mqttClientFactory = new MqttClientFactory(kubernetes, environment, sharedAddressSpace, username, password);
    }

    protected void createSharedAddressSpace(AddressSpace addressSpace) throws Exception {
        sharedAddressSpaces.put(addressSpace.getName(), addressSpace);
        super.createAddressSpace(addressSpace);
    }

    protected void scale(Destination destination, int numReplicas) throws Exception {
        scale(sharedAddressSpace, destination, numReplicas);
    }

    /**
     * get all addresses except 'dummy-address'
     */
    protected Future<List<String>> getAddresses(Optional<String> addressName) throws Exception {
        return TestUtils.getAddresses(addressApiClient, sharedAddressSpace, addressName, Arrays.asList(dummyAddress.getAddress()));
    }

    /**
     * check if dummy address exists
     */
    private boolean dummyExists() throws Exception {
        Future<List<String>> addresses = TestUtils.getAddresses(addressApiClient, sharedAddressSpace, Optional.empty(),
                new ArrayList<>());
        List<String> address = addresses.get(20, TimeUnit.SECONDS);
        log.info("found addresses");
        address.stream().forEach(addr -> log.info("- address '{}'", addr));
        log.info("looking for '{}' address", dummyAddress.getAddress());
        return address.contains(dummyAddress.getAddress());
    }

    protected Future<List<Address>> getAddressesObjects(Optional<String> addressName) throws Exception {
        return TestUtils.getAddressesObjects(addressApiClient, sharedAddressSpace, addressName, Arrays.asList(dummyAddress.getAddress()));
    }


    /**
     * delete all addresses except 'dummy-address' and append new addresses
     *
     * @param destinations
     * @throws Exception
     */
    protected void setAddresses(Destination... destinations) throws Exception {
        if (isBrokered(sharedAddressSpace) || !environment.useDummyAddress()) {
            setAddresses(sharedAddressSpace, destinations);
        } else {
            deleteAddresses(sharedAddressSpace, allSharedAddresses.toArray(new Destination[0]));
            allSharedAddresses.clear();
            if (destinations.length > 0) {
                appendAddresses(destinations);
            }
        }
    }

    /**
     * append new addresses into address-space and sharedAddresses list
     *
     * @param destinations
     * @throws Exception
     */
    protected void appendAddresses(Destination... destinations) throws Exception {
        appendAddresses(sharedAddressSpace, destinations);
        appendSharedAddresses(destinations);
    }

    /**
     * use DELETE html method for delete specific addresses
{'fraction_non_alphanumeric': 0.09828711256117455, 'fraction_numerical': 0.0028548123980424145, 'mean_word_length': 3.112073784237004, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1119005', 'n_tokens_mistral': 3720, 'n_tokens_neox': 3600, 'n_words': 794}