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.hamcrest.CoreMatchers.instanceOf;
021import static org.hamcrest.MatcherAssert.assertThat;
022import static org.junit.Assert.assertEquals;
023import static org.junit.Assert.assertFalse;
024import static org.junit.Assert.assertTrue;
025import static org.junit.Assert.fail;
026
027import java.io.File;
028import java.io.FileOutputStream;
029import java.io.IOException;
030import java.nio.ByteBuffer;
031import java.util.Arrays;
032import java.util.Base64;
033import java.util.List;
034import java.util.Set;
035import org.apache.hadoop.conf.Configuration;
036import org.apache.hadoop.hbase.HBaseClassTestRule;
037import org.apache.hadoop.hbase.HBaseConfiguration;
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.hbase.thirdparty.com.google.common.base.Throwables;
052
053import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
054import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos;
055
056// TODO: cover more test cases
057@Category({ ClientTests.class, SmallTests.class })
058public class TestGet {
059  @ClassRule
060  public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestGet.class);
061
062  private static final byte[] ROW = new byte[] { 'r' };
063
064  private static final String PB_GET = "CgNyb3ciEwoPdGVzdC5Nb2NrRmlsdGVyEgAwATgB";
065  private static final String PB_GET_WITH_FILTER_LIST =
066    "CgFyIosBCilvcmcuYXBhY2hlLmhhZG9vcC5oYmFzZS5maWx0ZXIuRmlsdGVyTGlzdBJeCAESEwoP"
067      + "dGVzdC5Nb2NrRmlsdGVyEgASEQoNbXkuTW9ja0ZpbHRlchIAEjIKLG9yZy5hcGFjaGUuaGFkb29w"
068      + "LmhiYXNlLmZpbHRlci5LZXlPbmx5RmlsdGVyEgIIADABOAE=";
069
070  private static final String MOCK_FILTER_JAR =
071    "UEsDBBQACAgIANWDlEMAAAAAAAAAAAAAAAAJAAQATUVUQS1JTkYv/soAAAMAUEsHCAAAAAACAAAA"
072      + "AAAAAFBLAwQUAAgICADVg5RDAAAAAAAAAAAAAAAAFAAAAE1FVEEtSU5GL01BTklGRVNULk1G803M"
073      + "y0xLLS7RDUstKs7Mz7NSMNQz4OVyLkpNLElN0XWqBAmY6xnEG1gqaPgXJSbnpCo45xcV5BcllgCV"
074      + "a/Jy8XIBAFBLBwgxyqRbQwAAAEQAAABQSwMEFAAICAgAUoOUQwAAAAAAAAAAAAAAABMAAABteS9N"
075      + "b2NrRmlsdGVyLmNsYXNzdZHPTsJAEMa/LYVCRVFQMd68gQc38YrxUJUTetGQGE7bstrVwjbbYsSn"
076      + "0hOJJj6AD2WcFoP/4iYzX+bb32xmd9/en18B7GPLhY11BxsurEw3GUoHaqzSQ4ZCq91nsI/0UDLU"
077      + "emoszyYjX5oL4Ufk1Hs6EFFfGJXVn6adhirJ6NGUn+rgtquiVJoOQyUWJpFdo0cMjdbAa/8hnNj3"
078      + "pqmkbmvgMbgn94GMU6XHiYMm1ed6YgJJeDbNV+fejbgTVRRRYlj+cSZDW5trLmIRhJKHYqh1zENf"
079      + "JJJf5QCfcx45DJ3/WLmYgx/LRNJ1I/UgMmMxIXbo9WxkywLLZqHsUMVJGWlxdwb2lG+XKZdys4kK"
080      + "5eocgIsl0grVy0Q5+e9Y+V75BdblDIXHX/3b3/rLWEGNdJXCJmeNop7zjQ9QSwcI1kzyMToBAADs"
081      + "AQAAUEsDBBQACAgIAFKDlEMAAAAAAAAAAAAAAAAVAAAAdGVzdC9Nb2NrRmlsdGVyLmNsYXNzdVHB"
082      + "TsJAFJwthUJFERQx3ryBBzfxivFQlRN60ZAYTtuy2tXCNtti1K/SE4kmfoAfZXwtBg3RTd6bzOy8"
083      + "zezux+frO4ADbLuwsemg6cLKcIuhdKgmKj1iKLQ7Awb7WI8kQ62vJvJ8OvaluRR+REqjrwMRDYRR"
084      + "Gf8W7TRUCUO9n8ok5Wc6uOupKJWmy1CJhUlkz+gxQ7M99Dp/eJzY9x5JZrCGHoN7+hDIOFV6kjho"
085      + "Eb/QUxNIsmeJfib3b8W9qKKIEslLpzJ0tLnhIhZBKHkoRlrHPPRFIvl1buBzn0cKQ/c/r1wk4Scy"
086      + "kXTpSD2JTFhkxC69oY1sWWBZGuoOMU7ICIt7M7CXfLtMvZSLLVSoV+cGuFghrBBfJZeT/5GV75Xf"
087      + "YF3NUHhemt/5NV/GGmqE61Q2KXWqRu7f+AJQSwcIrS5nKDoBAADyAQAAUEsBAhQAFAAICAgA1YOU"
088      + "QwAAAAACAAAAAAAAAAkABAAAAAAAAAAAAAAAAAAAAE1FVEEtSU5GL/7KAABQSwECFAAUAAgICADV"
089      + "g5RDMcqkW0MAAABEAAAAFAAAAAAAAAAAAAAAAAA9AAAATUVUQS1JTkYvTUFOSUZFU1QuTUZQSwEC"
090      + "FAAUAAgICABSg5RD1kzyMToBAADsAQAAEwAAAAAAAAAAAAAAAADCAAAAbXkvTW9ja0ZpbHRlci5j"
091      + "bGFzc1BLAQIUABQACAgIAFKDlEOtLmcoOgEAAPIBAAAVAAAAAAAAAAAAAAAAAD0CAAB0ZXN0L01v"
092      + "Y2tGaWx0ZXIuY2xhc3NQSwUGAAAAAAQABAABAQAAugMAAAAA";
093
094  @Test
095  public void testAttributesSerialization() throws IOException {
096    Get get = new Get(Bytes.toBytes("row"));
097    get.setAttribute("attribute1", Bytes.toBytes("value1"));
098    get.setAttribute("attribute2", Bytes.toBytes("value2"));
099    get.setAttribute("attribute3", Bytes.toBytes("value3"));
100
101    ClientProtos.Get getProto = ProtobufUtil.toGet(get);
102
103    Get get2 = ProtobufUtil.toGet(getProto);
104    Assert.assertNull(get2.getAttribute("absent"));
105    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), get2.getAttribute("attribute1")));
106    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), get2.getAttribute("attribute2")));
107    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value3"), get2.getAttribute("attribute3")));
108    Assert.assertEquals(3, get2.getAttributesMap().size());
109  }
110
111  @Test
112  public void testGetAttributes() {
113    Get get = new Get(ROW);
114    Assert.assertTrue(get.getAttributesMap().isEmpty());
115    Assert.assertNull(get.getAttribute("absent"));
116
117    get.setAttribute("absent", null);
118    Assert.assertTrue(get.getAttributesMap().isEmpty());
119    Assert.assertNull(get.getAttribute("absent"));
120
121    // adding attribute
122    get.setAttribute("attribute1", Bytes.toBytes("value1"));
123    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), get.getAttribute("attribute1")));
124    Assert.assertEquals(1, get.getAttributesMap().size());
125    Assert
126      .assertTrue(Arrays.equals(Bytes.toBytes("value1"), get.getAttributesMap().get("attribute1")));
127
128    // overriding attribute value
129    get.setAttribute("attribute1", Bytes.toBytes("value12"));
130    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), get.getAttribute("attribute1")));
131    Assert.assertEquals(1, get.getAttributesMap().size());
132    Assert.assertTrue(
133      Arrays.equals(Bytes.toBytes("value12"), get.getAttributesMap().get("attribute1")));
134
135    // adding another attribute
136    get.setAttribute("attribute2", Bytes.toBytes("value2"));
137    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), get.getAttribute("attribute2")));
138    Assert.assertEquals(2, get.getAttributesMap().size());
139    Assert
140      .assertTrue(Arrays.equals(Bytes.toBytes("value2"), get.getAttributesMap().get("attribute2")));
141
142    // removing attribute
143    get.setAttribute("attribute2", null);
144    Assert.assertNull(get.getAttribute("attribute2"));
145    Assert.assertEquals(1, get.getAttributesMap().size());
146    Assert.assertNull(get.getAttributesMap().get("attribute2"));
147
148    // removing non-existed attribute
149    get.setAttribute("attribute2", null);
150    Assert.assertNull(get.getAttribute("attribute2"));
151    Assert.assertEquals(1, get.getAttributesMap().size());
152    Assert.assertNull(get.getAttributesMap().get("attribute2"));
153
154    // removing another attribute
155    get.setAttribute("attribute1", null);
156    Assert.assertNull(get.getAttribute("attribute1"));
157    Assert.assertTrue(get.getAttributesMap().isEmpty());
158    Assert.assertNull(get.getAttributesMap().get("attribute1"));
159  }
160
161  @Test
162  public void testNullQualifier() {
163    Get get = new Get(ROW);
164    byte[] family = Bytes.toBytes("family");
165    get.addColumn(family, null);
166    Set<byte[]> qualifiers = get.getFamilyMap().get(family);
167    Assert.assertEquals(1, qualifiers.size());
168  }
169
170  @Test
171  public void TestGetRowFromGetCopyConstructor() throws Exception {
172    Get get = new Get(ROW);
173    get.setFilter(null);
174    get.setAuthorizations(new Authorizations("foo"));
175    get.setACL("u", new Permission(Permission.Action.READ));
176    get.setConsistency(Consistency.TIMELINE);
177    get.setReplicaId(2);
178    get.setIsolationLevel(IsolationLevel.READ_UNCOMMITTED);
179    get.setCheckExistenceOnly(true);
180    get.setTimeRange(3, 4);
181    get.setMaxVersions(11);
182    get.setMaxResultsPerColumnFamily(10);
183    get.setRowOffsetPerColumnFamily(11);
184    get.setCacheBlocks(true);
185
186    Get copyGet = new Get(get);
187    assertEquals(0, Bytes.compareTo(get.getRow(), copyGet.getRow()));
188
189    // from OperationWithAttributes
190    assertEquals(get.getId(), copyGet.getId());
191
192    // from Query class
193    assertEquals(get.getFilter(), copyGet.getFilter());
194    assertTrue(get.getAuthorizations().toString().equals(copyGet.getAuthorizations().toString()));
195    assertTrue(Bytes.equals(get.getACL(), copyGet.getACL()));
196    assertEquals(get.getConsistency(), copyGet.getConsistency());
197    assertEquals(get.getReplicaId(), copyGet.getReplicaId());
198    assertEquals(get.getIsolationLevel(), copyGet.getIsolationLevel());
199
200    // from Get class
201    assertEquals(get.isCheckExistenceOnly(), copyGet.isCheckExistenceOnly());
202    assertTrue(get.getTimeRange().equals(copyGet.getTimeRange()));
203    assertEquals(get.getMaxVersions(), copyGet.getMaxVersions());
204    assertEquals(get.getMaxResultsPerColumnFamily(), copyGet.getMaxResultsPerColumnFamily());
205    assertEquals(get.getRowOffsetPerColumnFamily(), copyGet.getRowOffsetPerColumnFamily());
206    assertEquals(get.getCacheBlocks(), copyGet.getCacheBlocks());
207    assertEquals(get.getId(), copyGet.getId());
208  }
209
210  @Test
211  public void testDynamicFilter() throws Exception {
212    Configuration conf = HBaseConfiguration.create();
213    String localPath = conf.get("hbase.local.dir") + 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());
217
218    ClientProtos.Get getProto1 = ClientProtos.Get.parseFrom(Base64.getDecoder().decode(PB_GET));
219    ClientProtos.Get getProto2 =
220      ClientProtos.Get.parseFrom(Base64.getDecoder().decode(PB_GET_WITH_FILTER_LIST));
221    try {
222      ProtobufUtil.toGet(getProto1);
223      fail("Should not be able to load the filter class");
224    } catch (IOException ioe) {
225      assertTrue(ioe.getCause() instanceof ClassNotFoundException);
226    }
227    try {
228      ProtobufUtil.toGet(getProto2);
229      fail("Should not be able to load the filter class");
230    } catch (IOException ioe) {
231      // This test is deserializing a FilterList, and one of the sub-filters is not found.
232      // So the actual caused by is buried a few levels deep.
233      assertThat(Throwables.getRootCause(ioe), instanceOf(ClassNotFoundException.class));
234    }
235    FileOutputStream fos = new FileOutputStream(jarFile);
236    fos.write(Base64.getDecoder().decode(MOCK_FILTER_JAR));
237    fos.close();
238
239    Get get1 = ProtobufUtil.toGet(getProto1);
240    assertEquals("test.MockFilter", get1.getFilter().getClass().getName());
241
242    Get get2 = ProtobufUtil.toGet(getProto2);
243    assertTrue(get2.getFilter() instanceof FilterList);
244    List<Filter> filters = ((FilterList) get2.getFilter()).getFilters();
245    assertEquals(3, filters.size());
246    assertEquals("test.MockFilter", filters.get(0).getClass().getName());
247    assertEquals("my.MockFilter", filters.get(1).getClass().getName());
248    assertTrue(filters.get(2) instanceof KeyOnlyFilter);
249  }
250
251  @Test
252  public void testGetRowConstructor() {
253    byte[] row1 = Bytes.toBytes("testRow");
254    byte[] row2 = Bytes.toBytes("testtestRow");
255    ByteBuffer rowBuffer = ByteBuffer.wrap(row1);
256    Get get1 = new Get(rowBuffer);
257    Get get2 = new Get(row2, 4, 7);
258    Assert.assertArrayEquals(get1.getRow(), get2.getRow());
259  }
260}