@Test
@SuppressWarnings("checkstyle:IllegalCatch")
public void testMakeLeaderLocal() throws Exception {

    final FiniteDuration timeout = new FiniteDuration(5, TimeUnit.SECONDS);
    final ActorRef localShardRef = mock(ActorRef.class);
    final Future<ActorRef> localShardRefFuture = Futures.successful(localShardRef);
    doReturn(localShardRefFuture).when(context).findLocalShardAsync(any());
    // MakeLeaderLocal will reply with success
    doReturn(Futures.successful(null)).when(context).executeOperationAsync((ActorRef) any(), any(), any());
    doReturn(getSystem().dispatcher()).when(context).getClientDispatcher();
    assertEquals(waitOnAsyncTask(shardAccess.makeLeaderLocal(), timeout), null);
    // MakeLeaderLocal will reply with failure
    doReturn(Futures.failed(new LeadershipTransferFailedException("Failure")))
            .when(context).executeOperationAsync((ActorRef) any(), any(), any());
    try {
        waitOnAsyncTask(shardAccess.makeLeaderLocal(), timeout);
        fail("makeLeaderLocal operation should not be successful");
    } catch (final Exception e) {
        assertTrue(e instanceof LeadershipTransferFailedException);
    }
    // we don't even find local shard
    doReturn(Futures.failed(new LocalShardNotFoundException("Local shard not found")))
            .when(context).findLocalShardAsync(any());
    try {
        waitOnAsyncTask(shardAccess.makeLeaderLocal(), timeout);
        fail("makeLeaderLocal operation should not be successful");
    } catch (final Exception e) {
        assertTrue(e instanceof LeadershipTransferFailedException);
        assertTrue(e.getCause() instanceof LocalShardNotFoundException);
    }
    // closed shard access should throw IllegalStateEx
    shardAccess.close();
    try {
        shardAccess.makeLeaderLocal();
        fail("Should have thrown IllegalStateEx. ShardAccess is closed");
    } catch (final Exception e) {
        assertTrue(e instanceof IllegalStateException);
    }
}