/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jdbi.v3;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.jdbi.v3.tweak.ColumnMapper;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class TestPreparedBatchGenerateKeysPostgres {

    private Handle h;

    @BeforeClass
    public static void isPostgresInstalled() {
        assumeTrue(Boolean.parseBoolean(System.getenv("TRAVIS")));
    }

    @Before
    public void setUp() throws Exception {
        h = Jdbi.open("jdbc:postgresql:jdbi_test", "postgres", "");
        h.execute("drop table if exists something");
        h.execute("create table something (id serial, name varchar(50), create_time timestamp default now())");
    }

    @After
    public void tearDown() throws Exception {
        h.execute("drop table something");
        h.close();
    }

    @Test
    public void testBatchInsertWithKeyGenerationAndExplicitColumnNames() {
        PreparedBatch batch = h.prepareBatch("insert into something (name) values (?) ");
        batch.add("Brian");
        batch.add("Thom");

        List<Integer> ids = batch.executeAndGenerateKeys(Integer.class, "id").list();
        assertEquals(Arrays.asList(1, 2), ids);

        List<Something> somethings = h.createQuery("select id, name from something")
                .mapToBean(Something.class)
                .list();
        assertEquals(Arrays.asList(new Something(1, "Brian"), new Something(2, "Thom")), somethings);
    }

    @Test
    public void testBatchInsertWithKeyGenerationAndExplicitSeveralColumnNames() {
        PreparedBatch batch = h.prepareBatch("insert into something (name) values (?) ");
        batch.add("Brian");
        batch.add("Thom");

        List<IdCreateTime> ids = batch.executeAndGenerateKeys((r, ctx) -> {
            return new IdCreateTime(r.getInt("id"), r.getDate("create_time"));
        }, "id", "create_time").list();

        assertEquals(ids.size(), 2);
        assertTrue(ids.get(0).id == 1);
        assertTrue(ids.get(1).id == 2);
        assertNotNull(ids.get(0).createTime);
        assertNotNull(ids.get(1).createTime);
    }

    private static class IdCreateTime {

        final Integer id;
        final Date createTime;

        public IdCreateTime(Integer id, Date createTime) {
            this.id = id;
            this.createTime = createTime;
        }
    }
}

@@ -710,51 +710,8 @@
 rue;
-%0Aimport static org.junit.Assume.assumeTrue;
 %0A%0Aim
@@ -784,47 +784,8 @@
 t;%0A%0A
-import org.jdbi.v3.tweak.ColumnMapper;%0A
 impo
@@ -846,27 +846,20 @@
 g.junit.
-BeforeClass
+Rule
 ;%0Aimport
@@ -938,341 +938,172 @@
     
-private Handle h;%0A%0A    @BeforeClass%0A    public static void isPostgresInstalled() %7B%0A        assumeTrue(Boolean.parseBoolean(System.getenv(%22TRAVIS%22)));
+@Rule%0A    public PGDatabaseRule pgdb = new PGDatabaseRule().withPreparer(new JdbiPreparer() %7B
 %0A    
-%7D%0A%0A
     @
-Before%0A    public void setUp() throws Exception %7B%0A        h = Jdbi.open(%22jdbc:postgresql:jdbi_test%22, %22postgres%22, %22%22);%0A        h.execute(%22drop table if exists something%22);%0A
+Override%0A        protected void prepare(Handle handle) %7B%0A    
     
@@ -1099,32 +1099,37 @@
  %7B%0A            h
+andle
 .execute(%22create
@@ -1223,21 +1223,57 @@
     
-%7D
+    %7D%0A    %7D);%0A%0A    private Handle h;
 %0A%0A    @
-After
+Before
 %0A   
@@ -1289,80 +1289,99 @@
 oid 
-tearDown() throws Exception %7B
+getHandle() %7B%0A        h = pgdb.getDbi().open();
 %0A    
+%7D%0A%0A
     
-h.execute(%22drop table something%22);
+@After%0A    public void close() %7B
 %0A   

Convert TestPreparedBatchGenerateKeysPostgres to use PGDatabaseRule