package org.apache.flink.api.java.io.jdbc.catalog;

import org.apache.flink.table.api.EnvironmentSettings;
import org.apache.flink.table.api.TableEnvironment;
import org.apache.flink.table.api.TableUtils;
import org.apache.flink.types.Row;

import org.junit.Test;

import java.util.List;

import static org.apache.flink.api.java.io.jdbc.catalog.PostgresCatalog.DEFAULT_DATABASE;
import static org.apache.flink.table.api.config.ExecutionConfigOptions.TABLE_EXEC_RESOURCE_DEFAULT_PARALLELISM;
import static org.junit.Assert.assertEquals;

/**
 * E2E test for {@link PostgresCatalog}.
 */
public class PostgresCatalogITCase extends PostgresCatalogTestBase {

	@Test
	public void test_withoutSchema() throws Exception {
		TableEnvironment tEnv = getTableEnvWithPgCatalog();

		List<Row> results = TableUtils.collectToList(
			tEnv.sqlQuery(String.format("select * from %s", TABLE1)));
		assertEquals("[1]", results.toString());
	}

	@Test
	public void test_withSchema() throws Exception {
		TableEnvironment tEnv = getTableEnvWithPgCatalog();

		List<Row> results = TableUtils.collectToList(
			tEnv.sqlQuery(String.format("select * from `%s`", PostgresTablePath.fromFlinkTableName(TABLE1))));
		assertEquals("[1]", results.toString());
	}

	@Test
	public void test_fullPath() throws Exception {
		TableEnvironment tEnv = getTableEnvWithPgCatalog();

		List<Row> results = TableUtils.collectToList(
			tEnv.sqlQuery(String.format("select * from %s.%s.`%s`",
				TEST_CATALOG_NAME,
				DEFAULT_DATABASE,
				PostgresTablePath.fromFlinkTableName(TABLE1))));
		assertEquals("[1]", results.toString());
	}

	@Test
	public void test_insert() throws Exception {
		TableEnvironment tEnv = getTableEnvWithPgCatalog();

		tEnv.sqlUpdate(String.format("insert into %s select * from `%s`", TABLE4, TABLE1));
		tEnv.execute("test");

		List<Row> results = TableUtils.collectToList(
			tEnv.sqlQuery(String.format("select * from %s", TABLE1)));
		assertEquals("[1]", results.toString());
	}

	private TableEnvironment getTableEnvWithPgCatalog() {
		EnvironmentSettings settings = EnvironmentSettings.newInstance().useBlinkPlanner().inStreamingMode().build();
		TableEnvironment tableEnv = TableEnvironment.create(settings);
		tableEnv.getConfig().getConfiguration().setInteger(TABLE_EXEC_RESOURCE_DEFAULT_PARALLELISM.key(), 1);

		tableEnv.registerCatalog(TEST_CATALOG_NAME, catalog);
		tableEnv.useCatalog(TEST_CATALOG_NAME);
		return tableEnv;
	}
}

@@ -1,8 +1,815 @@
+/*%0A * Licensed to the Apache Software Foundation (ASF) under one%0A * or more contributor license agreements.  See the NOTICE file%0A * distributed with this work for additional information%0A * regarding copyright ownership.  The ASF licenses this file%0A * to you under the Apache License, Version 2.0 (the%0A * %22License%22); you may not use this file except in compliance%0A * with the License.  You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0A
 package 

add license header to PostgresCatalogITCase