package com.treasure_data.jdbc;

import org.junit.Ignore;
import org.junit.Test;
import org.msgpack.type.ArrayValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

/**
 * Test cases for integration testing with production API
 */

public class TestProductionEnv
{
    private static Logger logger = LoggerFactory.getLogger(TestProductionEnv.class);

    /**
     * Read user user (e-mail address and password properties from $HOME/.td/td.conf
     * @return
     * @throws IOException
     */
    public static Properties readTDConf()
            throws IOException
    {
        Properties p = new Properties();
        File file = new File(System.getProperty("user.home", "./"), String.format(".td/td.conf"));
        if(!file.exists()) {
            logger.warn("config file %s is not found", file);
            return p;
        }

        BufferedReader reader = new BufferedReader(new FileReader(file));
        StringBuilder extracted = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) != null) {
            if(line.trim().startsWith("[")) {
                continue; // skip [... ] line
            }
            extracted.append(line.trim());
            extracted.append("\n");
        }
        String props = extracted.toString();
        p.load(new StringReader(props));
        return p;
    }

    public static Connection newPrestoConnection(String database)
            throws IOException, SQLException
    {
        Properties prop = readTDConf();
        Connection conn = DriverManager.getConnection(
                String.format("jdbc:td://api.treasuredata.com/%s;useSSL=true;type=presto", database),
                prop.getProperty("user", ""),
                prop.getProperty("password", "")
        );
        return conn;
    }


    @Ignore
    @Test
    public void readArrayType()
            throws SQLException, IOException
    {
        /**
         * <pre>
            presto:cs_modeanalytics> select * from arraytest;
            id |   nums    |    time
            ----+-----------+------------
            1 | [1, 2, 3] | 1432099776
            (1 row)
         </pre>
         */

        Connection conn = newPrestoConnection("cs_modeanalytics");
        Statement stat = conn.createStatement();
        stat.execute("select time, id, nums from arraytest");
        {
            ResultSet rs = stat.getResultSet();
            if (rs.next()) {
                long time = rs.getLong(1);
                assertEquals(1432099776L, time);
                int id = rs.getInt(2);
                assertEquals(1, id);

                ArrayValue arr = (ArrayValue) rs.getObject(3);
                logger.debug("getObject result: {}, type: {}", arr, arr.getClass());
                for (int i = 0; i < arr.size(); ++i) {
                    int v = arr.get(i).asIntegerValue().getInt();
                    assertEquals(i + 1, v);
                }
            }
            assertFalse(rs.next());
            rs.close();
        }

        stat.execute("select nums from arraytest");
        {
            ResultSet rs = stat.getResultSet();
            if(rs.next()) {
                ArrayValue arr = (ArrayValue) rs.getObject(1);
                logger.debug("getObject result: {}, type: {}", arr, arr.getClass());
                for (int i = 0; i < arr.size(); ++i) {
                    int v = arr.get(i).asIntegerValue().getInt();
                    assertEquals(i + 1, v);
                }
            }
            assertFalse(rs.next());
            rs.close();
        }

        stat.close();
        conn.close();
    }
}

@@ -4020,10 +4020,778 @@
 ;%0A    %7D%0A
+%0A    @Ignore%0A    @Test%0A    public void readJsonArray()%0A            throws IOException, SQLException%0A    %7B%0A%0A        Connection conn = newPrestoConnection(%22cs_modeanalytics%22);%0A        Statement stat = conn.createStatement();%0A        stat.execute(%22select nums from arraytest_str%22);%0A        ResultSet rs = stat.getResultSet();%0A        if(rs.next()) %7B%0A            ArrayValue arr = (ArrayValue) rs.getObject(1);%0A            logger.debug(%22getObject result: %7B%7D, type: %7B%7D%22, arr, arr.getClass());%0A            for (int i = 0; i %3C arr.size(); ++i) %7B%0A                int v = arr.get(i).asIntegerValue().getInt();%0A                assertEquals(i + 1, v);%0A            %7D%0A        %7D%0A        assertFalse(rs.next());%0A        rs.close();%0A        stat.close();%0A        conn.close();%0A%0A    %7D%0A%0A
 %7D%0A

Add a failing test for reading arary<bigint> value, but whose actual data type is json string (varchar)