
package com.loafle.overflow;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.loafle.overflow.crawler.config.Config;
import com.loafle.overflow.crawler.result.OFResultSet;
import com.loafle.overflow.crawler.sql.SQLCrawler;
import org.junit.Ignore;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AppTest {
    //        //MySQL
//        info.setUrl("jdbc:mysql://192.168.1.215:3306"); // ?useSSL=true&verifyServerCertificate=false
//        info.setId("root");
//        info.setPw("qwe123");
//        info.setSSL(false);
//        info.setQuery("show session status");
//        //MSSQL
//        info.setUrl("jdbc:sqlserver://192.168.1.103:1433;"); //encrypt=false;trustServerCertificate=true"
//        info.setId("sa");
//        info.setPw("qwer5795");
//        info.setSSL(true);
//        info.setQuery("select * from master.dbo.sysprocesses");
//        //PGSQL
//        info.setUrl("jdbc:postgresql://192.168.1.107:5432/postgres");
//        info.setId("postgres");
//        info.setPw("!@#$qwer1234");
//        info.setSSL(false);
//        info.setQuery("select * from pg_stat_activity");
//        //Oracle
//        info.setUrl("jdbc:oracle:thin:@192.168.1.30:1521/oracle.loafle.com");
//        info.setId("sys as sysdba");
//        info.setPw("qwer5795QWER");
//        info.setSSL(false);
//        info.setQuery("select * from v$sysstat");
    private Config getConfig(String path) throws IOException {
        ClassLoader classLoader = getClass().getClassLoader();
        String filePath = classLoader.getResource(path).getFile();

        ObjectMapper mapper = new ObjectMapper();
        Config c = mapper.readValue(new File(filePath),Config.class);
        return c;
    }

    private void print(String s, List<OFResultSet> mm) {

        System.out.println(s);

        Map<String,String> m = new HashMap<>();
        for(OFResultSet r : mm) {
            m.putAll(r.getData());
        }

        for (Map.Entry<String, String> item : m.entrySet()) {
            System.out.println("key=" + item.getKey() +  " |||||||||||  value=" +  item.getValue());
        }
    }

    @Test
    @Ignore
    public void testMYSQL() throws IOException {
        Object result = new SQLCrawler().getInternal( getConfig("config/mysql.json"));
        print("Mysql row", (List<OFResultSet>) result);
    }

    @Test
    @Ignore
    public void testPGSQL() throws IOException {
        Object result = new SQLCrawler().getInternal(getConfig("config/pgsql.json"));
        print("PGSql", (List<OFResultSet>) result);
    }

    @Test
    @Ignore
    public void testSQLServerConnection_Count() throws IOException {
        Object result = new SQLCrawler().getInternal(getConfig("config/sqlserver_connection_count.json"));
        print("MSSql Server", (List<OFResultSet>) result);

    }



    @Test
    @Ignore
    public void testSQLServerMultiKeyArray() throws IOException {
        Object result = new SQLCrawler().getInternal(getConfig("config/sqlserver_multiple_key_array.json"));
        print("MSSql Server MuoltiArray", (List< OFResultSet>) result);
    }
}