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.client;
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.hbase.CatalogFamilyFormat;
028import org.apache.hadoop.hbase.Cell;
029import org.apache.hadoop.hbase.HBaseTestingUtil;
030import org.apache.hadoop.hbase.HConstants;
031import org.apache.hadoop.hbase.KeyValue;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.apache.hadoop.hbase.testclassification.MiscTests;
034import org.junit.jupiter.api.AfterEach;
035import org.junit.jupiter.api.BeforeEach;
036import org.junit.jupiter.api.Tag;
037import org.junit.jupiter.api.Test;
038
039/**
040 * Test MetaTableAccessor but without spinning up a cluster. We mock regionserver back and forth (we
041 * do spin up a zk cluster).
042 */
043@Tag(MiscTests.TAG)
044@Tag(MediumTests.TAG)
045public class TestMetaTableAccessorNoCluster {
046
047  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
048
049  @BeforeEach
050  public void before() throws Exception {
051    UTIL.startMiniZKCluster();
052  }
053
054  @AfterEach
055  public void after() throws IOException {
056    UTIL.shutdownMiniZKCluster();
057  }
058
059  @Test
060  public void testGetHRegionInfo() throws IOException {
061    assertNull(CatalogFamilyFormat.getRegionInfo(new Result()));
062
063    List<Cell> kvs = new ArrayList<>();
064    Result r = Result.create(kvs);
065    assertNull(CatalogFamilyFormat.getRegionInfo(r));
066
067    byte[] f = HConstants.CATALOG_FAMILY;
068    // Make a key value that doesn't have the expected qualifier.
069    kvs.add(new KeyValue(HConstants.EMPTY_BYTE_ARRAY, f, HConstants.SERVER_QUALIFIER, f));
070    r = Result.create(kvs);
071    assertNull(CatalogFamilyFormat.getRegionInfo(r));
072    // Make a key that does not have a regioninfo value.
073    kvs.add(new KeyValue(HConstants.EMPTY_BYTE_ARRAY, f, HConstants.REGIONINFO_QUALIFIER, f));
074    RegionInfo hri = CatalogFamilyFormat.getRegionInfo(Result.create(kvs));
075    assertTrue(hri == null);
076    // OK, give it what it expects
077    kvs.clear();
078    kvs.add(new KeyValue(HConstants.EMPTY_BYTE_ARRAY, f, HConstants.REGIONINFO_QUALIFIER,
079      RegionInfo.toByteArray(RegionInfoBuilder.FIRST_META_REGIONINFO)));
080    hri = CatalogFamilyFormat.getRegionInfo(Result.create(kvs));
081    assertNotNull(hri);
082    assertTrue(RegionInfo.COMPARATOR.compare(hri, RegionInfoBuilder.FIRST_META_REGIONINFO) == 0);
083  }
084}