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.mockito.ArgumentMatchers.argThat;
021import static org.mockito.Mockito.mock;
022import static org.mockito.Mockito.verify;
023import static org.mockito.Mockito.when;
024
025import java.util.Arrays;
026import java.util.List;
027import java.util.Map.Entry;
028import java.util.NavigableMap;
029import java.util.Objects;
030import org.apache.hadoop.hbase.Cell;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.client.Connection;
033import org.apache.hadoop.hbase.client.Mutation;
034import org.apache.hadoop.hbase.client.Put;
035import org.apache.hadoop.hbase.client.Table;
036import org.apache.hadoop.hbase.quotas.SpaceQuotaSnapshot.SpaceQuotaStatus;
037import org.apache.hadoop.hbase.testclassification.SmallTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.junit.jupiter.api.BeforeEach;
040import org.junit.jupiter.api.Tag;
041import org.junit.jupiter.api.Test;
042import org.mockito.ArgumentMatcher;
043
044import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos;
045
046/**
047 * Test case for {@link TableSpaceQuotaSnapshotNotifier}.
048 */
049@Tag(SmallTests.TAG)
050public class TestTableSpaceQuotaViolationNotifier {
051
052  private TableSpaceQuotaSnapshotNotifier notifier;
053  private Connection conn;
054
055  @BeforeEach
056  public void setup() throws Exception {
057    notifier = new TableSpaceQuotaSnapshotNotifier();
058    conn = mock(Connection.class);
059    notifier.initialize(conn);
060  }
061
062  @Test
063  public void testToViolation() throws Exception {
064    final TableName tn = TableName.valueOf("inviolation");
065    final SpaceQuotaSnapshot snapshot =
066      new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 1024L, 512L);
067    final Table quotaTable = mock(Table.class);
068    when(conn.getTable(QuotaTableUtil.QUOTA_TABLE_NAME)).thenReturn(quotaTable);
069
070    final Put expectedPut = new Put(Bytes.toBytes("t." + tn.getNameAsString()));
071    final QuotaProtos.SpaceQuotaSnapshot protoQuota = QuotaProtos.SpaceQuotaSnapshot.newBuilder()
072      .setQuotaStatus(QuotaProtos.SpaceQuotaStatus.newBuilder().setInViolation(true)
073        .setViolationPolicy(QuotaProtos.SpaceViolationPolicy.NO_INSERTS))
074      .setQuotaLimit(512L).setQuotaUsage(1024L).build();
075    expectedPut.addColumn(Bytes.toBytes("u"), Bytes.toBytes("p"), protoQuota.toByteArray());
076
077    notifier.transitionTable(tn, snapshot);
078
079    verify(quotaTable).put(argThat(new SingleCellMutationMatcher<Put>(expectedPut)));
080  }
081
082  /**
083   * Quick hack to verify a Mutation with one column.
084   */
085  final private static class SingleCellMutationMatcher<T> implements ArgumentMatcher<T> {
086    private final Mutation expected;
087
088    private SingleCellMutationMatcher(Mutation expected) {
089      this.expected = expected;
090    }
091
092    @Override
093    public boolean matches(T argument) {
094      if (!expected.getClass().isAssignableFrom(argument.getClass())) {
095        return false;
096      }
097      Mutation actual = (Mutation) argument;
098      if (!Arrays.equals(expected.getRow(), actual.getRow())) {
099        return false;
100      }
101      if (expected.size() != actual.size()) {
102        return false;
103      }
104      NavigableMap<byte[], List<Cell>> expectedCells = expected.getFamilyCellMap();
105      NavigableMap<byte[], List<Cell>> actualCells = actual.getFamilyCellMap();
106      Entry<byte[], List<Cell>> expectedEntry = expectedCells.entrySet().iterator().next();
107      Entry<byte[], List<Cell>> actualEntry = actualCells.entrySet().iterator().next();
108      if (!Arrays.equals(expectedEntry.getKey(), actualEntry.getKey())) {
109        return false;
110      }
111      return Objects.equals(expectedEntry.getValue(), actualEntry.getValue());
112    }
113  }
114}