package uk.sky.cqlmigrate;

import com.datastax.driver.core.Cluster;
import com.google.common.collect.ImmutableMap;
import org.junit.*;
import org.scassandra.cql.PrimitiveType;
import org.scassandra.http.client.ActivityClient;
import org.scassandra.http.client.PrimingClient;
import org.scassandra.http.client.PrimingRequest;
import org.scassandra.junit.ScassandraServerRule;
import uk.sky.cqlmigrate.util.PortScavenger;

import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.scassandra.http.client.PrimingRequest.then;
import static org.scassandra.http.client.types.ColumnMetadata.column;

public class CassandraNoOpLockingMechanismTest {

    private static final int BINARY_PORT = PortScavenger.getFreePort();
    private static final int ADMIN_PORT = PortScavenger.getFreePort();
    private static final String CLIENT_ID = UUID.randomUUID().toString();

    @ClassRule
    public static final ScassandraServerRule SCASSANDRA = new ScassandraServerRule(BINARY_PORT, ADMIN_PORT);

    @Rule
    public final ScassandraServerRule resetScassandra = SCASSANDRA;

    private final PrimingClient primingClient = SCASSANDRA.primingClient();
    private final ActivityClient activityClient = SCASSANDRA.activityClient();

    private Cluster cluster;
    private CassandraNoOpLockingMechanism lockingMechanism;

    @Before
    public void baseSetup() throws Exception {

        cluster = Cluster.builder()
                .addContactPoint("localhost")
                .withPort(BINARY_PORT)
                .build();

        primingClient.prime(PrimingRequest.preparedStatementBuilder()
                .withQuery("INSERT INTO cqlmigrate.locks (name, client) VALUES (?, ?) IF NOT EXISTS")
                .withThen(then()
                        .withVariableTypes(PrimitiveType.TEXT, PrimitiveType.TEXT)
                        .withColumnTypes(column("[applied]", PrimitiveType.BOOLEAN))
                        .withRows(ImmutableMap.of("[applied]", true)))
                .build()
        );

        primingClient.prime(PrimingRequest.preparedStatementBuilder()
                .withQuery("DELETE FROM cqlmigrate.locks WHERE name = ? IF client = ?")
                .withThen(then()
                        .withVariableTypes(PrimitiveType.TEXT, PrimitiveType.TEXT)
                        .withColumnTypes(column("[applied]", PrimitiveType.BOOLEAN))
                        .withRows(ImmutableMap.of("[applied]", true)))
                .build()
        );

        lockingMechanism = new CassandraNoOpLockingMechanism();
        lockingMechanism.init();

        activityClient.clearAllRecordedActivity();
    }

    @After
    public void baseTearDown() throws Exception {
        cluster.close();
    }

    @Test
    public void shouldPrepareNoInsertLocksQueryWhenInit() throws Throwable {
        //when
        lockingMechanism.init();

        //then
        assertThat(activityClient.retrievePreparedStatementPreparations()).isEmpty();
    }

    @Test
    public void nothingShouldInsertLockWhenAcquiringLock() throws Exception {
        //when
        lockingMechanism.acquire(CLIENT_ID);

        //then
        assertThat(activityClient.retrievePreparedStatementExecutions()).isEmpty();
    }

    @Test
    public void shouldDeleteLockWhenReleasingLock() throws Exception {
        //when
        lockingMechanism.release(CLIENT_ID);

        //then
        assertThat(activityClient.retrievePreparedStatementExecutions()).isEmpty();
    }

    @Test
    public void shouldAlwaysReleasingLock() throws Exception {
        //when
        boolean released = lockingMechanism.release(CLIENT_ID);

        //then
        assertThat(released).isTrue();
    }

    @Test
    public void shouldAlwaysAcquireLock() throws Exception {
        //when
        boolean acquired = lockingMechanism.acquire(CLIENT_ID);

        //then
        assertThat(acquired).isTrue();
    }
}

@@ -32,3469 +32,524 @@
 ort 
