package com.pinterest.secor.io.impl;

import com.google.common.io.Files;
import com.pinterest.secor.common.LogFilePath;
import com.pinterest.secor.common.SecorConfig;
import com.pinterest.secor.io.FileReader;
import com.pinterest.secor.io.FileWriter;
import com.pinterest.secor.io.KeyValue;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.GzipCodec;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertArrayEquals;

public class JsonORCFileReaderWriterFactoryTest {

    private JsonORCFileReaderWriterFactory factory;
    private SecorConfig config;
    private CompressionCodec codec;

    @Before
    public void setUp() throws Exception {
        PropertiesConfiguration properties = new PropertiesConfiguration();
        properties.setProperty("secor.orc.schema.provider", "com.pinterest.secor.util.orc.schema.DefaultORCSchemaProvider");
        properties.setProperty("secor.orc.message.schema.test-topic", "struct<firstname:string\\,age:int>");
        config = new SecorConfig(properties);
        factory = new JsonORCFileReaderWriterFactory(config);
        codec = new GzipCodec();
    }

    @Test(expected = IllegalArgumentException.class)
    public void testNoSchema() throws Exception {
        PropertiesConfiguration properties = new PropertiesConfiguration();
        properties.setProperty("secor.orc.schema.provider", "com.pinterest.secor.util.orc.schema.DefaultORCSchemaProvider");

        SecorConfig config = new SecorConfig(properties);
        JsonORCFileReaderWriterFactory factory = new JsonORCFileReaderWriterFactory(config);

        LogFilePath tempLogFilePath = new LogFilePath(
            Files.createTempDir().toString(),
            "test-topic",
            new String[]{"part-1"},
            0, 1, 0, ".log"
        );

        // IllegalArgumentException is expected
        FileWriter fileWriter = factory.BuildFileWriter(tempLogFilePath, codec);
    }

//    @Test
    public void test2() throws Exception {
        LogFilePath tempLogFilePath = new LogFilePath(Files.createTempDir().toString(),
            "test-topic2",
            new String[]{"part-1"},
            0,
            1,
            0,
            ".log"
        );

        FileWriter fileWriter = factory.BuildFileWriter(tempLogFilePath, codec);
        KeyValue kv1 = new KeyValue(12345, "{\"values\":[1, 2, 3, 4, 5]}".getBytes());
        fileWriter.write(kv1);
        fileWriter.close();
    }

    @Test
    public void testJsonORCReadWriteRoundTrip() throws Exception {
        LogFilePath tempLogFilePath = new LogFilePath(Files.createTempDir().toString(),
            "test-topic",
            new String[]{"part-1"},
            0,
            1,
            0,
            ".log"
        );

        FileWriter fileWriter = factory.BuildFileWriter(tempLogFilePath, codec);
        KeyValue kv1 = new KeyValue(23232, "{\"firstname\":\"Jason\",\"age\":48}".getBytes());
        KeyValue kv2 = new KeyValue(23233, "{\"firstname\":\"Christina\",\"age\":37}".getBytes());
        fileWriter.write(kv1);
        fileWriter.write(kv2);
        fileWriter.close();

        FileReader fileReader = factory.BuildFileReader(tempLogFilePath, codec);
        KeyValue kv3 = fileReader.next();
        KeyValue kv4 = fileReader.next();
        fileReader.close();

        assertArrayEquals(kv1.getValue(), kv3.getValue());
        assertArrayEquals(kv2.getValue(), kv4.getValue());
    }
}
@@ -2033,16 +2033,1259 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testMapOfStringToString() throws Exception %7B%0A        PropertiesConfiguration properties = new PropertiesConfiguration();%0A        properties.setProperty(%22secor.orc.schema.provider%22, %22com.pinterest.secor.util.orc.schema.DefaultORCSchemaProvider%22);%0A        properties.setProperty(%22secor.orc.message.schema.test-topic-map1%22, %22struct%3Cmappings:map%3Cstring%5C%5C,string%3E%3E%22);%0A%0A        SecorConfig config = new SecorConfig(properties);%0A        JsonORCFileReaderWriterFactory factory = new JsonORCFileReaderWriterFactory(config);%0A%0A        LogFilePath tempLogFilePath = new LogFilePath(%0A            Files.createTempDir().toString(),%0A            %22test-topic-map1%22,%0A            new String%5B%5D%7B%22part-1%22%7D,%0A            0, 1, 0, %22.log%22%0A        );%0A%0A        FileWriter fileWriter = factory.BuildFileWriter(tempLogFilePath, codec);%0A        KeyValue written1 = new KeyValue(12345, %22%7B%5C%22mappings%5C%22:%7B%5C%22key1%5C%22:%5C%22value1%5C%22,%5C%22key2%5C%22:%5C%22value2%5C%22%7D%7D%22.getBytes());%0A        fileWriter.write(written1);%0A        fileWriter.close();%0A%0A        FileReader fileReader = factory.BuildFileReader(tempLogFilePath, codec);%0A        KeyValue read1 = fileReader.next();%0A        fileReader.close();%0A%0A        assertArrayEquals(read1.getValue(), written1.getValue());%0A    %7D%0A%0A
 //    @T

Test case for map<string, string> type