/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.ignite.internal.processors.cache;

import org.apache.ignite.*;
import org.apache.ignite.cache.*;
import org.apache.ignite.cache.affinity.*;
import org.apache.ignite.cache.affinity.consistenthash.*;
import org.apache.ignite.cache.affinity.fair.*;
import org.apache.ignite.cache.affinity.rendezvous.*;
import org.apache.ignite.configuration.*;
import org.apache.ignite.internal.*;
import org.apache.ignite.testframework.junits.common.*;

import static org.apache.ignite.cache.CacheDistributionMode.*;

/**
 * Tests affinity assignment for different affinity types.
 */
public class IgniteClientAffinityAssignmentSelfTest extends GridCommonAbstractTest {
    /** */
    public static final int PARTS = 256;

    /** */
    private boolean client;

    /** */
    private boolean cache;

    /** */
    private int aff;

    /** {@inheritDoc} */
    @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception {
        IgniteConfiguration cfg = super.getConfiguration(gridName);

        if (cache) {
            CacheConfiguration ccfg = new CacheConfiguration();

            ccfg.setCacheMode(CacheMode.PARTITIONED);
            ccfg.setBackups(1);
            ccfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
            ccfg.setDistributionMode(client ? CLIENT_ONLY : PARTITIONED_ONLY);

            if (aff == 0)
                ccfg.setAffinity(new CacheConsistentHashAffinityFunction(false, PARTS));
            else if (aff == 1)
                ccfg.setAffinity(new CacheRendezvousAffinityFunction(false, PARTS));
            else
                ccfg.setAffinity(new CachePartitionFairAffinity(PARTS));

            cfg.setCacheConfiguration(ccfg);
        }

        return cfg;
    }

    /**
     * @throws Exception If failed.
     */
    public void testConsistentHashAssignment() throws Exception {
        aff = 0;

        checkAffinityFunction();
    }

    /**
     * @throws Exception If failed.
     */
    public void testRendezvousAssignment() throws Exception {
        aff = 1;

        checkAffinityFunction();
    }

    /**
     * @throws Exception If failed.
     */
    public void testFairAssignment() throws Exception {
        aff = 2;

        checkAffinityFunction();
    }

    /**
     * @throws Exception If failed.
     */
    private void checkAffinityFunction() throws Exception {
        cache = true;

        startGrids(3);

        try {
            checkAffinity();

            client = true;

            startGrid(3);

            checkAffinity();

            startGrid(4);

            checkAffinity();

            cache = false;

            startGrid(5);

            checkAffinity();

            stopGrid(5);

            checkAffinity();

            stopGrid(4);

            checkAffinity();

            stopGrid(3);

            checkAffinity();
        }
        finally {
            stopAllGrids();
        }
    }

    /**
     * @throws Exception If failed.
     */
    private void checkAffinity() throws Exception {
        CacheAffinity<Object> aff = ((IgniteKernal)grid(0)).cache(null).affinity();

        for (Ignite grid : Ignition.allGrids()) {
            try {
                if (grid.cluster().localNode().id().equals(grid(0).localNode().id()))
                    continue;

                CacheAffinity<Object> checkAff = ((IgniteKernal)grid).cache(null).affinity();

                for (int p = 0; p < PARTS; p++)
                    assertEquals(aff.mapPartitionToPrimaryAndBackups(p), checkAff.mapPartitionToPrimaryAndBackups(p));
            }
            catch (IllegalArgumentException ignored) {
                // Skip the node without cache.
            }
        }
    }
}

@@ -960,66 +960,8 @@
 .*;%0A
-import org.apache.ignite.cache.affinity.consistenthash.*;%0A
 impo
@@ -2117,128 +2117,8 @@
  0)%0A
-                ccfg.setAffinity(new CacheConsistentHashAffinityFunction(false, PARTS));%0A            else if (aff == 1)%0A
     
@@ -2194,24 +2194,24 @@
 e, PARTS));%0A
+
             
@@ -2448,22 +2448,18 @@
 test
-ConsistentHash
+Rendezvous
 Assi
@@ -2548,180 +2548,8 @@
  %7D%0A%0A
-    /**%0A     * @throws Exception If failed.%0A     */%0A    public void testRendezvousAssignment() throws Exception %7B%0A        aff = 1;%0A%0A        checkAffinityFunction();%0A    %7D%0A%0A
     
@@ -2666,17 +2666,17 @@
   aff = 
-2
+1
 ;%0A%0A     

remove consistent hash from IgniteClientAffinityAssignmentSelfTest.