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