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

import alluxio.CommonTestUtils;
import alluxio.Configuration;
import alluxio.PropertyKey;
import alluxio.client.WriteType;
import alluxio.security.authorization.Mode;
import alluxio.thrift.CreateDirectoryTOptions;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.util.Random;

/**
 * Tests for the {@link CreateDirectoryOptions} class.
 */
@RunWith(PowerMockRunner.class)
// Need to mock Mode to use CommonTestUtils#testEquals.
@PrepareForTest(Mode.class)
public class CreateDirectoryOptionsTest {
  private final WriteType mDefaultWriteType =
      Configuration.getEnum(PropertyKey.USER_FILE_WRITE_TYPE_DEFAULT, WriteType.class);

  @Test
  public void defaults() {
    CreateDirectoryOptions options = CreateDirectoryOptions.defaults();
    Assert.assertFalse(options.isAllowExists());
    Assert.assertFalse(options.isRecursive());
    Assert.assertEquals(mDefaultWriteType, options.getWriteType());
  }

  /**
   * Tests getting and setting fields.
   */
  @Test
  public void fields() {
    Random random = new Random();
    boolean allowExists = random.nextBoolean();
    boolean recursive = random.nextBoolean();
    Mode mode = new Mode((short) 0123);
    WriteType writeType = WriteType.NONE;

    CreateDirectoryOptions options = CreateDirectoryOptions.defaults();
    options.setAllowExists(allowExists);
    options.setMode(mode);
    options.setRecursive(recursive);
    options.setWriteType(writeType);

    Assert.assertEquals(allowExists, options.isAllowExists());
    Assert.assertEquals(mode, options.getMode());
    Assert.assertEquals(recursive, options.isRecursive());
    Assert.assertEquals(writeType, options.getWriteType());
  }

  /**
   * Tests conversion to thrift representation.
   */
  @Test
  public void toThrift() {
    Random random = new Random();
    boolean allowExists = random.nextBoolean();
    boolean recursive = random.nextBoolean();
    Mode mode = new Mode((short) 0123);
    WriteType writeType = WriteType.NONE;

    CreateDirectoryOptions options = CreateDirectoryOptions.defaults();
    options.setAllowExists(allowExists);
    options.setMode(mode);
    options.setRecursive(recursive);
    options.setWriteType(writeType);

    CreateDirectoryTOptions thriftOptions = options.toThrift();
    Assert.assertEquals(allowExists, thriftOptions.isAllowExists());
    Assert.assertEquals(recursive, thriftOptions.isRecursive());
    Assert.assertEquals(writeType.getUnderStorageType().isSyncPersist(),
        thriftOptions.isPersisted());
    Assert.assertEquals(mode.toShort(), thriftOptions.getMode());
  }

  @Test
  public void equalsTest() throws Exception {
    CommonTestUtils.testEquals(CreateDirectoryOptions.class);
  }
}

@@ -607,16 +607,42 @@
 ration;%0A
+import alluxio.Constants;%0A
 import a
@@ -661,16 +661,16 @@
 rtyKey;%0A
-
 import a
@@ -780,24 +780,55 @@
 oryTOptions;
+%0Aimport alluxio.wire.TtlAction;
 %0A%0Aimport org
@@ -1595,32 +1595,160 @@
 isRecursive());%0A
+    Assert.assertEquals(Constants.NO_TTL, options.getTtl());%0A    Assert.assertEquals(TtlAction.DELETE, options.getTtlAction());%0A
     Assert.asser
@@ -2048,32 +2048,66 @@
 ((short) 0123);%0A
+    long ttl = random.nextLong();%0A
     WriteType wr
@@ -2265,32 +2265,99 @@
 .setMode(mode);%0A
+    options.setTtl(ttl);%0A    options.setTtlAction(TtlAction.FREE);%0A
     options.setR
@@ -2579,32 +2579,145 @@
 isRecursive());%0A
+    Assert.assertEquals(ttl, options.getTtl());%0A    Assert.assertEquals(TtlAction.FREE, options.getTtlAction());%0A
     Assert.asser
@@ -3557,16 +3557,16 @@
 sist(),%0A
-
         
@@ -3591,24 +3591,180 @@
 rsisted());%0A
+    Assert.assertEquals(Constants.NO_TTL, thriftOptions.getTtl());%0A    Assert.assertEquals(alluxio.thrift.TTtlAction.Delete, thriftOptions.getTtlAction());%0A
     Assert.a

add directory ttl test