package com.cookpad.puree;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import com.cookpad.puree.outputs.OutputConfiguration;
import com.cookpad.puree.outputs.PureeOutput;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.List;
import java.util.Map;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;

@RunWith(AndroidJUnit4.class)
public class PureeConfigurationTest {
    @Test
    public void checkDefaultValue() {
        Context context = InstrumentationRegistry.getContext();
        PureeConfiguration conf = new PureeConfiguration.Builder(context)
                .build();

        assertThat(conf.getApplicationContext(), notNullValue());
        assertThat(conf.getGson(), notNullValue());

        Map<Key, List<PureeOutput>> sourceOutputMap = conf.getSourceOutputMap();
        assertThat(sourceOutputMap.size(), is(0));
    }

    @Test
    public void build() {
        Context context = InstrumentationRegistry.getContext();
        PureeConfiguration conf = new PureeConfiguration.Builder(context)
                .source(FooLog.class).filter(new FooFilter()).filter(new BarFilter()).to(new OutFoo())
                .source(BarLog.class).filters(new FooFilter(), new BarFilter()).to(new OutFoo())
                .build();

        Map<Key, List<PureeOutput>> sourceOutputMap = conf.getSourceOutputMap();
        assertThat(sourceOutputMap.size(), is(2));
    }

    private static class FooLog extends JsonConvertible {
    }

    private static class BarLog extends JsonConvertible {
    }

    private static class FooFilter implements PureeFilter {
        @Override
        public JSONObject apply(JSONObject jsonLog) throws JSONException {
            return null;
        }
    }

    private static class BarFilter implements PureeFilter {
        @Override
        public JSONObject apply(JSONObject jsonLog) throws JSONException {
            return null;
        }
    }

    private static class OutFoo extends PureeOutput {
        @Override
        public String type() {
            return null;
        }

        @Override
        public OutputConfiguration configure(OutputConfiguration conf) {
            return null;
        }

        @Override
        public void emit(JSONObject jsonLog) {

        }
    }
}

@@ -258,16 +258,46 @@
 utput;%0A%0A
+import org.hamcrest.Matchers;%0A
 import o
@@ -1364,38 +1364,69 @@
 ss).
-filter(new FooFilter()
+to(new OutFoo())%0A                .source(FooLog.class
 ).filter
 (new
@@ -1425,16 +1425,17 @@
 lter
+s
 (new 
-Bar
+Foo
 Filt
@@ -1481,19 +1481,19 @@
 .source(
-Bar
+Foo
 Log.clas
@@ -1493,33 +1493,32 @@
 og.class).filter
-s
 (new FooFilter()
@@ -1521,17 +1521,103 @@
 er()
-, new Bar
+).filter(new BarFilter()).to(new OutBar())%0A                .source(BarLog.class).filter(new Foo
 Filt
@@ -1797,16 +1797,618 @@
  is(2));
+%0A%0A        %7B%0A            List%3CPureeOutput%3E outputs = sourceOutputMap.get(Key.from(FooLog.class));%0A            assertThat(outputs.size(), is(3));%0A        %7D%0A        %7B%0A            List%3CPureeOutput%3E outputs = sourceOutputMap.get(Key.from(BarLog.class));%0A            assertThat(outputs.size(), is(1));%0A            assertThat(outputs.get(0).getClass().getName(), is(OutFoo.class.getName()));%0A            List%3CPureeFilter%3E filters = outputs.get(0).getFilters();%0A            assertThat(filters.size(), is(1));%0A            assertThat(filters.get(0).getClass().getName(), is(FooFilter.class.getName()));%0A        %7D
 %0A    %7D%0A%0A
@@ -3259,16 +3259,365 @@
 Log) %7B%0A%0A
+        %7D%0A    %7D%0A%0A    private static class OutBar extends PureeOutput %7B%0A        @Override%0A        public String type() %7B%0A            return null;%0A        %7D%0A%0A        @Override%0A        public OutputConfiguration configure(OutputConfiguration conf) %7B%0A            return null;%0A        %7D%0A%0A        @Override%0A        public void emit(JSONObject jsonLog) %7B%0A%0A
         

Add test for PureeConfiguration