001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023import static org.junit.Assert.fail;
024
025import java.io.File;
026import java.io.FileOutputStream;
027import java.io.IOException;
028import java.lang.reflect.InvocationTargetException;
029import java.nio.ByteBuffer;
030import java.util.Arrays;
031import java.util.Base64;
032import java.util.List;
033import java.util.Set;
034import org.apache.hadoop.conf.Configuration;
035import org.apache.hadoop.hbase.HBaseClassTestRule;
036import org.apache.hadoop.hbase.HBaseConfiguration;
037import org.apache.hadoop.hbase.exceptions.DeserializationException;
038import org.apache.hadoop.hbase.filter.Filter;
039import org.apache.hadoop.hbase.filter.FilterList;
040import org.apache.hadoop.hbase.filter.KeyOnlyFilter;
041import org.apache.hadoop.hbase.security.access.Permission;
042import org.apache.hadoop.hbase.security.visibility.Authorizations;
043import org.apache.hadoop.hbase.testclassification.ClientTests;
044import org.apache.hadoop.hbase.testclassification.SmallTests;
045import org.apache.hadoop.hbase.util.Bytes;
046import org.junit.Assert;
047import org.junit.ClassRule;
048import org.junit.Test;
049import org.junit.experimental.categories.Category;
050
051import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
052import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos;
053
054// TODO: cover more test cases
055@Category({ ClientTests.class, SmallTests.class })
056public class TestGet {
057  @ClassRule
058  public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestGet.class);
059
060  private static final byte[] ROW = new byte[] { 'r' };
061
062  private static final String PB_GET = "CgNyb3ciEwoPdGVzdC5Nb2NrRmlsdGVyEgAwATgB";
063  private static final String PB_GET_WITH_FILTER_LIST =
064    "CgFyIosBCilvcmcuYXBhY2hlLmhhZG9vcC5oYmFzZS5maWx0ZXIuRmlsdGVyTGlzdBJeCAESEwoP"
065      + "dGVzdC5Nb2NrRmlsdGVyEgASEQoNbXkuTW9ja0ZpbHRlchIAEjIKLG9yZy5hcGFjaGUuaGFkb29w"
066      + "LmhiYXNlLmZpbHRlci5LZXlPbmx5RmlsdGVyEgIIADABOAE=";
067
068  private static final String MOCK_FILTER_JAR =
069    "UEsDBBQACAgIANWDlEMAAAAAAAAAAAAAAAAJAAQATUVUQS1JTkYv/soAAAMAUEsHCAAAAAACAAAA"
070      + "AAAAAFBLAwQUAAgICADVg5RDAAAAAAAAAAAAAAAAFAAAAE1FVEEtSU5GL01BTklGRVNULk1G803M"
071      + "y0xLLS7RDUstKs7Mz7NSMNQz4OVyLkpNLElN0XWqBAmY6xnEG1gqaPgXJSbnpCo45xcV5BcllgCV"
072      + "a/Jy8XIBAFBLBwgxyqRbQwAAAEQAAABQSwMEFAAICAgAUoOUQwAAAAAAAAAAAAAAABMAAABteS9N"
073      + "b2NrRmlsdGVyLmNsYXNzdZHPTsJAEMa/LYVCRVFQMd68gQc38YrxUJUTetGQGE7bstrVwjbbYsSn"
074      + "0hOJJj6AD2WcFoP/4iYzX+bb32xmd9/en18B7GPLhY11BxsurEw3GUoHaqzSQ4ZCq91nsI/0UDLU"
075      + "emoszyYjX5oL4Ufk1Hs6EFFfGJXVn6adhirJ6NGUn+rgtquiVJoOQyUWJpFdo0cMjdbAa/8hnNj3"
076      + "pqmkbmvgMbgn94GMU6XHiYMm1ed6YgJJeDbNV+fejbgTVRRRYlj+cSZDW5trLmIRhJKHYqh1zENf"
077      + "JJJf5QCfcx45DJ3/WLmYgx/LRNJ1I/UgMmMxIXbo9WxkywLLZqHsUMVJGWlxdwb2lG+XKZdys4kK"
078      + "5eocgIsl0grVy0Q5+e9Y+V75BdblDIXHX/3b3/rLWEGNdJXCJmeNop7zjQ9QSwcI1kzyMToBAADs"
079      + "AQAAUEsDBBQACAgIAFKDlEMAAAAAAAAAAAAAAAAVAAAAdGVzdC9Nb2NrRmlsdGVyLmNsYXNzdVHB"
080      + "TsJAFJwthUJFERQx3ryBBzfxivFQlRN60ZAYTtuy2tXCNtti1K/SE4kmfoAfZXwtBg3RTd6bzOy8"
081      + "zezux+frO4ADbLuwsemg6cLKcIuhdKgmKj1iKLQ7Awb7WI8kQ62vJvJ8OvaluRR+REqjrwMRDYRR"
082      + "Gf8W7TRUCUO9n8ok5Wc6uOupKJWmy1CJhUlkz+gxQ7M99Dp/eJzY9x5JZrCGHoN7+hDIOFV6kjho"
083      + "Eb/QUxNIsmeJfib3b8W9qKKIEslLpzJ0tLnhIhZBKHkoRlrHPPRFIvl1buBzn0cKQ/c/r1wk4Scy"
084      + "kXTpSD2JTFhkxC69oY1sWWBZGuoOMU7ICIt7M7CXfLtMvZSLLVSoV+cGuFghrBBfJZeT/5GV75Xf"
085      + "YF3NUHhemt/5NV/GGmqE61Q2KXWqRu7f+AJQSwcIrS5nKDoBAADyAQAAUEsBAhQAFAAICAgA1YOU"
086      + "QwAAAAACAAAAAAAAAAkABAAAAAAAAAAAAAAAAAAAAE1FVEEtSU5GL/7KAABQSwECFAAUAAgICADV"
087      + "g5RDMcqkW0MAAABEAAAAFAAAAAAAAAAAAAAAAAA9AAAATUVUQS1JTkYvTUFOSUZFU1QuTUZQSwEC"
088      + "FAAUAAgICABSg5RD1kzyMToBAADsAQAAEwAAAAAAAAAAAAAAAADCAAAAbXkvTW9ja0ZpbHRlci5j"
089      + "bGFzc1BLAQIUABQACAgIAFKDlEOtLmcoOgEAAPIBAAAVAAAAAAAAAAAAAAAAAD0CAAB0ZXN0L01v"
090      + "Y2tGaWx0ZXIuY2xhc3NQSwUGAAAAAAQABAABAQAAugMAAAAA";
091
092  @Test
093  public void testAttributesSerialization() throws IOException {
094    Get get = new Get(Bytes.toBytes("row"));
095    get.setAttribute("attribute1", Bytes.toBytes("value1"));
096    get.setAttribute("attribute2", Bytes.toBytes("value2"));
097    get.setAttribute("attribute3", Bytes.toBytes("value3"));
098
099    ClientProtos.Get getProto = ProtobufUtil.toGet(get);
100
101    Get get2 = ProtobufUtil.toGet(getProto);
102    Assert.assertNull(get2.getAttribute("absent"));
103    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), get2.getAttribute("attribute1")));
104    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), get2.getAttribute("attribute2")));
105    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value3"), get2.getAttribute("attribute3")));
106    Assert.assertEquals(3, get2.getAttributesMap().size());
107  }
108
109  @Test
110  public void testGetAttributes() {
111    Get get = new Get(ROW);
112    Assert.assertTrue(get.getAttributesMap().isEmpty());
113    Assert.assertNull(get.getAttribute("absent"));
114
115    get.setAttribute("absent", null);
116    Assert.assertTrue(get.getAttributesMap().isEmpty());
117    Assert.assertNull(get.getAttribute("absent"));
118
119    // adding attribute
120    get.setAttribute("attribute1", Bytes.toBytes("value1"));
121    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), get.getAttribute("attribute1")));
122    Assert.assertEquals(1, get.getAttributesMap().size());
123    Assert
124      .assertTrue(Arrays.equals(Bytes.toBytes("value1"), get.getAttributesMap().get("attribute1")));
125
126    // overriding attribute value
127    get.setAttribute("attribute1", Bytes.toBytes("value12"));
128    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), get.getAttribute("attribute1")));
129    Assert.assertEquals(1, get.getAttributesMap().size());
130    Assert.assertTrue(
131      Arrays.equals(Bytes.toBytes("value12"), get.getAttributesMap().get("attribute1")));
132
133    // adding another attribute
134    get.setAttribute("attribute2", Bytes.toBytes("value2"));
135    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), get.getAttribute("attribute2")));
136    Assert.assertEquals(2, get.getAttributesMap().size());
137    Assert
138      .assertTrue(Arrays.equals(Bytes.toBytes("value2"), get.getAttributesMap().get("attribute2")));
139
140    // removing attribute
141    get.setAttribute("attribute2", null);
142    Assert.assertNull(get.getAttribute("attribute2"));
143    Assert.assertEquals(1, get.getAttributesMap().size());
144    Assert.assertNull(get.getAttributesMap().get("attribute2"));
145
146    // removing non-existed attribute
147    get.setAttribute("attribute2", null);
148    Assert.assertNull(get.getAttribute("attribute2"));
149    Assert.assertEquals(1, get.getAttributesMap().size());
150    Assert.assertNull(get.getAttributesMap().get("attribute2"));
151
152    // removing another attribute
153    get.setAttribute("attribute1", null);
154    Assert.assertNull(get.getAttribute("attribute1"));
155    Assert.assertTrue(get.getAttributesMap().isEmpty());
156    Assert.assertNull(get.getAttributesMap().get("attribute1"));
157  }
158
159  @Test
160  public void testNullQualifier() {
161    Get get = new Get(ROW);
162    byte[] family = Bytes.toBytes("family");
163    get.addColumn(family, null);
164    Set<byte[]> qualifiers = get.getFamilyMap().get(family);
165    Assert.assertEquals(1, qualifiers.size());
166  }
167
168  @Test
169  public void TestGetRowFromGetCopyConstructor() throws Exception {
170    Get get = new Get(ROW);
171    get.setFilter(null);
172    get.setAuthorizations(new Authorizations("foo"));
173    get.setACL("u", new Permission(Permission.Action.READ));
174    get.setConsistency(Consistency.TIMELINE);
175    get.setReplicaId(2);
176    get.setIsolationLevel(IsolationLevel.READ_UNCOMMITTED);
177    get.setCheckExistenceOnly(true);
178    get.setTimeRange(3, 4);
179    get.setMaxVersions(11);
180    get.setMaxResultsPerColumnFamily(10);
181    get.setRowOffsetPerColumnFamily(11);
182    get.setCacheBlocks(true);
183
184    Get copyGet = new Get(get);
185    assertEquals(0, Bytes.compareTo(get.getRow(), copyGet.getRow()));
186
187    // from OperationWithAttributes
188    assertEquals(get.getId(), copyGet.getId());
189
190    // from Query class
191    assertEquals(get.getFilter(), copyGet.getFilter());
192    assertTrue(get.getAuthorizations().toString().equals(copyGet.getAuthorizations().toString()));
193    assertTrue(Bytes.equals(get.getACL(), copyGet.getACL()));
194    assertEquals(get.getConsistency(), copyGet.getConsistency());
195    assertEquals(get.getReplicaId(), copyGet.getReplicaId());
196    assertEquals(get.getIsolationLevel(), copyGet.getIsolationLevel());
197
198    // from Get class
199    assertEquals(get.isCheckExistenceOnly(), copyGet.isCheckExistenceOnly());
200    assertTrue(get.getTimeRange().equals(copyGet.getTimeRange()));
201    assertEquals(get.getMaxVersions(), copyGet.getMaxVersions());
202    assertEquals(get.getMaxResultsPerColumnFamily(), copyGet.getMaxResultsPerColumnFamily());
203    assertEquals(get.getRowOffsetPerColumnFamily(), copyGet.getRowOffsetPerColumnFamily());
204    assertEquals(get.getCacheBlocks(), copyGet.getCacheBlocks());
205    assertEquals(get.getId(), copyGet.getId());
206  }
207
208  @Test
209  public void testDynamicFilter() throws Exception {
210    Configuration conf = HBaseConfiguration.create();
211    String localPath = conf.get("hbase.local.dir") + File.separator + "jars" + File.separator;
212    File jarFile = new File(localPath, "MockFilter.jar");
213    jarFile.delete();
214    assertFalse("Should be deleted: " + jarFile.getPath(), jarFile.exists());
215
216    ClientProtos.Get getProto1 = ClientProtos.Get.parseFrom(Base64.getDecoder().decode(PB_GET));
217    ClientProtos.Get getProto2 =
218      ClientProtos.Get.parseFrom(Base64.getDecoder().decode(PB_GET_WITH_FILTER_LIST));
219    try {
220      ProtobufUtil.toGet(getProto1);
221      fail("Should not be able to load the filter class");
222    } catch (IOException ioe) {
223      assertTrue(ioe.getCause() instanceof ClassNotFoundException);
224    }
225    try {
226      ProtobufUtil.toGet(getProto2);
227      fail("Should not be able to load the filter class");
228    } catch (IOException ioe) {
229      assertTrue(ioe.getCause() instanceof InvocationTargetException);
230      InvocationTargetException ite = (InvocationTargetException) ioe.getCause();
231      assertTrue(ite.getTargetException() instanceof DeserializationException);
232    }
233    FileOutputStream fos = new FileOutputStream(jarFile);
234    fos.write(Base64.getDecoder().decode(MOCK_FILTER_JAR));
235    fos.close();
236
237    Get get1 = ProtobufUtil.toGet(getProto1);
238    assertEquals("test.MockFilter", get1.getFilter().getClass().getName());
239
240    Get get2 = ProtobufUtil.toGet(getProto2);
241    assertTrue(get2.getFilter() instanceof FilterList);
242    List<Filter> filters = ((FilterList) get2.getFilter()).getFilters();
243    assertEquals(3, filters.size());
244    assertEquals("test.MockFilter", filters.get(0).getClass().getName());
245    assertEquals("my.MockFilter", filters.get(1).getClass().getName());
246    assertTrue(filters.get(2) instanceof KeyOnlyFilter);
247  }
248
249  @Test
250  public void testGetRowConstructor() {
251    byte[] row1 = Bytes.toBytes("testRow");
252    byte[] row2 = Bytes.toBytes("testtestRow");
253    ByteBuffer rowBuffer = ByteBuffer.wrap(row1);
254    Get get1 = new Get(rowBuffer);
255    Get get2 = new Get(row2, 4, 7);
256    Assert.assertArrayEquals(get1.getRow(), get2.getRow());
257  }
258}