/*
 * Copyright 2015 Red Hat Inc., and individual contributors
 *
 * 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 org.jgroups.protocols.azure;

import org.jgroups.JChannel;
import org.jgroups.util.Util;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;

import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * Functional tests for AZURE_PING discovery.
 *
 * @author Radoslav Husar
 */
public class AZURE_PINGDiscoveryTest {

    public static final int CHANNEL_COUNT = 5;

    // The cluster names need to randomized so that multiple test runs can be run in parallel with the same
    // credentials (e.g. running JDK8 and JDK9 on the CI).
    public static final String RANDOM_CLUSTER_NAME = UUID.randomUUID().toString();

    @Test
    public void testDiscovery() throws Exception {
        assumeCredentials();

        discover(RANDOM_CLUSTER_NAME);
    }

    @Test
    public void testDiscoveryObscureClusterName() throws Exception {
        assumeCredentials();

        String obscureClusterName = "``\\//--+ěščřžýáíé==''!@#$%^&*()_{}<>?";
        discover(obscureClusterName + RANDOM_CLUSTER_NAME);
    }

    private static void assumeCredentials() {
        Assume.assumeTrue("Credentials are not available, test is ignored!", System.getProperty("azure.access_key") != null && System.getProperty("azure.account_name") != null);
    }

    private void discover(String clusterName) throws Exception {
        List<JChannel> channels = create(clusterName);

        Thread.sleep(TimeUnit.SECONDS.toMillis(2));

        printViews(channels);

        // Asserts the views are there
        for (JChannel channel : channels) {
            Assert.assertEquals("member count", CHANNEL_COUNT, channel.getView().getMembers().size());
        }

        // Stop all channels
        // n.b. all channels must be closed, only disconnecting all concurrently can leave stale data
        for (JChannel channel : channels) {
            channel.close();
        }
    }

    private List<JChannel> create(String clusterName) throws Exception {
        List<JChannel> result = new LinkedList<>();
        for (int i = 0; i < CHANNEL_COUNT; i++) {
            JChannel channel = new JChannel("org/jgroups/protocols/azure/tpc-azure.xml");

            channel.connect(clusterName);
            if (i == 0) {
                // Lets be clear about the coordinator
                Util.sleep(1000);
            }
            result.add(channel);
        }
        return result;
    }

    protected static void printViews(List<JChannel> channels) {
        for (JChannel ch : channels) {
            System.out.println("Channel " + ch.getName() + " has view " + ch.getView());
        }
    }
}

@@ -653,16 +653,130 @@
 azure;%0A%0A
+import java.util.LinkedList;%0Aimport java.util.List;%0Aimport java.util.UUID;%0Aimport java.util.concurrent.TimeUnit;%0A%0A
 import o
@@ -904,122 +904,8 @@
 t;%0A%0A
-import java.util.LinkedList;%0Aimport java.util.List;%0Aimport java.util.UUID;%0Aimport java.util.concurrent.TimeUnit;%0A%0A
 /**%0A
@@ -1318,24 +1318,923 @@
 oString();%0A%0A
+    /**%0A     * This is a simple smoke test to run with credentials are available. Verify that the test stack will fail to start%0A     * the channel in %7B@link AZURE_PING#validateConfiguration%7D throwing %7B@link IllegalArgumentException%7D and%0A     * not elsewhere, e.g. missing a protocol after a major jgroups.jar upgrade.%0A     */%0A    @Test(expected = IllegalArgumentException.class)%0A    public void testProtocolStack() throws Exception %7B%0A        JChannel channel = new JChannel(%22org/jgroups/protocols/azure/tpc-azure.xml%22);%0A%0A        channel.getProtocolStack().getProtocols().replaceAll(protocol -%3E %7B%0A            if (protocol instanceof AZURE_PING) %7B%0A                return new AZURE_PING();%0A            %7D else %7B%0A                return protocol;%0A            %7D%0A        %7D);%0A%0A        try %7B%0A            channel.connect(RANDOM_CLUSTER_NAME);%0A        %7D finally %7B%0A            channel.close();%0A        %7D%0A%0A    %7D%0A%0A
     @Test%0A  
@@ -3822,16 +3822,17 @@
   // Let
+'
 s be cle

Add a simple test to verify the rest of the test stack.