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.util.Iterator;
23  
24  import org.apache.hadoop.hbase.testclassification.SmallTests;
25  import org.apache.hadoop.hbase.util.Bytes;
26  
27  import org.junit.experimental.categories.Category;
28  
29  @Category(SmallTests.class)
30  public class TestTableInfoModel extends TestModelBase<TableInfoModel> {
31    private static final String TABLE = "testtable";
32    private static final byte[] START_KEY = Bytes.toBytes("abracadbra");
33    private static final byte[] END_KEY = Bytes.toBytes("zzyzx");
34    private static final long ID = 8731042424L;
35    private static final String LOCATION = "testhost:9876";
36  
37    public TestTableInfoModel() throws Exception {
38      super(TableInfoModel.class);
39      AS_XML =
40        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><TableInfo " +
41        "name=\"testtable\"><Region endKey=\"enp5eng=\" id=\"8731042424\" " +
42        "location=\"testhost:9876\" " +
43        "name=\"testtable,abracadbra,8731042424.ad9860f031282c46ed431d7af8f94aca.\" " +
44        "startKey=\"YWJyYWNhZGJyYQ==\"/></TableInfo>";
45  
46      AS_PB =
47        "Cgl0ZXN0dGFibGUSSQofdGVzdHRhYmxlLGFicmFjYWRicmEsODczMTA0MjQyNBIKYWJyYWNhZGJy" +
48        "YRoFenp5engg+MSkwyAqDXRlc3Rob3N0Ojk4NzY=";
49  
50      AS_JSON =
51        "{\"name\":\"testtable\",\"Region\":[{\"endKey\":\"enp5eng=\",\"id\":8731042424," +
52        "\"location\":\"testhost:9876\",\"" +
53        "name\":\"testtable,abracadbra,8731042424.ad9860f031282c46ed431d7af8f94aca.\",\"" +
54        "startKey\":\"YWJyYWNhZGJyYQ==\"}]}";
55    }
56  
57    protected TableInfoModel buildTestModel() {
58      TableInfoModel model = new TableInfoModel();
59      model.setName(TABLE);
60      model.add(new TableRegionModel(TABLE, ID, START_KEY, END_KEY, LOCATION));
61      return model;
62    }
63  
64    protected void checkModel(TableInfoModel model) {
65      assertEquals(model.getName(), TABLE);
66      Iterator<TableRegionModel> regions = model.getRegions().iterator();
67      TableRegionModel region = regions.next();
68      assertTrue(Bytes.equals(region.getStartKey(), START_KEY));
69      assertTrue(Bytes.equals(region.getEndKey(), END_KEY));
70      assertEquals(region.getId(), ID);
71      assertEquals(region.getLocation(), LOCATION);
72      assertFalse(regions.hasNext());
73    }
74  
75    public void testBuildModel() throws Exception {
76      checkModel(buildTestModel());
77    }
78  
79    public void testFromXML() throws Exception {
80      checkModel(fromXML(AS_XML));
81    }
82  
83    public void testFromPB() throws Exception {
84      checkModel(fromPB(AS_PB));
85    }
86  
87  }
88