// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package de.bytefish.pgbulkinsert.test.pgsql.handlers;

import de.bytefish.pgbulkinsert.PgBulkInsert;
import de.bytefish.pgbulkinsert.mapping.AbstractMapping;
import de.bytefish.pgbulkinsert.test.utils.TransactionalTestBase;
import de.bytefish.pgbulkinsert.util.PostgreSqlUtils;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HstoreExtensionTest extends TransactionalTestBase {

    private class HStoreEntity {

        private Map<String, String> col_hstore;

        public Map<String, String> getCol_hstore() {
            return col_hstore;
        }

        public void setCol_hstore(Map<String, String> col_hstore) {
            this.col_hstore = col_hstore;
        }
    }

    @Override
    protected void onSetUpInTransaction() throws Exception {
        createTable();
    }

    @Override
    protected void onSetUpBeforeTransaction() throws Exception {

    }

    private class HStoreEntityMapping extends AbstractMapping<HStoreEntity> {

        public HStoreEntityMapping() {
            super(schema, "hstore_table");

            mapHstore("col_hstore", HStoreEntity::getCol_hstore);
        }
    }

    private boolean createTable() throws SQLException {
        String sqlStatement = String.format("CREATE TABLE %s.hstore_table(\n", schema) +
                "                col_hstore hstore \n" +
                "            );";

        Statement statement = connection.createStatement();

        return statement.execute(sqlStatement);
    }

    @Test
    @Ignore("This Test Requires the hstore extension to be enabled.")
    @SuppressWarnings("unchecked")
    public void saveAll_Hstore_Test() throws SQLException {

        // This list will be inserted.
        List<HStoreEntity> entities = new ArrayList<>();

        // Create the Map to Store:
        Map<String, String> hstoreData = new HashMap<>();


        hstoreData.put("Philipp", "Cool Cool Cool!");

        // Create the Entity to insert:
        HStoreEntity entity = new HStoreEntity();

        entity.setCol_hstore(hstoreData);

        entities.add(entity);

        PgBulkInsert<HStoreEntity> bulkInsert = new PgBulkInsert<>(new HStoreEntityMapping());

        bulkInsert.saveAll(PostgreSqlUtils.getPGConnection(connection), entities.stream());

        ResultSet rs = getAll();

        while (rs.next()) {
            Map<String, String> v = (Map<String,String>) rs.getObject("col_hstore");

            Assert.assertEquals(1, v.size());
            Assert.assertEquals(true, v.containsKey("Philipp"));
            Assert.assertEquals("Cool Cool Cool!", v.get("Philipp"));
        }
    }

    private ResultSet getAll() throws SQLException {
        String sqlStatement = String.format("SELECT * FROM %s.hstore_table", schema);

        Statement statement = connection.createStatement();

        return statement.executeQuery(sqlStatement);
    }




}

@@ -403,33 +403,8 @@
 rt;%0A
-import org.junit.Ignore;%0A
 impo
@@ -422,16 +422,16 @@
 t.Test;%0A
+
 %0Aimport 
@@ -1045,32 +1045,60 @@
 ows Exception %7B%0A
+        installExtension();%0A
         createTa
@@ -1170,33 +1170,16 @@
 action()
- throws Exception
  %7B%0A%0A    
@@ -1418,32 +1418,282 @@
        %7D%0A    %7D%0A%0A
+    private boolean installExtension() throws SQLException %7B%0A        String sqlStatement = %22CREATE EXTENSION IF NOT EXISTS hstore;%22;%0A%0A        Statement statement = connection.createStatement();%0A%0A        return statement.execute(sqlStatement);%0A    %7D%0A%0A
     private bool
@@ -2043,78 +2043,8 @@
 est%0A
-    @Ignore(%22This Test Requires the hstore extension to be enabled.%22)%0A
     
@@ -2960,21 +2960,13 @@
 sert
-Equals(true, 
+True(
 v.co

install extension as part of test