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.util.Base64;
32  
33  import junit.framework.TestCase;
34  
35  public class TestTableSchemaModel extends TestCase {
36  
37    public static final String TABLE_NAME = "testTable";
38    private static final boolean IS_META = false;
39    private static final boolean IS_ROOT = false;
40    private static final boolean READONLY = false;
41  
42    private static final String AS_XML =
43      "<TableSchema name=\"testTable\"" +
44        " IS_META=\"false\"" +
45        " IS_ROOT=\"false\"" +
46        " READONLY=\"false\">" +
47        TestColumnSchemaModel.AS_XML + 
48      "</TableSchema>";
49  
50    private static final String AS_PB = 
51      "Cgl0ZXN0VGFibGUSEAoHSVNfTUVUQRIFZmFsc2USEAoHSVNfUk9PVBIFZmFsc2USEQoIUkVBRE9O" +
52      "TFkSBWZhbHNlGpcBCgp0ZXN0Y29sdW1uEhIKCUJMT0NLU0laRRIFMTYzODQSEwoLQkxPT01GSUxU" +
53      "RVISBE5PTkUSEgoKQkxPQ0tDQUNIRRIEdHJ1ZRIRCgtDT01QUkVTU0lPThICR1oSDQoIVkVSU0lP" +
54      "TlMSATESDAoDVFRMEgU4NjQwMBISCglJTl9NRU1PUlkSBWZhbHNlGICjBSABKgJHWigA";
55  
56    private JAXBContext context;
57  
58    public TestTableSchemaModel() throws JAXBException {
59      super();
60      context = JAXBContext.newInstance(
61        ColumnSchemaModel.class,
62        TableSchemaModel.class);
63    }
64  
65    public static TableSchemaModel buildTestModel() {
66      return buildTestModel(TABLE_NAME);
67    }
68  
69    public static TableSchemaModel buildTestModel(String name) {
70      TableSchemaModel model = new TableSchemaModel();
71      model.setName(name);
72      model.__setIsMeta(IS_META);
73      model.__setIsRoot(IS_ROOT);
74      model.__setReadOnly(READONLY);
75      model.addColumnFamily(TestColumnSchemaModel.buildTestModel());
76      return model;
77    }
78  
79    @SuppressWarnings("unused")
80    private String toXML(TableSchemaModel model) throws JAXBException {
81      StringWriter writer = new StringWriter();
82      context.createMarshaller().marshal(model, writer);
83      return writer.toString();
84    }
85  
86    private TableSchemaModel fromXML(String xml) throws JAXBException {
87      return (TableSchemaModel)
88        context.createUnmarshaller().unmarshal(new StringReader(xml));
89    }
90  
91    @SuppressWarnings("unused")
92    private byte[] toPB(TableSchemaModel model) {
93      return model.createProtobufOutput();
94    }
95  
96    private TableSchemaModel fromPB(String pb) throws IOException {
97      return (TableSchemaModel) 
98        new TableSchemaModel().getObjectFromMessage(Base64.decode(AS_PB));
99    }
100 
101   public static void checkModel(TableSchemaModel model) {
102     checkModel(model, TABLE_NAME);
103   }
104 
105   public static void checkModel(TableSchemaModel model, String tableName) {
106     assertEquals(model.getName(), tableName);
107     assertEquals(model.__getIsMeta(), IS_META);
108     assertEquals(model.__getIsRoot(), IS_ROOT);
109     assertEquals(model.__getReadOnly(), READONLY);
110     Iterator<ColumnSchemaModel> families = model.getColumns().iterator();
111     assertTrue(families.hasNext());
112     ColumnSchemaModel family = families.next();
113     TestColumnSchemaModel.checkModel(family);
114     assertFalse(families.hasNext());
115   }
116 
117   public void testBuildModel() throws Exception {
118     checkModel(buildTestModel());
119   }
120 
121   public void testFromXML() throws Exception {
122     checkModel(fromXML(AS_XML));
123   }
124 
125   public void testFromPB() throws Exception {
126     checkModel(fromPB(AS_PB));
127   }
128 }