Revert "HBASE-26523 Upgrade hbase-thirdparty dependency to 4.0.0 (#3910)"
[hbase.git] / hbase-rest / src / test / java / org / apache / hadoop / hbase / rest / TestTableResource.java
blob36b2d3db63176bd816096ec573b22f563a27ba7e
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.rest;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
23 import java.io.ByteArrayInputStream;
24 import java.io.IOException;
25 import java.net.InetSocketAddress;
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
29 import javax.xml.bind.JAXBContext;
30 import javax.xml.bind.JAXBException;
31 import org.apache.hadoop.hbase.CellUtil;
32 import org.apache.hadoop.hbase.HBaseClassTestRule;
33 import org.apache.hadoop.hbase.HBaseTestingUtil;
34 import org.apache.hadoop.hbase.HRegionLocation;
35 import org.apache.hadoop.hbase.ServerName;
36 import org.apache.hadoop.hbase.TableName;
37 import org.apache.hadoop.hbase.client.Connection;
38 import org.apache.hadoop.hbase.client.Durability;
39 import org.apache.hadoop.hbase.client.Put;
40 import org.apache.hadoop.hbase.client.RegionInfo;
41 import org.apache.hadoop.hbase.client.RegionLocator;
42 import org.apache.hadoop.hbase.client.Table;
43 import org.apache.hadoop.hbase.rest.client.Client;
44 import org.apache.hadoop.hbase.rest.client.Cluster;
45 import org.apache.hadoop.hbase.rest.client.Response;
46 import org.apache.hadoop.hbase.rest.model.TableInfoModel;
47 import org.apache.hadoop.hbase.rest.model.TableListModel;
48 import org.apache.hadoop.hbase.rest.model.TableModel;
49 import org.apache.hadoop.hbase.rest.model.TableRegionModel;
50 import org.apache.hadoop.hbase.testclassification.MediumTests;
51 import org.apache.hadoop.hbase.testclassification.RestTests;
52 import org.apache.hadoop.hbase.util.Bytes;
53 import org.junit.AfterClass;
54 import org.junit.BeforeClass;
55 import org.junit.ClassRule;
56 import org.junit.Test;
57 import org.junit.experimental.categories.Category;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
61 @Category({RestTests.class, MediumTests.class})
62 public class TestTableResource {
64 @ClassRule
65 public static final HBaseClassTestRule CLASS_RULE =
66 HBaseClassTestRule.forClass(TestTableResource.class);
68 private static final Logger LOG = LoggerFactory.getLogger(TestTableResource.class);
70 private static final TableName TABLE = TableName.valueOf("TestTableResource");
71 private static final String COLUMN_FAMILY = "test";
72 private static final String COLUMN = COLUMN_FAMILY + ":qualifier";
73 private static final int NUM_REGIONS = 4;
74 private static List<HRegionLocation> regionMap;
76 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
77 private static final HBaseRESTTestingUtility REST_TEST_UTIL =
78 new HBaseRESTTestingUtility();
79 private static Client client;
80 private static JAXBContext context;
82 @BeforeClass
83 public static void setUpBeforeClass() throws Exception {
84 TEST_UTIL.startMiniCluster(3);
85 REST_TEST_UTIL.startServletContainer(TEST_UTIL.getConfiguration());
86 client = new Client(new Cluster().add("localhost",
87 REST_TEST_UTIL.getServletPort()));
88 context = JAXBContext.newInstance(
89 TableModel.class,
90 TableInfoModel.class,
91 TableListModel.class,
92 TableRegionModel.class);
93 TEST_UTIL.createMultiRegionTable(TABLE, Bytes.toBytes(COLUMN_FAMILY), NUM_REGIONS);
94 byte[] k = new byte[3];
95 byte [][] famAndQf = CellUtil.parseColumn(Bytes.toBytes(COLUMN));
96 List<Put> puts = new ArrayList<>();
97 for (byte b1 = 'a'; b1 < 'z'; b1++) {
98 for (byte b2 = 'a'; b2 < 'z'; b2++) {
99 for (byte b3 = 'a'; b3 < 'z'; b3++) {
100 k[0] = b1;
101 k[1] = b2;
102 k[2] = b3;
103 Put put = new Put(k);
104 put.setDurability(Durability.SKIP_WAL);
105 put.addColumn(famAndQf[0], famAndQf[1], k);
106 puts.add(put);
111 Connection connection = TEST_UTIL.getConnection();
113 Table table = connection.getTable(TABLE);
114 table.put(puts);
115 table.close();
117 RegionLocator regionLocator = connection.getRegionLocator(TABLE);
118 List<HRegionLocation> m = regionLocator.getAllRegionLocations();
120 // should have four regions now
121 assertEquals(NUM_REGIONS, m.size());
122 regionMap = m;
123 LOG.error("regions: " + regionMap);
124 regionLocator.close();
127 @AfterClass
128 public static void tearDownAfterClass() throws Exception {
129 REST_TEST_UTIL.shutdownServletContainer();
130 TEST_UTIL.shutdownMiniCluster();
133 private static void checkTableList(TableListModel model) {
134 boolean found = false;
135 Iterator<TableModel> tables = model.getTables().iterator();
136 assertTrue(tables.hasNext());
137 while (tables.hasNext()) {
138 TableModel table = tables.next();
139 if (table.getName().equals(TABLE.getNameAsString())) {
140 found = true;
141 break;
144 assertTrue(found);
147 void checkTableInfo(TableInfoModel model) {
148 assertEquals(model.getName(), TABLE.getNameAsString());
149 Iterator<TableRegionModel> regions = model.getRegions().iterator();
150 assertTrue(regions.hasNext());
151 while (regions.hasNext()) {
152 TableRegionModel region = regions.next();
153 boolean found = false;
154 LOG.debug("looking for region " + region.getName());
155 for (HRegionLocation e: regionMap) {
156 RegionInfo hri = e.getRegion();
157 // getRegionNameAsString uses Bytes.toStringBinary which escapes some non-printable
158 // characters
159 String hriRegionName = Bytes.toString(hri.getRegionName());
160 String regionName = region.getName();
161 LOG.debug("comparing to region " + hriRegionName);
162 if (hriRegionName.equals(regionName)) {
163 found = true;
164 byte[] startKey = hri.getStartKey();
165 byte[] endKey = hri.getEndKey();
166 ServerName serverName = e.getServerName();
167 InetSocketAddress sa =
168 new InetSocketAddress(serverName.getHostname(), serverName.getPort());
169 String location = sa.getHostName() + ":" +
170 Integer.valueOf(sa.getPort());
171 assertEquals(hri.getRegionId(), region.getId());
172 assertTrue(Bytes.equals(startKey, region.getStartKey()));
173 assertTrue(Bytes.equals(endKey, region.getEndKey()));
174 assertEquals(location, region.getLocation());
175 break;
178 assertTrue("Couldn't find region " + region.getName(), found);
182 @Test
183 public void testTableListText() throws IOException {
184 Response response = client.get("/", Constants.MIMETYPE_TEXT);
185 assertEquals(200, response.getCode());
186 assertEquals(Constants.MIMETYPE_TEXT, response.getHeader("content-type"));
189 @Test
190 public void testTableListXML() throws IOException, JAXBException {
191 Response response = client.get("/", Constants.MIMETYPE_XML);
192 assertEquals(200, response.getCode());
193 assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
194 TableListModel model = (TableListModel)
195 context.createUnmarshaller()
196 .unmarshal(new ByteArrayInputStream(response.getBody()));
197 checkTableList(model);
200 @Test
201 public void testTableListJSON() throws IOException {
202 Response response = client.get("/", Constants.MIMETYPE_JSON);
203 assertEquals(200, response.getCode());
204 assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
207 @Test
208 public void testTableListPB() throws IOException, JAXBException {
209 Response response = client.get("/", Constants.MIMETYPE_PROTOBUF);
210 assertEquals(200, response.getCode());
211 assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
212 TableListModel model = new TableListModel();
213 model.getObjectFromMessage(response.getBody());
214 checkTableList(model);
215 response = client.get("/", Constants.MIMETYPE_PROTOBUF_IETF);
216 assertEquals(200, response.getCode());
217 assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type"));
218 model = new TableListModel();
219 model.getObjectFromMessage(response.getBody());
220 checkTableList(model);
223 @Test
224 public void testTableInfoText() throws IOException {
225 Response response = client.get("/" + TABLE + "/regions", Constants.MIMETYPE_TEXT);
226 assertEquals(200, response.getCode());
227 assertEquals(Constants.MIMETYPE_TEXT, response.getHeader("content-type"));
230 @Test
231 public void testTableInfoXML() throws IOException, JAXBException {
232 Response response = client.get("/" + TABLE + "/regions", Constants.MIMETYPE_XML);
233 assertEquals(200, response.getCode());
234 assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
235 TableInfoModel model = (TableInfoModel)
236 context.createUnmarshaller()
237 .unmarshal(new ByteArrayInputStream(response.getBody()));
238 checkTableInfo(model);
241 @Test
242 public void testTableInfoJSON() throws IOException {
243 Response response = client.get("/" + TABLE + "/regions", Constants.MIMETYPE_JSON);
244 assertEquals(200, response.getCode());
245 assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
248 @Test
249 public void testTableInfoPB() throws IOException, JAXBException {
250 Response response = client.get("/" + TABLE + "/regions", Constants.MIMETYPE_PROTOBUF);
251 assertEquals(200, response.getCode());
252 assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
253 TableInfoModel model = new TableInfoModel();
254 model.getObjectFromMessage(response.getBody());
255 checkTableInfo(model);
256 response = client.get("/" + TABLE + "/regions", Constants.MIMETYPE_PROTOBUF_IETF);
257 assertEquals(200, response.getCode());
258 assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type"));
259 model = new TableInfoModel();
260 model.getObjectFromMessage(response.getBody());
261 checkTableInfo(model);
264 @Test
265 public void testTableNotFound() throws IOException {
266 String notExistTable = "notexist";
267 Response response1 = client.get("/" + notExistTable + "/schema", Constants.MIMETYPE_JSON);
268 assertEquals(404, response1.getCode());
269 Response response2 = client.get("/" + notExistTable + "/regions", Constants.MIMETYPE_XML);
270 assertEquals(404, response2.getCode());