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.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.util.ArrayList;
025import java.util.List;
026import org.apache.hadoop.hbase.Cell;
027import org.apache.hadoop.hbase.CellComparatorImpl;
028import org.apache.hadoop.hbase.CompareOperator;
029import org.apache.hadoop.hbase.KeyValue;
030import org.apache.hadoop.hbase.testclassification.FilterTests;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.junit.jupiter.api.Tag;
034import org.junit.jupiter.api.Test;
035
036/**
037 * Tests for {@link SingleColumnValueExcludeFilter}. Because this filter extends
038 * {@link SingleColumnValueFilter}, only the added functionality is tested. That is, method
039 * filterCell(Cell).
040 */
041@Tag(FilterTests.TAG)
042@Tag(SmallTests.TAG)
043public class TestSingleColumnValueExcludeFilter {
044
045  private static final byte[] ROW = Bytes.toBytes("test");
046  private static final byte[] COLUMN_FAMILY = Bytes.toBytes("test");
047  private static final byte[] COLUMN_QUALIFIER = Bytes.toBytes("foo");
048  private static final byte[] COLUMN_QUALIFIER_2 = Bytes.toBytes("foo_2");
049  private static final byte[] VAL_1 = Bytes.toBytes("a");
050  private static final byte[] VAL_2 = Bytes.toBytes("ab");
051
052  /**
053   * Test the overridden functionality of filterCell(Cell)
054   */
055  @Test
056  public void testFilterCell() throws Exception {
057    Filter filter = new SingleColumnValueExcludeFilter(COLUMN_FAMILY, COLUMN_QUALIFIER,
058      CompareOperator.EQUAL, VAL_1);
059
060    // A 'match' situation
061    List<Cell> kvs = new ArrayList<>();
062    KeyValue c = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1);
063
064    kvs.add(new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1));
065    kvs.add(new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, VAL_1));
066    kvs.add(new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1));
067
068    filter.filterRowCells(kvs);
069
070    assertEquals(2, kvs.size(), "resultSize");
071    assertTrue(CellComparatorImpl.COMPARATOR.compare(kvs.get(0), c) == 0, "leftKV1");
072    assertTrue(CellComparatorImpl.COMPARATOR.compare(kvs.get(1), c) == 0, "leftKV2");
073    assertFalse(filter.filterAllRemaining(), "allRemainingWhenMatch");
074
075    // A 'mismatch' situation
076    filter.reset();
077    // INCLUDE expected because test column has not yet passed
078    c = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1);
079    assertTrue(filter.filterCell(c) == Filter.ReturnCode.INCLUDE, "otherColumn");
080    // Test column will pass (wont match), expect NEXT_ROW
081    c = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER, VAL_2);
082    assertTrue(filter.filterCell(c) == Filter.ReturnCode.NEXT_ROW, "testedMismatch");
083    // After a mismatch (at least with LatestVersionOnly), subsequent columns are EXCLUDE
084    c = new KeyValue(ROW, COLUMN_FAMILY, COLUMN_QUALIFIER_2, VAL_1);
085    assertTrue(filter.filterCell(c) == Filter.ReturnCode.NEXT_ROW, "otherColumn");
086  }
087
088}