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