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.filter;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNotNull;
022import static org.junit.Assert.assertNull;
023import static org.junit.Assert.assertTrue;
024
025import java.io.IOException;
026import java.util.ArrayList;
027import java.util.List;
028import org.apache.hadoop.conf.Configuration;
029import org.apache.hadoop.hbase.Cell;
030import org.apache.hadoop.hbase.CellUtil;
031import org.apache.hadoop.hbase.CompareOperator;
032import org.apache.hadoop.hbase.HBaseClassTestRule;
033import org.apache.hadoop.hbase.HBaseConfiguration;
034import org.apache.hadoop.hbase.HBaseTestingUtil;
035import org.apache.hadoop.hbase.HConstants;
036import org.apache.hadoop.hbase.MasterNotRunningException;
037import org.apache.hadoop.hbase.TableName;
038import org.apache.hadoop.hbase.ZooKeeperConnectionException;
039import org.apache.hadoop.hbase.client.Admin;
040import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
041import org.apache.hadoop.hbase.client.Connection;
042import org.apache.hadoop.hbase.client.ConnectionFactory;
043import org.apache.hadoop.hbase.client.Put;
044import org.apache.hadoop.hbase.client.Result;
045import org.apache.hadoop.hbase.client.ResultScanner;
046import org.apache.hadoop.hbase.client.Scan;
047import org.apache.hadoop.hbase.client.Table;
048import org.apache.hadoop.hbase.client.TableDescriptor;
049import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
050import org.apache.hadoop.hbase.testclassification.FilterTests;
051import org.apache.hadoop.hbase.testclassification.MediumTests;
052import org.apache.hadoop.hbase.util.Bytes;
053import org.junit.AfterClass;
054import org.junit.BeforeClass;
055import org.junit.ClassRule;
056import org.junit.Test;
057import org.junit.experimental.categories.Category;
058import org.slf4j.Logger;
059import org.slf4j.LoggerFactory;
060
061/**
062 * Test if the FilterWrapper retains the same semantics defined in the
063 * {@link org.apache.hadoop.hbase.filter.Filter}
064 */
065@Category({FilterTests.class, MediumTests.class})
066public class TestFilterWrapper {
067
068  @ClassRule
069  public static final HBaseClassTestRule CLASS_RULE =
070      HBaseClassTestRule.forClass(TestFilterWrapper.class);
071
072  private static final Logger LOG = LoggerFactory.getLogger(TestFilterWrapper.class);
073
074  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
075  private static Configuration conf = null;
076  private static Admin admin = null;
077  private static TableName name = TableName.valueOf("test");
078  private static Connection connection;
079
080  @Test
081  public void testFilterWrapper() {
082    int kv_number = 0;
083    int row_number = 0;
084    try {
085      Scan scan = new Scan();
086      List<Filter> fs = new ArrayList<>();
087
088      DependentColumnFilter f1 = new DependentColumnFilter(Bytes.toBytes("f1"),
089          Bytes.toBytes("c5"), true, CompareOperator.EQUAL,
090          new SubstringComparator("c5"));
091      PageFilter f2 = new PageFilter(2);
092      fs.add(f1);
093      fs.add(f2);
094      FilterList filter = new FilterList(fs);
095
096      scan.setFilter(filter);
097      Table table = connection.getTable(name);
098      ResultScanner scanner = table.getScanner(scan);
099
100      // row2 (c1-c4) and row3(c1-c4) are returned
101      for (Result result : scanner) {
102        row_number++;
103        for (Cell kv : result.listCells()) {
104          LOG.debug(kv_number + ". kv: " + kv);
105          kv_number++;
106          assertEquals("Returned row is not correct", Bytes.toString(CellUtil.cloneRow(kv)),
107              "row" + ( row_number + 1 ));
108        }
109      }
110
111      scanner.close();
112      table.close();
113    } catch (Exception e) {
114      // no correct result is expected
115      assertNull("Exception happens in scan", e);
116    }
117    LOG.debug("check the fetched kv number");
118    assertEquals("We should get 8 results returned.", 8, kv_number);
119    assertEquals("We should get 2 rows returned", 2, row_number);
120  }
121
122  private static void prepareData() {
123    try {
124      Table table = connection.getTable(name);
125      assertTrue("Fail to create the table", admin.tableExists(name));
126      List<Put> puts = new ArrayList<>();
127
128      // row1 => <f1:c1, 1_c1, ts=1>, <f1:c2, 1_c2, ts=2>, <f1:c3, 1_c3,ts=3>,
129      // <f1:c4,1_c4, ts=4>, <f1:c5, 1_c5, ts=5>
130      // row2 => <f1:c1, 2_c1, ts=2>, <f1,c2, 2_c2, ts=2>, <f1:c3, 2_c3,ts=2>,
131      // <f1:c4,2_c4, ts=2>, <f1:c5, 2_c5, ts=2>
132      // row3 => <f1:c1, 3_c1, ts=3>, <f1:c2, 3_c2, ts=3>, <f1:c3, 3_c3,ts=2>,
133      // <f1:c4,3_c4, ts=3>, <f1:c5, 3_c5, ts=3>
134      for (int i = 1; i < 4; i++) {
135        Put put = new Put(Bytes.toBytes("row" + i));
136        for (int j = 1; j < 6; j++) {
137          long timestamp = j;
138          if (i != 1)
139            timestamp = i;
140          put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("c" + j), timestamp,
141                  Bytes.toBytes(i + "_c" + j));
142        }
143        puts.add(put);
144      }
145
146      table.put(puts);
147      table.close();
148    } catch (IOException e) {
149      assertNull("Exception found while putting data into table", e);
150    }
151  }
152
153  private static void createTable() {
154    assertNotNull("HBaseAdmin is not initialized successfully.", admin);
155    if (admin != null) {
156      TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(name)
157        .setColumnFamily(ColumnFamilyDescriptorBuilder.of(Bytes.toBytes("f1"))).build();
158
159      try {
160        admin.createTable(tableDescriptor);
161        assertTrue("Fail to create the table", admin.tableExists(name));
162      } catch (IOException e) {
163        assertNull("Exception found while creating table", e);
164      }
165    }
166  }
167
168  private static void deleteTable() {
169    if (admin != null) {
170      try {
171        admin.disableTable(name);
172        admin.deleteTable(name);
173      } catch (IOException e) {
174        assertNull("Exception found deleting the table", e);
175      }
176    }
177  }
178
179  private static void initialize(Configuration conf) {
180    TestFilterWrapper.conf = HBaseConfiguration.create(conf);
181    TestFilterWrapper.conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1);
182    try {
183      connection = ConnectionFactory.createConnection(TestFilterWrapper.conf);
184      admin = TEST_UTIL.getAdmin();
185    } catch (MasterNotRunningException e) {
186      assertNull("Master is not running", e);
187    } catch (ZooKeeperConnectionException e) {
188      assertNull("Cannot connect to ZooKeeper", e);
189    } catch (IOException e) {
190      assertNull("Caught IOException", e);
191    }
192    createTable();
193    prepareData();
194  }
195
196  @BeforeClass
197  public static void setUp() throws Exception {
198    TEST_UTIL.startMiniCluster(1);
199    initialize(TEST_UTIL.getConfiguration());
200  }
201
202  @AfterClass
203  public static void tearDown() throws Exception {
204    deleteTable();
205    connection.close();
206    TEST_UTIL.shutdownMiniCluster();
207  }
208
209}