// Path: src/test/java/uk/sky/cqlmigrate/util/PortScavenger.java
// public class PortScavenger {
// 
//     public static int getFreePort() {
//         ServerSocket socket = null;
//         try {
//             socket = new ServerSocket(0);
//             socket.setReuseAddress(true);
//             int port = socket.getLocalPort();
//             closeSocket(socket);
//             return port;
//         } catch (IOException ignored) {
//         } finally {
//             closeSocket(socket);
//         }
//         throw new IllegalStateException("Could not find a free TCP/IP port to start embedded Jetty HTTP Server on");
// 
//     }
// 
//     private static void closeSocket(ServerSocket socket) {
//         if (socket != null) {
//             try {
//                 socket.close();
//             } catch (IOException ignored) {
//             }
//         }
//     }
// }


import com.datastax.oss.driver.api.core.ConsistencyLevel;
import com.datastax.oss.simulacron.common.cluster.ClusterSpec;
import com.datastax.oss.simulacron.common.cluster.DataCenterSpec;
import com.datastax.oss.simulacron.common.cluster.QueryLog;
import com.datastax.oss.simulacron.common.cluster.RequestPrime;
import com.datastax.oss.simulacron.common.request.Query;
import com.datastax.oss.simulacron.common.result.SuccessResult;
import com.datastax.oss.simulacron.common.stubbing.Prime;
import com.datastax.oss.simulacron.common.stubbing.PrimeDsl;
import com.datastax.oss.simulacron.server.BoundCluster;
import com.datastax.oss.simulacron.server.Server;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.apache.cassandra.exceptions.ConfigurationException;
import org.junit.*;
import uk.sky.cqlmigrate.util.PortScavenger;
import java.net.Inet4Address;
import java.net.InetSocketAddress;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import static com.datastax.oss.simulacron.common.stubbing.PrimeDsl.*;
import static java.lang.String.valueOf;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
                prepareInsertQuery,
                Lists.newArrayList(
                        com.datastax.oss.simulacron.common.codec.ConsistencyLevel.ONE,
                        com.datastax.oss.simulacron.common.codec.ConsistencyLevel.ALL),
                new LinkedHashMap<>(ImmutableMap.of("name", lockName + ".schema_migration", "client", clientId)),
                new LinkedHashMap<>(ImmutableMap.of("name", "varchar", "client", "varchar"))))
                .then(rows().row(
                        "[applied]", valueOf(lockApplied), "client", CLIENT_ID).columnTypes("[applied]", "boolean", "clientid", "varchar")
                );
        return primeBuilder;
    }

    private static PrimeDsl.PrimeBuilder primeDeleteQuery(String lockName, String clientId, boolean lockApplied, String lockHoldingClient) {
        String deleteQuery = "DELETE FROM cqlmigrate.locks WHERE name = ? IF client = ?";

        PrimeDsl.PrimeBuilder primeBuilder = when(query(
                deleteQuery,
                Lists.newArrayList(
                        com.datastax.oss.simulacron.common.codec.ConsistencyLevel.ONE,
                        com.datastax.oss.simulacron.common.codec.ConsistencyLevel.ALL),
                new LinkedHashMap<>(ImmutableMap.of("name", lockName + ".schema_migration", "client", clientId)),
                new LinkedHashMap<>(ImmutableMap.of("name", "varchar", "client", "varchar"))))
                .then(rows()
                        .row("[applied]", valueOf(lockApplied), "client", lockHoldingClient).columnTypes("[applied]", "boolean", "clientid", "varchar"));
        return primeBuilder;
    }

    private void setupKeyspace(String keyspaceName) {
        BoundCluster simulacron = cluster;
        Map<String, String> keyspaceColumns = ImmutableMap.of(
// Path: src/test/java/uk/sky/cqlmigrate/util/PortScavenger.java
// public class PortScavenger {
// 
//     public static int getFreePort() {
//         ServerSocket socket = null;
//         try {
//             socket = new ServerSocket(0);
//             socket.setReuseAddress(true);
//             int port = socket.getLocalPort();
//             closeSocket(socket);
//             return port;
//         } catch (IOException ignored) {
//         } finally {
//             closeSocket(socket);
//         }
//         throw new IllegalStateException("Could not find a free TCP/IP port to start embedded Jetty HTTP Server on");
// 
//     }
// 
//     private static void closeSocket(ServerSocket socket) {
//         if (socket != null) {
//             try {
//                 socket.close();
//             } catch (IOException ignored) {
//             }
//         }
//     }
// }


// Path: src/test/java/uk/sky/cqlmigrate/CqlMigratorConsistencyLevelIntegrationTest.java
import com.datastax.oss.driver.api.core.ConsistencyLevel;
import com.datastax.oss.simulacron.common.cluster.ClusterSpec;
import com.datastax.oss.simulacron.common.cluster.DataCenterSpec;
import com.datastax.oss.simulacron.common.cluster.QueryLog;
import com.datastax.oss.simulacron.common.cluster.RequestPrime;
import com.datastax.oss.simulacron.common.request.Query;
import com.datastax.oss.simulacron.common.result.SuccessResult;
import com.datastax.oss.simulacron.common.stubbing.Prime;
import com.datastax.oss.simulacron.common.stubbing.PrimeDsl;
import com.datastax.oss.simulacron.server.BoundCluster;
import com.datastax.oss.simulacron.server.Server;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.apache.cassandra.exceptions.ConfigurationException;
import org.junit.*;
import uk.sky.cqlmigrate.util.PortScavenger;
import java.net.Inet4Address;
import java.net.InetSocketAddress;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import static com.datastax.oss.simulacron.common.stubbing.PrimeDsl.*;
import static java.lang.String.valueOf;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;

                prepareInsertQuery,
                Lists.newArrayList(
                        com.datastax.oss.simulacron.common.codec.ConsistencyLevel.ONE,
                        com.datastax.oss.simulacron.common.codec.ConsistencyLevel.ALL),
                new LinkedHashMap<>(ImmutableMap.of("name", lockName + ".schema_migration", "client", clientId)),
                new LinkedHashMap<>(ImmutableMap.of("name", "varchar", "client", "varchar"))))
                .then(rows().row(
                        "[applied]", valueOf(lockApplied), "client", CLIENT_ID).columnTypes("[applied]", "boolean", "clientid", "varchar")
                );
        return primeBuilder;
    }

    private static PrimeDsl.PrimeBuilder primeDeleteQuery(String lockName, String clientId, boolean lockApplied, String lockHoldingClient) {
        String deleteQuery = "DELETE FROM cqlmigrate.locks WHERE name = ? IF client = ?";

        PrimeDsl.PrimeBuilder primeBuilder = when(query(
                deleteQuery,
                Lists.newArrayList(
                        com.datastax.oss.simulacron.common.codec.ConsistencyLevel.ONE,
                        com.datastax.oss.simulacron.common.codec.ConsistencyLevel.ALL),
                new LinkedHashMap<>(ImmutableMap.of("name", lockName + ".schema_migration", "client", clientId)),
                new LinkedHashMap<>(ImmutableMap.of("name", "varchar", "client", "varchar"))))
                .then(rows()
                        .row("[applied]", valueOf(lockApplied), "client", lockHoldingClient).columnTypes("[applied]", "boolean", "clientid", "varchar"));
        return primeBuilder;
    }

    private void setupKeyspace(String keyspaceName) {
        BoundCluster simulacron = cluster;
        Map<String, String> keyspaceColumns = ImmutableMap.of(
                "keyspace_name", "varchar",