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