/*
 * Licensed to Crate under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.  Crate licenses this file
 * to you 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.
 *
 * However, if you have executed another commercial license agreement
 * with Crate these terms will supersede the license and you may use the
 * software solely pursuant to the terms of the relevant commercial
 * agreement.
 */

package io.crate.client.jdbc.integrationtests;

import com.carrotsearch.randomizedtesting.RandomizedTest;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope;
import io.crate.testing.CrateTestCluster;
import io.crate.testing.CrateTestServer;
import org.junit.AfterClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.rules.ExpectedException;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;

@ThreadLeakScope(ThreadLeakScope.Scope.SUITE)
public class CrateJDBCIntegrationTest extends RandomizedTest {

    private static final String CRATE_SERVER_VERSION = "0.57.0";

    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    @ClassRule
    public static CrateTestCluster testCluster = CrateTestCluster
        .fromVersion(CRATE_SERVER_VERSION)
        .keepWorkingDir(false)
        .build();

    @AfterClass
    public static void tearDown() throws Exception {
        tearDownTables();
    }

    static String getConnectionString() {
        CrateTestServer server = testCluster.randomServer();
        return String.format("crate://%s:%s/", server.crateHost(), server.psqlPort());
    }

    private static void tearDownTables() throws SQLException {
        try (Connection conn = DriverManager.getConnection(getConnectionString())) {
            ResultSet rs = conn.createStatement()
                .executeQuery("select schema_name, table_name " +
                              "from information_schema.tables where schema_name " +
                              "not in ('pg_catalog', 'sys', 'information_schema', 'blob')");
            while (rs.next()) {
                conn.createStatement().execute(String.format(
                    "drop table if exists \"%s\".\"%s\"", rs.getString("schema_name"), rs.getString("table_name")
                ));
            }
        }
    }

    static void setUpTestTable() throws SQLException, InterruptedException {
        try (Connection conn = DriverManager.getConnection(getConnectionString())) {
            conn.createStatement().execute(
                "create table if not exists test (" +
                " id integer primary key," +
                " string_field string," +
                " boolean_field boolean," +
                " byte_field byte," +
                " short_field short," +
                " integer_field integer," +
                " long_field long," +
                " float_field float," +
                " double_field double," +
                " timestamp_field timestamp," +
                " object_field object as (\"inner\" string)," +
                " ip_field ip" +
                ") clustered by (id) into 1 shards with (number_of_replicas=0)");
        }
        ensureYellow();
    }

    static void insertIntoTestTable() throws SQLException {
        Map<String, Object> objectField = new HashMap<String, Object>() {{
            put("inner", "Zoon");
        }};
        try (Connection conn = DriverManager.getConnection(getConnectionString())) {
            PreparedStatement preparedStatement =
                conn.prepareStatement("insert into test (id, string_field, boolean_field, byte_field, " +
                                      "short_field, integer_field, long_field, float_field, double_field, object_field, " +
                                      "timestamp_field, ip_field) values " +
                                      "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            preparedStatement.setInt(1, 1);
            preparedStatement.setString(2, "Youri");
            preparedStatement.setBoolean(3, true);
            preparedStatement.setByte(4, (byte) 120);
            preparedStatement.setShort(5, (short) 1000);
            preparedStatement.setInt(6, 1200000);
            preparedStatement.setLong(7, 120000000000L);
            preparedStatement.setFloat(8, 1.4f);
            preparedStatement.setDouble(9, 3.456789);
            preparedStatement.setObject(10, objectField);
            preparedStatement.setTimestamp(11, new Timestamp(1000L));
            preparedStatement.setString(12, "127.0.0.1");
            preparedStatement.execute();
            conn.createStatement().execute("refresh table test");
        }
    }

    static void ensureYellow() throws SQLException, InterruptedException {
        while (countUnassigned() > 0) {
            Thread.sleep(100);
        }
    }

    private static Long countUnassigned() throws SQLException {
        try (Connection conn = DriverManager.getConnection(getConnectionString())) {
            ResultSet rs = conn.createStatement()
                .executeQuery("SELECT count(*) FROM sys.shards WHERE state != 'STARTED'");
            rs.next();
            return rs.getLong(1);
        }
    }
}

@@ -1441,16 +1441,41 @@
 til.Map;
+%0Aimport java.util.Random;
 %0A%0A@Threa
@@ -1582,74 +1582,8 @@
  %7B%0A%0A
-    private static final String CRATE_SERVER_VERSION = %220.57.0%22;%0A%0A
     
@@ -1770,28 +1770,32 @@
 ion(
-CRATE_SERVER_VERSION
+getRandomServerVersion()
 )%0A  
@@ -1838,24 +1838,257 @@
  .build();%0A%0A
+    private static String getRandomServerVersion() %7B%0A        String%5B%5D crateVersions = new String%5B%5D%7B%220.56.0%22, %220.57.0%22, %220.56.1%22%7D;%0A        Random r = new Random();%0A        return crateVersions%5Br.nextInt(crateVersions.length)%5D;%0A    %7D%0A%0A
     @AfterCl

implement the check version table_schema/schema_name