/*
 * Copyright (c) 2008-2017, 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.map.impl.nearcache;

import com.hazelcast.config.Config;
import com.hazelcast.config.InMemoryFormat;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.internal.adapter.IMapDataStructureAdapter;
import com.hazelcast.internal.nearcache.AbstractNearCacheBasicTest;
import com.hazelcast.internal.nearcache.NearCache;
import com.hazelcast.internal.nearcache.NearCacheManager;
import com.hazelcast.internal.nearcache.NearCacheTestContext;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.test.HazelcastParametersRunnerFactory;
import com.hazelcast.test.TestHazelcastInstanceFactory;
import com.hazelcast.test.annotation.ParallelTest;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.After;
import org.junit.Before;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

import java.util.Collection;

import static com.hazelcast.internal.nearcache.NearCacheTestUtils.createNearCacheConfig;
import static com.hazelcast.internal.nearcache.NearCacheTestUtils.getMapNearCacheManager;
import static java.util.Arrays.asList;

/**
 * Basic Near Cache tests for {@link IMap} on Hazelcast members.
 */
@RunWith(Parameterized.class)
@Parameterized.UseParametersRunnerFactory(HazelcastParametersRunnerFactory.class)
@Category({ParallelTest.class, QuickTest.class})
public class MapNearCacheBasicTest extends AbstractNearCacheBasicTest<Data, String> {

    @Parameter
    public InMemoryFormat inMemoryFormat;

    private final TestHazelcastInstanceFactory hazelcastFactory = createHazelcastInstanceFactory(2);

    @Parameters(name = "format:{0}")
    public static Collection<Object[]> parameters() {
        return asList(new Object[][]{
                {InMemoryFormat.BINARY},
                {InMemoryFormat.OBJECT},
        });
    }

    @Before
    public void setUp() {
        nearCacheConfig = createNearCacheConfig(inMemoryFormat)
                .setCacheLocalEntries(true);
    }

    @After
    public void tearDown() {
        hazelcastFactory.shutdownAll();
    }

    @Override
    protected <K, V> NearCacheTestContext<K, V, Data, String> createContext() {
        Config config = getConfig();
        config.getMapConfig(DEFAULT_NEAR_CACHE_NAME).setNearCacheConfig(nearCacheConfig);

        HazelcastInstance[] instances = hazelcastFactory.newInstances(config);
        HazelcastInstance member = instances[0];
        IMap<K, V> map = member.getMap(DEFAULT_NEAR_CACHE_NAME);

        NearCacheManager nearCacheManager = getMapNearCacheManager(member);
        NearCache<Data, String> nearCache = nearCacheManager.getNearCache(DEFAULT_NEAR_CACHE_NAME);

        return new NearCacheTestContext<K, V, Data, String>(
                getSerializationService(member),
                member,
                new IMapDataStructureAdapter<K, V>(map),
                nearCacheConfig,
                true,
                nearCache,
                nearCacheManager);
    }
}

@@ -2383,17 +2383,16 @@
 Factory(
-2
 );%0A%0A    
@@ -2973,16 +2973,29 @@
 g config
+WithNearCache
  = getCo
@@ -3016,16 +3016,29 @@
   config
+WithNearCache
 .getMapC
@@ -3087,16 +3087,21 @@
 eConfig(
+this.
 nearCach
@@ -3140,20 +3140,26 @@
 ance
-%5B%5D i
+ nearCacheI
 nstance
-s
  = h
@@ -3177,24 +3177,33 @@
 tory.new
+Hazelcast
 Instance
 s(config
@@ -3198,16 +3198,28 @@
 ance
-s
 (config
+WithNearCache
 );%0A 
@@ -3247,62 +3247,202 @@
 nce 
-member = instances%5B0%5D;%0A        IMap%3CK, V%3E map = member
+dataInstance = hazelcastFactory.newHazelcastInstance(getConfig());%0A%0A        IMap%3CK, V%3E nearCacheMap = nearCacheInstance.getMap(DEFAULT_NEAR_CACHE_NAME);%0A        IMap%3CK, V%3E dataMap = dataInstance
 .get
@@ -3539,22 +3539,33 @@
 Manager(
-member
+nearCacheInstance
 );%0A     
@@ -3761,22 +3761,33 @@
 Service(
-member
+nearCacheInstance
 ),%0A     
@@ -3797,22 +3797,129 @@
         
-member
+nearCacheInstance,%0A                dataInstance,%0A                new IMapDataStructureAdapter%3CK, V%3E(nearCacheMap)
 ,%0A      
@@ -3963,17 +3963,21 @@
 r%3CK, V%3E(
-m
+dataM
 ap),%0A   

Create a distinct config for HZ instances in MapNearCacheBasicTest