View Javadoc

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
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
17   */
18  package org.apache.omid.transaction;
19  
20  import com.google.common.base.Charsets;
21  import org.apache.hadoop.hbase.client.Delete;
22  import org.apache.hadoop.hbase.client.Get;
23  import org.apache.hadoop.hbase.client.HTableInterface;
24  import org.apache.hadoop.hbase.client.Put;
25  import org.apache.hadoop.hbase.client.Scan;
26  import org.apache.hadoop.hbase.util.Bytes;
27  import org.mockito.Mockito;
28  import org.testng.annotations.Test;
29  
30  import static org.testng.Assert.fail;
31  
32  @Test(groups = "noHBase")
33  public class TestTTableBehaviour {
34  
35      private byte[] row = Bytes.toBytes("1row");
36      private byte[] famName = Bytes.toBytes("tf");
37      private byte[] colName = Bytes.toBytes("tc");
38      private byte[] dataValue = Bytes.toBytes("test-data");
39  
40      @Test(timeOut = 10_000)
41      public void testUserOperationsDontAllowTimestampSpecification() throws Exception {
42  
43          // Component under test
44          TTable tt = new TTable(Mockito.mock(HTableInterface.class), Mockito.mock(HTableInterface.class));
45  
46          long randomTimestampValue = Bytes.toLong("deadbeef".getBytes());
47  
48          Transaction tx = Mockito.mock(Transaction.class);
49  
50          // Test put fails when a timestamp is specified in the put
51          Put put = new Put(row, randomTimestampValue);
52          put.add(famName, colName, dataValue);
53          try {
54              tt.put(tx, put);
55              fail("Should have thrown an IllegalArgumentException due to timestamp specification");
56          } catch (IllegalArgumentException e) {
57              // Continue
58          }
59  
60          // Test put fails when a timestamp is specified in a qualifier
61          put = new Put(row);
62          put.add(famName, colName, randomTimestampValue, dataValue);
63          try {
64              tt.put(tx, put);
65              fail("Should have thrown an IllegalArgumentException due to timestamp specification");
66          } catch (IllegalArgumentException e) {
67              // Continue
68          }
69  
70          // Test that get fails when a timestamp is specified
71          Get get = new Get(row);
72          get.setTimeStamp(randomTimestampValue);
73          try {
74              tt.get(tx, get);
75              fail("Should have thrown an IllegalArgumentException due to timestamp specification");
76          } catch (IllegalArgumentException e) {
77              // Continue
78          }
79  
80          // Test scan fails when a timerange is specified
81          Scan scan = new Scan(get);
82          try {
83              tt.getScanner(tx, scan);
84              fail("Should have thrown an IllegalArgumentException due to timestamp specification");
85          } catch (IllegalArgumentException e) {
86              // Continue
87          }
88  
89          // Test delete fails when a timestamp is specified
90          Delete delete = new Delete(row);
91          delete.setTimestamp(randomTimestampValue);
92          try {
93              tt.delete(tx, delete);
94              fail("Should have thrown an IllegalArgumentException due to timestamp specification");
95          } catch (IllegalArgumentException e) {
96              // Continue
97          }
98  
99          // Test delete fails when a timestamp is specified in a qualifier
100         delete = new Delete(row);
101         delete.deleteColumn(famName, colName, randomTimestampValue);
102         try {
103             tt.delete(tx, delete);
104             fail("Should have thrown an IllegalArgumentException due to timestamp specification");
105         } catch (IllegalArgumentException e) {
106             // Continue
107         }
108 
109     }
110 
111     /**
112      * Test that we cannot use reserved names for shadow cell identifiers as qualifiers in user operations
113      */
114     @Test(timeOut = 10_000)
115     public void testReservedNamesForShadowCellsCanNotBeUsedAsQualifiersInUserOperations() throws Exception {
116         byte[] nonValidQualifier1 = "blahblah\u0080".getBytes(Charsets.UTF_8);
117         byte[] validQualifierIncludingOldShadowCellSuffix = "blahblah:OMID_CTS".getBytes(Charsets.UTF_8);
118 
119         TTable table = new TTable(Mockito.mock(HTableInterface.class), Mockito.mock(HTableInterface.class));
120 
121         HBaseTransaction t1 = Mockito.mock(HBaseTransaction.class);
122         Put put = new Put(row);
123         put.add(famName, nonValidQualifier1, dataValue);
124         try {
125             table.put(t1, put);
126             fail("Shouldn't be able to put this");
127         } catch (IllegalArgumentException iae) {
128             // correct
129         }
130         Delete del = new Delete(row);
131         del.deleteColumn(famName, nonValidQualifier1);
132         try {
133             table.delete(t1, del);
134             fail("Shouldn't be able to delete this");
135         } catch (IllegalArgumentException iae) {
136             // correct
137         }
138 
139         put = new Put(row);
140         put.add(famName, validQualifierIncludingOldShadowCellSuffix, dataValue);
141         try {
142             table.put(t1, put);
143         } catch (IllegalArgumentException iae) {
144             fail("Qualifier shouldn't be rejected anymore");
145         }
146         del = new Delete(row);
147         del.deleteColumn(famName, validQualifierIncludingOldShadowCellSuffix);
148         try {
149             table.delete(t1, del);
150         } catch (IllegalArgumentException iae) {
151             fail("Qualifier shouldn't be rejected anymore");
152         }
153     }
154 
155 }