/*
 * 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.worker.block.meta;

import alluxio.worker.block.BlockStoreLocation;
import alluxio.worker.block.TieredBlockStoreTestUtils;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/**
 * Unit tests for {@link AbstractBlockMeta}.
 */
public class AbstractBlockMetaTest {
  // This class extending AbstractBlockMeta is only for test purpose
  private class AbstractBlockMetaForTest extends AbstractBlockMeta {
    public AbstractBlockMetaForTest(long blockId, StorageDir dir) {
      super(blockId, dir);
    }

    @Override
    public long getBlockSize() {
      return 0;
    }

    @Override
    public String getPath() {
      return "";
    }
  }

  /** Rule to create a new temporary folder during each test. */
  @Rule
  public TemporaryFolder mFolder = new TemporaryFolder();

  private static final long TEST_BLOCK_ID = 9;
  private static final int TEST_TIER_ORDINAL = 0;
  private static final String TEST_TIER_ALIAS = "MEM";
  private static final long[] TEST_TIER_CAPACITY_BYTES = {100};
  private static final String[] TEST_TIER_MEDIUM_TYPES = {"MEM"};
  private StorageTier mTier;
  private StorageDir mDir;
  private AbstractBlockMetaForTest mBlockMeta;

  /**
   * Sets up all dependencies before a test runs.
   */
  @Before
  public void before() throws Exception {
    String testDirPath = mFolder.newFolder().getAbsolutePath();
    // Sets up tier with one storage dir under mTestDirPath with 100 bytes capacity.
    TieredBlockStoreTestUtils.setupConfWithSingleTier(null, TEST_TIER_ORDINAL,
        TEST_TIER_ALIAS, new String[] {testDirPath}, TEST_TIER_CAPACITY_BYTES,
        TEST_TIER_MEDIUM_TYPES, null);

    mTier = StorageTier.newStorageTier(TEST_TIER_ALIAS, false);
    mDir = mTier.getDir(0);
    mBlockMeta = new AbstractBlockMetaForTest(TEST_BLOCK_ID, mDir);
  }

  /**
   * Tests the {@link AbstractBlockMeta#getBlockId()} method.
   */
  @Test
  public void getBlockId() {
    Assert.assertEquals(TEST_BLOCK_ID, mBlockMeta.getBlockId());
  }

  /**
   * Tests the {@link AbstractBlockMeta#getBlockLocation()} method.
   */
  @Test
  public void getBlockLocation() {
    BlockStoreLocation expectedLocation =
        new BlockStoreLocation(mTier.getTierAlias(), mDir.getDirIndex(), mDir.getDirMedium());
    Assert.assertEquals(expectedLocation, mBlockMeta.getBlockLocation());
  }

  /**
   * Tests the {@link AbstractBlockMeta#getParentDir()} method.
   */
  @Test
  public void getParentDir() {
    Assert.assertEquals(mDir, mBlockMeta.getParentDir());
  }
}
