package com.rackspacecloud.blueflood.service;

import static junit.framework.Assert.*;

import org.junit.*;

import java.io.IOException;

public class BluefloodServiceStarterTest {

    @Before
    public void setUp() throws IOException {

        Configuration config = Configuration.getInstance();

        config.init();

        // take from demo/local/config/blueflood.conf
        config.setProperty(CoreConfig.CASSANDRA_HOSTS, "127.0.0.1:9160");
        config.setProperty(CoreConfig.DEFAULT_CASSANDRA_PORT, "9160");
        config.setProperty(CoreConfig.MAX_CASSANDRA_CONNECTIONS, "70");
        config.setProperty(CoreConfig.QUERY_MODULES, "com.rackspacecloud.blueflood.service.HttpQueryService");
        config.setProperty(CoreConfig.INGESTION_MODULES, "com.rackspacecloud.blueflood.service.HttpIngestionService");
        config.setProperty(CoreConfig.DISCOVERY_MODULES, "com.rackspacecloud.blueflood.io.ElasticIO");
        config.setProperty(CoreConfig.EVENTS_MODULES, "com.rackspacecloud.blueflood.io.EventElasticSearchIO");
        config.setProperty(CoreConfig.ENUMS_DISCOVERY_MODULES, "com.rackspacecloud.blueflood.io.EnumElasticIO");
        config.setProperty(CoreConfig.MAX_ROLLUP_WRITE_THREADS, "20");

        config.setProperty(CoreConfig.ZOOKEEPER_CLUSTER, "NONE");

        config.setProperty(CoreConfig.INGEST_MODE, "false");
        config.setProperty(CoreConfig.ROLLUP_MODE, "false");
        config.setProperty(CoreConfig.QUERY_MODE, "false");

        BluefloodServiceStarter.SkipInstantiateRestartGauge = true;
    }

    @Test
    public void testAllModesDisabled() {

        // given
        Configuration config = Configuration.getInstance();
        config.setProperty(CoreConfig.INGEST_MODE, "false");
        config.setProperty(CoreConfig.QUERY_MODE, "false");
        config.setProperty(CoreConfig.ROLLUP_MODE, "false");

        // when
        BluefloodServiceStarter.run();

        // then
        // no exception was thrown
    }

    @Test(expected = BluefloodServiceStarterException.class)
    public void testNoCassandraHostsFailsValidation() {

        // given
        Configuration config = Configuration.getInstance();
        config.setProperty(CoreConfig.CASSANDRA_HOSTS, "");

        // when
        BluefloodServiceStarter.validateCassandraHosts();

        // then
        // exception was thrown
    }

    @Test(expected = BluefloodServiceStarterException.class)
    public void testInvalidCassandraHostsFailsValidation() {

        // given
        Configuration config = Configuration.getInstance();
        config.setProperty(CoreConfig.CASSANDRA_HOSTS, "something");

        // when
        BluefloodServiceStarter.validateCassandraHosts();

        // then
        // exception was thrown
    }

    @Test(expected = BluefloodServiceStarterException.class)
    public void testCassandraHostWithoutPortFailsValidation() {

        // given
        Configuration config = Configuration.getInstance();
        config.setProperty(CoreConfig.CASSANDRA_HOSTS, "127.0.0.1");

        // when
        BluefloodServiceStarter.validateCassandraHosts();

        // then
        // exception was thrown
    }

    @Test(expected = BluefloodServiceStarterException.class)
    public void testIngestModeEnabledWithoutModules() {

        // given
        Configuration config = Configuration.getInstance();
        config.setProperty(CoreConfig.INGEST_MODE, "true");
        config.setProperty(CoreConfig.INGESTION_MODULES, "");

        // when
        BluefloodServiceStarter.run();

        // then
        // exception was thrown
    }

