HBASE-26474 Implement connection-level attributes (addendum)
[hbase.git] / hbase-client / src / test / java / org / apache / hadoop / hbase / client / TestGet.java
blobb52ad3e8d2a46b820349654873654e66b4ac72ac
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.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
25 import java.io.File;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.lang.reflect.InvocationTargetException;
29 import java.nio.ByteBuffer;
30 import java.util.Arrays;
31 import java.util.Base64;
32 import java.util.List;
33 import java.util.Set;
34 import org.apache.hadoop.conf.Configuration;
35 import org.apache.hadoop.hbase.HBaseClassTestRule;
36 import org.apache.hadoop.hbase.HBaseConfiguration;
37 import org.apache.hadoop.hbase.exceptions.DeserializationException;
38 import org.apache.hadoop.hbase.filter.Filter;
39 import org.apache.hadoop.hbase.filter.FilterList;
40 import org.apache.hadoop.hbase.filter.KeyOnlyFilter;
41 import org.apache.hadoop.hbase.security.access.Permission;
42 import org.apache.hadoop.hbase.security.visibility.Authorizations;
43 import org.apache.hadoop.hbase.testclassification.ClientTests;
44 import org.apache.hadoop.hbase.testclassification.SmallTests;
45 import org.apache.hadoop.hbase.util.Bytes;
46 import org.junit.Assert;
47 import org.junit.ClassRule;
48 import org.junit.Test;
49 import org.junit.experimental.categories.Category;
51 import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
52 import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos;
54 // TODO: cover more test cases
55 @Category({ClientTests.class, SmallTests.class})
56 public class TestGet {
57 @ClassRule
58 public static final HBaseClassTestRule CLASS_RULE =
59 HBaseClassTestRule.forClass(TestGet.class);
61 private static final byte [] ROW = new byte [] {'r'};
63 private static final String PB_GET = "CgNyb3ciEwoPdGVzdC5Nb2NrRmlsdGVyEgAwATgB";
64 private static final String PB_GET_WITH_FILTER_LIST =
65 "CgFyIosBCilvcmcuYXBhY2hlLmhhZG9vcC5oYmFzZS5maWx0ZXIuRmlsdGVyTGlzdBJeCAESEwoP" +
66 "dGVzdC5Nb2NrRmlsdGVyEgASEQoNbXkuTW9ja0ZpbHRlchIAEjIKLG9yZy5hcGFjaGUuaGFkb29w" +
67 "LmhiYXNlLmZpbHRlci5LZXlPbmx5RmlsdGVyEgIIADABOAE=";
69 private static final String MOCK_FILTER_JAR =
70 "UEsDBBQACAgIANWDlEMAAAAAAAAAAAAAAAAJAAQATUVUQS1JTkYv/soAAAMAUEsHCAAAAAACAAAA" +
71 "AAAAAFBLAwQUAAgICADVg5RDAAAAAAAAAAAAAAAAFAAAAE1FVEEtSU5GL01BTklGRVNULk1G803M" +
72 "y0xLLS7RDUstKs7Mz7NSMNQz4OVyLkpNLElN0XWqBAmY6xnEG1gqaPgXJSbnpCo45xcV5BcllgCV" +
73 "a/Jy8XIBAFBLBwgxyqRbQwAAAEQAAABQSwMEFAAICAgAUoOUQwAAAAAAAAAAAAAAABMAAABteS9N" +
74 "b2NrRmlsdGVyLmNsYXNzdZHPTsJAEMa/LYVCRVFQMd68gQc38YrxUJUTetGQGE7bstrVwjbbYsSn" +
75 "0hOJJj6AD2WcFoP/4iYzX+bb32xmd9/en18B7GPLhY11BxsurEw3GUoHaqzSQ4ZCq91nsI/0UDLU" +
76 "emoszyYjX5oL4Ufk1Hs6EFFfGJXVn6adhirJ6NGUn+rgtquiVJoOQyUWJpFdo0cM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bXkvTW9ja0ZpbHRlci5j" +
90 "bGFzc1BLAQIUABQACAgIAFKDlEOtLmcoOgEAAPIBAAAVAAAAAAAAAAAAAAAAAD0CAAB0ZXN0L01v" +
91 "Y2tGaWx0ZXIuY2xhc3NQSwUGAAAAAAQABAABAQAAugMAAAAA";
93 @Test
94 public void testAttributesSerialization() throws IOException {
95 Get get = new Get(Bytes.toBytes("row"));
96 get.setAttribute("attribute1", Bytes.toBytes("value1"));
97 get.setAttribute("attribute2", Bytes.toBytes("value2"));
98 get.setAttribute("attribute3", Bytes.toBytes("value3"));
100 ClientProtos.Get getProto = ProtobufUtil.toGet(get);
102 Get get2 = ProtobufUtil.toGet(getProto);
103 Assert.assertNull(get2.getAttribute("absent"));
104 Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), get2.getAttribute("attribute1")));
105 Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), get2.getAttribute("attribute2")));
106 Assert.assertTrue(Arrays.equals(Bytes.toBytes("value3"), get2.getAttribute("attribute3")));
107 Assert.assertEquals(3, get2.getAttributesMap().size());
110 @Test
111 public void testGetAttributes() {
112 Get get = new Get(ROW);
113 Assert.assertTrue(get.getAttributesMap().isEmpty());
114 Assert.assertNull(get.getAttribute("absent"));
116 get.setAttribute("absent", null);
117 Assert.assertTrue(get.getAttributesMap().isEmpty());
118 Assert.assertNull(get.getAttribute("absent"));
120 // adding attribute
121 get.setAttribute("attribute1", Bytes.toBytes("value1"));
122 Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), get.getAttribute("attribute1")));
123 Assert.assertEquals(1, get.getAttributesMap().size());
124 Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"),
125 get.getAttributesMap().get("attribute1")));
127 // overriding attribute value
128 get.setAttribute("attribute1", Bytes.toBytes("value12"));
129 Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), get.getAttribute("attribute1")));
130 Assert.assertEquals(1, get.getAttributesMap().size());
131 Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"),
132 get.getAttributesMap().get("attribute1")));
134 // adding another attribute
135 get.setAttribute("attribute2", Bytes.toBytes("value2"));
136 Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), get.getAttribute("attribute2")));
137 Assert.assertEquals(2, get.getAttributesMap().size());
138 Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"),
139 get.getAttributesMap().get("attribute2")));
141 // removing attribute
142 get.setAttribute("attribute2", null);
143 Assert.assertNull(get.getAttribute("attribute2"));
144 Assert.assertEquals(1, get.getAttributesMap().size());
145 Assert.assertNull(get.getAttributesMap().get("attribute2"));
147 // removing non-existed attribute
148 get.setAttribute("attribute2", null);
149 Assert.assertNull(get.getAttribute("attribute2"));
150 Assert.assertEquals(1, get.getAttributesMap().size());
151 Assert.assertNull(get.getAttributesMap().get("attribute2"));
153 // removing another attribute
154 get.setAttribute("attribute1", null);
155 Assert.assertNull(get.getAttribute("attribute1"));
156 Assert.assertTrue(get.getAttributesMap().isEmpty());
157 Assert.assertNull(get.getAttributesMap().get("attribute1"));
160 @Test
161 public void testNullQualifier() {
162 Get get = new Get(ROW);
163 byte[] family = Bytes.toBytes("family");
164 get.addColumn(family, null);
165 Set<byte[]> qualifiers = get.getFamilyMap().get(family);
166 Assert.assertEquals(1, qualifiers.size());
169 @Test
170 public void TestGetRowFromGetCopyConstructor() throws Exception {
171 Get get = new Get(ROW);
172 get.setFilter(null);
173 get.setAuthorizations(new Authorizations("foo"));
174 get.setACL("u", new Permission(Permission.Action.READ));
175 get.setConsistency(Consistency.TIMELINE);
176 get.setReplicaId(2);
177 get.setIsolationLevel(IsolationLevel.READ_UNCOMMITTED);
178 get.setCheckExistenceOnly(true);
179 get.setTimeRange(3, 4);
180 get.readVersions(11);
181 get.setMaxResultsPerColumnFamily(10);
182 get.setRowOffsetPerColumnFamily(11);
183 get.setCacheBlocks(true);
185 Get copyGet = new Get(get);
186 assertEquals(0, Bytes.compareTo(get.getRow(), copyGet.getRow()));
188 // from OperationWithAttributes
189 assertEquals(get.getId(), copyGet.getId());
191 // from Query class
192 assertEquals(get.getFilter(), copyGet.getFilter());
193 assertTrue(get.getAuthorizations().toString().equals(copyGet.getAuthorizations().toString()));
194 assertTrue(Bytes.equals(get.getACL(), copyGet.getACL()));
195 assertEquals(get.getConsistency(), copyGet.getConsistency());
196 assertEquals(get.getReplicaId(), copyGet.getReplicaId());
197 assertEquals(get.getIsolationLevel(), copyGet.getIsolationLevel());
199 // from Get class
200 assertEquals(get.isCheckExistenceOnly(), copyGet.isCheckExistenceOnly());
201 assertTrue(get.getTimeRange().equals(copyGet.getTimeRange()));
202 assertEquals(get.getMaxVersions(), copyGet.getMaxVersions());
203 assertEquals(get.getMaxResultsPerColumnFamily(), copyGet.getMaxResultsPerColumnFamily());
204 assertEquals(get.getRowOffsetPerColumnFamily(), copyGet.getRowOffsetPerColumnFamily());
205 assertEquals(get.getCacheBlocks(), copyGet.getCacheBlocks());
206 assertEquals(get.getId(), copyGet.getId());
209 @Test
210 public void testDynamicFilter() throws Exception {
211 Configuration conf = HBaseConfiguration.create();
212 String localPath = conf.get("hbase.local.dir")
213 + File.separator + "jars" + File.separator;
214 File jarFile = new File(localPath, "MockFilter.jar");
215 jarFile.delete();
216 assertFalse("Should be deleted: " + jarFile.getPath(), jarFile.exists());
218 ClientProtos.Get getProto1 =
219 ClientProtos.Get.parseFrom(Base64.getDecoder().decode(PB_GET));
220 ClientProtos.Get getProto2 =
221 ClientProtos.Get.parseFrom(Base64.getDecoder().decode(PB_GET_WITH_FILTER_LIST));
222 try {
223 ProtobufUtil.toGet(getProto1);
224 fail("Should not be able to load the filter class");
225 } catch (IOException ioe) {
226 assertTrue(ioe.getCause() instanceof ClassNotFoundException);
228 try {
229 ProtobufUtil.toGet(getProto2);
230 fail("Should not be able to load the filter class");
231 } catch (IOException ioe) {
232 assertTrue(ioe.getCause() instanceof InvocationTargetException);
233 InvocationTargetException ite = (InvocationTargetException)ioe.getCause();
234 assertTrue(ite.getTargetException()
235 instanceof DeserializationException);
237 FileOutputStream fos = new FileOutputStream(jarFile);
238 fos.write(Base64.getDecoder().decode(MOCK_FILTER_JAR));
239 fos.close();
241 Get get1 = ProtobufUtil.toGet(getProto1);
242 assertEquals("test.MockFilter", get1.getFilter().getClass().getName());
244 Get get2 = ProtobufUtil.toGet(getProto2);
245 assertTrue(get2.getFilter() instanceof FilterList);
246 List<Filter> filters = ((FilterList)get2.getFilter()).getFilters();
247 assertEquals(3, filters.size());
248 assertEquals("test.MockFilter", filters.get(0).getClass().getName());
249 assertEquals("my.MockFilter", filters.get(1).getClass().getName());
250 assertTrue(filters.get(2) instanceof KeyOnlyFilter);
253 @Test
254 public void testGetRowConstructor() {
255 byte[] row1 = Bytes.toBytes("testRow");
256 byte[] row2 = Bytes.toBytes("testtestRow");
257 ByteBuffer rowBuffer = ByteBuffer.allocate(16);
258 rowBuffer = ByteBuffer.wrap(row1);
259 Get get1 = new Get(rowBuffer);
260 Get get2 = new Get(row2, 4, 7);
261 Assert.assertArrayEquals(get1.getRow(), get2.getRow());