/*
 * 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 jdbi.doc;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.PGDatabaseRule;
import org.jdbi.v3.core.mapper.ConstructorMapper;
import org.jdbi.v3.sqlobject.GetGeneratedKeys;
import org.jdbi.v3.sqlobject.SqlBatch;
import org.jdbi.v3.sqlobject.SqlObjectPlugin;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

public class GeneratedKeysTest {
    @Rule
    public PGDatabaseRule db = new PGDatabaseRule().withPlugin(new SqlObjectPlugin());
    private Jdbi dbi;

    @Before
    public void getHandle() {
        dbi = db.getDbi();
    }

    // tag::setup[]
    public static class User {
        final int id;
        final String name;

        public User(int id, String name) {
            this.id = id;
            this.name = name;
        }
    }

    @Before
    public void setUp() throws Exception {
        dbi.useHandle(h -> h.execute("CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR)"));
        dbi.registerRowMapper(ConstructorMapper.factoryFor(User.class));
    }
    // end::setup[]

    @Test
    // tag::fluent[]
    public void fluentInsertKeys() {
        dbi.useHandle(handle -> {
            User data = handle.createStatement("INSERT INTO users (name) VALUES(?)")
                    .bind(0, "Data")
                    .executeAndReturnGeneratedKeys(User.class)
                    .findOnly();

            assertEquals(1, data.id); // This value is generated by the database
            assertEquals("Data", data.name);
        });
    }
    // end::fluent[]

    @Test
    // tag::sqlObject[]
    public void sqlObjectBatchKeys() {
        dbi.useExtension(UserDao.class, dao -> {
            List<User> users = dao.createUsers("Alice", "Bob", "Charlie");
            assertEquals(3, users.size());

            assertEquals(1, users.get(0).id);
            assertEquals("Alice", users.get(0).name);

            assertEquals(2, users.get(1).id);
            assertEquals("Bob", users.get(1).name);

            assertEquals(3, users.get(2).id);
            assertEquals("Charlie", users.get(2).name);
        });
    }

    interface UserDao {
        @SqlBatch("INSERT INTO users (name) VALUES(?)")
        @GetGeneratedKeys
        List<User> createUsers(String... names);
    }
    // end::sqlObject[]
}

@@ -695,17 +695,17 @@
 3.core.P
-G
+g
 Database
@@ -741,16 +741,24 @@
 .mapper.
+reflect.
 Construc
@@ -768,16 +768,64 @@
 Mapper;%0A
+import org.jdbi.v3.postgres.PostgresJdbiPlugin;%0A
 import o
@@ -1075,17 +1075,17 @@
 public P
-G
+g
 Database
@@ -1099,17 +1099,17 @@
  = new P
-G
+g
 Database
@@ -1114,16 +1114,25 @@
 seRule()
+%0A        
 .withPlu
@@ -1157,16 +1157,62 @@
 lugin())
+%0A        .withPlugin(new PostgresJdbiPlugin())
 ;%0A    pr
@@ -1290,17 +1290,18 @@
 = db.get
-D
+Jd
 bi();%0A  
@@ -1728,18 +1728,10 @@
 per.
+o
 f
-actoryFor
 (Use
@@ -1911,17 +1911,14 @@
 eate
-Statement
+Update
 (%22IN
@@ -2814,20 +2814,27 @@
 %0A    %7D%0A%0A
-
     
+public 
 interfac

Rebase fixup for docs tests