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