package me.prettyprint.cassandra.service;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

public class CassandraHostConfiguratorTest {

  @Before
  public void setup() {

  }

  @Test
  public void testSimpleCassandraHostSetup() {
    CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator("localhost:9170");
    CassandraHost[] cassandraHosts = cassandraHostConfigurator.buildCassandraHosts();
    assertEquals(1,cassandraHosts.length);
  }

  @Test
  public void testCassandraHostSetupSplit() {
    CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator("localhost:9170,localhost:9171,localhost:9172");
    CassandraHost[] cassandraHosts = cassandraHostConfigurator.buildCassandraHosts();
    assertEquals(3,cassandraHosts.length);
    assertEquals(9172,cassandraHosts[2].getPort());
  }

  @Test
  public void testConfigValuesPropogated() {
    CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator("localhost:9170,localhost:9171,localhost:9172");
    cassandraHostConfigurator.setMaxActive(20);
    cassandraHostConfigurator.setMaxIdle(5);
    cassandraHostConfigurator.setCassandraThriftSocketTimeout(3000);
    cassandraHostConfigurator.setMaxWaitTimeWhenExhausted(4000);
    cassandraHostConfigurator.setExhaustedPolicy(ExhaustedPolicy.WHEN_EXHAUSTED_GROW);
    CassandraHost[] cassandraHosts = cassandraHostConfigurator.buildCassandraHosts();
    // no need to test all, just a smattering
    assertEquals(20, cassandraHosts[1].getMaxActive());
    assertEquals(20, cassandraHosts[0].getMaxActive());
    assertEquals(5, cassandraHosts[1].getMaxIdle());
    assertEquals(ExhaustedPolicy.WHEN_EXHAUSTED_GROW, cassandraHosts[1].getExhaustedPolicy());
    assertEquals(4000, cassandraHosts[1].getMaxWaitTimeWhenExhausted());
    assertEquals(3000, cassandraHosts[2].getCassandraThriftSocketTimeout());
    assertEquals(3000, cassandraHosts[0].getCassandraThriftSocketTimeout());
  }

  @Test
  public void testApplyConfig() {
    CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator("localhost:9170");
    cassandraHostConfigurator.setMaxActive(15);
    CassandraHost extraHost = new CassandraHost("localhost:9171");
    cassandraHostConfigurator.applyConfig(extraHost);
    assertEquals(15, extraHost.getMaxActive());
  }
  
  @Test
  public void testHostnameOnlyDefaultPort() {
    CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator("localhost");
    CassandraHost[] cassandraHosts = cassandraHostConfigurator.buildCassandraHosts();
    assertEquals(CassandraHost.DEFAULT_PORT,cassandraHosts[0].getPort());
  }
  
  @Test
  public void testConfiguratorPort() {
    CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator("localhost");
    cassandraHostConfigurator.setPort(9177);
    CassandraHost[] cassandraHosts = cassandraHostConfigurator.buildCassandraHosts();
    assertEquals(9177,cassandraHosts[0].getPort());
  } 
}

@@ -2380,35 +2380,33 @@
 xActive());%0A  %7D%0A
-  
 %0A
+
   @Test%0A  public
@@ -2709,18 +2709,483 @@
 ));%0A  %7D%0A
+%0A
   
+@Test%0A  public void testHostnameOnlyDefaultPortMultipleHosts() %7B%0A    CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator(%22h1,h2,h3:1111%22);%0A    CassandraHost%5B%5D cassandraHosts = cassandraHostConfigurator.buildCassandraHosts();%0A    assertEquals(CassandraHost.DEFAULT_PORT, cassandraHosts%5B0%5D.getPort());%0A    assertEquals(CassandraHost.DEFAULT_PORT, cassandraHosts%5B1%5D.getPort());%0A    assertEquals(1111, cassandraHosts%5B2%5D.getPort());%0A  %7D%0A
 %0A  @Test
@@ -3513,11 +3513,10 @@
 ));%0A
+
   %7D
- 
 %0A%7D%0A

Add test coverage to CassandraHostConfiguratorTest. multiple hosts with default/non default ports