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