/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.client.file;

import alluxio.AlluxioURI;
import alluxio.client.ReadType;
import alluxio.client.WriteType;
import alluxio.client.file.FileOutStream;
import alluxio.client.file.FileSystem;
import alluxio.client.file.URIStatus;
import alluxio.client.file.options.CreateFileOptions;
import alluxio.client.file.options.OpenFileOptions;
import alluxio.exception.AlluxioException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.concurrent.ThreadSafe;

/**
 * Utility class for testing the Alluxio file system.
 */
@ThreadSafe
public final class FileSystemTestUtils {
  /**
   * Creates a simple file with {@code len} bytes.
   *
   * @param fs a {@link FileSystem} handler
   * @param fileName the name of the file to be created
   * @param len file size in bytes
   * @param options options to create the file with
   * @throws IOException if an IO exception occurs
   * @throws AlluxioException if an Alluxio exception occurs
   */
  public static void createByteFile(FileSystem fs, String fileName, int len,
      CreateFileOptions options) throws IOException, AlluxioException {
    createByteFile(fs, new AlluxioURI(fileName), options, len);
  }

  /**
   * Creates a simple file with {@code len} bytes.
   *
   * @param fs a {@link FileSystem} handler
   * @param fileName the name of the file to be created
   * @param writeType {@link WriteType} used to create the file
   * @param len file size
   * @throws IOException if an IO exception occurs
   * @throws AlluxioException if an Alluxio exception occurs
   */
  public static void createByteFile(FileSystem fs, String fileName,
      WriteType writeType, int len) throws IOException, AlluxioException {
    createByteFile(fs, new AlluxioURI(fileName), writeType, len);
  }

  /**
   * Creates a simple file with {@code len} bytes.
   *
   * @param fs a {@link FileSystem} handler
   * @param fileURI URI of the file
   * @param writeType {@link WriteType} used to create the file
   * @param len file size
   * @throws IOException if an IO exception occurs
   * @throws AlluxioException if an Alluxio exception occurs
   */
  public static void createByteFile(FileSystem fs, AlluxioURI fileURI,
      WriteType writeType, int len) throws IOException, AlluxioException {
    CreateFileOptions options = CreateFileOptions.defaults().setWriteType(writeType);
    createByteFile(fs, fileURI, options, len);
  }

  /**
   * Creates a simple file with {@code len} bytes.
   *
   * @param fs a {@link FileSystem} handler
   * @param fileURI URI of the file
   * @param options client options to create the file with
   * @param len file size
   * @throws IOException if an IO exception occurs
   * @throws AlluxioException if an Alluxio exception occurs
   */
  public static void createByteFile(FileSystem fs, AlluxioURI fileURI,
      CreateFileOptions options, int len) throws IOException, AlluxioException {
    try (FileOutStream os = fs.createFile(fileURI, options)) {
      byte[] arr = new byte[len];
      for (int k = 0; k < len; k++) {
        arr[k] = (byte) k;
      }
      os.write(arr);
    }
  }

  /**
   * Creates a simple file with {@code len} bytes.
   *
   * @param fs a {@link FileSystem} handler
   * @param fileName the name of the file to be created
   * @param writeType {@link WriteType} used to create the file
   * @param len file size
   * @param blockCapacityByte block size of the file
   * @throws IOException if an IO exception occurs
   * @throws AlluxioException if an Alluxio exception occurs
   */
  public static void createByteFile(FileSystem fs, String fileName,
      WriteType writeType, int len, long blockCapacityByte) throws IOException, AlluxioException {
    CreateFileOptions options =
        CreateFileOptions.defaults().setWriteType(writeType).setBlockSizeBytes(blockCapacityByte);
    createByteFile(fs, new AlluxioURI(fileName), options, len);
  }

  /**
   * Returns a list of files at a given {@code path}.
   *
   * @param fs a {@link FileSystem} handler
   * @param path a path in alluxio file system
   * @return a list of strings representing the file names under the given path
   */
  public static List<String> listFiles(FileSystem fs, String path) {
    try {
      List<URIStatus> statuses = fs.listStatus(new AlluxioURI(path));
      List<String> res = new ArrayList<>();
      for (URIStatus status : statuses) {
        res.add(status.getPath());
        if (status.isFolder()) {
          res.addAll(listFiles(fs, status.getPath()));
        }
      }
      return res;
    } catch (IOException | AlluxioException e) {
      throw new RuntimeException(e);
    }
  }

  /**
   * Converts a {@link CreateFileOptions} object to an {@link OpenFileOptions} object with a
   * matching Alluxio storage type.
   *
   * @param op a {@link CreateFileOptions} object
   * @return an {@link OpenFileOptions} object with a matching Alluxio storage type
   */
  public static OpenFileOptions toOpenFileOptions(CreateFileOptions op) {
    if (op.getWriteType().getAlluxioStorageType().isStore()) {
      return OpenFileOptions.defaults().setReadType(ReadType.CACHE);
    }
    return OpenFileOptions.defaults().setReadType(ReadType.NO_CACHE);
  }

  private FileSystemTestUtils() {} // prevent instantiation
}

@@ -633,127 +633,8 @@
 pe;%0A
-import alluxio.client.file.FileOutStream;%0Aimport alluxio.client.file.FileSystem;%0Aimport alluxio.client.file.URIStatus;%0A
 impo
@@ -1275,120 +1275,8 @@
 ith%0A
-   * @throws IOException if an IO exception occurs%0A   * @throws AlluxioException if an Alluxio exception occurs%0A
    *
@@ -1390,45 +1390,8 @@
 ons)
- throws IOException, AlluxioException
  %7B%0A 
@@ -1714,120 +1714,8 @@
 ize%0A
-   * @throws IOException if an IO exception occurs%0A   * @throws AlluxioException if an Alluxio exception occurs%0A
    *
@@ -1823,45 +1823,8 @@
 len)
- throws IOException, AlluxioException
  %7B%0A 
@@ -2129,120 +2129,8 @@
 ize%0A
-   * @throws IOException if an IO exception occurs%0A   * @throws AlluxioException if an Alluxio exception occurs%0A
    *
@@ -2241,45 +2241,8 @@
 len)
- throws IOException, AlluxioException
  %7B%0A 
@@ -2609,120 +2609,8 @@
 ize%0A
-   * @throws IOException if an IO exception occurs%0A   * @throws AlluxioException if an Alluxio exception occurs%0A
    *
@@ -2681,22 +2681,16 @@
 fileURI,
-%0A     
  CreateF
@@ -2708,16 +2708,22 @@
 options,
+%0A     
  int len
@@ -2727,45 +2727,8 @@
 len)
- throws IOException, AlluxioException
  %7B%0A 
@@ -2917,16 +2917,102 @@
 e(arr);%0A
+    %7D catch (IOException %7C AlluxioException e) %7B%0A      throw new RuntimeException(e);%0A
     %7D%0A  
@@ -3323,120 +3323,8 @@
 ile%0A
-   * @throws IOException if an IO exception occurs%0A   * @throws AlluxioException if an Alluxio exception occurs%0A
    *
@@ -3384,38 +3384,32 @@
 String fileName,
-%0A     
  WriteType write
@@ -3422,16 +3422,22 @@
 int len,
+%0A     
  long bl
@@ -3456,45 +3456,8 @@
 yte)
- throws IOException, AlluxioException
  %7B%0A 

Simplify exception usage in FileSystemTestUtils