/*
 * Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved.
 *
 * 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 com.hazelcast.jet;

import com.hazelcast.client.HazelcastClient;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.jet.config.JetClientConfig;
import com.hazelcast.jet.core.JetTestSupport;
import com.hazelcast.jet.core.TestProcessors;
import com.hazelcast.test.HazelcastSerialClassRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;

@RunWith(HazelcastSerialClassRunner.class)
public class MulticastDiscoveryTest extends JetTestSupport {

    private static final String UNABLE_TO_CONNECT_MESSAGE = "Unable to connect";

    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    @Before
    public void before() {
        TestProcessors.reset(1);
    }

    @After
    public void after() {
        Hazelcast.shutdownAll();
        Jet.shutdownAll();
    }

    @Test
    public void when_twoJetInstancesCreated_then_clusterOfTwoShouldBeFormed() {
        JetInstance instance1 = Jet.newJetInstance();
        JetInstance instance2 = Jet.newJetInstance();

        assertEquals(2, instance1.getCluster().getMembers().size());
    }

    @Test
    public void when_twoJetAndTwoHzInstancesCreated_then_twoClustersOfTwoShouldBeFormed() {
        JetInstance jetInstance1 = Jet.newJetInstance();
        JetInstance jetInstance2 = Jet.newJetInstance();

        HazelcastInstance hazelcastInstance1 = Hazelcast.newHazelcastInstance();
        HazelcastInstance hazelcastInstance2 = Hazelcast.newHazelcastInstance();

        assertEquals(2, jetInstance1.getCluster().getMembers().size());
        assertEquals(2, hazelcastInstance1.getCluster().getMembers().size());
    }

    @Test
    public void when_jetClientCreated_then_connectsToJetCluster() {
        JetInstance jetInstance1 = Jet.newJetInstance();
        JetInstance jetInstance2 = Jet.newJetInstance();

        JetInstance client = Jet.newJetClient(new JetClientConfig());

        assertEquals(2, client.getHazelcastInstance().getCluster().getMembers().size());
    }

    @Test
    public void when_jetClientCreated_then_doesNotConnectToHazelcastCluster() {
        Hazelcast.newHazelcastInstance();

        expectedException.expect(IllegalStateException.class);
        expectedException.expectMessage(UNABLE_TO_CONNECT_MESSAGE);
        Jet.newJetClient();
    }

    @Test
    public void when_hazelcastClientCreated_then_doesNotConnectToJetCluster() {
        Jet.newJetInstance();

        expectedException.expect(IllegalStateException.class);
        expectedException.expectMessage(UNABLE_TO_CONNECT_MESSAGE);
        HazelcastClient.newHazelcastClient();
    }
    }

@@ -693,16 +693,72 @@
 Client;%0A
+import com.hazelcast.client.config.ClientNetworkConfig;%0A
 import c
@@ -782,24 +782,24 @@
 .Hazelcast;%0A
-
 import com.h
@@ -972,16 +972,50 @@
 essors;%0A
+import com.hazelcast.nio.Address;%0A
 import c
@@ -2730,32 +2730,204 @@
 JetInstance();%0A%0A
+        // Configure client with the address of the created instance%0A        // Sometimes the instances are created with a different port number%0A        // like 5704, 5705%0A
         JetInsta
@@ -2960,16 +2960,9 @@
 ent(
-new JetC
+c
 lien
@@ -2969,16 +2969,42 @@
 tConfig(
+jetInstance1, jetInstance2
 ));%0A%0A   
@@ -3699,14 +3699,579 @@
 ;%0A    %7D%0A
+%0A
     
+/**%0A     * Creates a client config which configured with the address of the jet%0A     * instances provided.%0A     */%0A    private static JetClientConfig clientConfig(JetInstance... jetInstances) %7B%0A        JetClientConfig jetClientConfig = new JetClientConfig();%0A        ClientNetworkConfig networkConfig = jetClientConfig.getNetworkConfig();%0A        for (JetInstance jet : jetInstances) %7B%0A            Address address = getAddress(jet);%0A            networkConfig.addAddress(address.getHost() + %22:%22 + address.getPort());%0A        %7D%0A        return jetClientConfig;%0A    %7D%0A
 %7D%0A

set the address of the created instance to the client