/*
 * 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.core.mapper;

import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;

import org.immutables.value.Value;
import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.generic.GenericType;
import org.jdbi.v3.core.mapper.immutables.ImmutablesPlugin;
import org.jdbi.v3.core.rule.H2DatabaseRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class ImmutablesTest {
    @Rule
    public H2DatabaseRule dbRule = new H2DatabaseRule()
        .withPlugin(ImmutablesPlugin.forImmutable(SubValue.class))
        .withPlugin(ImmutablesPlugin.forImmutable(FooBarBaz.class))
        .withPlugin(ImmutablesPlugin.forModifiable(FooBarBaz.class));

    private Jdbi jdbi;
    private Handle h;

    @Before
    public void setup() {
        jdbi = dbRule.getJdbi();
        h = dbRule.getSharedHandle();
        h.execute("create table immutables (t int, x varchar)");
    }

    // tag::example[]
    // First, install the plugin: ;

    @Value.Immutable
    public interface Train {
        String name();
        int carriages();
        boolean observationCar();
    }

    @Test
    public void simpleTest() {
        jdbi.installPlugin(ImmutablesPlugin.forImmutable(Train.class));
        h.execute("create table train (name varchar, carriages int, observation_car boolean)");

        assertThat(
            h.createUpdate("insert into train(name, carriages, observation_car) values (:name, :carriages, :observationCar)")
                .bindPojo(ImmutableTrain.builder().name("Zephyr").carriages(8).observationCar(true).build())
                .execute())
            .isEqualTo(1);

        assertThat(
            h.createQuery("select * from train")
                .mapTo(Train.class)
                .findOnly())
            .extracting("name", "carriages", "observationCar")
            .containsExactly("Zephyr", 8, true);
    }
    // end::example[]

    @Test
    public void parameterizedTest() {
        assertThat(
            h.createUpdate("insert into immutables(t, x) values (:t, :x)")
                .bindPojo(ImmutableSubValue.<String, Integer>builder().t(42).x("foo").build())
                .execute())
            .isEqualTo(1);

        assertThat(
            h.createQuery("select * from immutables")
                .mapTo(new GenericType<SubValue<String, Integer>>() {})
                .findOnly())
            .extracting("t", "x")
            .containsExactly(42, "foo");
    }

    public interface BaseValue<T> {
        T t();
    }

    @Value.Immutable
    public interface SubValue<X, T> extends BaseValue<T> {
        X x();
    }

    @Value.Immutable
    @Value.Modifiable
    public interface FooBarBaz {
        int id();
        Optional<String> foo();
        OptionalInt bar();
        OptionalDouble baz();
    }

    @Test
    public void testModifiable() {
        h.execute("create table fbb (id serial, foo varchar, bar int, baz real)");

        assertThat(h.createUpdate("insert into fbb (id, foo, bar, baz) values (:id, :foo, :bar, :baz)")
                .bindPojo(ModifiableFooBarBaz.create().setFoo("foo").setBar(42).setBaz(1.0))
                .execute())
            .isEqualTo(1);

        assertThat(h.createQuery("select * from fbb")
                .mapTo(ModifiableFooBarBaz.class)
                .findOnly())
            .extracting("id", "foo", "bar", "baz")
            .containsExactly(1, Optional.of("foo"), OptionalInt.of(42), OptionalDouble.of(1.0));

        assertThat(h.createQuery("select * from fbb")
                .mapTo(ImmutableFooBarBaz.class)
                .findOnly())
            .extracting("id", "foo", "bar", "baz")
        // FIXME: https://github.com/joel-costigliola/assertj-core/pull/1360
            .containsExactly(1, "foo", 42, 1.0);
    }
}

@@ -1894,33 +1894,86 @@
 lass));%0A        
-h
+try (Handle handle = jdbi.open()) %7B%0A            handle
 .execute(%22create
@@ -2044,32 +2044,36 @@
 an)%22);%0A%0A        
+    
 assertThat(%0A    
@@ -2072,33 +2072,42 @@
 at(%0A            
+  
-h
+  handle
 .createUpdate(%22i
@@ -2211,32 +2211,36 @@
                 
+    
 .bindPojo(Immuta
@@ -2308,32 +2308,36 @@
 (true).build())%0A
+    
                 
@@ -2352,32 +2352,36 @@
 ())%0A            
+    
 .isEqualTo(1);%0A%0A
@@ -2372,32 +2372,36 @@
 .isEqualTo(1);%0A%0A
+    
         assertTh
@@ -2408,33 +2408,42 @@
 at(%0A            
-h
+    handle
 .createQuery(%22se
@@ -2474,24 +2474,28 @@
             
+    
 .mapTo(Train
@@ -2510,32 +2510,36 @@
                 
+ 
+   
 .findOnly())%0A   
@@ -2527,32 +2527,36 @@
    .findOnly())%0A
+    
             .ext
@@ -2606,32 +2606,36 @@
 r%22)%0A            
+    
 .containsExactly
@@ -2655,16 +2655,26 @@
  true);%0A
+        %7D%0A
     %7D%0A  

Fix test case