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 = "{\"endKey\":\"enp5eng=\",\"id\":8731042424,\"location\":\"testhost:9876\","
056      + "\"name\":\"testtable,abracadbra,8731042424.ad9860f031282c46ed431d7af8f94aca.\",\""
057      + "startKey\":\"YWJyYWNhZGJyYQ==\"}";
058  }
059
060  @Override
061  protected TableRegionModel buildTestModel() {
062    TableRegionModel model = new TableRegionModel(TABLE, ID, START_KEY, END_KEY, LOCATION);
063    return model;
064  }
065
066  @Override
067  protected void checkModel(TableRegionModel model) {
068    assertTrue(Bytes.equals(model.getStartKey(), START_KEY));
069    assertTrue(Bytes.equals(model.getEndKey(), END_KEY));
070    assertEquals(ID, model.getId());
071    assertEquals(LOCATION, model.getLocation());
072    assertEquals(model.getName(), TABLE + "," + Bytes.toString(START_KEY) + "," + Long.toString(ID)
073      + ".ad9860f031282c46ed431d7af8f94aca.");
074  }
075
076  @Test
077  public void testGetName() {
078    TableRegionModel model = buildTestModel();
079    String modelName = model.getName();
080    HRegionInfo hri = new HRegionInfo(TableName.valueOf(TABLE), START_KEY, END_KEY, false, ID);
081    assertEquals(modelName, hri.getRegionNameAsString());
082  }
083
084  @Test
085  public void testSetName() {
086    TableRegionModel model = buildTestModel();
087    String name = model.getName();
088    model.setName(name);
089    assertEquals(name, model.getName());
090  }
091
092  @Override
093  public void testFromPB() throws Exception {
094    // no pb ignore
095  }
096}