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 TestTableListModel extends TestCase {
39    private static final String TABLE1 = "table1";
40    private static final String TABLE2 = "table2";
41    private static final String TABLE3 = "table3";
42    
43    private static final String AS_XML =
44      "<TableList><table name=\"table1\"/><table name=\"table2\"/>" +
45        "<table name=\"table3\"/></TableList>";
46  
47    private static final String AS_PB = "CgZ0YWJsZTEKBnRhYmxlMgoGdGFibGUz";
48  
49    private JAXBContext context;
50  
51    public TestTableListModel() throws JAXBException {
52      super();
53      context = JAXBContext.newInstance(
54          TableListModel.class,
55          TableModel.class);
56    }
57  
58    private TableListModel buildTestModel() {
59      TableListModel model = new TableListModel();
60      model.add(new TableModel(TABLE1));
61      model.add(new TableModel(TABLE2));
62      model.add(new TableModel(TABLE3));
63      return model;
64    }
65  
66    @SuppressWarnings("unused")
67    private String toXML(TableListModel model) throws JAXBException {
68      StringWriter writer = new StringWriter();
69      context.createMarshaller().marshal(model, writer);
70      return writer.toString();
71    }
72  
73    private TableListModel fromXML(String xml) throws JAXBException {
74      return (TableListModel)
75        context.createUnmarshaller().unmarshal(new StringReader(xml));
76    }
77  
78    @SuppressWarnings("unused")
79    private byte[] toPB(TableListModel model) {
80      return model.createProtobufOutput();
81    }
82  
83    private TableListModel fromPB(String pb) throws IOException {
84      return (TableListModel) 
85        new TableListModel().getObjectFromMessage(Base64.decode(AS_PB));
86    }
87  
88    private void checkModel(TableListModel model) {
89      Iterator<TableModel> tables = model.getTables().iterator();
90      TableModel table = tables.next();
91      assertEquals(table.getName(), TABLE1);
92      table = tables.next();
93      assertEquals(table.getName(), TABLE2);
94      table = tables.next();
95      assertEquals(table.getName(), TABLE3);
96      assertFalse(tables.hasNext());
97    }
98  
99    public void testBuildModel() throws Exception {
100     checkModel(buildTestModel());
101   }
102 
103   public void testFromXML() throws Exception {
104     checkModel(fromXML(AS_XML));
105   }
106 
107   public void testFromPB() throws Exception {
108     checkModel(fromPB(AS_PB));
109   }
110 
111   @org.junit.Rule
112   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
113     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
114 }
115