/*
 * Copyright 2016 Red Hat Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package enmasse.systemtest;

import enmasse.systemtest.amqp.AmqpClient;
import enmasse.systemtest.amqp.TerminusFactory;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.PemTrustOptions;
import io.vertx.proton.ProtonClientOptions;
import org.junit.After;
import org.junit.Before;

import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;

public abstract class AmqpTestBase {
    protected Vertx vertx;
    protected AddressApiClient addressApiClient;
    protected Environment environment = new Environment();
    protected OpenShift openShift;

    protected abstract String getInstanceName();

    @Before
    public void setup() throws Exception {
        vertx = Vertx.vertx();
        openShift = new OpenShift(environment, environment.isMultitenant() ? getInstanceName().toLowerCase() : environment.namespace());
        addressApiClient = new AddressApiClient(vertx, openShift.getRestEndpoint(), environment.isMultitenant());
        addressApiClient.deployInstance(getInstanceName().toLowerCase());
    }

    @After
    public void teardown() throws Exception {
        cleanup();
        addressApiClient.close();
        vertx.close();
    }

    private void cleanup() throws Exception {
        deploy();
    }

    protected void deploy(Destination ... destinations) throws Exception {
        TimeoutBudget budget = new TimeoutBudget(5, TimeUnit.MINUTES);
        TestUtils.deploy(addressApiClient, openShift, budget, getInstanceName().toLowerCase(), destinations);
    }

    protected void scale(Destination destination, int numReplicas) throws Exception {
        TimeoutBudget budget = new TimeoutBudget(5, TimeUnit.MINUTES);
        TestUtils.setReplicas(openShift, destination, numReplicas, budget);
        if (destination.isStoreAndForward() && !destination.isMulticast()) {
            TestUtils.waitForAddress(openShift, destination.getAddress(), budget);
        }
    }

    protected AmqpClient createQueueClient() throws UnknownHostException {
        return createClient(new QueueTerminusFactory());
    }

    protected AmqpClient createTopicClient() throws UnknownHostException {
        return createClient(new TopicTerminusFactory());
    }

    protected AmqpClient createDurableTopicClient() throws UnknownHostException {
        return createClient(new DurableTopicTerminusFactory());
    }

    protected AmqpClient createClient(TerminusFactory terminusFactory) throws UnknownHostException {
        ProtonClientOptions options = new ProtonClientOptions();
        if (environment.useTLS()) {
            options.setSsl(true);
            options.setHostnameVerificationAlgorithm("");
            options.setPemTrustOptions(new PemTrustOptions().addCertValue(Buffer.buffer(environment.messagingCert())));
          //  options.setSniServerName(openShift.getRouteHost());
            options.setTrustAll(true);
            return createClient(terminusFactory, options, openShift.getSecureEndpoint());
        } else {
            return createClient(terminusFactory, options, openShift.getInsecureEndpoint());
        }
    }

    protected AmqpClient createClient(TerminusFactory terminusFactory, ProtonClientOptions options, Endpoint endpoint) {
        return new AmqpClient(endpoint, terminusFactory);
    }
}

@@ -947,16 +947,67 @@
 eption;%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A
 import j
@@ -1036,16 +1036,16 @@
 meUnit;%0A
-
 %0Apublic 
@@ -1243,16 +1243,80 @@
 enShift;
+%0A    private final List%3CAmqpClient%3E clients = new ArrayList%3C%3E();
 %0A%0A    pr
@@ -1406,32 +1406,57 @@
 ows Exception %7B%0A
+        clients.clear();%0A
         vertx = 
@@ -1908,24 +1908,24 @@
 nt.close();%0A
-
         vert
@@ -1931,24 +1931,131 @@
 tx.close();%0A
+        for (AmqpClient client : clients) %7B%0A            client.close();%0A        %7D%0A        clients.clear();%0A
     %7D%0A%0A    p

Clean up clients between test runs