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