/*
 * 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.spi.impl.operationservice.impl;

import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.Partition;
import com.hazelcast.map.impl.MapService;
import com.hazelcast.map.impl.operation.GetOperation;
import com.hazelcast.nio.Address;
import com.hazelcast.nio.Connection;
import com.hazelcast.nio.ConnectionListener;
import com.hazelcast.nio.EndpointManager;
import com.hazelcast.nio.Packet;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.spi.InvocationBuilder;
import com.hazelcast.spi.Operation;
import com.hazelcast.test.HazelcastSerialClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;

import java.util.Collection;

@RunWith(HazelcastSerialClassRunner.class)
@Category(QuickTest.class)
public class Invocation_EndpointManagerTest
        extends HazelcastTestSupport {

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

    private HazelcastInstance hz1;
    private HazelcastInstance hz2;

    @After
    public void tearDown() {
        if (hz1 != null) {
            hz1.getLifecycleService().terminate();
        }
        if (hz2 != null) {
            hz2.getLifecycleService().terminate();
        }
    }

    @Test
    public void testInvocation_whenEndpointManagerIsNoop() {
        hz1 = Hazelcast.newHazelcastInstance(advancedNetworkConfig());
        hz2 = Hazelcast.newHazelcastInstance(advancedNetworkConfig());

        String key = generateKeyOwnedBy(hz2);
        Data dataKey = getSerializationService(hz1).toData(key);
        Partition partition = hz1.getPartitionService().getPartition(key);
        Operation op = new GetOperation("test", dataKey);
        InvocationBuilder builder = getNodeEngineImpl(hz1).getOperationService().createInvocationBuilder(MapService.SERVICE_NAME,
                op, partition.getPartitionId());
        builder.setEndpointManager(new NoopEndpointManager());
        expected.expect(UnsupportedOperationException.class);
        builder.invoke().join();
    }

    private Config advancedNetworkConfig() {
        Config config = new Config();
        config.getAdvancedNetworkConfig().setEnabled(true);
        return config;
    }

    private Operation prepareRemoteOperation(HazelcastInstance target) {
        String key = generateKeyOwnedBy(target);
        Data dataKey = getSerializationService(target).toData(key);
        return new GetOperation("test", dataKey);
    }

    class NoopEndpointManager implements EndpointManager {

        @Override
        public Collection getConnections() {
            return null;
        }

        @Override
        public Collection getActiveConnections() {
            return null;
        }

        @Override
        public boolean registerConnection(Address address, Connection connection) {
            return false;
        }

        @Override
        public Connection getConnection(Address address) {
            return null;
        }

        @Override
        public Connection getOrConnect(Address address) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Connection getOrConnect(Address address, boolean silent) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean transmit(Packet packet, Connection connection) {
            return false;
        }

        @Override
        public boolean transmit(Packet packet, Address target) {
            return false;
        }

        @Override
        public void addConnectionListener(ConnectionListener listener) {

        }

        @Override
        public void accept(Object o) {

        }
    }
}

@@ -679,81 +679,8 @@
 l;%0A%0A
-import com.hazelcast.config.Config;%0Aimport com.hazelcast.core.Hazelcast;%0A
 impo
@@ -1196,37 +1196,39 @@
 t.test.Hazelcast
-Seria
+Paralle
 lClassRunner;%0Aim
@@ -1273,16 +1273,123 @@
 upport;%0A
+import com.hazelcast.test.TestHazelcastInstanceFactory;%0Aimport com.hazelcast.test.annotation.ParallelTest;%0A
 import c
@@ -1432,32 +1432,8 @@
 st;%0A
-import org.junit.After;%0A
 impo
@@ -1649,21 +1649,23 @@
 azelcast
-Seria
+Paralle
 lClassRu
@@ -1686,16 +1686,17 @@
 ategory(
+%7B
 QuickTes
@@ -1702,16 +1702,37 @@
 st.class
+, ParallelTest.class%7D
 )%0Apublic
@@ -1902,288 +1902,50 @@
 ate 
-HazelcastInstance hz1;%0A    private HazelcastInstance hz2;%0A%0A    @After%0A    public void tearDown() %7B%0A        if (hz1 != null) %7B%0A            hz1.getLifecycleService().terminate();%0A        %7D%0A        if (hz2 != null) %7B%0A            hz2.getLifecycleService().terminate();%0A        %7D%0A    %7D
+static final String EXPECTED_MSG = %22NOOP%22;
 %0A%0A  
@@ -2025,14 +2025,12 @@
     
-hz1 = 
+Test
 Haze
@@ -2038,53 +2038,65 @@
 cast
-.newHazelcastInstance(advancedNetworkConfig()
+InstanceFactory factory = createHazelcastInstanceFactory(
 );%0A 
@@ -2105,14 +2105,8 @@
     
- hz2 =
  Haz
@@ -2115,53 +2115,116 @@
 cast
-.newHazelcastInstance(advancedNetworkConfig()
+Instance hz1 = factory.newHazelcastInstance();%0A        HazelcastInstance hz2 = factory.newHazelcastInstance(
 );%0A%0A
@@ -2405,24 +2405,25 @@
 ition(key);%0A
+%0A
         Oper
@@ -2501,16 +2501,32 @@
  builder
+%0A               
  = getNo
@@ -2564,16 +2564,57 @@
 ervice()
+%0A                                        
 .createI
@@ -2654,32 +2654,16 @@
 CE_NAME,
-%0A               
  op, par
@@ -2825,450 +2825,76 @@
     
-builder.invoke().join();%0A    %7D%0A%0A    private Config advancedNetworkConfig() %7B%0A        Config config = new Config();%0A        config.getAdvancedNetworkConfig().setEnabled(true);%0A        return config;%0A    %7D%0A%0A    private Operation prepareRemoteOperation(HazelcastInstance target) %7B%0A        String key = generateKeyOwnedBy(target);%0A        Data dataKey = getSerializationService(target).toData(key);%0A        return new GetOperation(%22test%22, dataKey
+expected.expectMessage(EXPECTED_MSG);%0A        builder.invoke().join(
 );%0A 
@@ -3535,32 +3535,44 @@
 rationException(
+EXPECTED_MSG
 );%0A        %7D%0A%0A  
@@ -3713,16 +3713,28 @@
 ception(
+EXPECTED_MSG
 );%0A     

Improve NOOP endpoint-manager invocation test (#14593)