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