/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.accumulo.shell.commands;

import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;

import org.apache.accumulo.core.client.AccumuloClient;
import org.apache.accumulo.core.client.admin.TableOperations;
import org.apache.accumulo.shell.Shell;
import org.apache.commons.cli.CommandLine;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ImportDirectoryCommandTest {

  private ImportDirectoryCommand cmd;

  private AccumuloClient client;
  private CommandLine cli;
  private Shell shellState;
  private TableOperations tableOperations;

  @Before
  public void setup() {
    cmd = new ImportDirectoryCommand();

    // Initialize that internal state
    cmd.getOptions();

    client = createMock(AccumuloClient.class);
    cli = createMock(CommandLine.class);
    shellState = createMock(Shell.class);
    tableOperations = createMock(TableOperations.class);
  }

  @After
  public void verifyMocks() {
    verify(client, cli, shellState, tableOperations);
  }

  /**
   * Test original command form - no -t tablename option provided.
   *
   * @throws Exception
   *           any exception is a test failure.
   */
  @Test
  public void testOriginalCmdForm() throws Exception {
    String[] cliArgs = {"in_dir", "fail_dir", "false"};

    // no -t option, use current table context
    expect(cli.hasOption("t")).andReturn(false).once();
    expect(shellState.getTableName()).andReturn("tablename").once();

    expect(cli.getArgs()).andReturn(cliArgs).atLeastOnce();
    expect(shellState.getAccumuloClient()).andReturn(client).atLeastOnce();
    expect(client.tableOperations()).andReturn(tableOperations);

    shellState.checkTableState();
    expectLastCall().once();

    tableOperations.importDirectory("tablename", "in_dir", "fail_dir", false);
    expectLastCall().once();

    replay(client, cli, shellState, tableOperations);
    cmd.execute("importdirectory in_dir fail_dir false", cli, shellState);
  }

  /**
   * Test with -t tablename option provided.
   *
   * @throws Exception
   *           any exception is a test failure.
   */
  @Test
  public void testPassTableOptCmdForm() throws Exception {
    String[] cliArgs = {"in_dir", "fail_dir", "false"};

    // -t option specified, table is from option
    expect(cli.hasOption("t")).andReturn(true).once();
    expect(cli.getOptionValue("t")).andReturn("passedName").once();
    expect(tableOperations.exists("passedName")).andReturn(true).once();

    expect(cli.getArgs()).andReturn(cliArgs).atLeastOnce();
    expect(shellState.getAccumuloClient()).andReturn(client).atLeastOnce();
    expect(client.tableOperations()).andReturn(tableOperations).atLeastOnce();

    // shellState.checkTableState() is NOT called

    tableOperations.importDirectory("passedName", "in_dir", "fail_dir", false);
    expectLastCall().once();

    replay(client, cli, shellState, tableOperations);
    cmd.execute("importdirectory in_dir fail_dir false", cli, shellState);
  }
}

@@ -1190,16 +1190,76 @@
 ations;%0A
+import org.apache.accumulo.core.clientImpl.bulk.BulkImport;%0A
 import o
@@ -1618,16 +1618,49 @@
 rations;
+%0A  private BulkImport bulkImport;
 %0A%0A  @Bef
@@ -1972,24 +1972,71 @@
 ons.class);%0A
+    bulkImport = createMock(BulkImport.class);%0A
   %7D%0A%0A  @Afte
@@ -2114,24 +2114,36 @@
 leOperations
+, bulkImport
 );%0A  %7D%0A%0A  /*
@@ -2385,36 +2385,24 @@
 = %7B%22in_dir%22,
- %22fail_dir%22,
  %22false%22%7D;%0A%0A
@@ -2840,24 +2840,31 @@
 nce();%0A%0A    
+expect(
 tableOperati
@@ -2888,47 +2888,207 @@
 ry(%22
-tablename%22, %22in_dir%22, %22fail_dir%22, false
+in_dir%22)).andReturn(bulkImport).once();%0A    expect(bulkImport.to(%22tablename%22)).andReturn(bulkImport).once();%0A    expect(bulkImport.tableTime(false)).andReturn(bulkImport).once();%0A    bulkImport.load(
 );%0A 
@@ -3159,32 +3159,44 @@
  tableOperations
+, bulkImport
 );%0A    cmd.execu
@@ -3218,33 +3218,24 @@
 tory in_dir 
-fail_dir 
 false%22, cli,
@@ -3486,28 +3486,16 @@
 in_dir%22,
- %22fail_dir%22,
  %22false%22
@@ -4011,24 +4011,31 @@
 called%0A%0A    
+expect(
 tableOperati
@@ -4059,48 +4059,208 @@
 ry(%22
-passedName%22, %22in_dir%22, %22fail_dir%22, false
+in_dir%22)).andReturn(bulkImport).once();%0A    expect(bulkImport.to(%22passedName%22)).andReturn(bulkImport).once();%0A    expect(bulkImport.tableTime(false)).andReturn(bulkImport).once();%0A    bulkImport.load(
 );%0A 
@@ -4339,16 +4339,28 @@
 erations
+, bulkImport
 );%0A    c
@@ -4398,17 +4398,8 @@
 dir 
-fail_dir 
 fals

Use newer importDirectory method for unit test of #1299