    @Test
    public void testIngestModeEnabledWithModules() {

        // given
        Configuration config = Configuration.getInstance();
        config.setProperty(CoreConfig.INGEST_MODE, "true");
        config.setProperty(CoreConfig.INGESTION_MODULES, "com.rackspacecloud.blueflood.service.DummyIngestionService");

        // when
        BluefloodServiceStarter.run();

        // then
        assertNotNull(DummyIngestionService.getInstances());
        assertEquals(1, DummyIngestionService.getInstances().size());
        assertNotNull(DummyIngestionService.getMostRecentInstance());

        assertTrue(DummyIngestionService.getMostRecentInstance().getStartServiceCalled());
        assertFalse(DummyIngestionService.getMostRecentInstance().getShutdownServiceCalled());
    }

    @Test(expected = BluefloodServiceStarterException.class)
    public void testQueryModeEnabledWithoutModules() {

        // given
        Configuration config = Configuration.getInstance();
        config.setProperty(CoreConfig.QUERY_MODE, "true");
        config.setProperty(CoreConfig.QUERY_MODULES, "");

        // when
        BluefloodServiceStarter.run();

        // then
        // exception was thrown
    }

    @Test
    public void testQueryModeEnabledWithDummyModule() {

        // given
        Configuration config = Configuration.getInstance();
        config.setProperty(CoreConfig.QUERY_MODE, "true");
        config.setProperty(CoreConfig.QUERY_MODULES, "com.rackspacecloud.blueflood.service.DummyQueryService");

        // when
        BluefloodServiceStarter.run();

        // then
        assertNotNull(DummyQueryService.getInstances());
        assertEquals(1, DummyQueryService.getInstances().size());
        assertNotNull(DummyQueryService.getMostRecentInstance());

        assertTrue(DummyQueryService.getMostRecentInstance().getStartServiceCalled());
    }

    @Test
    public void testRollupModeEnabled() {

        // given
        Configuration config = Configuration.getInstance();
        config.setProperty(CoreConfig.ROLLUP_MODE, "true");

        // when
        BluefloodServiceStarter.run();

        // then
        // no exception thrown
    }

    @Test
    public void testEventListenerServiceEnabledWithDummyModule() {

        // given
        Configuration config = Configuration.getInstance();
        config.setProperty(CoreConfig.EVENT_LISTENER_MODULES, "com.rackspacecloud.blueflood.service.DummyEventListenerService");

        // when
        BluefloodServiceStarter.run();

        // then
        assertNotNull(DummyEventListenerService.getInstances());
        assertEquals(1, DummyEventListenerService.getInstances().size());
        assertNotNull(DummyEventListenerService.getMostRecentInstance());

        assertTrue(DummyEventListenerService.getMostRecentInstance().getStartServiceCalled());
    }

    @After
    public void tearDown() throws IOException {
        Configuration config = Configuration.getInstance();
        config.init();
    }
}

@@ -322,908 +322,8 @@
 );%0A%0A
-        // take from demo/local/config/blueflood.conf%0A        config.setProperty(CoreConfig.CASSANDRA_HOSTS, %22127.0.0.1:9160%22);%0A        config.setProperty(CoreConfig.DEFAULT_CASSANDRA_PORT, %229160%22);%0A        config.setProperty(CoreConfig.MAX_CASSANDRA_CONNECTIONS, %2270%22);%0A        config.setProperty(CoreConfig.QUERY_MODULES, %22com.rackspacecloud.blueflood.service.HttpQueryService%22);%0A        config.setProperty(CoreConfig.INGESTION_MODULES, %22com.rackspacecloud.blueflood.service.HttpIngestionService%22);%0A        config.setProperty(CoreConfig.DISCOVERY_MODULES, %22com.rackspacecloud.blueflood.io.ElasticIO%22);%0A        config.setProperty(CoreConfig.EVENTS_MODULES, %22com.rackspacecloud.blueflood.io.EventElasticSearchIO%22);%0A        config.setProperty(CoreConfig.ENUMS_DISCOVERY_MODULES, %22com.rackspacecloud.blueflood.io.EnumElasticIO%22);%0A        config.setProperty(CoreConfig.MAX_ROLLUP_WRITE_THREADS, %2220%22);%0A%0A
     

Remove additional unnecessary config values.