View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.client;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertTrue;
25  import static org.junit.Assert.fail;
26  
27  import java.io.File;
28  import java.io.FileOutputStream;
29  import java.io.IOException;
30  import java.lang.reflect.InvocationTargetException;
31  import java.util.Arrays;
32  import java.util.List;
33  import java.util.Set;
34  
35  import org.apache.hadoop.conf.Configuration;
36  import org.apache.hadoop.hbase.HBaseConfiguration;
37  import org.apache.hadoop.hbase.testclassification.SmallTests;
38  import org.apache.hadoop.hbase.exceptions.DeserializationException;
39  import org.apache.hadoop.hbase.filter.Filter;
40  import org.apache.hadoop.hbase.filter.FilterList;
41  import org.apache.hadoop.hbase.filter.KeyOnlyFilter;
42  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
43  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
44  import org.apache.hadoop.hbase.security.access.Permission;
45  import org.apache.hadoop.hbase.security.visibility.Authorizations;
46  import org.apache.hadoop.hbase.util.Base64;
47  import org.apache.hadoop.hbase.util.Bytes;
48  import org.junit.Assert;
49  import org.junit.Test;
50  import org.junit.experimental.categories.Category;
51  
52  // TODO: cover more test cases
53  @Category(SmallTests.class)
54  public class TestGet {
55    private static final byte [] ROW = new byte [] {'r'};
56  
57    private static final String PB_GET = "CgNyb3ciEwoPdGVzdC5Nb2NrRmlsdGVyEgAwATgB";
58    private static final String PB_GET_WITH_FILTER_LIST =
59      "CgFyIosBCilvcmcuYXBhY2hlLmhhZG9vcC5oYmFzZS5maWx0ZXIuRmlsdGVyTGlzdBJeCAESEwoP" +
60      "dGVzdC5Nb2NrRmlsdGVyEgASEQoNbXkuTW9ja0ZpbHRlchIAEjIKLG9yZy5hcGFjaGUuaGFkb29w" +
61      "LmhiYXNlLmZpbHRlci5LZXlPbmx5RmlsdGVyEgIIADABOAE=";
62  
63    private static final String MOCK_FILTER_JAR =
64      "UEsDBBQACAgIANWDlEMAAAAAAAAAAAAAAAAJAAQATUVUQS1JTkYv/soAAAMAUEsHCAAAAAACAAAA" +
65      "AAAAAFBLAwQUAAgICADVg5RDAAAAAAAAAAAAAAAAFAAAAE1FVEEtSU5GL01BTklGRVNULk1G803M" +
66      "y0xLLS7RDUstKs7Mz7NSMNQz4OVyLkpNLElN0XWqBAmY6xnEG1gqaPgXJSbnpCo45xcV5BcllgCV" +
67      "a/Jy8XIBAFBLBwgxyqRbQwAAAEQAAABQSwMEFAAICAgAUoOUQwAAAAAAAAAAAAAAABMAAABteS9N" +
68      "b2NrRmlsdGVyLmNsYXNzdZHPTsJAEMa/LYVCRVFQMd68gQc38YrxUJUTetGQGE7bstrVwjbbYsSn" +
69      "0hOJJj6AD2WcFoP/4iYzX+bb32xmd9/en18B7GPLhY11BxsurEw3GUoHaqzSQ4ZCq91nsI/0UDLU" +
70      "emoszyYjX5oL4Ufk1Hs6EFFfGJXVn6adhirJ6NGUn+rgtquiVJoOQyUWJpFdo0cMjdbAa/8hnNj3" +
71      "pqmkbmvgMbgn94GMU6XHiYMm1ed6YgJJeDbNV+fejbgTVRRRYlj+cSZDW5trLmIRhJKHYqh1zENf" +
72      "JJJf5QCfcx45DJ3/WLmYgx/LRNJ1I/UgMmMxIXbo9WxkywLLZqHsUMVJGWlxdwb2lG+XKZdys4kK" +
73      "5eocgIsl0grVy0Q5+e9Y+V75BdblDIXHX/3b3/rLWEGNdJXCJmeNop7zjQ9QSwcI1kzyMToBAADs" +
74      "AQAAUEsDBBQACAgIAFKDlEMAAAAAAAAAAAAAAAAVAAAAdGVzdC9Nb2NrRmlsdGVyLmNsYXNzdVHB" +
75      "TsJAFJwthUJFERQx3ryBBzfxivFQlRN60ZAYTtuy2tXCNtti1K/SE4kmfoAfZXwtBg3RTd6bzOy8" +
76      "zezux+frO4ADbLuwsemg6cLKcIuhdKgmKj1iKLQ7Awb7WI8kQ62vJvJ8OvaluRR+REqjrwMRDYRR" +
77      "Gf8W7TRUCUO9n8ok5Wc6uOupKJWmy1CJhUlkz+gxQ7M99Dp/eJzY9x5JZrCGHoN7+hDIOFV6kjho" +
78      "Eb/QUxNIsmeJfib3b8W9qKKIEslLpzJ0tLnhIhZBKHkoRlrHPPRFIvl1buBzn0cKQ/c/r1wk4Scy" +
79      "kXTpSD2JTFhkxC69oY1sWWBZGuoOMU7ICIt7M7CXfLtMvZSLLVSoV+cGuFghrBBfJZeT/5GV75Xf" +
80      "YF3NUHhemt/5NV/GGmqE61Q2KXWqRu7f+AJQSwcIrS5nKDoBAADyAQAAUEsBAhQAFAAICAgA1YOU" +
81      "QwAAAAACAAAAAAAAAAkABAAAAAAAAAAAAAAAAAAAAE1FVEEtSU5GL/7KAABQSwECFAAUAAgICADV" +
82      "g5RDMcqkW0MAAABEAAAAFAAAAAAAAAAAAAAAAAA9AAAATUVUQS1JTkYvTUFOSUZFU1QuTUZQSwEC" +
83      "FAAUAAgICABSg5RD1kzyMToBAADsAQAAEwAAAAAAAAAAAAAAAADCAAAAbXkvTW9ja0ZpbHRlci5j" +
84      "bGFzc1BLAQIUABQACAgIAFKDlEOtLmcoOgEAAPIBAAAVAAAAAAAAAAAAAAAAAD0CAAB0ZXN0L01v" +
85      "Y2tGaWx0ZXIuY2xhc3NQSwUGAAAAAAQABAABAQAAugMAAAAA";
86  
87    @Test
88    public void testAttributesSerialization() throws IOException {
89      Get get = new Get(Bytes.toBytes("row"));
90      get.setAttribute("attribute1", Bytes.toBytes("value1"));
91      get.setAttribute("attribute2", Bytes.toBytes("value2"));
92      get.setAttribute("attribute3", Bytes.toBytes("value3"));
93  
94      ClientProtos.Get getProto = ProtobufUtil.toGet(get);
95  
96      Get get2 = ProtobufUtil.toGet(getProto);
97      Assert.assertNull(get2.getAttribute("absent"));
98      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), get2.getAttribute("attribute1")));
99      Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), get2.getAttribute("attribute2")));
100     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value3"), get2.getAttribute("attribute3")));
101     Assert.assertEquals(3, get2.getAttributesMap().size());
102   }
103 
104   @Test
105   public void testGetAttributes() {
106     Get get = new Get(ROW);
107     Assert.assertTrue(get.getAttributesMap().isEmpty());
108     Assert.assertNull(get.getAttribute("absent"));
109 
110     get.setAttribute("absent", null);
111     Assert.assertTrue(get.getAttributesMap().isEmpty());
112     Assert.assertNull(get.getAttribute("absent"));
113 
114     // adding attribute
115     get.setAttribute("attribute1", Bytes.toBytes("value1"));
116     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), get.getAttribute("attribute1")));
117     Assert.assertEquals(1, get.getAttributesMap().size());
118     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), get.getAttributesMap().get("attribute1")));
119 
120     // overriding attribute value
121     get.setAttribute("attribute1", Bytes.toBytes("value12"));
122     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), get.getAttribute("attribute1")));
123     Assert.assertEquals(1, get.getAttributesMap().size());
124     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), get.getAttributesMap().get("attribute1")));
125 
126     // adding another attribute
127     get.setAttribute("attribute2", Bytes.toBytes("value2"));
128     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), get.getAttribute("attribute2")));
129     Assert.assertEquals(2, get.getAttributesMap().size());
130     Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), get.getAttributesMap().get("attribute2")));
131 
132     // removing attribute
133     get.setAttribute("attribute2", null);
134     Assert.assertNull(get.getAttribute("attribute2"));
135     Assert.assertEquals(1, get.getAttributesMap().size());
136     Assert.assertNull(get.getAttributesMap().get("attribute2"));
137 
138     // removing non-existed attribute
139     get.setAttribute("attribute2", null);
140     Assert.assertNull(get.getAttribute("attribute2"));
141     Assert.assertEquals(1, get.getAttributesMap().size());
142     Assert.assertNull(get.getAttributesMap().get("attribute2"));
143 
144     // removing another attribute
145     get.setAttribute("attribute1", null);
146     Assert.assertNull(get.getAttribute("attribute1"));
147     Assert.assertTrue(get.getAttributesMap().isEmpty());
148     Assert.assertNull(get.getAttributesMap().get("attribute1"));
149   }
150 
151   @Test
152   public void testNullQualifier() {
153     Get get = new Get(ROW);
154     byte[] family = Bytes.toBytes("family");
155     get.addColumn(family, null);
156     Set<byte[]> qualifiers = get.getFamilyMap().get(family);
157     Assert.assertEquals(1, qualifiers.size());
158   }
159 
160   @Test
161   public void TestGetRowFromGetCopyConstructor() throws Exception {
162     Get get = new Get(ROW);
163     get.setFilter(null);
164     get.setAuthorizations(new Authorizations("foo"));
165     get.setACL("u", new Permission(Permission.Action.READ));
166     get.setConsistency(Consistency.TIMELINE);
167     get.setReplicaId(2);
168     get.setIsolationLevel(IsolationLevel.READ_UNCOMMITTED);
169     get.setCheckExistenceOnly(true);
170     get.setClosestRowBefore(true);
171     get.setTimeRange(3, 4);
172     get.setMaxVersions(11);
173     get.setMaxResultsPerColumnFamily(10);
174     get.setRowOffsetPerColumnFamily(11);
175     get.setCacheBlocks(true);
176 
177     Get copyGet = new Get(get);
178     assertEquals(0, Bytes.compareTo(get.getRow(), copyGet.getRow()));
179 
180     // from OperationWithAttributes
181     assertEquals(get.getId(), copyGet.getId());
182 
183     // from Query class
184     assertEquals(get.getFilter(), copyGet.getFilter());
185     assertTrue(get.getAuthorizations().toString().equals(copyGet.getAuthorizations().toString()));
186     assertTrue(Bytes.equals(get.getACL(), copyGet.getACL()));
187     assertEquals(get.getConsistency(), copyGet.getConsistency());
188     assertEquals(get.getReplicaId(), copyGet.getReplicaId());
189     assertEquals(get.getIsolationLevel(), copyGet.getIsolationLevel());
190 
191     // from Get class
192     assertEquals(get.isCheckExistenceOnly(), copyGet.isCheckExistenceOnly());
193     assertEquals(get.isClosestRowBefore(), copyGet.isClosestRowBefore());
194     assertTrue(get.getTimeRange().equals(copyGet.getTimeRange()));
195     assertEquals(get.isClosestRowBefore(), copyGet.isClosestRowBefore());
196     assertEquals(get.getMaxVersions(), copyGet.getMaxVersions());
197     assertEquals(get.getMaxResultsPerColumnFamily(), copyGet.getMaxResultsPerColumnFamily());
198     assertEquals(get.getRowOffsetPerColumnFamily(), copyGet.getRowOffsetPerColumnFamily());
199     assertEquals(get.getCacheBlocks(), copyGet.getCacheBlocks());
200     assertEquals(get.getId(), copyGet.getId());
201   }
202 
203   @Test
204   public void testDynamicFilter() throws Exception {
205     Configuration conf = HBaseConfiguration.create();
206     String localPath = conf.get("hbase.local.dir")
207       + File.separator + "jars" + File.separator;
208     File jarFile = new File(localPath, "MockFilter.jar");
209     jarFile.delete();
210     assertFalse("Should be deleted: " + jarFile.getPath(), jarFile.exists());
211 
212     ClientProtos.Get getProto1 =
213       ClientProtos.Get.parseFrom(Base64.decode(PB_GET));
214     ClientProtos.Get getProto2 =
215       ClientProtos.Get.parseFrom(Base64.decode(PB_GET_WITH_FILTER_LIST));
216     try {
217       ProtobufUtil.toGet(getProto1);
218       fail("Should not be able to load the filter class");
219     } catch (IOException ioe) {
220       assertTrue(ioe.getCause() instanceof ClassNotFoundException);
221     }
222     try {
223       ProtobufUtil.toGet(getProto2);
224       fail("Should not be able to load the filter class");
225     } catch (IOException ioe) {
226       assertTrue(ioe.getCause() instanceof InvocationTargetException);
227       InvocationTargetException ite = (InvocationTargetException)ioe.getCause();
228       assertTrue(ite.getTargetException()
229         instanceof DeserializationException);
230     }
231     FileOutputStream fos = new FileOutputStream(jarFile);
232     fos.write(Base64.decode(MOCK_FILTER_JAR));
233     fos.close();
234 
235     Get get1 = ProtobufUtil.toGet(getProto1);
236     assertEquals("test.MockFilter", get1.getFilter().getClass().getName());
237 
238     Get get2 = ProtobufUtil.toGet(getProto2);
239     assertTrue(get2.getFilter() instanceof FilterList);
240     List<Filter> filters = ((FilterList)get2.getFilter()).getFilters();
241     assertEquals(3, filters.size());
242     assertEquals("test.MockFilter", filters.get(0).getClass().getName());
243     assertEquals("my.MockFilter", filters.get(1).getClass().getName());
244     assertTrue(filters.get(2) instanceof KeyOnlyFilter);
245   }
246 }