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.TableName;
025import org.apache.hadoop.hbase.client.RegionInfo;
026import org.apache.hadoop.hbase.client.RegionInfoBuilder;
027import org.apache.hadoop.hbase.testclassification.RestTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.apache.hadoop.hbase.util.Bytes;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033
034@Category({ RestTests.class, SmallTests.class })
035public class TestTableRegionModel extends TestModelBase<TableRegionModel> {
036
037  @ClassRule
038  public static final HBaseClassTestRule CLASS_RULE =
039    HBaseClassTestRule.forClass(TestTableRegionModel.class);
040
041  private static final String TABLE = "testtable";
042  private static final byte[] START_KEY = Bytes.toBytes("abracadbra");
043  private static final byte[] END_KEY = Bytes.toBytes("zzyzx");
044  private static final long ID = 8731042424L;
045  private static final String LOCATION = "testhost:9876";
046
047  public TestTableRegionModel() throws Exception {
048    super(TableRegionModel.class);
049
050    AS_XML =
051      "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Region endKey=\"enp5eng=\" "
052        + "id=\"8731042424\" location=\"testhost:9876\" "
053        + "name=\"testtable,abracadbra,8731042424.ad9860f031282c46ed431d7af8f94aca.\" "
054        + "startKey=\"YWJyYWNhZGJyYQ==\"/>";
055
056    AS_JSON = "{\"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 = new TableRegionModel(TABLE, ID, START_KEY, END_KEY, LOCATION);
064    return model;
065  }
066
067  @Override
068  protected void checkModel(TableRegionModel model) {
069    assertTrue(Bytes.equals(model.getStartKey(), START_KEY));
070    assertTrue(Bytes.equals(model.getEndKey(), END_KEY));
071    assertEquals(ID, model.getId());
072    assertEquals(LOCATION, model.getLocation());
073    assertEquals(model.getName(), TABLE + "," + Bytes.toString(START_KEY) + "," + Long.toString(ID)
074      + ".ad9860f031282c46ed431d7af8f94aca.");
075  }
076
077  @Test
078  public void testGetName() {
079    TableRegionModel model = buildTestModel();
080    String modelName = model.getName();
081    RegionInfo hri = RegionInfoBuilder.newBuilder(TableName.valueOf(TABLE)).setStartKey(START_KEY)
082      .setEndKey(END_KEY).setRegionId(ID).build();
083    assertEquals(modelName, hri.getRegionNameAsString());
084  }
085
086  @Test
087  public void testSetName() {
088    TableRegionModel model = buildTestModel();
089    String name = model.getName();
090    model.setName(name);
091    assertEquals(name, model.getName());
092  }
093
094  @Override
095  public void testFromPB() throws Exception {
096    // no pb ignore
097  }
098}