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
{
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");
50 public TestName name
= new TestName();
53 public static void setUpBeforeClass() throws Exception
{
54 TEST_UTIL
.startMiniCluster();
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);
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"));
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"));
106 Delete del
= new Delete(ROWKEY
);
107 del
.addColumn(FAMILY
, Bytes
.toBytes("C"));
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
);
122 public void testCheckAndMutate() throws Throwable
{
123 try (Table table
= createTable()) {
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
);
135 // get row back and assert the values
136 getOneRowAndAssertAllButCExist(table
);
138 //Test that we get a region level exception
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
) {
147 } catch (NoSuchColumnFamilyException e1
) {
155 public void testCheckAndMutateWithBuilder() throws Throwable
{
156 try (Table table
= createTable()) {
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
);
168 // get row back and assert the values
169 getOneRowAndAssertAllButCExist(table
);
171 //Test that we get a region level exception
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
) {
180 } catch (NoSuchColumnFamilyException e1
) {