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