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.coprocessor.example; 019 020import static org.junit.jupiter.api.Assertions.assertArrayEquals; 021import static org.junit.jupiter.api.Assertions.assertEquals; 022import static org.junit.jupiter.api.Assertions.assertNotNull; 023import static org.junit.jupiter.api.Assertions.assertNull; 024 025import java.io.IOException; 026import java.util.ArrayList; 027import java.util.List; 028import org.apache.hadoop.hbase.HBaseTestingUtil; 029import org.apache.hadoop.hbase.TableName; 030import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor; 031import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 032import org.apache.hadoop.hbase.client.Put; 033import org.apache.hadoop.hbase.client.Result; 034import org.apache.hadoop.hbase.client.ResultScanner; 035import org.apache.hadoop.hbase.client.Scan; 036import org.apache.hadoop.hbase.client.Table; 037import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 038import org.apache.hadoop.hbase.testclassification.CoprocessorTests; 039import org.apache.hadoop.hbase.testclassification.MediumTests; 040import org.apache.hadoop.hbase.util.Bytes; 041import org.junit.jupiter.api.AfterAll; 042import org.junit.jupiter.api.BeforeAll; 043import org.junit.jupiter.api.Tag; 044import org.junit.jupiter.api.Test; 045 046@Tag(CoprocessorTests.TAG) 047@Tag(MediumTests.TAG) 048public class TestScanModifyingObserver { 049 050 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 051 private static final TableName NAME = TableName.valueOf("TestScanModifications"); 052 private static final byte[] FAMILY = Bytes.toBytes("f"); 053 private static final ColumnFamilyDescriptor CFD = 054 ColumnFamilyDescriptorBuilder.newBuilder(FAMILY).build(); 055 private static final int NUM_ROWS = 5; 056 private static final byte[] EXPLICIT_QUAL = Bytes.toBytes("our_qualifier"); 057 private static final byte[] IMPLICIT_QUAL = Bytes.toBytes("their_qualifier"); 058 private static final byte[] EXPLICIT_VAL = Bytes.toBytes("provided"); 059 private static final byte[] IMPLICIT_VAL = Bytes.toBytes("implicit"); 060 061 @BeforeAll 062 public static void setUp() throws Exception { 063 UTIL.startMiniCluster(1); 064 UTIL.getAdmin() 065 .createTable(TableDescriptorBuilder.newBuilder(NAME) 066 .setCoprocessor(ScanModifyingObserver.class.getName()) 067 .setValue(ScanModifyingObserver.FAMILY_TO_ADD_KEY, Bytes.toString(FAMILY)) 068 .setValue(ScanModifyingObserver.QUALIFIER_TO_ADD_KEY, Bytes.toString(IMPLICIT_QUAL)) 069 .setColumnFamily(CFD).build()); 070 } 071 072 @AfterAll 073 public static void tearDown() throws Exception { 074 UTIL.shutdownMiniCluster(); 075 } 076 077 private void writeData(Table t) throws IOException { 078 List<Put> puts = new ArrayList<>(NUM_ROWS); 079 for (int i = 0; i < NUM_ROWS; i++) { 080 Put p = new Put(Bytes.toBytes(i + 1)); 081 p.addColumn(FAMILY, EXPLICIT_QUAL, EXPLICIT_VAL); 082 p.addColumn(FAMILY, IMPLICIT_QUAL, IMPLICIT_VAL); 083 puts.add(p); 084 } 085 t.put(puts); 086 } 087 088 @Test 089 public void test() throws IOException { 090 try (Table t = UTIL.getConnection().getTable(NAME)) { 091 writeData(t); 092 093 Scan s = new Scan(); 094 s.addColumn(FAMILY, EXPLICIT_QUAL); 095 096 try (ResultScanner scanner = t.getScanner(s)) { 097 for (int i = 0; i < NUM_ROWS; i++) { 098 Result result = scanner.next(); 099 assertNotNull(result, "The " + (i + 1) + "th result was unexpectedly null"); 100 assertEquals(2, result.getFamilyMap(FAMILY).size()); 101 assertArrayEquals(Bytes.toBytes(i + 1), result.getRow()); 102 assertArrayEquals(EXPLICIT_VAL, result.getValue(FAMILY, EXPLICIT_QUAL)); 103 assertArrayEquals(IMPLICIT_VAL, result.getValue(FAMILY, IMPLICIT_QUAL)); 104 } 105 assertNull(scanner.next()); 106 } 107 } 108 } 109}