/*
 * 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 java.util.LinkedList;
import java.util.List;
import java.util.UUID;

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

/**
 * Functional test for AZURE_PING discovery.
 *
 * @author Radoslav Husar
 * @version Jun 2015
 */
public class AZURE_PINGDiscoveryTest {

    public static final int CHANNEL_COUNT = 5;
    public static final String CLUSTER_NAME = UUID.randomUUID().toString();

    @Test
    public void testDiscovery() throws Exception {

        List<JChannel> channels = create();

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

        printViews(channels);

        // 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() throws Exception {
        List<JChannel> result = new LinkedList<>();
        for (int i = 0; i < CHANNEL_COUNT; i++) {
            JChannel channel = new JChannel(this.getClass().getResource("/org/jgroups/protocols/azure/tpc-azure.xml"));

            channel.connect(CLUSTER_NAME);
            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());
        }
    }
}

@@ -859,16 +859,17 @@
 nal test
+s
  for AZU
@@ -1166,16 +1166,321 @@
 ption %7B%0A
+        discover(CLUSTER_NAME);%0A    %7D%0A%0A    @Test%0A    public void testDiscoveryObscureClusterName() throws Exception %7B%0A        String obscureClusterName = %22%60%60%5C%5C//--+%C4%9B%C5%A1%C4%8D%C5%99%C5%BE%C3%BD%C3%A1%C3%AD%C3%A9==''!@#$%25%5E&*()_%7B%7D%3C%3E?%22;%0A        discover(obscureClusterName);%0A    %7D%0A%0A    private void discover(String clusterName) throws Exception %7B
 %0A       
@@ -1513,16 +1513,27 @@
  create(
+clusterName
 );%0A%0A    
@@ -1966,25 +1966,24 @@
 ;%0A        %7D%0A
-%0A
     %7D%0A%0A    p
@@ -2011,16 +2011,34 @@
  create(
+String clusterName
 ) throws
@@ -2301,28 +2301,27 @@
 connect(
-CLUSTER_NAME
+clusterName
 );%0A     

Add test for obscure cluster names