HBASE-22002 Remove the deprecated methods in Admin interface
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / client / TestCheckAndMutate.java
blob15ef065d4737dd01deb294a1c10f3c1a0935fa8b
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.client;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
23 import java.io.IOException;
24 import org.apache.hadoop.hbase.HBaseClassTestRule;
25 import org.apache.hadoop.hbase.HBaseTestingUtility;
26 import org.apache.hadoop.hbase.TableName;
27 import org.apache.hadoop.hbase.regionserver.NoSuchColumnFamilyException;
28 import org.apache.hadoop.hbase.testclassification.MediumTests;
29 import org.apache.hadoop.hbase.util.Bytes;
30 import org.junit.AfterClass;
31 import org.junit.BeforeClass;
32 import org.junit.ClassRule;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.experimental.categories.Category;
36 import org.junit.rules.TestName;
38 @Category(MediumTests.class)
39 public class TestCheckAndMutate {
41 @ClassRule
42 public static final HBaseClassTestRule CLASS_RULE =
43 HBaseClassTestRule.forClass(TestCheckAndMutate.class);
45 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
46 private static final byte[] ROWKEY = Bytes.toBytes("12345");
47 private static final byte[] FAMILY = Bytes.toBytes("cf");
49 @Rule
50 public TestName name = new TestName();
52 @BeforeClass
53 public static void setUpBeforeClass() throws Exception {
54 TEST_UTIL.startMiniCluster();
57 @AfterClass
58 public static void tearDownAfterClass() throws Exception {
59 TEST_UTIL.shutdownMiniCluster();
62 private Table createTable()
63 throws IOException, InterruptedException {
64 final TableName tableName = TableName.valueOf(name.getMethodName());
65 Table table = TEST_UTIL.createTable(tableName, FAMILY);
66 TEST_UTIL.waitTableAvailable(tableName.getName(), 5000);
67 return table;
70 private void putOneRow(Table table) throws IOException {
71 Put put = new Put(ROWKEY);
72 put.addColumn(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a"));
73 put.addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b"));
74 put.addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c"));
75 table.put(put);
78 private void getOneRowAndAssertAllExist(final Table table) throws IOException {
79 Get get = new Get(ROWKEY);
80 Result result = table.get(get);
81 assertTrue("Column A value should be a",
82 Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("A"))).equals("a"));
83 assertTrue("Column B value should be b",
84 Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))).equals("b"));
85 assertTrue("Column C value should be c",
86 Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("C"))).equals("c"));
89 private void getOneRowAndAssertAllButCExist(final Table table) throws IOException {
90 Get get = new Get(ROWKEY);
91 Result result = table.get(get);
92 assertTrue("Column A value should be a",
93 Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("A"))).equals("a"));
94 assertTrue("Column B value should be b",
95 Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))).equals("b"));
96 assertTrue("Column C should not exist",
97 result.getValue(FAMILY, Bytes.toBytes("C")) == null);
100 private RowMutations makeRowMutationsWithColumnCDeleted() throws IOException {
101 RowMutations rm = new RowMutations(ROWKEY, 2);
102 Put put = new Put(ROWKEY);
103 put.addColumn(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a"));
104 put.addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b"));
105 rm.add(put);
106 Delete del = new Delete(ROWKEY);
107 del.addColumn(FAMILY, Bytes.toBytes("C"));
108 rm.add(del);
109 return rm;
112 private RowMutations getBogusRowMutations() throws IOException {
113 Put p = new Put(ROWKEY);
114 byte[] value = new byte[0];
115 p.addColumn(new byte[]{'b', 'o', 'g', 'u', 's'}, new byte[]{'A'}, value);
116 RowMutations rm = new RowMutations(ROWKEY);
117 rm.add(p);
118 return rm;
121 @Test
122 public void testCheckAndMutate() throws Throwable {
123 try (Table table = createTable()) {
124 // put one row
125 putOneRow(table);
126 // get row back and assert the values
127 getOneRowAndAssertAllExist(table);
129 // put the same row again with C column deleted
130 RowMutations rm = makeRowMutationsWithColumnCDeleted();
131 boolean res = table.checkAndMutate(ROWKEY, FAMILY).qualifier(Bytes.toBytes("A"))
132 .ifEquals(Bytes.toBytes("a")).thenMutate(rm);
133 assertTrue(res);
135 // get row back and assert the values
136 getOneRowAndAssertAllButCExist(table);
138 //Test that we get a region level exception
139 try {
140 rm = getBogusRowMutations();
141 table.checkAndMutate(ROWKEY, FAMILY).qualifier(Bytes.toBytes("A"))
142 .ifEquals(Bytes.toBytes("a")).thenMutate(rm);
143 fail("Expected NoSuchColumnFamilyException");
144 } catch (RetriesExhaustedWithDetailsException e) {
145 try {
146 throw e.getCause(0);
147 } catch (NoSuchColumnFamilyException e1) {
148 // expected
154 @Test
155 public void testCheckAndMutateWithBuilder() throws Throwable {
156 try (Table table = createTable()) {
157 // put one row
158 putOneRow(table);
159 // get row back and assert the values
160 getOneRowAndAssertAllExist(table);
162 // put the same row again with C column deleted
163 RowMutations rm = makeRowMutationsWithColumnCDeleted();
164 boolean res = table.checkAndMutate(ROWKEY, FAMILY).qualifier(Bytes.toBytes("A"))
165 .ifEquals(Bytes.toBytes("a")).thenMutate(rm);
166 assertTrue(res);
168 // get row back and assert the values
169 getOneRowAndAssertAllButCExist(table);
171 //Test that we get a region level exception
172 try {
173 rm = getBogusRowMutations();
174 table.checkAndMutate(ROWKEY, FAMILY).qualifier(Bytes.toBytes("A"))
175 .ifEquals(Bytes.toBytes("a")).thenMutate(rm);
176 fail("Expected NoSuchColumnFamilyException");
177 } catch (RetriesExhaustedWithDetailsException e) {
178 try {
179 throw e.getCause(0);
180 } catch (NoSuchColumnFamilyException e1) {
181 // expected