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.client;
019
020import static org.junit.Assert.assertEquals;
021
022import java.util.Arrays;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.TableDescriptorUtils.TableDescriptorDelta;
026import org.apache.hadoop.hbase.testclassification.SmallTests;
027import org.apache.hadoop.hbase.util.Bytes;
028import org.junit.ClassRule;
029import org.junit.Test;
030import org.junit.experimental.categories.Category;
031
032@Category(SmallTests.class)
033public class TestTableDescriptorUtils {
034  @ClassRule
035  public static final HBaseClassTestRule CLASS_RULE =
036    HBaseClassTestRule.forClass(TestTableDescriptorUtils.class);
037
038  @Test
039  public void testDelta() {
040    ColumnFamilyDescriptor cf1 = ColumnFamilyDescriptorBuilder.of("cf1");
041    ColumnFamilyDescriptor cf2 = ColumnFamilyDescriptorBuilder.of("cf2");
042    ColumnFamilyDescriptor cf3 = ColumnFamilyDescriptorBuilder.of("cf3");
043    ColumnFamilyDescriptor cf4 = ColumnFamilyDescriptorBuilder.of("cf4");
044    TableDescriptor td = TableDescriptorBuilder.newBuilder(TableName.valueOf("test"))
045      .setColumnFamilies(Arrays.asList(cf1, cf2, cf3, cf4)).build();
046
047    TableDescriptorDelta selfCompare = TableDescriptorUtils.computeDelta(td, td);
048    assertEquals(0, selfCompare.getColumnsAdded().size());
049    assertEquals(0, selfCompare.getColumnsDeleted().size());
050    assertEquals(0, selfCompare.getColumnsModified().size());
051
052    ColumnFamilyDescriptor modCf2 =
053      ColumnFamilyDescriptorBuilder.newBuilder(cf2).setMaxVersions(5).build();
054    ColumnFamilyDescriptor modCf3 =
055      ColumnFamilyDescriptorBuilder.newBuilder(cf3).setMaxVersions(5).build();
056    ColumnFamilyDescriptor cf5 = ColumnFamilyDescriptorBuilder.of("cf5");
057    ColumnFamilyDescriptor cf6 = ColumnFamilyDescriptorBuilder.of("cf6");
058    ColumnFamilyDescriptor cf7 = ColumnFamilyDescriptorBuilder.of("cf7");
059    TableDescriptor newTd =
060      TableDescriptorBuilder.newBuilder(td).removeColumnFamily(Bytes.toBytes("cf1"))
061        .modifyColumnFamily(modCf2).modifyColumnFamily(modCf3).setColumnFamily(cf5)
062        .setColumnFamily(cf6).setColumnFamily(cf7).build();
063
064    TableDescriptorDelta delta = TableDescriptorUtils.computeDelta(td, newTd);
065
066    assertEquals(3, delta.getColumnsAdded().size());
067    assertEquals(1, delta.getColumnsDeleted().size());
068    assertEquals(2, delta.getColumnsModified().size());
069
070    TableDescriptorDelta inverseDelta = TableDescriptorUtils.computeDelta(newTd, td);
071
072    // Equality here relies on implementation detail of the returned Set being a TreeSet
073    assertEquals(delta.getColumnsDeleted(), inverseDelta.getColumnsAdded());
074    assertEquals(delta.getColumnsAdded(), inverseDelta.getColumnsDeleted());
075    assertEquals(delta.getColumnsModified(), inverseDelta.getColumnsModified());
076  }
077}