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.mapreduce;
019
020import static org.mockito.Mockito.*;
021
022import java.util.ArrayList;
023import java.util.List;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.Cell;
026import org.apache.hadoop.hbase.KeyValue;
027import org.apache.hadoop.hbase.client.Result;
028import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
029import org.apache.hadoop.hbase.testclassification.MapReduceTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.apache.hadoop.hbase.util.Bytes;
032import org.apache.hadoop.mapreduce.Mapper;
033import org.junit.jupiter.api.Tag;
034import org.junit.jupiter.api.Test;
035
036@Tag(MapReduceTests.TAG)
037@Tag(SmallTests.TAG)
038public class TestGroupingTableMapper {
039
040  /**
041   * Test GroupingTableMapper class
042   */
043  @Test
044  public void testGroupingTableMapper() throws Exception {
045
046    GroupingTableMapper mapper = new GroupingTableMapper();
047    Configuration configuration = new Configuration();
048    configuration.set(GroupingTableMapper.GROUP_COLUMNS, "family1:clm family2:clm");
049    mapper.setConf(configuration);
050
051    Result result = mock(Result.class);
052    @SuppressWarnings("unchecked")
053    Mapper<ImmutableBytesWritable, Result, ImmutableBytesWritable, Result>.Context context =
054      mock(Mapper.Context.class);
055    context.write(any(), any());
056    List<Cell> keyValue = new ArrayList<>();
057    byte[] row = {};
058    keyValue.add(
059      new KeyValue(row, Bytes.toBytes("family2"), Bytes.toBytes("clm"), Bytes.toBytes("value1")));
060    keyValue.add(
061      new KeyValue(row, Bytes.toBytes("family1"), Bytes.toBytes("clm"), Bytes.toBytes("value2")));
062    when(result.listCells()).thenReturn(keyValue);
063    mapper.map(null, result, context);
064    // template data
065    byte[][] data = { Bytes.toBytes("value1"), Bytes.toBytes("value2") };
066    ImmutableBytesWritable ibw = mapper.createGroupKey(data);
067    verify(context).write(ibw, result);
068  }
069
070}