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

import alluxio.AlluxioURI;
import alluxio.client.FileSystemTestUtils;
import alluxio.client.WriteType;
import alluxio.shell.AbstractAlluxioShellTest;
import alluxio.underfs.UnderFileSystem;
import alluxio.underfs.options.DeleteOptions;

import org.junit.Assert;
import org.junit.Test;

public class CheckConsistencyCommandTest extends AbstractAlluxioShellTest {
  /**
   * Tests the check consistency shell command correctly identifies a consistent subtree.
   */
  @Test
  public void consistent() throws Exception {
    FileSystemTestUtils
        .createByteFile(mFileSystem, "/testRoot/testFileA", WriteType.CACHE_THROUGH, 10);
    FileSystemTestUtils.createByteFile(mFileSystem, "/testRoot/testDir/testFileB",
        WriteType.CACHE_THROUGH, 20);
    mFsShell.run("checkConsistency", "/testRoot");
    String expected = "/testRoot is consistent with the under storage system.\n";
    Assert.assertEquals(expected, mOutput.toString());
    mOutput.reset();
    mFsShell.run("checkConsistency", "-r", "/testRoot");
    Assert.assertEquals(expected, mOutput.toString());
  }

  /**
   * Tests the check consistency shell command correctly identifies inconsistent files in a subtree.
   */
  @Test
  public void inconsistent() throws Exception {
    FileSystemTestUtils
        .createByteFile(mFileSystem, "/testRoot/testFileA", WriteType.CACHE_THROUGH, 10);
    FileSystemTestUtils.createByteFile(mFileSystem, "/testRoot/testDir/testFileB",
        WriteType.CACHE_THROUGH, 20);
    String ufsPath = mFileSystem.getStatus(new AlluxioURI("/testRoot/testDir")).getUfsPath();
    UnderFileSystem ufs = UnderFileSystem.Factory.get(ufsPath);
    ufs.deleteDirectory(ufsPath, DeleteOptions.defaults().setRecursive(true));
    mFsShell.run("checkConsistency", "/testRoot");
    StringBuilder expected = new StringBuilder();
    expected.append("The following files are inconsistent:\n");
    expected.append("/testRoot/testDir\n");
    expected.append("/testRoot/testDir/testFileB\n");
    Assert.assertEquals(expected.toString(), mOutput.toString());

    mOutput.reset();
    mFsShell.run("checkConsistency", "-r", "/testRoot");
    String res = mOutput.toString();
    Assert.assertTrue(res.contains("/testRoot" + " has: " + "2 inconsistent files.\n")
        && res.contains("repairing path: " + "/testRoot/testDir\n")
        && res.contains("repairing path: " + "/testRoot/testDir/testFileB\n"));
    Assert.assertTrue(!mFileSystem.exists(new AlluxioURI("/testRoot/testDir")));
    Assert.assertTrue(!mFileSystem.exists(new AlluxioURI("/testRoot/testDir/testFileB")));
  }
}

@@ -3110,15 +3110,773 @@
 leB%22)));
+%0A%0A    FileSystemTestUtils.createByteFile(mFileSystem, %22/testRoot/testDir/testFileB%22,%0A        WriteType.CACHE_THROUGH, 20);%0A    ufsPath = mFileSystem.getStatus(new AlluxioURI(%22/testRoot/testDir/testFileB%22)).getUfsPath();%0A    ufs.deleteFile(ufsPath);%0A    ufs.create(ufsPath);%0A    mOutput.reset();%0A    mFsShell.run(%22checkConsistency%22, %22-r%22, %22/testRoot%22);%0A    res = mOutput.toString();%0A    Assert.assertTrue(res.contains(%22/testRoot%22 + %22 has: %22 + %221 inconsistent files.%5Cn%22)%0A        && res.contains(%22repairing path: %22 + %22/testRoot/testDir/testFileB%5Cn%22));%0A    Assert.assertTrue(!mFileSystem.exists(new AlluxioURI(%22/testRoot/testDir/testFileB%22)));%0A    Assert.assertTrue(0 == mFileSystem.getStatus(new AlluxioURI(%22/testRoot/testDir/testFileB%22))%0A        .getLength());
 %0A  %7D%0A%7D%0A

Add unit tests