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