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.StringReader;
24  import java.io.StringWriter;
25  
26  import javax.xml.bind.JAXBContext;
27  import javax.xml.bind.JAXBException;
28  
29  import org.apache.hadoop.hbase.HRegionInfo;
30  import org.apache.hadoop.hbase.HTableDescriptor;
31  import org.apache.hadoop.hbase.util.Bytes;
32  
33  import junit.framework.TestCase;
34  
35  public class TestTableRegionModel extends TestCase {
36    private static final String TABLE = "testtable";
37    private static final byte[] START_KEY = Bytes.toBytes("abracadbra");
38    private static final byte[] END_KEY = Bytes.toBytes("zzyzx");
39    private static final long ID = 8731042424L;
40    private static final String LOCATION = "testhost:9876";
41  
42    private static final String AS_XML =
43      "<Region location=\"testhost:9876\"" +
44        " endKey=\"enp5eng=\"" +
45        " startKey=\"YWJyYWNhZGJyYQ==\"" +
46        " id=\"8731042424\"" +
47        " name=\"testtable,abracadbra,8731042424\"/>";
48  
49    private JAXBContext context;
50  
51    public TestTableRegionModel() throws JAXBException {
52      super();
53      context = JAXBContext.newInstance(TableRegionModel.class);
54    }
55  
56    private TableRegionModel buildTestModel() {
57      TableRegionModel model =
58        new TableRegionModel(TABLE, ID, START_KEY, END_KEY, LOCATION);
59      return model;
60    }
61  
62    @SuppressWarnings("unused")
63    private String toXML(TableRegionModel model) throws JAXBException {
64      StringWriter writer = new StringWriter();
65      context.createMarshaller().marshal(model, writer);
66      return writer.toString();
67    }
68  
69    private TableRegionModel fromXML(String xml) throws JAXBException {
70      return (TableRegionModel)
71        context.createUnmarshaller().unmarshal(new StringReader(xml));
72    }
73  
74    private void checkModel(TableRegionModel model) {
75      assertTrue(Bytes.equals(model.getStartKey(), START_KEY));
76      assertTrue(Bytes.equals(model.getEndKey(), END_KEY));
77      assertEquals(model.getId(), ID);
78      assertEquals(model.getLocation(), LOCATION);
79      assertEquals(model.getName(), 
80        TABLE + "," + Bytes.toString(START_KEY) + "," + Long.toString(ID) +
81        ".ad9860f031282c46ed431d7af8f94aca.");
82    }
83  
84    public void testBuildModel() throws Exception {
85      checkModel(buildTestModel());
86    }
87  
88    public void testGetName() {
89      TableRegionModel model = buildTestModel();
90      String modelName = model.getName();
91      HRegionInfo hri = new HRegionInfo(new HTableDescriptor(TABLE),
92        START_KEY, END_KEY, false, ID);
93      assertEquals(modelName, hri.getRegionNameAsString());
94    }
95  
96    public void testSetName() {
97      TableRegionModel model = buildTestModel();
98      String name = model.getName();
99      model.setName(name);
100     assertEquals(name, model.getName());
101   }
102 
103   public void testFromXML() throws Exception {
104     checkModel(fromXML(AS_XML));
105   }
106 }