package leigh.esp.goryu.engine;

import leigh.eo.EO;
import leigh.eo.io.LoggingEOOutputStream;
import leigh.eo.io.QueueEOInputStream;
import leigh.esp.goryu.sdk.schema.v1.MutationElementDAO;
import leigh.esp.goryu.sdk.schema.v1.QueryElementDAO;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;
import org.apache.commons.dbcp.BasicDataSource;
import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.Properties;

public class TransactionProcessorTest {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(TransactionProcessorTest.class);

    @Test
    public void testThing() throws IOException, SQLException {
        String currentDir = System.getProperty("user.dir");
        System.out.println("Current dir using System:" + currentDir);

        Properties p = new Properties();
        p.load(new FileInputStream("../test.properties"));
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setUrl("jdbc:mysql://db1.leigh-co.com/aleigh_test");
        dataSource.setUsername("aleigh");
        dataSource.setPassword(String.valueOf(p.get("db1_password")));

        TransactionProcessor tp = new TransactionProcessor();
        LinkedList<EO> request = new LinkedList<>();

        EO mut1 = MutationElementDAO.create();
        MutationElementDAO.setQuery(mut1, "TRUNCATE TABLE discounts");
        request.add(mut1);

        request.add(MutationElementDAO.create().setValue(MutationElementDAO.KEY_QUERY, "INSERT INTO discounts (expired_date, title, amount) value (now(), 'test', 1.23)"));

        EO q = QueryElementDAO.create();
        QueryElementDAO.setQuery(q, "SELECT * FROM discounts");
        request.add(q);

        tp.process(dataSource.getConnection(), new QueueEOInputStream(request), new LoggingEOOutputStream(logger));
    }
}
