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.mob;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.testclassification.SmallTests;
026import org.apache.hadoop.hbase.util.Bytes;
027import org.junit.ClassRule;
028import org.junit.Test;
029import org.junit.experimental.categories.Category;
030import org.junit.rules.TestName;
031
032import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableSet;
033import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableSetMultimap;
034
035@Category(SmallTests.class)
036public class TestMobUtils {
037
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040    HBaseClassTestRule.forClass(TestMobUtils.class);
041  public static final TableName TEST_TABLE_1 = TableName.valueOf("testTable1");
042  public static final TableName TEST_TABLE_2 = TableName.valueOf("testTable2");
043  public static final TableName TEST_TABLE_3 = TableName.valueOf("testTable3");
044
045  @Test
046  public void serializeSingleMobFileRefs() {
047    ImmutableSetMultimap<TableName, String> mobRefSet =
048      ImmutableSetMultimap.<TableName, String> builder().putAll(TEST_TABLE_1, "file1a").build();
049    byte[] result = MobUtils.serializeMobFileRefs(mobRefSet);
050    assertEquals("testTable1/file1a", Bytes.toString(result));
051  }
052
053  @Test
054  public void serializeMultipleMobFileRefs() {
055    ImmutableSetMultimap<TableName, String> mobRefSet =
056      ImmutableSetMultimap.<TableName, String> builder().putAll(TEST_TABLE_1, "file1a", "file1b")
057        .putAll(TEST_TABLE_2, "file2a").putAll(TEST_TABLE_3, "file3a", "file3b").build();
058    byte[] result = MobUtils.serializeMobFileRefs(mobRefSet);
059    assertEquals("testTable1/file1a,file1b//testTable2/file2a//testTable3/file3a,file3b",
060      Bytes.toString(result));
061  }
062
063  @Test
064  public void deserializeSingleMobFileRefs() {
065    ImmutableSetMultimap<TableName, String> mobRefSet =
066      MobUtils.deserializeMobFileRefs(Bytes.toBytes("testTable1/file1a")).build();
067    assertEquals(1, mobRefSet.size());
068    ImmutableSet<String> testTable1Refs = mobRefSet.get(TEST_TABLE_1);
069    assertEquals(1, testTable1Refs.size());
070    assertTrue(testTable1Refs.contains("file1a"));
071  }
072
073  @Test
074  public void deserializeMultipleMobFileRefs() {
075    ImmutableSetMultimap<TableName,
076      String> mobRefSet = MobUtils
077        .deserializeMobFileRefs(
078          Bytes.toBytes("testTable1/file1a,file1b//testTable2/file2a//testTable3/file3a,file3b"))
079        .build();
080    assertEquals(5, mobRefSet.size());
081    ImmutableSet<String> testTable1Refs = mobRefSet.get(TEST_TABLE_1);
082    ImmutableSet<String> testTable2Refs = mobRefSet.get(TEST_TABLE_2);
083    ImmutableSet<String> testTable3Refs = mobRefSet.get(TEST_TABLE_3);
084    assertEquals(2, testTable1Refs.size());
085    assertEquals(1, testTable2Refs.size());
086    assertEquals(2, testTable3Refs.size());
087    assertTrue(testTable1Refs.contains("file1a"));
088    assertTrue(testTable1Refs.contains("file1b"));
089    assertTrue(testTable2Refs.contains("file2a"));
090    assertTrue(testTable3Refs.contains("file3a"));
091    assertTrue(testTable3Refs.contains("file3b"));
092  }
093
094  public static String getTableName(TestName test) {
095    return test.getMethodName().replace("[", "-").replace("]", "");
096  }
097}