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.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.util.Iterator;
025import org.apache.hadoop.hbase.testclassification.RestTests;
026import org.apache.hadoop.hbase.testclassification.SmallTests;
027import org.apache.hadoop.hbase.util.Bytes;
028import org.junit.jupiter.api.Tag;
029import org.junit.jupiter.api.Test;
030
031@Tag(RestTests.TAG)
032@Tag(SmallTests.TAG)
033public class TestTableInfoModel extends TestModelBase<TableInfoModel> {
034
035  private static final String TABLE = "testtable";
036  private static final byte[] START_KEY = Bytes.toBytes("abracadbra");
037  private static final byte[] END_KEY = Bytes.toBytes("zzyzx");
038  private static final long ID = 8731042424L;
039  private static final String LOCATION = "testhost:9876";
040
041  public TestTableInfoModel() throws Exception {
042    super(TableInfoModel.class);
043    AS_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><TableInfo "
044      + "name=\"testtable\"><Region endKey=\"enp5eng=\" id=\"8731042424\" "
045      + "location=\"testhost:9876\" "
046      + "name=\"testtable,abracadbra,8731042424.ad9860f031282c46ed431d7af8f94aca.\" "
047      + "startKey=\"YWJyYWNhZGJyYQ==\"/></TableInfo>";
048
049    AS_PB = "Cgl0ZXN0dGFibGUSSQofdGVzdHRhYmxlLGFicmFjYWRicmEsODczMTA0MjQyNBIKYWJyYWNhZGJy"
050      + "YRoFenp5engg+MSkwyAqDXRlc3Rob3N0Ojk4NzY=";
051
052    AS_JSON = "{\"name\":\"testtable\",\"Region\":[{\"endKey\":\"enp5eng=\",\"id\":8731042424,"
053      + "\"location\":\"testhost:9876\",\""
054      + "name\":\"testtable,abracadbra,8731042424.ad9860f031282c46ed431d7af8f94aca.\",\""
055      + "startKey\":\"YWJyYWNhZGJyYQ==\"}]}";
056  }
057
058  @Override
059  protected TableInfoModel buildTestModel() {
060    TableInfoModel model = new TableInfoModel();
061    model.setName(TABLE);
062    model.add(new TableRegionModel(TABLE, ID, START_KEY, END_KEY, LOCATION));
063    return model;
064  }
065
066  @Override
067  protected void checkModel(TableInfoModel model) {
068    assertEquals(TABLE, model.getName());
069    Iterator<TableRegionModel> regions = model.getRegions().iterator();
070    TableRegionModel region = regions.next();
071    assertTrue(Bytes.equals(region.getStartKey(), START_KEY));
072    assertTrue(Bytes.equals(region.getEndKey(), END_KEY));
073    assertEquals(ID, region.getId());
074    assertEquals(LOCATION, region.getLocation());
075    assertFalse(regions.hasNext());
076  }
077
078  @Override
079  @Test
080  public void testBuildModel() throws Exception {
081    checkModel(buildTestModel());
082  }
083
084  @Override
085  @Test
086  public void testFromXML() throws Exception {
087    checkModel(fromXML(AS_XML));
088  }
089
090  @Override
091  @Test
092  public void testFromPB() throws Exception {
093    checkModel(fromPB(AS_PB));
094  }
095}