View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest.model;
21  
22  import java.io.IOException;
23  import java.io.StringReader;
24  import java.io.StringWriter;
25  import java.util.Iterator;
26  
27  import javax.xml.bind.JAXBContext;
28  import javax.xml.bind.JAXBException;
29  
30  import org.apache.hadoop.hbase.SmallTests;
31  import org.apache.hadoop.hbase.util.Base64;
32  import org.apache.hadoop.hbase.util.Bytes;
33  
34  import junit.framework.TestCase;
35  import org.junit.experimental.categories.Category;
36  
37  @Category(SmallTests.class)
38  public class TestTableInfoModel extends TestModelBase<TableInfoModel> {
39    private static final String TABLE = "testtable";
40    private static final byte[] START_KEY = Bytes.toBytes("abracadbra");
41    private static final byte[] END_KEY = Bytes.toBytes("zzyzx");
42    private static final long ID = 8731042424L;
43    private static final String LOCATION = "testhost:9876";
44  
45    public TestTableInfoModel() throws Exception {
46      super(TableInfoModel.class);
47      AS_XML =
48        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><TableInfo " +
49        "name=\"testtable\"><Region endKey=\"enp5eng=\" id=\"8731042424\" " +
50        "location=\"testhost:9876\" " +
51        "name=\"testtable,abracadbra,8731042424.ad9860f031282c46ed431d7af8f94aca.\" " +
52        "startKey=\"YWJyYWNhZGJyYQ==\"/></TableInfo>";
53  
54      AS_PB =
55        "Cgl0ZXN0dGFibGUSSQofdGVzdHRhYmxlLGFicmFjYWRicmEsODczMTA0MjQyNBIKYWJyYWNhZGJy" +
56        "YRoFenp5engg+MSkwyAqDXRlc3Rob3N0Ojk4NzY=";
57  
58      AS_JSON =
59        "{\"name\":\"testtable\",\"Region\":[{\"endKey\":\"enp5eng=\",\"id\":8731042424," +
60        "\"location\":\"testhost:9876\",\"" +
61        "name\":\"testtable,abracadbra,8731042424.ad9860f031282c46ed431d7af8f94aca.\",\"" +
62        "startKey\":\"YWJyYWNhZGJyYQ==\"}]}";
63    }
64  
65    protected TableInfoModel buildTestModel() {
66      TableInfoModel model = new TableInfoModel();
67      model.setName(TABLE);
68      model.add(new TableRegionModel(TABLE, ID, START_KEY, END_KEY, LOCATION));
69      return model;
70    }
71  
72    protected void checkModel(TableInfoModel model) {
73      assertEquals(model.getName(), TABLE);
74      Iterator<TableRegionModel> regions = model.getRegions().iterator();
75      TableRegionModel region = regions.next();
76      assertTrue(Bytes.equals(region.getStartKey(), START_KEY));
77      assertTrue(Bytes.equals(region.getEndKey(), END_KEY));
78      assertEquals(region.getId(), ID);
79      assertEquals(region.getLocation(), LOCATION);
80      assertFalse(regions.hasNext());
81    }
82  
83    public void testBuildModel() throws Exception {
84      checkModel(buildTestModel());
85    }
86  
87    public void testFromXML() throws Exception {
88      checkModel(fromXML(AS_XML));
89    }
90  
91    public void testFromPB() throws Exception {
92      checkModel(fromPB(AS_PB));
93    }
94  
95  }
96