001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.filter;
019
020import static org.junit.jupiter.api.Assertions.assertNotNull;
021import static org.junit.jupiter.api.Assertions.assertNull;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.io.IOException;
025import java.util.ArrayList;
026import java.util.List;
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.hbase.HBaseConfiguration;
029import org.apache.hadoop.hbase.HBaseTestingUtil;
030import org.apache.hadoop.hbase.HConstants;
031import org.apache.hadoop.hbase.MasterNotRunningException;
032import org.apache.hadoop.hbase.TableName;
033import org.apache.hadoop.hbase.ZooKeeperConnectionException;
034import org.apache.hadoop.hbase.client.Admin;
035import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
036import org.apache.hadoop.hbase.client.Table;
037import org.apache.hadoop.hbase.client.TableDescriptor;
038import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
039import org.apache.hadoop.hbase.util.Bytes;
040import org.junit.jupiter.api.AfterAll;
041
042/**
043 * By using this class as the super class of a set of tests you will have a HBase testing cluster
044 * available that is very suitable for writing tests for scanning and filtering against.
045 */
046public abstract class FilterTestingCluster {
047  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
048  private static Admin admin = null;
049  private static List<TableName> createdTables = new ArrayList<>();
050
051  protected static void createTable(TableName tableName, String columnFamilyName) {
052    assertNotNull(admin, "HBaseAdmin is not initialized successfully.");
053    TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName)
054      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(Bytes.toBytes(columnFamilyName))).build();
055
056    try {
057      admin.createTable(tableDescriptor);
058      createdTables.add(tableName);
059      assertTrue(admin.tableExists(tableName), "Fail to create the table");
060    } catch (IOException e) {
061      assertNull(e, "Exception found while creating table");
062    }
063  }
064
065  protected static Table openTable(TableName tableName) throws IOException {
066    Table table = TEST_UTIL.getConnection().getTable(tableName);
067    assertTrue(admin.tableExists(tableName), "Fail to create the table");
068    return table;
069  }
070
071  private static void deleteTables() {
072    if (admin != null) {
073      for (TableName tableName : createdTables) {
074        try {
075          if (admin.tableExists(tableName)) {
076            admin.disableTable(tableName);
077            admin.deleteTable(tableName);
078          }
079        } catch (IOException e) {
080          assertNull(e, "Exception found deleting the table");
081        }
082      }
083    }
084  }
085
086  private static void initialize(Configuration conf) {
087    conf = HBaseConfiguration.create(conf);
088    conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1);
089    try {
090      admin = TEST_UTIL.getAdmin();
091    } catch (MasterNotRunningException e) {
092      assertNull(e, "Master is not running");
093    } catch (ZooKeeperConnectionException e) {
094      assertNull(e, "Cannot connect to ZooKeeper");
095    } catch (IOException e) {
096      assertNull(e, "IOException");
097    }
098  }
099
100  public static void setUp() throws Exception {
101    TEST_UTIL.startMiniCluster(1);
102    initialize(TEST_UTIL.getConfiguration());
103  }
104
105  @AfterAll
106  public static void tearDown() throws Exception {
107    deleteTables();
108    TEST_UTIL.shutdownMiniCluster();
109  }
110}