/*
 * 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.flink.runtime.rpc;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.akka.AkkaUtils;
import org.apache.flink.runtime.concurrent.FutureUtils;
import org.apache.flink.runtime.rpc.akka.AkkaRpcService;
import org.apache.flink.runtime.rpc.akka.AkkaRpcServiceConfiguration;

import java.io.Serializable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

import static org.apache.flink.util.Preconditions.checkNotNull;

/**
 * An RPC Service implementation for testing. This RPC service acts as a replacement for
 * the regular RPC service for cases where tests need to return prepared mock gateways instead of
 * proper RPC gateways.
 * 
 * <p>The TestingRpcService can be used for example in the following fashion,
 * using <i>Mockito</i> for mocks and verification:
 * 
 * <pre>{@code
 * TestingRpcService rpc = new TestingRpcService();
 *
 * ResourceManagerGateway testGateway = mock(ResourceManagerGateway.class);
 * rpc.registerGateway("myAddress", testGateway);
 * 
 * MyComponentToTest component = new MyComponentToTest();
 * component.triggerSomethingThatCallsTheGateway();
 * 
 * verify(testGateway, timeout(1000)).theTestMethod(any(UUID.class), anyString());
 * }</pre>
 */
public class TestingRpcService extends AkkaRpcService {

	/** Map of pre-registered connections */
	private final ConcurrentHashMap<String, RpcGateway> registeredConnections;

	/**
	 * Creates a new {@code TestingRpcService}. 
	 */
	public TestingRpcService() {
		this(new Configuration());
	}

	/**
	 * Creates a new {@code TestingRpcService}, using the given configuration. 
	 */
	public TestingRpcService(Configuration configuration) {
		super(AkkaUtils.createLocalActorSystem(configuration),
			AkkaRpcServiceConfiguration.fromConfiguration(configuration));

		this.registeredConnections = new ConcurrentHashMap<>();
	}

	// ------------------------------------------------------------------------

	@Override
	public CompletableFuture<Void> stopService() {
		final CompletableFuture<Void> terminationFuture = super.stopService();

		terminationFuture.whenComplete(
			(Void ignored, Throwable throwable) -> {
				registeredConnections.clear();
			});

		return terminationFuture;
	}

	// ------------------------------------------------------------------------
	// connections
	// ------------------------------------------------------------------------

	public void registerGateway(String address, RpcGateway gateway) {
		checkNotNull(address);
		checkNotNull(gateway);

		if (registeredConnections.putIfAbsent(address, gateway) != null) {
			throw new IllegalStateException("a gateway is already registered under " + address);
		}
	}

	@Override
	public <C extends RpcGateway> CompletableFuture<C> connect(String address, Class<C> clazz) {
		RpcGateway gateway = registeredConnections.get(address);

		if (gateway != null) {
			if (clazz.isAssignableFrom(gateway.getClass())) {
				@SuppressWarnings("unchecked")
				C typedGateway = (C) gateway;
				return CompletableFuture.completedFuture(typedGateway);
			} else {
				return FutureUtils.completedExceptionally(new Exception("Gateway registered under " + address + " is not of type " + clazz));
			}
		} else {
			return super.connect(address, clazz);
		}
	}

	@Override
	public <F extends Serializable, C extends FencedRpcGateway<F>> CompletableFuture<C> connect(
			String address,
			F fencingToken,
			Class<C> clazz) {
		RpcGateway gateway = registeredConnections.get(address);

		if (gateway != null) {
			if (clazz.isAssignableFrom(gateway.getClass())) {
				@SuppressWarnings("unchecked")
				C typedGateway = (C) gateway;
				return CompletableFuture.completedFuture(typedGateway);
			} else {
				return FutureUtils.completedExceptionally(new Exception("Gateway registered under " + address + " is not of type " + clazz));
			}
		} else {
			return super.connect(address, fencingToken, clazz);
		}
	}

	public void clearGateways() {
		registeredConnections.clear();
	}
}

@@ -1530,18 +1530,17 @@
 ways.%0A *
- 
 %0A
+
  * %3Cp%3ETh
@@ -1663,17 +1663,16 @@
 tion:%0A *
- 
 %0A * %3Cpre
@@ -1858,25 +1858,24 @@
 Gateway);%0A *
- 
 %0A * MyCompon
@@ -1975,17 +1975,16 @@
 ay();%0A *
- 
 %0A * veri
@@ -2169,16 +2169,17 @@
 nections
+.
  */%0A%09pri
@@ -2300,17 +2300,16 @@
 ervice%7D.
- 
 %0A%09 */%0A%09p
@@ -2370,24 +2370,24 @@
 );%0A%09%7D%0A%0A%09/**%0A
+
 %09 * Creates 
@@ -2449,17 +2449,16 @@
 uration.
- 
 %0A%09 */%0A%09p

Fix checkstyle violations in TestingRpcService