/*
 *  Copyright 2015 Red Hat, Inc.
 *
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  and Apache License v2.0 which accompanies this distribution.
 *
 *  The Eclipse Public License is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  The Apache License v2.0 is available at
 *  http://www.opensource.org/licenses/apache2.0.php
 *
 *  You may elect to redistribute this code under either of these licenses.
 */

package io.vertx.ext.asyncsql;

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.test.core.VertxTestBase;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;

/**
 * @author <a href="http://www.campudus.com">Joern Bernhardt</a>.
 */
public class PostgreSQLTest extends VertxTestBase {

  AsyncSQLClient asyncSqlClient;

  final String address = "campudus.postgresql";

  final JsonObject config = new JsonObject()
      .put("host", System.getProperty("db.host", "localhost"))
      .put("postgresql", new JsonObject().put("address", address));

  @Override
  public void setUp() throws Exception {
    super.setUp();
    asyncSqlClient = PostgreSQLClient.createNonShared(vertx, config);
  }

  @Override
  public void tearDown() throws Exception {
    CountDownLatch latch;
    if (this.asyncSqlClient != null) {
      latch = new CountDownLatch(1);
      this.asyncSqlClient.close((ar) -> {
        latch.countDown();
      });
      this.awaitLatch(latch);
    }

    super.tearDown();
  }

  @Test
  public void someTest() throws Exception {
    asyncSqlClient.getConnection(onSuccess(conn -> {
      conn.query("SELECT 1 AS something", onSuccess(resultSet -> {
        System.out.println(resultSet.getResults());
        assertNotNull(resultSet);
        assertNotNull(resultSet.getColumnNames());
        assertNotNull(resultSet.getResults());
        assertEquals(new JsonArray().add(1), resultSet.getResults().get(0));
        conn.close((ar) -> {
          if (ar.succeeded()) {
            testComplete();
          } else {
            fail("should be able to close the asyncSqlClient");
          }
        });
      }));
    }));

    await();
  }

  @Test
  public void queryTypeTimestampWithTimezoneTest() throws Exception {
    asyncSqlClient.getConnection(onSuccess(conn -> {
      conn.execute("CREATE TABLE IF NOT EXISTS timestamptest (ts timestamp with time zone)", onSuccess(resultSet -> {
        conn.execute("INSERT INTO timestamptest (ts) VALUES (now())", onSuccess(rs1 -> {
          conn.query("SELECT * FROM timestamptest;", onSuccess(rs2 -> {

            conn.close((ar) -> {
              if (ar.succeeded()) {
                testComplete();
              } else {
                fail("should be able to close the asyncSqlClient");
              }
            });
          }));
        }));
      }));
    }));

    await();
  }
}

@@ -2650,16 +2650,876 @@
 s2 -%3E %7B%0A
+            assertNotNull(rs2);%0A            assertNotNull(rs2.getResults());%0A            conn.close((ar) -%3E %7B%0A              if (ar.succeeded()) %7B%0A                testComplete();%0A              %7D else %7B%0A                fail(%22should be able to close the asyncSqlClient%22);%0A              %7D%0A            %7D);%0A          %7D));%0A        %7D));%0A      %7D));%0A    %7D));%0A%0A    await();%0A  %7D%0A%0A  @Test%0A  public void queryTypeTimestampWithoutTimezoneTest() throws Exception %7B%0A    asyncSqlClient.getConnection(onSuccess(conn -%3E %7B%0A      conn.execute(%22CREATE TABLE IF NOT EXISTS timestamptest (ts timestamp without time zone)%22, onSuccess(resultSet -%3E %7B%0A        conn.execute(%22INSERT INTO timestamptest (ts) VALUES (now())%22, onSuccess(rs1 -%3E %7B%0A          conn.query(%22SELECT * FROM timestamptest;%22, onSuccess(rs2 -%3E %7B%0A            assertNotNull(rs2);%0A            assertNotNull(rs2.getResults());
 %0A       

test both timestamp types.