/*
 * 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.hadoop.hbase.mob;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import java.util.stream.Stream;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseParameterizedTestTemplate;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.client.TableDescriptor;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.regionserver.storefiletracker.StoreFileTrackerFactory;
import org.apache.hadoop.hbase.testclassification.LargeTests;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.params.provider.Arguments;

@Tag(LargeTests.TAG)
@HBaseParameterizedTestTemplate(name = "{index}: useFileBasedSFT={0}")
public class TestDefaultMobStoreFlusher {

  private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
  private final static byte[] row1 = Bytes.toBytes("row1");
  private final static byte[] row2 = Bytes.toBytes("row2");
  private final static byte[] family = Bytes.toBytes("family");
  private final static byte[] qf1 = Bytes.toBytes("qf1");
  private final static byte[] qf2 = Bytes.toBytes("qf2");
  private final static byte[] value1 = Bytes.toBytes("value1");
  private final static byte[] value2 = Bytes.toBytes("value2");

  private String testMethodName;

  protected Boolean useFileBasedSFT;

  public TestDefaultMobStoreFlusher(Boolean useFileBasedSFT) {
    this.useFileBasedSFT = useFileBasedSFT;
  }

  public static Stream<Arguments> parameters() {
    return Stream.of(false, true).map(Arguments::of);
  }

  @BeforeEach
  public void setUpBefore(TestInfo testInfo) throws Exception {
    testMethodName = testInfo.getTestMethod().get().getName()
      + testInfo.getDisplayName().replaceAll("[:= ]", "_").replaceAll("_+", "_").trim();
    if (useFileBasedSFT) {
      TEST_UTIL.getConfiguration().set(StoreFileTrackerFactory.TRACKER_IMPL,
        "org.apache.hadoop.hbase.regionserver.storefiletracker.FileBasedStoreFileTracker");
    }
    TEST_UTIL.startMiniCluster(1);
  }

  @AfterEach
  public void tearDownAfter() throws Exception {
    TEST_UTIL.shutdownMiniCluster();
  }

  @TestTemplate
  public void testFlushNonMobFile() throws Exception {
    final TableName tableName = TableName.valueOf(TestMobUtils.getTableName(testMethodName));
    TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName)
      .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(family).setMaxVersions(4).build())
      .build();
    testFlushFile(tableDescriptor);
  }

  @TestTemplate
  public void testFlushMobFile() throws Exception {
    final TableName tableName = TableName.valueOf(TestMobUtils.getTableName(testMethodName));
    TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName)
      .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(family).setMobEnabled(true)
        .setMobThreshold(3L).setMaxVersions(4).build())
      .build();
    testFlushFile(tableDescriptor);
  }

  private void testFlushFile(TableDescriptor htd) throws Exception {
    Table table = null;
    try {
      table = TEST_UTIL.createTable(htd, null);

      // put data
      Put put0 = new Put(row1);
      put0.addColumn(family, qf1, 1, value1);
      table.put(put0);

      // put more data
      Put put1 = new Put(row2);
      put1.addColumn(family, qf2, 1, value2);
      table.put(put1);

      // flush
      TEST_UTIL.flush(htd.getTableName());

      // Scan
      Scan scan = new Scan();
      scan.addColumn(family, qf1);
      scan.setMaxVersions(4);
      ResultScanner scanner = table.getScanner(scan);

      // Compare
      int size = 0;
      for (Result result : scanner) {
        size++;
        List<Cell> cells = result.getColumnCells(family, qf1);
        // Verify the cell size
        assertEquals(1, cells.size());
        // Verify the value
        assertArrayEquals(value1, CellUtil.cloneValue(cells.get(0)));
      }
      scanner.close();
      assertEquals(1, size);
    } finally {
      table.close();
    }
  }
}
