/*
 * Copyright 2010 Ning, Inc.
 *
 * Ning licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.ning.metrics.eventtracker;

import org.skife.config.ConfigurationObjectFactory;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.ning.metrics.eventtracker.CollectorControllerModule.Type;

import java.io.File;
import java.util.Properties;

@Test(groups = {"fast"})
public class TestConfigTypes
{
    Properties p = null;

    private final File tmpDir = new File(System.getProperty("java.io.tmpdir"), "collector");

    @BeforeMethod(alwaysRun = true)
    public void setUp()
    {
        p = new Properties();
        p.put("eventtracker.directory", tmpDir.getAbsolutePath());

        if (!tmpDir.exists() && !tmpDir.mkdirs()) {
            throw new RuntimeException("Failed to create: " + tmpDir);
        }

        if (!tmpDir.isDirectory()) {
            throw new RuntimeException("Path points to something that's not a directory: " + tmpDir);
        }

        tmpDir.deleteOnExit();
    }

    @AfterMethod(alwaysRun = true)
    public void tearDown()
    {
        p = null;
    }

    public void testEmpty()
    {
        final EventTrackerConfig eventTrackerConfig = new ConfigurationObjectFactory(p).build(EventTrackerConfig.class);
        Assert.assertEquals(Type.valueOf(eventTrackerConfig.getType()), Type.COLLECTOR);
    }

    public void testCollector()
    {
        p.put("eventtracker.type", "COLLECTOR");

        final EventTrackerConfig eventTrackerConfig = new ConfigurationObjectFactory(p).build(EventTrackerConfig.class);
        Assert.assertEquals(Type.valueOf(eventTrackerConfig.getType()), Type.COLLECTOR);
    }

    public void testScribe()
    {
        p.put("eventtracker.type", "SCRIBE");
        final EventTrackerConfig eventTrackerConfig = new ConfigurationObjectFactory(p).build(EventTrackerConfig.class);
        Assert.assertEquals(Type.valueOf(eventTrackerConfig.getType()), Type.SCRIBE);
    }

    public void testNoLogging()
    {
        p.put("eventtracker.type", "NO_LOGGING");
        final EventTrackerConfig eventTrackerConfig = new ConfigurationObjectFactory(p).build(EventTrackerConfig.class);
        Assert.assertEquals(Type.valueOf(eventTrackerConfig.getType()), Type.NO_LOGGING);
    }

    public void testEventEncodingType()
    {
        p.put("eventtracker.http.eventEncoding", "THRIFT");
        EventTrackerConfig eventTrackerConfig = new ConfigurationObjectFactory(p).build(EventTrackerConfig.class);
        Assert.assertEquals(EventEncodingType.valueOf(eventTrackerConfig.getHttpEventEncodingType()), EventEncodingType.THRIFT);

        p.put("eventtracker.http.eventEncoding", "JSON");
        eventTrackerConfig = new ConfigurationObjectFactory(p).build(EventTrackerConfig.class);
        Assert.assertEquals(EventEncodingType.valueOf(eventTrackerConfig.getHttpEventEncodingType()), EventEncodingType.JSON);

        //TODO
        //eventTrackerConfig.setHttpEventEncodingType("SMILE");
        //Assert.assertEquals(EventEncodingType.valueOf(eventTrackerConfig.getHttpEventEncodingType()), EventEncodingType.SMILE);

        try {
            p.put("eventtracker.http.eventEncoding", "TYPO");
            eventTrackerConfig = new ConfigurationObjectFactory(p).build(EventTrackerConfig.class);
            EventEncodingType.valueOf(eventTrackerConfig.getHttpEventEncodingType());
            Assert.fail("expected exception");
        }
        catch (Exception e) {
            // does anything need to go here?
        }
    }
}

@@ -652,16 +652,85 @@
 acker;%0A%0A
+import com.ning.metrics.eventtracker.CollectorControllerModule.Type;%0A
 import o
@@ -927,78 +927,8 @@
 t;%0A%0A
-import com.ning.metrics.eventtracker.CollectorControllerModule.Type;%0A%0A
 impo
@@ -2888,1262 +2888,7 @@
    %7D
-%0A%0A    public void testEventEncodingType()%0A    %7B%0A        p.put(%22eventtracker.http.eventEncoding%22, %22THRIFT%22);%0A        EventTrackerConfig eventTrackerConfig = new ConfigurationObjectFactory(p).build(EventTrackerConfig.class);%0A        Assert.assertEquals(EventEncodingType.valueOf(eventTrackerConfig.getHttpEventEncodingType()), EventEncodingType.THRIFT);%0A%0A        p.put(%22eventtracker.http.eventEncoding%22, %22JSON%22);%0A        eventTrackerConfig = new ConfigurationObjectFactory(p).build(EventTrackerConfig.class);%0A        Assert.assertEquals(EventEncodingType.valueOf(eventTrackerConfig.getHttpEventEncodingType()), EventEncodingType.JSON);%0A%0A        //TODO%0A        //eventTrackerConfig.setHttpEventEncodingType(%22SMILE%22);%0A        //Assert.assertEquals(EventEncodingType.valueOf(eventTrackerConfig.getHttpEventEncodingType()), EventEncodingType.SMILE);%0A%0A        try %7B%0A            p.put(%22eventtracker.http.eventEncoding%22, %22TYPO%22);%0A            eventTrackerConfig = new ConfigurationObjectFactory(p).build(EventTrackerConfig.class);%0A            EventEncodingType.valueOf(eventTrackerConfig.getHttpEventEncodingType());%0A            Assert.fail(%22expected exception%22);%0A        %7D%0A        catch (Exception e) %7B%0A            // does anything need to go here?%0A        %7D%0A    %7D
 %0A%7D%0A

remove deprecated testEventEncodingType