/*
 * 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.master.backcompat.ops;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import alluxio.AlluxioURI;
import alluxio.Constants;
import alluxio.client.WriteType;
import alluxio.client.file.FileSystem;
import alluxio.client.file.options.CreateFileOptions;
import alluxio.master.backcompat.FsTestOp;
import alluxio.master.backcompat.Utils;
import alluxio.security.authorization.Mode;
import alluxio.security.authorization.ModeParser;
import alluxio.wire.TtlAction;

import java.util.Arrays;

/**
 * Test for file creation.
 */
public final class CreateFile extends FsTestOp {
  private static final AlluxioURI PATH = new AlluxioURI("/createFile");
  private static final AlluxioURI NESTED = new AlluxioURI("/createFileNested/a");
  private static final Mode TEST_MODE = ModeParser.parse("u=rw,g=x");
  private static final AlluxioURI MODE = new AlluxioURI("/createFileMode/a");
  private static final AlluxioURI THROUGH = new AlluxioURI("/createFileThrough/a");
  private static final long TEST_TTL = Long.MAX_VALUE / 2;
  private static final AlluxioURI TTL = new AlluxioURI("/createFileTtl/a");

  @Override
  public void apply(FileSystem fs) throws Exception {
    Utils.createFile(fs, PATH);
    Utils.createFile(fs, NESTED);
    Utils.createFile(fs, MODE, CreateFileOptions.defaults().setBlockSizeBytes(Constants.KB)
        .setRecursive(true)
        .setMode(TEST_MODE));
    Utils.createFile(fs, THROUGH, CreateFileOptions.defaults().setBlockSizeBytes(Constants.KB)
        .setRecursive(true)
        .setWriteType(WriteType.THROUGH));
    Utils.createFile(fs, TTL, CreateFileOptions.defaults().setBlockSizeBytes(Constants.KB)
        .setRecursive(true)
        .setTtl(TEST_TTL)
        .setTtlAction(TtlAction.FREE));
  }

  @Override
  public void check(FileSystem fs) throws Exception {
    for (AlluxioURI file : Arrays.asList(PATH, NESTED, THROUGH, TTL)) {
      assertTrue(fs.exists(file));
    }
    assertEquals(TEST_MODE, new Mode((short) fs.getStatus(MODE).getMode()));
    assertTrue(fs.getStatus(THROUGH).isPersisted());
    assertEquals(TEST_TTL, fs.getStatus(TTL).getTtl());
    assertEquals(TtlAction.FREE, fs.getStatus(TTL).getTtlAction());
  }
}
