/*
 * 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.AuthenticatedUserRule;
import alluxio.CommonTestUtils;
import alluxio.Configuration;
import alluxio.ConfigurationTestUtils;
import alluxio.PropertyKey;
import alluxio.security.GroupMappingServiceTestUtils;
import alluxio.security.LoginUserTestUtils;
import alluxio.security.authentication.AuthType;
import alluxio.security.authorization.Mode;
import alluxio.security.group.provider.IdentityUserGroupsMapping;
import alluxio.thrift.CreateUfsFileTOptions;
import alluxio.util.CommonUtils;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.io.IOException;
import java.util.Random;

/**
 * Tests for the {@link CreateUfsFileOptions} class.
 */
public final class CreateUfsFileOptionsTest {
  private static final String TEST_USER = "test";

  @Rule
  public AuthenticatedUserRule mRule = new AuthenticatedUserRule(TEST_USER);

  @Before
  public void before() {
    LoginUserTestUtils.resetLoginUser();
    GroupMappingServiceTestUtils.resetCache();
    Configuration.set(PropertyKey.SECURITY_AUTHENTICATION_TYPE, AuthType.SIMPLE.getAuthName());
    Configuration.set(PropertyKey.SECURITY_LOGIN_USERNAME, TEST_USER);
    // Use IdentityOwnerGroupMapping to map owner "foo" to group "foo".
    Configuration.set(PropertyKey.SECURITY_GROUP_MAPPING_CLASS,
        IdentityUserGroupsMapping.class.getName());
  }

  @After
  public void after() {
    ConfigurationTestUtils.resetConfiguration();
  }

  /**
   * Tests that building a {@link CreateUfsFileOptions} with the defaults works.
   */
  @Test
  public void defaults() throws IOException {
    CreateUfsFileOptions options = CreateUfsFileOptions.defaults();
    Assert.assertEquals(TEST_USER, options.getOwner());
    Assert.assertEquals(TEST_USER, options.getGroup());
    Assert.assertEquals(Mode.defaults().applyFileUMask(), options.getMode());
  }

  /**
   * Tests getting and setting fields.
   */
  @Test
  public void fields() throws IOException {
    Random random = new Random();
    String owner = CommonUtils.randomAlphaNumString(10);
    String group = CommonUtils.randomAlphaNumString(10);
    Mode mode = new Mode((short) random.nextInt());

    CreateUfsFileOptions options = CreateUfsFileOptions.defaults();
    options.setOwner(owner);
    options.setGroup(group);
    options.setMode(mode);

    Assert.assertEquals(owner, options.getOwner());
    Assert.assertEquals(group, options.getGroup());
    Assert.assertEquals(mode, options.getMode());
  }

  /**
   * Tests conversion to thrift representation.
   */
  @Test
  public void toThrift() throws IOException {
    Random random = new Random();
    String owner = CommonUtils.randomAlphaNumString(10);
    String group = CommonUtils.randomAlphaNumString(10);
    Mode mode = new Mode((short) random.nextInt());

    CreateUfsFileOptions options = CreateUfsFileOptions.defaults();
    options.setOwner(owner);
    options.setGroup(group);
    options.setMode(mode);

    CreateUfsFileTOptions thriftOptions = options.toThrift();
    Assert.assertEquals(owner, thriftOptions.getOwner());
    Assert.assertEquals(group, thriftOptions.getGroup());
    Assert.assertEquals(mode.toShort(), thriftOptions.getMode());
  }

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


@@ -549,46 +549,8 @@
 s;%0A%0A
-import alluxio.AuthenticatedUserRule;%0A
 impo
@@ -603,24 +603,58 @@
 figuration;%0A
+import alluxio.ConfigurationRule;%0A
 import allux
@@ -672,32 +672,62 @@
 ationTestUtils;%0A
+import alluxio.LoginUserRule;%0A
 import alluxio.P
@@ -1070,24 +1070,71 @@
 mmonUtils;%0A%0A
+import com.google.common.collect.ImmutableMap;%0A
 import org.j
@@ -1471,29 +1471,21 @@
  public 
-Authenticated
+Login
 UserRule
@@ -1501,21 +1501,13 @@
 new 
-Authenticated
+Login
 User
@@ -1527,21 +1527,19 @@
 R);%0A%0A  @
-Befor
+Rul
 e%0A  publ
@@ -1545,372 +1545,87 @@
 lic 
-void before() %7B%0A    LoginUserTestUtils.resetLoginUser();%0A    GroupMappingServiceTestUtils.resetCache();%0A    Configuration.set(PropertyKey.SECURITY_AUTHENTICATION_TYPE, AuthType.SIMPLE.getAuthName());%0A    Configuration.set(PropertyKey.SECURITY_LOGIN_USERNAME, TEST_USER);%0A    // Use IdentityOwnerGroupMapping to map owner %22foo%22 to group %22foo%22.%0A    Configuration.set
+ConfigurationRule mConfiguration = new ConfigurationRule(ImmutableMap%0A      .of
 (Pro
@@ -1666,16 +1666,8 @@
 ASS,
-%0A       
  Ide
@@ -1709,101 +1709,13 @@
 e())
-;%0A  %7D%0A%0A  @After%0A  public void after() %7B%0A    ConfigurationTestUtils.resetConfiguration(
 );%0A  
-%7D%0A
 %0A  /

Update CreateUfsFileOptionsTest to use LoginUserRule