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