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

import alluxio.security.authentication.AuthenticatedClientUser;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runners.model.Statement;

/**
 * Unit tests for {@link ConfigurationRule}.
 */
public final class AuthenticatedUserRuleTest {

  @Test
  public void changeConfiguration() throws Throwable {
    Statement statement = new Statement() {
      @Override
      public void evaluate() throws Throwable {
        Assert.assertEquals("testuser", AuthenticatedClientUser.get().getName());
        Assert.assertTrue(Configuration.containsKey(PropertyKey.SECURITY_LOGIN_USERNAME));
      }
    };
    Assert.assertNull(AuthenticatedClientUser.get());
    Assert.assertFalse(Configuration.containsKey(PropertyKey.SECURITY_LOGIN_USERNAME));
    new AuthenticatedUserRule("testuser").apply(statement, null).evaluate();
    Assert.assertNull(AuthenticatedClientUser.get());
    Assert.assertFalse(Configuration.containsKey(PropertyKey.SECURITY_LOGIN_USERNAME));
  }
}

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

import alluxio.security.authentication.AuthenticatedClientUser;

import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runners.model.Statement;

/**
 * Unit tests for {@link AuthenticatedUserRule}.
 */
public final class AuthenticatedUserRuleTest {
  private static final String TESTCASE_USER = "testcase-user";
  private static final String RULE_USER = "rule-user";
  private static final String OUTSIDE_RULE_USER = "outside-rule-user";

  private final Statement mStatement = new Statement() {
    @Override
    public void evaluate() throws Throwable {
      Assert.assertEquals(RULE_USER, AuthenticatedClientUser.get().getName());
      AuthenticatedClientUser.set(TESTCASE_USER);
      Assert.assertEquals(TESTCASE_USER, AuthenticatedClientUser.get().getName());
    }
  };

  @After
  public void after() throws Exception {
    AuthenticatedClientUser.remove();
  }

  @Test
  public void userSetBeforeRule() throws Throwable {
    AuthenticatedClientUser.set(OUTSIDE_RULE_USER);
    new AuthenticatedUserRule(RULE_USER).apply(mStatement, null).evaluate();
    Assert.assertEquals(OUTSIDE_RULE_USER, AuthenticatedClientUser.get().getName());
  }

  @Test
  public void noUserBeforeRule() throws Throwable {
    AuthenticatedClientUser.remove();
    new AuthenticatedUserRule(RULE_USER).apply(mStatement, null).evaluate();
    Assert.assertEquals(null, AuthenticatedClientUser.get());
  }
}

Add unit test for AuthenticatedUserRule