/*
 * 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 io.prestosql.tests.iceberg;

import io.prestosql.tempto.AfterTestWithContext;
import io.prestosql.tempto.BeforeTestWithContext;
import io.prestosql.tempto.ProductTest;
import io.prestosql.tempto.query.QueryExecutor;
import io.prestosql.testng.services.Flaky;
import org.testng.annotations.Test;

import static io.prestosql.tempto.assertions.QueryAssert.Row.row;
import static io.prestosql.tempto.assertions.QueryAssert.assertThat;
import static io.prestosql.tests.TestGroups.ICEBERG;
import static io.prestosql.tests.TestGroups.STORAGE_FORMATS;
import static io.prestosql.tests.hive.util.TemporaryHiveTable.randomTableSuffix;
import static io.prestosql.tests.utils.QueryExecutors.onPresto;

public class TestIcebergCreateTable
        extends ProductTest
{
    @BeforeTestWithContext
    public void setUp()
    {
        onPresto().executeQuery("CREATE SCHEMA iceberg.iceberg");
    }

    @AfterTestWithContext
    public void cleanUp()
    {
        onPresto().executeQuery("DROP SCHEMA iceberg.iceberg");
    }

    @Test(groups = {ICEBERG, STORAGE_FORMATS})
    @Flaky(issue = "https://github.com/prestosql/presto/issues/4864", match = "Failed to read footer of file")
    public void testCreateTable()
    {
        String tableName = "test_create_table_" + randomTableSuffix();
        QueryExecutor queryExecutor = onPresto();
        queryExecutor.executeQuery("use iceberg.iceberg");
        queryExecutor.executeQuery("CREATE TABLE " + tableName + "(a bigint, b varchar)");
        queryExecutor.executeQuery("INSERT INTO " + tableName + "(a, b) VALUES " +
                "(NULL, NULL), " +
                "(-42, 'abc'), " +
                "(9223372036854775807, 'abcdefghijklmnopqrstuvwxyz')");
        assertThat(queryExecutor.executeQuery("SELECT * FROM " + tableName))
                .containsOnly(
                        row(null, null),
                        row(-42, "abc"),
                        row(9223372036854775807L, "abcdefghijklmnopqrstuvwxyz"));
        queryExecutor.executeQuery("DROP TABLE " + tableName);
    }

    @Test(groups = {ICEBERG, STORAGE_FORMATS})
    @Flaky(issue = "https://github.com/prestosql/presto/issues/4864", match = "Failed to read footer of file")
    public void testCreateTableAsSelect()
    {
        String tableName = "test_create_table_as_select_" + randomTableSuffix();
        QueryExecutor queryExecutor = onPresto();
        queryExecutor.executeQuery("use iceberg.iceberg");
        queryExecutor.executeQuery("" +
                "CREATE TABLE " + tableName + " AS " +
                "SELECT * FROM (VALUES " +
                "  (NULL, NULL), " +
                "  (-42, 'abc'), " +
                "  (9223372036854775807, 'abcdefghijklmnopqrstuvwxyz')" +
                ") t(a, b)");
        assertThat(queryExecutor.executeQuery("SELECT * FROM " + tableName))
                .containsOnly(
                        row(null, null),
                        row(-42, "abc"),
                        row(9223372036854775807L, "abcdefghijklmnopqrstuvwxyz"));
        queryExecutor.executeQuery("DROP TABLE " + tableName);
    }
}

@@ -735,56 +735,8 @@
 st;%0A
-import io.prestosql.tempto.query.QueryExecutor;%0A
 impo
@@ -774,16 +774,16 @@
 .Flaky;%0A
+
 import o
@@ -1749,32 +1749,48 @@
 ng tableName = %22
+iceberg.iceberg.
 test_create_tabl
@@ -1828,130 +1828,18 @@
     
-QueryExecutor queryExecutor = onPresto();%0A        queryExecutor.executeQuery(%22use iceberg.iceberg%22);%0A        queryExecutor
+onPresto()
 .exe
@@ -1908,37 +1908,34 @@
 %22);%0A        
-queryExecutor
+onPresto()
 .executeQuer
@@ -2141,37 +2141,34 @@
  assertThat(
-queryExecutor
+onPresto()
 .executeQuer
@@ -2399,37 +2399,34 @@
 ));%0A        
-queryExecutor
+onPresto()
 .executeQuer
@@ -2696,16 +2696,32 @@
 Name = %22
+iceberg.iceberg.
 test_cre
@@ -2777,130 +2777,18 @@
     
-QueryExecutor queryExecutor = onPresto();%0A        queryExecutor.executeQuery(%22use iceberg.iceberg%22);%0A        queryExecutor
+onPresto()
 .exe
@@ -3097,29 +3097,26 @@
 ertThat(
-queryExecutor
+onPresto()
 .execute
@@ -3359,21 +3359,18 @@
     
-queryExecutor
+onPresto()
 .exe

Simplify QueryExecutor usage in TestIcebergCreateTable