-com.datastax.driver.core.Cluster;%0Aimport com.google.common.collect.ImmutableMap;%0Aimport org.junit.*;%0Aimport org.scassandra.cql.PrimitiveType;%0Aimport org.scassandra.http.client.ActivityClient;%0Aimport org.scassandra.http.client.PrimingClient;%0Aimport org.scassandra.http.client.PrimingRequest;%0Aimport org.scassandra.junit.ScassandraServerRule;%0Aimport uk.sky.cqlmigrate.util.PortScavenger;%0A%0Aimport java.util.UUID;%0A%0Aimport static org.assertj.core.api.Assertions.assertThat;%0Aimport static org.scassandra.http.client.PrimingRequest.then;%0Aimport static org.scassandra.http.client.types.ColumnMetadata.column;%0A%0Apublic class CassandraNoOpLockingMechanismTest %7B%0A%0A    private static final int BINARY_PORT = PortScavenger.getFreePort();%0A    private static final int ADMIN_PORT = PortScavenger.getFreePort();%0A    private static final String CLIENT_ID = UUID.randomUUID().toString();%0A%0A    @ClassRule%0A    public static final ScassandraServerRule SCASSANDRA = new ScassandraServerRule(BINARY_PORT, ADMIN_PORT);%0A%0A    @Rule%0A    public final ScassandraServerRule resetScassandra = SCASSANDRA;%0A%0A    private final PrimingClient primingClient = SCASSANDRA.primingClient();%0A    private final ActivityClient activityClient = SCASSANDRA.activityClient();%0A%0A    private Cluster cluster;%0A    private CassandraNoOpLockingMechanism lockingMechanism;%0A%0A    @Before%0A    public void baseSetup() throws Exception %7B%0A%0A        cluster = Cluster.builder()%0A                .addContactPoint(%22localhost%22)%0A                .withPort(BINARY_PORT)%0A                .build();%0A%0A        primingClient.prime(PrimingRequest.preparedStatementBuilder()%0A                .withQuery(%22INSERT INTO cqlmigrate.locks (name, client) VALUES (?, ?) IF NOT EXISTS%22)%0A                .withThen(then()%0A                        .withVariableTypes(PrimitiveType.TEXT, PrimitiveType.TEXT)%0A                        .withColumnTypes(column(%22%5Bapplied%5D%22, PrimitiveType.BOOLEAN))%0A                        .withRows(ImmutableMap.of(%22%5Bapplied%5D%22, true)))%0A                .build()%0A        );%0A%0A        primingClient.prime(PrimingRequest.preparedStatementBuilder()%0A                .withQuery(%22DELETE FROM cqlmigrate.locks WHERE name = ? IF client = ?%22)%0A                .withThen(then()%0A                        .withVariableTypes(PrimitiveType.TEXT, PrimitiveType.TEXT)%0A                        .withColumnTypes(column(%22%5Bapplied%5D%22, PrimitiveType.BOOLEAN))%0A                        .withRows(ImmutableMap.of(%22%5Bapplied%5D%22, true)))%0A                .build()%0A        );%0A%0A        lockingMechanism = new CassandraNoOpLockingMechanism();%0A        lockingMechanism.init();%0A%0A        activityClient.clearAllRecordedActivity();%0A    %7D%0A%0A    @After%0A    public void baseTearDown() throws Exception %7B%0A        cluster.close();%0A    %7D%0A%0A    @Test%0A    public void shouldPrepareNoInsertLocksQueryWhenInit() throws Throwable %7B%0A        //when%0A        lockingMechanism.init();%0A%0A        //then%0A        assertThat(activityClient.retrievePreparedStatementPreparations()).isEmpty();%0A    %7D%0A%0A    @Test%0A    public void nothingShouldInsertLockWhenAcquiringLock() throws Exception %7B%0A        //when%0A        lockingMechanism.acquire(CLIENT_ID);%0A%0A        //then%0A        assertThat(activityClient.retrievePreparedStatementExecutions()).isEmpty();%0A    %7D%0A%0A    @Test%0A    public void shouldDeleteLockWhenReleasingLock() throws Exception %7B%0A        //when%0A        lockingMechanism.release(CLIENT_ID);%0A%0A        //then%0A        assertThat(activityClient.retrievePreparedStatementExecutions()).isEmpty
+org.junit.Test;%0Aimport org.junit.runner.RunWith;%0Aimport org.mockito.junit.MockitoJUnitRunner;%0A%0Aimport static org.assertj.core.api.Assertions.assertThat;%0A%0A@RunWith(MockitoJUnitRunner.class)%0Apublic class CassandraNoOpLockingMechanismTest %7B%0A%0A    public static final String CLIENT_ID = %22CLIENT_ID%22;%0A%0A    private final CassandraNoOpLockingMechanism lockingMechanism = new CassandraNoOpLockingMechanism();%0A%0A    @Test%0A    public void shouldDoNothingWhenInit() throws Throwable %7B%0A        //when%0A        lockingMechanism.init
 ();%0A

Remove scassandra from CassandraNoOpLockingMechanismTest