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