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.quotas;
019
020import static org.junit.Assert.assertEquals;
021import static org.mockito.Mockito.mock;
022import static org.mockito.Mockito.when;
023
024import java.util.HashMap;
025import java.util.Map;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.client.Connection;
029import org.apache.hadoop.hbase.client.RegionInfo;
030import org.apache.hadoop.hbase.client.RegionInfoBuilder;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.junit.Before;
034import org.junit.ClassRule;
035import org.junit.Test;
036import org.junit.experimental.categories.Category;
037
038import org.apache.hbase.thirdparty.com.google.common.collect.Iterables;
039
040/**
041 * Non-HBase cluster unit tests for {@link QuotaObserverChore}.
042 */
043@Category(SmallTests.class)
044public class TestQuotaObserverChore {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048    HBaseClassTestRule.forClass(TestQuotaObserverChore.class);
049
050  private Connection conn;
051  private QuotaObserverChore chore;
052
053  @Before
054  public void setup() throws Exception {
055    conn = mock(Connection.class);
056    chore = mock(QuotaObserverChore.class);
057  }
058
059  @Test
060  public void testNumRegionsForTable() {
061    TableName tn1 = TableName.valueOf("t1");
062    TableName tn2 = TableName.valueOf("t2");
063    TableName tn3 = TableName.valueOf("t3");
064
065    final int numTable1Regions = 10;
066    final int numTable2Regions = 15;
067    final int numTable3Regions = 8;
068    Map<RegionInfo, Long> regionReports = new HashMap<>();
069    for (int i = 0; i < numTable1Regions; i++) {
070      regionReports.put(RegionInfoBuilder.newBuilder(tn1).setStartKey(Bytes.toBytes(i))
071        .setEndKey(Bytes.toBytes(i + 1)).build(), 0L);
072    }
073
074    for (int i = 0; i < numTable2Regions; i++) {
075      regionReports.put(RegionInfoBuilder.newBuilder(tn2).setStartKey(Bytes.toBytes(i))
076        .setEndKey(Bytes.toBytes(i + 1)).build(), 0L);
077    }
078
079    for (int i = 0; i < numTable3Regions; i++) {
080      regionReports.put(RegionInfoBuilder.newBuilder(tn3).setStartKey(Bytes.toBytes(i))
081        .setEndKey(Bytes.toBytes(i + 1)).build(), 0L);
082    }
083
084    TableQuotaSnapshotStore store = new TableQuotaSnapshotStore(conn, chore, regionReports);
085    when(chore.getTableSnapshotStore()).thenReturn(store);
086
087    assertEquals(numTable1Regions, Iterables.size(store.filterBySubject(tn1)));
088    assertEquals(numTable2Regions, Iterables.size(store.filterBySubject(tn2)));
089    assertEquals(numTable3Regions, Iterables.size(store.filterBySubject(tn3)));
090  }
091}