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

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

import com.google.common.collect.ImmutableMap;
import org.apache.commons.cli.CommandLine;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;

import java.util.Map;

/**
 * Unit tests for {@link AbstractShell}.
 */
public final class AbstractShellTest {

  private static final String SHELL_NAME = "TestShell";

  @Rule
  public ExpectedException mExpectedException = ExpectedException.none();

  private final class TestShell extends AbstractShell {

    public TestShell() {
      super(ImmutableMap.<String, String[]>builder().put("cmdAlias", new String[] {"cmd", "-O"})
          .build());
    }

    @Override
    protected String getShellName() {
      return SHELL_NAME;
    }

    @Override
    protected Map<String, Command> loadCommands() {
      final Command cmd = Mockito.mock(Command.class);
      try {
        Mockito.when(cmd.run(Mockito.any(CommandLine.class))).thenReturn(0);
        Mockito.when(cmd.parseAndValidateArgs(Mockito.any(String[].class)))
            .thenReturn(Mockito.mock(CommandLine.class));
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
      return ImmutableMap.<String, Command>builder().put("cmd", cmd).build();
    }
  }

  @Test
  public void commandExists() throws Exception {
    TestShell shell = new TestShell();
    assertEquals(0, shell.run("cmd"));
  }

  @Test
  public void commandAliasExists() throws Exception {
    TestShell shell = new TestShell();
    assertEquals(0, shell.run("cmdAlias"));
  }

  @Test
  public void commandDoesNotExist() throws Exception {
    TestShell shell = new TestShell();
    assertTrue(shell.run("cmdNotExist") < 0);
  }
}

@@ -616,16 +616,135 @@
 ertTrue;
+%0Aimport static org.mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.when;%0Aimport static org.mockito.Mockito.any;
 %0A%0Aimport
@@ -918,36 +918,8 @@
 ion;
-%0Aimport org.mockito.Mockito;
 %0A%0Aim
@@ -1551,24 +1551,16 @@
 d cmd = 
-Mockito.
 mock(Com
@@ -1588,32 +1588,24 @@
 y %7B%0A        
-Mockito.
 when(cmd.run
@@ -1605,24 +1605,16 @@
 cmd.run(
-Mockito.
 any(Comm
@@ -1653,24 +1653,16 @@
         
-Mockito.
 when(cmd
@@ -1683,24 +1683,16 @@
 ateArgs(
-Mockito.
 any(Stri
@@ -1729,24 +1729,16 @@
 nReturn(
-Mockito.
 mock(Com

use static imports for standard test utilities in class AbstractShellTest