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.client; 019 020import static junit.framework.TestCase.assertTrue; 021 022import java.io.IOException; 023import java.util.Arrays; 024import java.util.Optional; 025import org.apache.hadoop.hbase.Cell; 026import org.apache.hadoop.hbase.CellBuilderType; 027import org.apache.hadoop.hbase.CellUtil; 028import org.apache.hadoop.hbase.CoprocessorEnvironment; 029import org.apache.hadoop.hbase.ExtendedCellBuilderFactory; 030import org.apache.hadoop.hbase.HBaseClassTestRule; 031import org.apache.hadoop.hbase.HBaseTestingUtil; 032import org.apache.hadoop.hbase.KeyValue; 033import org.apache.hadoop.hbase.TableName; 034import org.apache.hadoop.hbase.coprocessor.ObserverContext; 035import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor; 036import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment; 037import org.apache.hadoop.hbase.coprocessor.RegionObserver; 038import org.apache.hadoop.hbase.testclassification.ClientTests; 039import org.apache.hadoop.hbase.testclassification.MediumTests; 040import org.apache.hadoop.hbase.util.Bytes; 041import org.junit.AfterClass; 042import org.junit.BeforeClass; 043import org.junit.ClassRule; 044import org.junit.Test; 045import org.junit.experimental.categories.Category; 046 047@Category({ MediumTests.class, ClientTests.class }) 048public class TestResultFromCoprocessor { 049 050 @ClassRule 051 public static final HBaseClassTestRule CLASS_RULE = 052 HBaseClassTestRule.forClass(TestResultFromCoprocessor.class); 053 054 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 055 private static final byte[] ROW = Bytes.toBytes("normal_row"); 056 private static final byte[] FAMILY = Bytes.toBytes("fm"); 057 private static final byte[] QUAL = Bytes.toBytes("qual"); 058 private static final byte[] VALUE = Bytes.toBytes(100L); 059 private static final byte[] FIXED_VALUE = Bytes.toBytes("fixed_value"); 060 private static final Cell FIXED_CELL = ExtendedCellBuilderFactory 061 .create(CellBuilderType.DEEP_COPY).setRow(ROW).setFamily(FAMILY).setQualifier(QUAL) 062 .setTimestamp(0).setType(KeyValue.Type.Put.getCode()).setValue(FIXED_VALUE).build(); 063 private static final Result FIXED_RESULT = Result.create(Arrays.asList(FIXED_CELL)); 064 private static final TableName TABLE_NAME = TableName.valueOf("TestResultFromCoprocessor"); 065 066 @BeforeClass 067 public static void setUpBeforeClass() throws Exception { 068 TEST_UTIL.startMiniCluster(3); 069 TableDescriptor desc = 070 TableDescriptorBuilder.newBuilder(TABLE_NAME).setCoprocessor(MyObserver.class.getName()) 071 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY)).build(); 072 TEST_UTIL.getAdmin().createTable(desc); 073 } 074 075 @AfterClass 076 public static void tearDownAfterClass() throws Exception { 077 TEST_UTIL.shutdownMiniCluster(); 078 } 079 080 @Test 081 public void testAppend() throws IOException { 082 try (Table t = TEST_UTIL.getConnection().getTable(TABLE_NAME)) { 083 Put put = new Put(ROW); 084 put.addColumn(FAMILY, QUAL, VALUE); 085 t.put(put); 086 assertRowAndValue(t.get(new Get(ROW)), ROW, VALUE); 087 Append append = new Append(ROW); 088 append.addColumn(FAMILY, QUAL, FIXED_VALUE); 089 assertRowAndValue(t.append(append), ROW, FIXED_VALUE); 090 assertRowAndValue(t.get(new Get(ROW)), ROW, Bytes.add(VALUE, FIXED_VALUE)); 091 } 092 } 093 094 @Test 095 public void testIncrement() throws IOException { 096 try (Table t = TEST_UTIL.getConnection().getTable(TABLE_NAME)) { 097 Put put = new Put(ROW); 098 put.addColumn(FAMILY, QUAL, VALUE); 099 t.put(put); 100 assertRowAndValue(t.get(new Get(ROW)), ROW, VALUE); 101 Increment inc = new Increment(ROW); 102 inc.addColumn(FAMILY, QUAL, 99); 103 assertRowAndValue(t.increment(inc), ROW, FIXED_VALUE); 104 assertRowAndValue(t.get(new Get(ROW)), ROW, Bytes.toBytes(199L)); 105 } 106 } 107 108 private static void assertRowAndValue(Result r, byte[] row, byte[] value) { 109 for (Cell c : r.rawCells()) { 110 assertTrue(Bytes.equals(CellUtil.cloneRow(c), row)); 111 assertTrue(Bytes.equals(CellUtil.cloneValue(c), value)); 112 } 113 } 114 115 public static class MyObserver implements RegionCoprocessor, RegionObserver { 116 @Override 117 public Optional<RegionObserver> getRegionObserver() { 118 return Optional.of(this); 119 } 120 121 @Override 122 public Result postAppend(final ObserverContext<? extends RegionCoprocessorEnvironment> c, 123 final Append append, final Result result) { 124 return FIXED_RESULT; 125 } 126 127 @Override 128 public Result postIncrement(final ObserverContext<? extends RegionCoprocessorEnvironment> c, 129 final Increment increment, final Result result) { 130 return FIXED_RESULT; 131 } 132 133 @Override 134 public void start(CoprocessorEnvironment env) throws IOException { 135 } 136 137 @Override 138 public void stop(CoprocessorEnvironment env) throws IOException { 139 } 140 } 141 142}