/*
 * 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 com.facebook.presto.hive;

import com.google.common.io.Files;
import org.apache.hadoop.fs.Path;
import org.testng.annotations.Test;

import java.util.UUID;

import static com.facebook.presto.hive.HiveTestUtils.SESSION;
import static com.facebook.presto.hive.HiveTestUtils.createTestHdfsEnvironment;
import static com.facebook.presto.hive.HiveWriteUtils.createTemporaryPath;
import static com.facebook.presto.hive.HiveWriteUtils.isS3FileSystem;
import static com.facebook.presto.hive.HiveWriteUtils.isViewFileSystem;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

public class TestHiveWriteUtils
{
    //HdfsContext CONTEXT = new HdfsContext(new ConnectorIdentity("test", Optional.empty(), Optional.empty()));
    private static final HdfsContext CONTEXT = new HdfsContext(SESSION, "test_schema");

    @Test
    public void testIsS3FileSystem()
    {
        HdfsEnvironment hdfsEnvironment = createTestHdfsEnvironment(new HiveClientConfig(), new MetastoreClientConfig());
        assertTrue(isS3FileSystem(CONTEXT, hdfsEnvironment, new Path("s3://test-bucket/test-folder")));
        assertFalse(isS3FileSystem(CONTEXT, hdfsEnvironment, new Path("/test-dir/test-folder")));
    }

    @Test
    public void testIsViewFileSystem()
    {
        HdfsEnvironment hdfsEnvironment = createTestHdfsEnvironment(new HiveClientConfig(), new MetastoreClientConfig());
        Path viewfsPath = new Path("viewfs://ns-default/");
        Path nonViewfsPath = new Path("hdfs://localhost/test-dir/test-folder");

        // ViewFS check requires the mount point config
        hdfsEnvironment.getConfiguration(CONTEXT, viewfsPath).set("fs.viewfs.mounttable.ns-default.link./test-folder", "hdfs://localhost/app");

        assertTrue(isViewFileSystem(CONTEXT, hdfsEnvironment, viewfsPath));
        assertFalse(isViewFileSystem(CONTEXT, hdfsEnvironment, nonViewfsPath));
    }

    @Test void testCreateTemporaryPathOnViewFS()
    {
        HdfsEnvironment hdfsEnvironment = createTestHdfsEnvironment(new HiveClientConfig(), new MetastoreClientConfig());
        Path viewfsPath = new Path("viewfs://ns-default/test-dir");

        // ViewFS check requires the mount point config
        hdfsEnvironment.getConfiguration(CONTEXT, viewfsPath).set("fs.viewfs.mounttable.ns-default.link./test-dir", "file://" + Files.createTempDir());

        Path temporaryPath = createTemporaryPath(SESSION, CONTEXT, hdfsEnvironment, viewfsPath);

        assertEquals(temporaryPath.getParent().toString(), "viewfs://ns-default/test-dir/.hive-staging");
        try {
            UUID.fromString(temporaryPath.getName());
        }
        catch (IllegalArgumentException e) {
            fail("Expected a UUID folder name ");
        }
    }

    @Test void testCreateTemporaryPathOnNonViewFS()
    {
        HdfsEnvironment hdfsEnvironment = createTestHdfsEnvironment(new HiveClientConfig(), new MetastoreClientConfig());
        Path nonViewfsPath = new Path("file://" + Files.createTempDir());

        Path temporaryPath = createTemporaryPath(SESSION, CONTEXT, hdfsEnvironment, nonViewfsPath);

        assertEquals(temporaryPath.getParent().toString(), "file:/tmp/presto-user");
        try {
            UUID.fromString(temporaryPath.getName());
        }
        catch (IllegalArgumentException e) {
            fail("Expected a UUID folder name ");
        }
    }
}

@@ -696,16 +696,37 @@
 .Test;%0A%0A
+import java.io.File;%0A
 import j
@@ -1312,120 +1312,8 @@
 s%0A%7B%0A
-    //HdfsContext CONTEXT = new HdfsContext(new ConnectorIdentity(%22test%22, Optional.empty(), Optional.empty()));%0A
     
@@ -2705,24 +2705,73 @@
 est-dir%22);%0A%0A
+        File storageDir = Files.createTempDir();%0A
         // V
@@ -2805,32 +2805,78 @@
 unt point config
+, using system temporary folder as the storage
 %0A        hdfsEnv
@@ -2992,130 +2992,1252 @@
 %22 + 
-Files.createTempDir());%0A%0A        Path temporaryPath = createTemporaryPath(SESSION, CONTEXT, hdfsEnvironment, viewfsPath);%0A
+storageDir);%0A%0A        //Make temporary folder under an existing data folder without staging folder %22.hive-staging%22%0A        Path temporaryPath = createTemporaryPath(SESSION, CONTEXT, hdfsEnvironment, viewfsPath);%0A%0A        assertEquals(temporaryPath.getParent().toString(), %22viewfs://ns-default/test-dir/.hive-staging%22);%0A        try %7B%0A            UUID.fromString(temporaryPath.getName());%0A        %7D%0A        catch (IllegalArgumentException e) %7B%0A            fail(%22Expected a UUID folder name %22);%0A        %7D%0A%0A        //Make temporary folder under an existing data folder with an existing staging folder %22.hive-staging%22%0A        temporaryPath = createTemporaryPath(SESSION, CONTEXT, hdfsEnvironment, viewfsPath);%0A%0A        assertEquals(temporaryPath.getParent().toString(), %22viewfs://ns-default/test-dir/.hive-staging%22);%0A        try %7B%0A            UUID.fromString(temporaryPath.getName());%0A        %7D%0A        catch (IllegalArgumentException e) %7B%0A            fail(%22Expected a UUID folder name %22);%0A        %7D%0A%0A        //Make temporary folder under a non-existing data folder (for new tables), it would use the temporary folder of the parent%0A        temporaryPath = createTemporaryPath(SESSION, CONTEXT, hdfsEnvironment, new Path(viewfsPath, %22non-existing%22));
 %0A   

Add test cases for making temp dir under both exist and non-exist folder