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.rest.model;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023
024import java.util.Iterator;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.testclassification.RestTests;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.junit.ClassRule;
029import org.junit.Test;
030import org.junit.experimental.categories.Category;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034@Category({ RestTests.class, SmallTests.class })
035public class TestTableSchemaModel extends TestModelBase<TableSchemaModel> {
036
037  @ClassRule
038  public static final HBaseClassTestRule CLASS_RULE =
039    HBaseClassTestRule.forClass(TestTableSchemaModel.class);
040
041  private static final Logger LOG = LoggerFactory.getLogger(TestTableSchemaModel.class);
042
043  public static final String TABLE_NAME = "testTable";
044  private static final boolean IS_META = false;
045  private static final boolean IS_ROOT = false;
046  private static final boolean READONLY = false;
047
048  TestColumnSchemaModel testColumnSchemaModel;
049
050  public TestTableSchemaModel() throws Exception {
051    super(TableSchemaModel.class);
052    testColumnSchemaModel = new TestColumnSchemaModel();
053
054    AS_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
055      + "<TableSchema name=\"testTable\" IS_META=\"false\" IS_ROOT=\"false\" READONLY=\"false\">"
056      + "<ColumnSchema name=\"testcolumn\" BLOCKSIZE=\"16384\" BLOOMFILTER=\"NONE\" "
057      + "BLOCKCACHE=\"true\" COMPRESSION=\"GZ\" VERSIONS=\"1\" TTL=\"86400\" IN_MEMORY=\"false\"/>"
058      + "</TableSchema>";
059
060    AS_PB = "Cgl0ZXN0VGFibGUSEAoHSVNfTUVUQRIFZmFsc2USEAoHSVNfUk9PVBIFZmFsc2USEQoIUkVBRE9O"
061      + "TFkSBWZhbHNlGpcBCgp0ZXN0Y29sdW1uEhIKCUJMT0NLU0laRRIFMTYzODQSEwoLQkxPT01GSUxU"
062      + "RVISBE5PTkUSEgoKQkxPQ0tDQUNIRRIEdHJ1ZRIRCgtDT01QUkVTU0lPThICR1oSDQoIVkVSU0lP"
063      + "TlMSATESDAoDVFRMEgU4NjQwMBISCglJTl9NRU1PUlkSBWZhbHNlGICjBSABKgJHWigA";
064
065    AS_JSON = "{\"name\":\"testTable\",\"IS_META\":\"false\",\"IS_ROOT\":\"false\","
066      + "\"READONLY\":\"false\",\"ColumnSchema\":[{\"name\":\"testcolumn\","
067      + "\"BLOCKSIZE\":\"16384\",\"BLOOMFILTER\":\"NONE\",\"BLOCKCACHE\":\"true\","
068      + "\"COMPRESSION\":\"GZ\",\"VERSIONS\":\"1\",\"TTL\":\"86400\",\"IN_MEMORY\":\"false\"}]}";
069  }
070
071  @Override
072  protected TableSchemaModel buildTestModel() {
073    return buildTestModel(TABLE_NAME);
074  }
075
076  public TableSchemaModel buildTestModel(String name) {
077    TableSchemaModel model = new TableSchemaModel();
078    model.setName(name);
079    model.__setIsMeta(IS_META);
080    model.__setIsRoot(IS_ROOT);
081    model.__setReadOnly(READONLY);
082    model.addColumnFamily(testColumnSchemaModel.buildTestModel());
083    return model;
084  }
085
086  @Override
087  protected void checkModel(TableSchemaModel model) {
088    checkModel(model, TABLE_NAME);
089  }
090
091  public void checkModel(TableSchemaModel model, String tableName) {
092    assertEquals(model.getName(), tableName);
093    assertEquals(IS_META, model.__getIsMeta());
094    assertEquals(IS_ROOT, model.__getIsRoot());
095    assertEquals(READONLY, model.__getReadOnly());
096    Iterator<ColumnSchemaModel> families = model.getColumns().iterator();
097    assertTrue(families.hasNext());
098    ColumnSchemaModel family = families.next();
099    testColumnSchemaModel.checkModel(family);
100    assertFalse(families.hasNext());
101  }
102
103  @Override
104  @Test
105  public void testBuildModel() throws Exception {
106    checkModel(buildTestModel());
107  }
108
109  @Override
110  @Test
111  public void testFromXML() throws Exception {
112    checkModel(fromXML(AS_XML));
113  }
114
115  @Override
116  @Test
117  public void testFromPB() throws Exception {
118    checkModel(fromPB(AS_PB));
119  }
120
121}