View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest.model;
21  
22  import java.io.IOException;
23  import java.io.StringReader;
24  import java.io.StringWriter;
25  import java.util.Iterator;
26  
27  import javax.xml.bind.JAXBContext;
28  import javax.xml.bind.JAXBException;
29  
30  import org.apache.hadoop.hbase.SmallTests;
31  import org.apache.hadoop.hbase.util.Base64;
32  
33  import org.apache.hadoop.hbase.rest.model.TableSchemaModel;
34  
35  import junit.framework.TestCase;
36  import org.junit.experimental.categories.Category;
37  
38  @Category(SmallTests.class)
39  public class TestTableSchemaModel extends TestModelBase<TableSchemaModel> {
40  
41    public static final String TABLE_NAME = "testTable";
42    private static final boolean IS_META = false;
43    private static final boolean IS_ROOT = false;
44    private static final boolean READONLY = false;
45  
46    TestColumnSchemaModel testColumnSchemaModel;
47  
48    private JAXBContext context;
49  
50    public TestTableSchemaModel() throws Exception {
51      super(TableSchemaModel.class);
52      testColumnSchemaModel = new TestColumnSchemaModel();
53  
54      AS_XML =
55        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
56        "<TableSchema name=\"testTable\" IS_META=\"false\" IS_ROOT=\"false\" READONLY=\"false\">" +
57        "<ColumnSchema name=\"testcolumn\" BLOCKSIZE=\"16384\" BLOOMFILTER=\"NONE\" " +
58        "BLOCKCACHE=\"true\" COMPRESSION=\"GZ\" VERSIONS=\"1\" TTL=\"86400\" IN_MEMORY=\"false\"/>" +
59        "</TableSchema>";
60  
61      AS_PB =
62        "Cgl0ZXN0VGFibGUSEAoHSVNfTUVUQRIFZmFsc2USEAoHSVNfUk9PVBIFZmFsc2USEQoIUkVBRE9O" +
63        "TFkSBWZhbHNlGpcBCgp0ZXN0Y29sdW1uEhIKCUJMT0NLU0laRRIFMTYzODQSEwoLQkxPT01GSUxU" +
64        "RVISBE5PTkUSEgoKQkxPQ0tDQUNIRRIEdHJ1ZRIRCgtDT01QUkVTU0lPThICR1oSDQoIVkVSU0lP" +
65        "TlMSATESDAoDVFRMEgU4NjQwMBISCglJTl9NRU1PUlkSBWZhbHNlGICjBSABKgJHWigA";
66  
67      AS_JSON =
68        "{\"name\":\"testTable\",\"IS_META\":\"false\",\"IS_ROOT\":\"false\"," +
69        "\"READONLY\":\"false\",\"ColumnSchema\":[{\"name\":\"testcolumn\"," +
70        "\"BLOCKSIZE\":\"16384\",\"BLOOMFILTER\":\"NONE\",\"BLOCKCACHE\":\"true\"," +
71        "\"COMPRESSION\":\"GZ\",\"VERSIONS\":\"1\",\"TTL\":\"86400\",\"IN_MEMORY\":\"false\"}]}";
72    }
73  
74    protected TableSchemaModel buildTestModel() {
75      return buildTestModel(TABLE_NAME);
76    }
77  
78    public TableSchemaModel buildTestModel(String name) {
79      TableSchemaModel model = new TableSchemaModel();
80      model.setName(name);
81      model.__setIsMeta(IS_META);
82      model.__setIsRoot(IS_ROOT);
83      model.__setReadOnly(READONLY);
84      model.addColumnFamily(testColumnSchemaModel.buildTestModel());
85      return model;
86    }
87  
88    protected void checkModel(TableSchemaModel model) {
89      checkModel(model, TABLE_NAME);
90    }
91  
92    public void checkModel(TableSchemaModel model, String tableName) {
93      assertEquals(model.getName(), tableName);
94      assertEquals(model.__getIsMeta(), IS_META);
95      assertEquals(model.__getIsRoot(), IS_ROOT);
96      assertEquals(model.__getReadOnly(), READONLY);
97      Iterator<ColumnSchemaModel> families = model.getColumns().iterator();
98      assertTrue(families.hasNext());
99      ColumnSchemaModel family = families.next();
100     testColumnSchemaModel.checkModel(family);
101     assertFalse(families.hasNext());
102   }
103 
104   public void testBuildModel() throws Exception {
105     checkModel(buildTestModel());
106   }
107 
108   public void testFromXML() throws Exception {
109     checkModel(fromXML(AS_XML));
110   }
111 
112   public void testFromPB() throws Exception {
113     checkModel(fromPB(AS_PB));
114   }
115 
116 }
117