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  import org.apache.hadoop.hbase.util.Bytes;
34  
35  import junit.framework.TestCase;
36  import org.junit.experimental.categories.Category;
37  
38  @Category(SmallTests.class)
39  public class TestTableInfoModel extends TestCase {
40    private static final String TABLE = "testtable";
41    private static final byte[] START_KEY = Bytes.toBytes("abracadbra");
42    private static final byte[] END_KEY = Bytes.toBytes("zzyzx");
43    private static final long ID = 8731042424L;
44    private static final String LOCATION = "testhost:9876";
45    
46    private static final String AS_XML =
47      "<TableInfo name=\"testtable\">" +
48        "<Region location=\"testhost:9876\"" +
49          " endKey=\"enp5eng=\"" +
50          " startKey=\"YWJyYWNhZGJyYQ==\"" +
51          " id=\"8731042424\"" +
52          " name=\"testtable,abracadbra,8731042424\"/>" +
53      "</TableInfo>";
54  
55    private static final String AS_PB = 
56      "Cgl0ZXN0dGFibGUSSQofdGVzdHRhYmxlLGFicmFjYWRicmEsODczMTA0MjQyNBIKYWJyYWNhZGJy" +
57      "YRoFenp5engg+MSkwyAqDXRlc3Rob3N0Ojk4NzY=";
58  
59    private JAXBContext context;
60  
61    public TestTableInfoModel() throws JAXBException {
62      super();
63      context = JAXBContext.newInstance(
64          TableInfoModel.class,
65          TableRegionModel.class);
66    }
67  
68    private TableInfoModel buildTestModel() {
69      TableInfoModel model = new TableInfoModel();
70      model.setName(TABLE);
71      model.add(new TableRegionModel(TABLE, ID, START_KEY, END_KEY, LOCATION));
72      return model;
73    }
74  
75    @SuppressWarnings("unused")
76    private String toXML(TableInfoModel model) throws JAXBException {
77      StringWriter writer = new StringWriter();
78      context.createMarshaller().marshal(model, writer);
79      return writer.toString();
80    }
81  
82    private TableInfoModel fromXML(String xml) throws JAXBException {
83      return (TableInfoModel)
84        context.createUnmarshaller().unmarshal(new StringReader(xml));
85    }
86  
87    @SuppressWarnings("unused")
88    private byte[] toPB(TableInfoModel model) {
89      return model.createProtobufOutput();
90    }
91  
92    private TableInfoModel fromPB(String pb) throws IOException {
93      return (TableInfoModel) 
94        new TableInfoModel().getObjectFromMessage(Base64.decode(AS_PB));
95    }
96  
97    private void checkModel(TableInfoModel model) {
98      assertEquals(model.getName(), TABLE);
99      Iterator<TableRegionModel> regions = model.getRegions().iterator();
100     TableRegionModel region = regions.next();
101     assertTrue(Bytes.equals(region.getStartKey(), START_KEY));
102     assertTrue(Bytes.equals(region.getEndKey(), END_KEY));
103     assertEquals(region.getId(), ID);
104     assertEquals(region.getLocation(), LOCATION);
105     assertFalse(regions.hasNext());
106   }
107 
108   public void testBuildModel() throws Exception {
109     checkModel(buildTestModel());
110   }
111 
112   public void testFromXML() throws Exception {
113     checkModel(fromXML(AS_XML));
114   }
115 
116   public void testFromPB() throws Exception {
117     checkModel(fromPB(AS_PB));
118   }
119 
120   @org.junit.Rule
121   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
122     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
123 }
124