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.HBaseTestingUtility;
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 HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
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)
062    .setRow(ROW).setFamily(FAMILY)
063    .setQualifier(QUAL).setTimestamp(0)
064    .setType(KeyValue.Type.Put.getCode())
065    .setValue(FIXED_VALUE)
066    .build();
067  private static final Result FIXED_RESULT = Result.create(Arrays.asList(FIXED_CELL));
068  private static final TableName TABLE_NAME = TableName.valueOf("TestResultFromCoprocessor");
069  @BeforeClass
070  public static void setUpBeforeClass() throws Exception {
071    TEST_UTIL.startMiniCluster(3);
072    TableDescriptor desc = TableDescriptorBuilder.newBuilder(TABLE_NAME)
073            .setCoprocessor(MyObserver.class.getName())
074            .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY))
075            .build();
076    TEST_UTIL.getAdmin().createTable(desc);
077  }
078
079  @AfterClass
080  public static void tearDownAfterClass() throws Exception {
081    TEST_UTIL.shutdownMiniCluster();
082  }
083
084  @Test
085  public void testAppend() throws IOException {
086    try (Table t = TEST_UTIL.getConnection().getTable(TABLE_NAME)) {
087      Put put = new Put(ROW);
088      put.addColumn(FAMILY, QUAL, VALUE);
089      t.put(put);
090      assertRowAndValue(t.get(new Get(ROW)), ROW, VALUE);
091      Append append = new Append(ROW);
092      append.addColumn(FAMILY, QUAL, FIXED_VALUE);
093      assertRowAndValue(t.append(append), ROW, FIXED_VALUE);
094      assertRowAndValue(t.get(new Get(ROW)), ROW, Bytes.add(VALUE, FIXED_VALUE));
095    }
096  }
097
098  @Test
099  public void testIncrement() throws IOException {
100    try (Table t = TEST_UTIL.getConnection().getTable(TABLE_NAME)) {
101      Put put = new Put(ROW);
102      put.addColumn(FAMILY, QUAL, VALUE);
103      t.put(put);
104      assertRowAndValue(t.get(new Get(ROW)), ROW, VALUE);
105      Increment inc = new Increment(ROW);
106      inc.addColumn(FAMILY, QUAL, 99);
107      assertRowAndValue(t.increment(inc), ROW, FIXED_VALUE);
108      assertRowAndValue(t.get(new Get(ROW)), ROW, Bytes.toBytes(199L));
109    }
110  }
111
112  private static void assertRowAndValue(Result r, byte[] row, byte[] value) {
113    for (Cell c : r.rawCells()) {
114      assertTrue(Bytes.equals(CellUtil.cloneRow(c), row));
115      assertTrue(Bytes.equals(CellUtil.cloneValue(c), value));
116    }
117  }
118
119  public static class MyObserver implements RegionCoprocessor, RegionObserver {
120    @Override
121    public Optional<RegionObserver> getRegionObserver() {
122      return Optional.of(this);
123    }
124
125    @Override
126    public Result postAppend(final ObserverContext<RegionCoprocessorEnvironment> c,
127      final Append append, final Result result) {
128      return FIXED_RESULT;
129    }
130
131    @Override
132    public Result postIncrement(final ObserverContext<RegionCoprocessorEnvironment> c,
133      final Increment increment, final Result result) {
134      return FIXED_RESULT;
135    }
136
137    @Override
138    public void start(CoprocessorEnvironment env) throws IOException {
139    }
140
141    @Override
142    public void stop(CoprocessorEnvironment env) throws IOException {
143    }
144  }
145
146}