001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.client;
020
021import static org.junit.Assert.assertEquals;
022
023import java.util.Arrays;
024
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.TableDescriptorUtils.TableDescriptorDelta;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.apache.hadoop.hbase.util.Bytes;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033
034@Category(SmallTests.class)
035public class TestTableDescriptorUtils {
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038      HBaseClassTestRule.forClass(TestTableDescriptorUtils.class);
039
040  @Test
041  public void testDelta() {
042    ColumnFamilyDescriptor cf1 = ColumnFamilyDescriptorBuilder.of("cf1");
043    ColumnFamilyDescriptor cf2 = ColumnFamilyDescriptorBuilder.of("cf2");
044    ColumnFamilyDescriptor cf3 = ColumnFamilyDescriptorBuilder.of("cf3");
045    ColumnFamilyDescriptor cf4 = ColumnFamilyDescriptorBuilder.of("cf4");
046    TableDescriptor td = TableDescriptorBuilder
047        .newBuilder(TableName.valueOf("test"))
048        .setColumnFamilies(Arrays.asList(cf1, cf2, cf3, cf4))
049        .build();
050
051    TableDescriptorDelta selfCompare = TableDescriptorUtils.computeDelta(td, td);
052    assertEquals(0, selfCompare.getColumnsAdded().size());
053    assertEquals(0, selfCompare.getColumnsDeleted().size());
054    assertEquals(0, selfCompare.getColumnsModified().size());
055
056    ColumnFamilyDescriptor modCf2 = ColumnFamilyDescriptorBuilder
057        .newBuilder(cf2).setMaxVersions(5).build();
058    ColumnFamilyDescriptor modCf3 = ColumnFamilyDescriptorBuilder
059        .newBuilder(cf3).setMaxVersions(5).build();
060    ColumnFamilyDescriptor cf5 = ColumnFamilyDescriptorBuilder.of("cf5");
061    ColumnFamilyDescriptor cf6 = ColumnFamilyDescriptorBuilder.of("cf6");
062    ColumnFamilyDescriptor cf7 = ColumnFamilyDescriptorBuilder.of("cf7");
063    TableDescriptor newTd = TableDescriptorBuilder
064        .newBuilder(td)
065        .removeColumnFamily(Bytes.toBytes("cf1"))
066        .modifyColumnFamily(modCf2)
067        .modifyColumnFamily(modCf3)
068        .setColumnFamily(cf5)
069        .setColumnFamily(cf6)
070        .setColumnFamily(cf7)
071        .build();
072
073    TableDescriptorDelta delta = TableDescriptorUtils.computeDelta(td, newTd);
074
075    assertEquals(3, delta.getColumnsAdded().size());
076    assertEquals(1, delta.getColumnsDeleted().size());
077    assertEquals(2, delta.getColumnsModified().size());
078
079    TableDescriptorDelta inverseDelta = TableDescriptorUtils.computeDelta(newTd, td);
080
081    // Equality here relies on implementation detail of the returned Set being a TreeSet
082    assertEquals(delta.getColumnsDeleted(), inverseDelta.getColumnsAdded());
083    assertEquals(delta.getColumnsAdded(), inverseDelta.getColumnsDeleted());
084    assertEquals(delta.getColumnsModified(), inverseDelta.getColumnsModified());
085  }
086}