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