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

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellScanner;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.TableNameTestExtension;
import org.apache.hadoop.hbase.ipc.AbstractRpcClient;
import org.apache.hadoop.hbase.testclassification.ClientTests;
import org.apache.hadoop.hbase.testclassification.MediumTests;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

/**
 * Do some ops and prove that client and server can work w/o codecs; that we can pb all the time.
 * Good for third-party clients or simple scripts that want to talk direct to hbase.
 */
@Tag(MediumTests.TAG)
@Tag(ClientTests.TAG)
public class TestFromClientSideNoCodec {

  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();

  @RegisterExtension
  private TableNameTestExtension name = new TableNameTestExtension();

  @BeforeAll
  public static void setUpBeforeClass() throws Exception {
    // Turn off codec use
    TEST_UTIL.getConfiguration().set("hbase.client.default.rpc.codec", "");
    TEST_UTIL.startMiniCluster(1);
  }

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

  @Test
  public void testBasics() throws IOException {
    final TableName tableName = name.getTableName();
    final byte[][] fs =
      new byte[][] { Bytes.toBytes("cf1"), Bytes.toBytes("cf2"), Bytes.toBytes("cf3") };
    Table ht = TEST_UTIL.createTable(tableName, fs);
    // Check put and get.
    final byte[] row = Bytes.toBytes("row");
    Put p = new Put(row);
    for (byte[] f : fs) {
      p.addColumn(f, f, f);
    }
    ht.put(p);
    Result r = ht.get(new Get(row));
    int i = 0;
    for (CellScanner cellScanner = r.cellScanner(); cellScanner.advance();) {
      Cell cell = cellScanner.current();
      byte[] f = fs[i++];
      assertTrue(Bytes.equals(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength(), f,
        0, f.length), Bytes.toString(f));
    }
    // Check getRowOrBefore
    byte[] f = fs[0];
    Get get = new Get(row);
    get.addFamily(f);
    r = ht.get(get);
    assertTrue(r.containsColumn(f, f), r.toString());
    // Check scan.
    ResultScanner scanner = ht.getScanner(new Scan());
    int count = 0;
    while ((r = scanner.next()) != null) {
      assertTrue(r.listCells().size() == 3);
      count++;
    }
    assertTrue(count == 1);
  }

  @Test
  public void testNoCodec() {
    Configuration c = new Configuration();
    c.set("hbase.client.default.rpc.codec", "");
    String codec = AbstractRpcClient.getDefaultCodec(c);
    assertTrue(codec == null || codec.length() == 0);
  }
}
