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 = 048 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><TableInfo " + 049 "name=\"testtable\"><Region endKey=\"enp5eng=\" id=\"8731042424\" " + 050 "location=\"testhost:9876\" " + 051 "name=\"testtable,abracadbra,8731042424.ad9860f031282c46ed431d7af8f94aca.\" " + 052 "startKey=\"YWJyYWNhZGJyYQ==\"/></TableInfo>"; 053 054 AS_PB = 055 "Cgl0ZXN0dGFibGUSSQofdGVzdHRhYmxlLGFicmFjYWRicmEsODczMTA0MjQyNBIKYWJyYWNhZGJy" + 056 "YRoFenp5engg+MSkwyAqDXRlc3Rob3N0Ojk4NzY="; 057 058 AS_JSON = 059 "{\"name\":\"testtable\",\"Region\":[{\"endKey\":\"enp5eng=\",\"id\":8731042424," + 060 "\"location\":\"testhost:9876\",\"" + 061 "name\":\"testtable,abracadbra,8731042424.ad9860f031282c46ed431d7af8f94aca.\",\"" + 062 "startKey\":\"YWJyYWNhZGJyYQ==\"}]}"; 063 } 064 065 @Override 066 protected TableInfoModel buildTestModel() { 067 TableInfoModel model = new TableInfoModel(); 068 model.setName(TABLE); 069 model.add(new TableRegionModel(TABLE, ID, START_KEY, END_KEY, LOCATION)); 070 return model; 071 } 072 073 @Override 074 protected void checkModel(TableInfoModel model) { 075 assertEquals(TABLE, model.getName()); 076 Iterator<TableRegionModel> regions = model.getRegions().iterator(); 077 TableRegionModel region = regions.next(); 078 assertTrue(Bytes.equals(region.getStartKey(), START_KEY)); 079 assertTrue(Bytes.equals(region.getEndKey(), END_KEY)); 080 assertEquals(ID, region.getId()); 081 assertEquals(LOCATION, region.getLocation()); 082 assertFalse(regions.hasNext()); 083 } 084 085 @Override 086 public void testBuildModel() throws Exception { 087 checkModel(buildTestModel()); 088 } 089 090 @Override 091 public void testFromXML() throws Exception { 092 checkModel(fromXML(AS_XML)); 093 } 094 095 @Override 096 public void testFromPB() throws Exception { 097 checkModel(fromPB(AS_PB)); 098 } 099 100} 101