001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.rest.model;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HRegionInfo;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.testclassification.RestTests;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.apache.hadoop.hbase.util.Bytes;
029import org.junit.ClassRule;
030import org.junit.Test;
031import org.junit.experimental.categories.Category;
032
033@Category({RestTests.class, SmallTests.class})
034public class TestTableRegionModel extends TestModelBase<TableRegionModel> {
035
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038      HBaseClassTestRule.forClass(TestTableRegionModel.class);
039
040  private static final String TABLE = "testtable";
041  private static final byte[] START_KEY = Bytes.toBytes("abracadbra");
042  private static final byte[] END_KEY = Bytes.toBytes("zzyzx");
043  private static final long ID = 8731042424L;
044  private static final String LOCATION = "testhost:9876";
045
046  public TestTableRegionModel() throws Exception {
047    super(TableRegionModel.class);
048
049    AS_XML =
050      "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Region endKey=\"enp5eng=\" " +
051          "id=\"8731042424\" location=\"testhost:9876\" " +
052          "name=\"testtable,abracadbra,8731042424.ad9860f031282c46ed431d7af8f94aca.\" " +
053          "startKey=\"YWJyYWNhZGJyYQ==\"/>";
054
055    AS_JSON =
056      "{\"endKey\":\"enp5eng=\",\"id\":8731042424,\"location\":\"testhost:9876\"," +
057          "\"name\":\"testtable,abracadbra,8731042424.ad9860f031282c46ed431d7af8f94aca.\",\"" +
058          "startKey\":\"YWJyYWNhZGJyYQ==\"}";
059  }
060
061  @Override
062  protected TableRegionModel buildTestModel() {
063    TableRegionModel model =
064      new TableRegionModel(TABLE, ID, START_KEY, END_KEY, LOCATION);
065    return model;
066  }
067
068  @Override
069  protected void checkModel(TableRegionModel model) {
070    assertTrue(Bytes.equals(model.getStartKey(), START_KEY));
071    assertTrue(Bytes.equals(model.getEndKey(), END_KEY));
072    assertEquals(ID, model.getId());
073    assertEquals(LOCATION, model.getLocation());
074    assertEquals(model.getName(),
075      TABLE + "," + Bytes.toString(START_KEY) + "," + Long.toString(ID) +
076      ".ad9860f031282c46ed431d7af8f94aca.");
077  }
078
079  @Test
080  public void testGetName() {
081    TableRegionModel model = buildTestModel();
082    String modelName = model.getName();
083    HRegionInfo hri = new HRegionInfo(TableName.valueOf(TABLE),
084      START_KEY, END_KEY, false, ID);
085    assertEquals(modelName, hri.getRegionNameAsString());
086  }
087
088  @Test
089  public void testSetName() {
090    TableRegionModel model = buildTestModel();
091    String name = model.getName();
092    model.setName(name);
093    assertEquals(name, model.getName());
094  }
095
096  @Override
097  public void testFromPB() throws Exception {
098    //no pb ignore
099  }
100}
101