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.Matchers.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 = new SpaceQuotaSnapshot( 072 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) 081 .setQuotaUsage(1024L) 082 .build(); 083 expectedPut.addColumn(Bytes.toBytes("u"), Bytes.toBytes("p"), protoQuota.toByteArray()); 084 085 notifier.transitionTable(tn, snapshot); 086 087 verify(quotaTable).put(argThat(new SingleCellMutationMatcher<Put>(expectedPut))); 088 } 089 090 /** 091 * Quick hack to verify a Mutation with one column. 092 */ 093 final private static class SingleCellMutationMatcher<T> implements ArgumentMatcher<T> { 094 private final Mutation expected; 095 096 private SingleCellMutationMatcher(Mutation expected) { 097 this.expected = expected; 098 } 099 100 @Override 101 public boolean matches(T argument) { 102 if (!expected.getClass().isAssignableFrom(argument.getClass())) { 103 return false; 104 } 105 Mutation actual = (Mutation) argument; 106 if (!Arrays.equals(expected.getRow(), actual.getRow())) { 107 return false; 108 } 109 if (expected.size() != actual.size()) { 110 return false; 111 } 112 NavigableMap<byte[],List<Cell>> expectedCells = expected.getFamilyCellMap(); 113 NavigableMap<byte[],List<Cell>> actualCells = actual.getFamilyCellMap(); 114 Entry<byte[],List<Cell>> expectedEntry = expectedCells.entrySet().iterator().next(); 115 Entry<byte[],List<Cell>> actualEntry = actualCells.entrySet().iterator().next(); 116 if (!Arrays.equals(expectedEntry.getKey(), actualEntry.getKey())) { 117 return false; 118 } 119 return Objects.equals(expectedEntry.getValue(), actualEntry.getValue()); 120 } 121 } 122}