/**
 * Copyright 2015 Palantir Technologies
 * <p>
 * Licensed under the BSD-3 License (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://opensource.org/licenses/BSD-3-Clause
 * <p>
 * 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.palantir.atlasdb.keyvalue.cassandra;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.Callable;

import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

import com.google.common.collect.ImmutableSet;
import com.jayway.awaitility.Awaitility;
import com.jayway.awaitility.Duration;
import com.palantir.atlasdb.cassandra.CassandraKeyValueServiceConfigManager;
import com.palantir.atlasdb.cassandra.ImmutableCassandraCredentialsConfig;
import com.palantir.atlasdb.cassandra.ImmutableCassandraKeyValueServiceConfig;
import com.palantir.atlasdb.config.ImmutableLeaderConfig;
import com.palantir.atlasdb.config.LeaderConfig;
import com.palantir.docker.compose.DockerComposition;
import com.palantir.docker.compose.connection.DockerPort;
import com.palantir.docker.compose.connection.waiting.HealthCheck;
import com.palantir.docker.compose.connection.waiting.SuccessOrFailure;

@RunWith(Suite.class)
@SuiteClasses({
        CassandraConnectionTest.class,
        CassandraKeyValueServiceTableCreationTest.class,
        CassandraKeyValueServiceSerializableTransactionTest.class,
        CassandraKeyValueServiceSweeperTest.class,
        CassandraTimestampTest.class,
        CassandraKeyValueServiceTest.class,
        SchemaMutationLockTest.class,
        UniqueSchemaMutationLockTableTest.class,
        SchemaMutationLockTablesTest.class,
})
public class CassandraTestSuite {

    public static final int THRIFT_PORT_NUMBER = 9160;
    @ClassRule
    public static final DockerComposition composition = DockerComposition.of("src/test/resources/docker-compose.yml")
            .waitingForHostNetworkedPort(THRIFT_PORT_NUMBER, toBeOpen())
            .saveLogsTo("container-logs")
            .build();

    static InetSocketAddress CASSANDRA_THRIFT_ADDRESS;

    static ImmutableCassandraKeyValueServiceConfig CASSANDRA_KVS_CONFIG_NO_LOCK_LEADER;

    static ImmutableCassandraKeyValueServiceConfig CASSANDRA_KVS_CONFIG;

    static LeaderConfig LEADER_CONFIG;

    @BeforeClass
    public static void waitUntilCassandraIsUp() throws IOException, InterruptedException {
        DockerPort port = composition.hostNetworkedPort(THRIFT_PORT_NUMBER);
        String hostname = port.getIp();
        CASSANDRA_THRIFT_ADDRESS = new InetSocketAddress(hostname, port.getExternalPort());

        CASSANDRA_KVS_CONFIG_NO_LOCK_LEADER = ImmutableCassandraKeyValueServiceConfig.builder()
                .addServers(CASSANDRA_THRIFT_ADDRESS)
                .poolSize(20)
                .keyspace("atlasdb")
                .credentials(ImmutableCassandraCredentialsConfig.builder()
                        .username("cassandra")
                        .password("cassandra")
                        .build())
                .ssl(false)
                .replicationFactor(1)
                .mutationBatchCount(10000)
                .mutationBatchSizeBytes(10000000)
                .fetchBatchCount(1000)
                .safetyDisabled(false)
                .autoRefreshNodes(false)
                .build();

        CASSANDRA_KVS_CONFIG = CASSANDRA_KVS_CONFIG_NO_LOCK_LEADER.withLockLeader(hostname);

        LEADER_CONFIG = ImmutableLeaderConfig
                .builder()
                .quorumSize(1)
                .localServer(hostname)
                .leaders(ImmutableSet.of(hostname))
                .build();

        Awaitility.await()
                .atMost(Duration.ONE_MINUTE)
                .pollInterval(Duration.ONE_SECOND)
                .until(canCreateKeyValueService());
    }

    private static Callable<Boolean> canCreateKeyValueService() {
        return new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                try {
                    CassandraKeyValueService.create(CassandraKeyValueServiceConfigManager.createSimpleManager(CASSANDRA_KVS_CONFIG), LEADER_CONFIG);
                    return true;
                } catch (Exception e) {
                    return false;
                }
            }
        };
    }

    private static HealthCheck<DockerPort> toBeOpen() {
        return port -> SuccessOrFailure.fromBoolean(port.isListeningNow(), "" + "" + port + " was not open");
    }
}

@@ -915,16 +915,56 @@
 asses;%0A%0A
+import com.google.common.base.Optional;%0A
 import c
@@ -1002,16 +1002,16 @@
 bleSet;%0A
-
 import c
@@ -2048,57 +2048,8 @@
 ss,%0A
-        UniqueSchemaMutationLockTableTest.class,%0A
     
@@ -2682,16 +2682,25 @@
  static 
+Optional%3C
 LeaderCo
@@ -2703,16 +2703,17 @@
 erConfig
+%3E
  LEADER_
@@ -3067,31 +3067,16 @@
 S_CONFIG
-_NO_LOCK_LEADER
  = Immut
@@ -3761,102 +3761,8 @@
     
-CASSANDRA_KVS_CONFIG = CASSANDRA_KVS_CONFIG_NO_LOCK_LEADER.withLockLeader(hostname);%0A%0A        
 LEAD
@@ -3765,32 +3765,44 @@
 LEADER_CONFIG = 
+Optional.of(
 ImmutableLeaderC
@@ -3956,16 +3956,16 @@
 tname))%0A
-
         
@@ -3972,32 +3972,33 @@
         .build()
+)
 ;%0A%0A        Await

Make LeaderConfig optional in CassandraTestSuite