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