package info.archinnov.achilles.configuration;

import static info.archinnov.achilles.configuration.CQLConfigurationParameters.*;
import static org.fest.assertions.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
import info.archinnov.achilles.exception.AchillesException;
import java.util.HashMap;
import java.util.Map;
import org.jboss.netty.channel.ChannelPipelineException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ProtocolOptions.Compression;
import com.datastax.driver.core.SSLOptions;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.NoHostAvailableException;
import com.datastax.driver.core.policies.Policies;

/**
 * CQLArgumentExtractorTest
 * 
 * @author DuyHai DOAN
 * 
 */

@RunWith(MockitoJUnitRunner.class)
public class CQLArgumentExtractorTest
{
    @Rule
    public ExpectedException exception = ExpectedException.none();

    @Mock
    private Cluster cluster;

    @Mock
    private Session session;

    private CQLArgumentExtractor extractor = new CQLArgumentExtractor();

    @Test(expected = ChannelPipelineException.class)
    public void should_init_cluster_with_all_params() throws Exception
    {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(CONNECTION_CONTACT_POINTS_PARAM, "localhost");
        params.put(CONNECTION_PORT_PARAM, 9111);
        params.put(COMPRESSION_TYPE, Compression.SNAPPY);
        params.put(RETRY_POLICY, Policies.defaultRetryPolicy());
        params.put(LOAD_BALANCING_POLICY, Policies.defaultLoadBalancingPolicy());
        params.put(RECONNECTION_POLICY, Policies.defaultReconnectionPolicy());
        params.put(USERNAME, "user");
        params.put(PASSWORD, "pass");
        params.put(DISABLE_JMX, true);
        params.put(DISABLE_METRICS, true);
        params.put(SSL_ENABLED, true);
        params.put(SSL_OPTIONS, new SSLOptions());

        extractor.initCluster(params);
    }

    @Test(expected = NoHostAvailableException.class)
    public void should_init_cluster_with_minimum_params() throws Exception
    {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(CONNECTION_CONTACT_POINTS_PARAM, "localhost");
        params.put(CONNECTION_PORT_PARAM, 9111);

        extractor.initCluster(params);
    }

    @Test
    public void should_exception_when_no_hostname_property() throws Exception
    {
        Map<String, Object> params = new HashMap<String, Object>();

        exception.expect(AchillesException.class);
        exception.expectMessage(CONNECTION_CONTACT_POINTS_PARAM + " property should be provided");

        extractor.initCluster(params);
    }

    @Test
    public void should_exception_when_no_port_property() throws Exception
    {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(CONNECTION_CONTACT_POINTS_PARAM, "localhost");

        exception.expect(AchillesException.class);
        exception.expectMessage(CONNECTION_PORT_PARAM + " property should be provided");

        extractor.initCluster(params);
    }

    @Test
    public void should_init_session() throws Exception
    {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(KEYSPACE_NAME_PARAM, "achilles");

        when(cluster.connect("achilles")).thenReturn(session);

        Session actual = extractor.initSession(cluster, params);

        assertThat(actual).isSameAs(session);
    }

    @Test
    public void should_exception_when_no_keyspace_name_param() throws Exception
    {
        Map<String, Object> params = new HashMap<String, Object>();

        exception.expect(AchillesException.class);
        exception.expectMessage(KEYSPACE_NAME_PARAM + " property should be provided");

        extractor.initSession(cluster, params);

    }
}

@@ -336,65 +336,8 @@
 ap;%0A
-import org.jboss.netty.channel.ChannelPipelineException;%0A
 impo
@@ -1233,23 +1233,8 @@
 d = 
-ChannelPipeline
 Exce

Fix a failing test for Travis