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

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.testclassification.MediumTests;
import org.apache.hadoop.hbase.testclassification.RegionServerTests;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Tag(RegionServerTests.TAG)
@Tag(MediumTests.TAG)
public class TestDisabledWAL {

  private static final Logger LOG = LoggerFactory.getLogger(TestDisabledWAL.class);
  static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
  private Table table;
  private TableName tableName;
  private byte[] fam = Bytes.toBytes("f1");

  @BeforeAll
  public static void beforeClass() throws Exception {
    Configuration conf = TEST_UTIL.getConfiguration();
    conf.setBoolean("hbase.regionserver.hlog.enabled", false);
    try {
      TEST_UTIL.startMiniCluster();
    } catch (RuntimeException | IOException e) {
      LOG.error("Master failed to start.", e);
      fail("Failed to start cluster. Reason being: " + e.getCause().getMessage());
    }
  }

  @AfterAll
  public static void afterClass() throws Exception {
    TEST_UTIL.shutdownMiniCluster();
  }

  @BeforeEach
  public void setup(TestInfo testInfo) throws Exception {
    tableName =
      TableName.valueOf(testInfo.getTestMethod().get().getName().replaceAll("[^a-zA-Z0-9]", "_"));
    LOG.info("Creating table " + tableName);
    table = TEST_UTIL.createTable(tableName, fam);
  }

  @AfterEach
  public void cleanup() throws Exception {
    LOG.info("Deleting table " + tableName);
    TEST_UTIL.deleteTable(tableName);
  }

  @Test
  public void testDisabledWAL() throws Exception {
    LOG.info("Writing data to table " + tableName);
    Put p = new Put(Bytes.toBytes("row"));
    p.addColumn(fam, Bytes.toBytes("qual"), Bytes.toBytes("val"));
    table.put(p);

    LOG.info("Flushing table " + tableName);
    TEST_UTIL.flush(tableName);

    LOG.info("Getting data from table " + tableName);
    Get get = new Get(Bytes.toBytes("row"));

    Result result = table.get(get);
    assertNotNull(result.getValue(fam, Bytes.toBytes("qual")));
  }
}
