/*
 * 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;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import io.prestosql.tempto.BeforeTestWithContext;
import io.prestosql.tempto.ProductTest;
import io.prestosql.tempto.hadoop.hdfs.HdfsClient;
import io.prestosql.tempto.query.QueryExecutor;
import org.testng.annotations.Test;

import static io.prestosql.tests.TestGroups.HDFS_IMPERSONATION;
import static io.prestosql.tests.TestGroups.HDFS_NO_IMPERSONATION;
import static io.prestosql.tests.TestGroups.PROFILE_SPECIFIC_TESTS;
import static io.prestosql.tests.utils.QueryExecutors.connectToPresto;
import static java.lang.String.format;
import static org.testng.Assert.assertEquals;

public class TestImpersonation
        extends ProductTest
{
    private QueryExecutor aliceExecutor;

    @Inject
    private HdfsClient hdfsClient;

    @Inject
    @Named("databases.alice@presto.jdbc_user")
    private String aliceJdbcUser;

    // The value for configuredHdfsUser is profile dependent
    // For non-Kerberos environments this variable will be equal to -DHADOOP_USER_NAME as set in jvm.config
    // For Kerberized environments this variable will be equal to the hive.hdfs.presto.principal property as set in hive.properties
    @Inject
    @Named("databases.presto.configured_hdfs_user")
    private String configuredHdfsUser;

    @Inject
    @Named("databases.hive.warehouse_directory_path")
    private String warehouseDirectoryPath;

    @BeforeTestWithContext
    public void setup()
    {
        aliceExecutor = connectToPresto("alice@presto");
    }

    @Test(groups = {HDFS_NO_IMPERSONATION, PROFILE_SPECIFIC_TESTS})
    public void testHdfsImpersonationDisabled()
    {
        String tableName = "check_hdfs_impersonation_disabled";
        checkTableOwner(tableName, configuredHdfsUser, aliceExecutor);
    }

    @Test(groups = {HDFS_IMPERSONATION, PROFILE_SPECIFIC_TESTS})
    public void testHdfsImpersonationEnabled()
    {
        String tableName = "check_hdfs_impersonation_enabled";
        checkTableOwner(tableName, aliceJdbcUser, aliceExecutor);
    }

    private String getTableLocation(String tableName)
    {
        return warehouseDirectoryPath + '/' + tableName;
    }

    private void checkTableOwner(String tableName, String expectedOwner, QueryExecutor executor)
    {
        executor.executeQuery(format("DROP TABLE IF EXISTS %s", tableName));
        executor.executeQuery(format("CREATE TABLE %s AS SELECT 'abc' c", tableName));
        String tableLocation = getTableLocation(tableName);
        String owner = hdfsClient.getOwner(tableLocation);
        assertEquals(owner, expectedOwner);
        executor.executeQuery(format("DROP TABLE IF EXISTS %s", tableName));
    }
}

@@ -880,16 +880,140 @@
 .Test;%0A%0A
+import java.net.URI;%0Aimport java.net.URISyntaxException;%0A%0Aimport static com.google.common.collect.Iterables.getOnlyElement;%0A
 import s
@@ -2014,118 +2014,8 @@
 r;%0A%0A
-    @Inject%0A    @Named(%22databases.hive.warehouse_directory_path%22)%0A    private String warehouseDirectoryPath;%0A%0A
     
@@ -2657,24 +2657,31 @@
     private 
+static 
 String getTa
@@ -2692,16 +2692,40 @@
 ocation(
+QueryExecutor executor, 
 String t
@@ -2752,55 +2752,460 @@
     
-return warehouseDirectoryPath + '/' + tableName
+String location = getOnlyElement(executor.executeQuery(format(%22SELECT DISTINCT regexp_replace(%5C%22$path%5C%22, '/%5B%5E/%5D*$', '') FROM %25s%22, tableName)).column(1));%0A        if (location.startsWith(%22hdfs://%22)) %7B%0A            try %7B%0A                URI uri = new URI(location);%0A                return uri.getPath();%0A            %7D%0A            catch (URISyntaxException e) %7B%0A                throw new RuntimeException(e);%0A            %7D%0A        %7D%0A        return location
 ;%0A  
@@ -3524,16 +3524,26 @@
 ocation(
+executor, 
 tableNam

Use actual table location in TestImpersonation