HBASE-2564 [rest] Tests use deprecated foundation

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1031418 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andrew Kyle Purtell 2010-11-05 03:38:58 +00:00
parent 0440bb0463
commit 9b91fe2113
12 changed files with 659 additions and 579 deletions

View File

@ -1101,6 +1101,7 @@ Release 0.21.0 - Unreleased
HBASE-3180 Review periodic master logging, especially ServerManager once HBASE-3180 Review periodic master logging, especially ServerManager once
a minute a minute
HBASE-3189 Stagger Major Compactions (Nicolas Spiegelberg via Stack) HBASE-3189 Stagger Major Compactions (Nicolas Spiegelberg via Stack)
HBASE-2564 [rest] Tests use deprecated foundation
NEW FEATURES NEW FEATURES

View File

@ -22,7 +22,6 @@ package org.apache.hadoop.hbase.rest;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseClusterTestCase;
import org.apache.hadoop.util.StringUtils; import org.apache.hadoop.util.StringUtils;
import org.mortbay.jetty.Server; import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context; import org.mortbay.jetty.servlet.Context;
@ -30,33 +29,30 @@ import org.mortbay.jetty.servlet.ServletHolder;
import com.sun.jersey.spi.container.servlet.ServletContainer; import com.sun.jersey.spi.container.servlet.ServletContainer;
public class HBaseRESTClusterTestBase extends HBaseClusterTestCase public class HBaseRESTTestingUtility {
implements Constants {
static final Log LOG = static final Log LOG = LogFactory.getLog(HBaseRESTTestingUtility.class);
LogFactory.getLog(HBaseRESTClusterTestBase.class);
protected int testServletPort; private Configuration conf;
Server server; private int testServletPort;
private Server server;
protected void setUp() throws Exception { public HBaseRESTTestingUtility(Configuration conf) {
super.setUp(); this.conf = conf;
startServletContainer();
} }
protected void tearDown() throws Exception { public int getServletPort() {
stopServletContainer(); return testServletPort;
super.tearDown();
} }
private void startServletContainer() throws Exception { public void startServletContainer() throws Exception {
if (server != null) { if (server != null) {
LOG.error("ServletContainer already running"); LOG.error("ServletContainer already running");
return; return;
} }
// Inject the conf from the test cluster by being first to make singleton // Inject the conf for the test by being first to make singleton
RESTServlet.getInstance(super.conf); RESTServlet.getInstance(conf);
// set up the Jersey servlet container for Jetty // set up the Jersey servlet container for Jetty
ServletHolder sh = new ServletHolder(ServletContainer.class); ServletHolder sh = new ServletHolder(ServletContainer.class);
@ -84,7 +80,7 @@ public class HBaseRESTClusterTestBase extends HBaseClusterTestCase
testServletPort); testServletPort);
} }
private void stopServletContainer() { public void shutdownServletContainer() {
if (server != null) try { if (server != null) try {
server.stop(); server.stop();
server = null; server = null;

View File

@ -31,6 +31,7 @@ import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller; import javax.xml.bind.Unmarshaller;
import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.Header;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.HTableDescriptor;
@ -43,38 +44,47 @@ import org.apache.hadoop.hbase.rest.model.CellSetModel;
import org.apache.hadoop.hbase.rest.model.RowModel; import org.apache.hadoop.hbase.rest.model.RowModel;
import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Bytes;
public class TestRowResource extends HBaseRESTClusterTestBase { import static org.junit.Assert.*;
static final String TABLE = "TestRowResource"; import org.junit.AfterClass;
static final String CFA = "a"; import org.junit.BeforeClass;
static final String CFB = "b"; import org.junit.Test;
static final String COLUMN_1 = CFA + ":1";
static final String COLUMN_2 = CFB + ":2";
static final String ROW_1 = "testrow1";
static final String VALUE_1 = "testvalue1";
static final String ROW_2 = "testrow2";
static final String VALUE_2 = "testvalue2";
static final String ROW_3 = "testrow3";
static final String VALUE_3 = "testvalue3";
static final String ROW_4 = "testrow4";
static final String VALUE_4 = "testvalue4";
Client client; public class TestRowResource {
JAXBContext context; private static final String TABLE = "TestRowResource";
Marshaller marshaller; private static final String CFA = "a";
Unmarshaller unmarshaller; private static final String CFB = "b";
HBaseAdmin admin; private static final String COLUMN_1 = CFA + ":1";
private static final String COLUMN_2 = CFB + ":2";
private static final String ROW_1 = "testrow1";
private static final String VALUE_1 = "testvalue1";
private static final String ROW_2 = "testrow2";
private static final String VALUE_2 = "testvalue2";
private static final String ROW_3 = "testrow3";
private static final String VALUE_3 = "testvalue3";
private static final String ROW_4 = "testrow4";
private static final String VALUE_4 = "testvalue4";
@Override private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
protected void setUp() throws Exception { private static final HBaseRESTTestingUtility REST_TEST_UTIL =
super.setUp(); new HBaseRESTTestingUtility(TEST_UTIL.getConfiguration());
private static Client client;
private static JAXBContext context;
private static Marshaller marshaller;
private static Unmarshaller unmarshaller;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
TEST_UTIL.startMiniCluster(3);
REST_TEST_UTIL.startServletContainer();
context = JAXBContext.newInstance( context = JAXBContext.newInstance(
CellModel.class, CellModel.class,
CellSetModel.class, CellSetModel.class,
RowModel.class); RowModel.class);
marshaller = context.createMarshaller(); marshaller = context.createMarshaller();
unmarshaller = context.createUnmarshaller(); unmarshaller = context.createUnmarshaller();
client = new Client(new Cluster().add("localhost", testServletPort)); client = new Client(new Cluster().add("localhost",
admin = new HBaseAdmin(conf); REST_TEST_UTIL.getServletPort()));
HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
if (admin.tableExists(TABLE)) { if (admin.tableExists(TABLE)) {
return; return;
} }
@ -84,13 +94,14 @@ public class TestRowResource extends HBaseRESTClusterTestBase {
admin.createTable(htd); admin.createTable(htd);
} }
@Override @AfterClass
protected void tearDown() throws Exception { public static void tearDownAfterClass() throws Exception {
client.shutdown(); REST_TEST_UTIL.shutdownServletContainer();
super.tearDown(); TEST_UTIL.shutdownMiniCluster();
} }
Response deleteRow(String table, String row) throws IOException { private static Response deleteRow(String table, String row)
throws IOException {
StringBuilder path = new StringBuilder(); StringBuilder path = new StringBuilder();
path.append('/'); path.append('/');
path.append(table); path.append(table);
@ -101,7 +112,7 @@ public class TestRowResource extends HBaseRESTClusterTestBase {
return response; return response;
} }
Response deleteValue(String table, String row, String column) private static Response deleteValue(String table, String row, String column)
throws IOException { throws IOException {
StringBuilder path = new StringBuilder(); StringBuilder path = new StringBuilder();
path.append('/'); path.append('/');
@ -115,7 +126,7 @@ public class TestRowResource extends HBaseRESTClusterTestBase {
return response; return response;
} }
Response getValueXML(String table, String row, String column) private static Response getValueXML(String table, String row, String column)
throws IOException { throws IOException {
StringBuilder path = new StringBuilder(); StringBuilder path = new StringBuilder();
path.append('/'); path.append('/');
@ -127,12 +138,12 @@ public class TestRowResource extends HBaseRESTClusterTestBase {
return getValueXML(path.toString()); return getValueXML(path.toString());
} }
Response getValueXML(String url) throws IOException { private static Response getValueXML(String url) throws IOException {
Response response = client.get(url, MIMETYPE_XML); Response response = client.get(url, Constants.MIMETYPE_XML);
return response; return response;
} }
Response getValuePB(String table, String row, String column) private static Response getValuePB(String table, String row, String column)
throws IOException { throws IOException {
StringBuilder path = new StringBuilder(); StringBuilder path = new StringBuilder();
path.append('/'); path.append('/');
@ -144,13 +155,13 @@ public class TestRowResource extends HBaseRESTClusterTestBase {
return getValuePB(path.toString()); return getValuePB(path.toString());
} }
Response getValuePB(String url) throws IOException { private static Response getValuePB(String url) throws IOException {
Response response = client.get(url, MIMETYPE_PROTOBUF); Response response = client.get(url, Constants.MIMETYPE_PROTOBUF);
return response; return response;
} }
Response putValueXML(String table, String row, String column, String value) private static Response putValueXML(String table, String row, String column,
throws IOException, JAXBException { String value) throws IOException, JAXBException {
StringBuilder path = new StringBuilder(); StringBuilder path = new StringBuilder();
path.append('/'); path.append('/');
path.append(table); path.append(table);
@ -161,8 +172,8 @@ public class TestRowResource extends HBaseRESTClusterTestBase {
return putValueXML(path.toString(), table, row, column, value); return putValueXML(path.toString(), table, row, column, value);
} }
Response putValueXML(String url, String table, String row, String column, private static Response putValueXML(String url, String table, String row,
String value) throws IOException, JAXBException { String column, String value) throws IOException, JAXBException {
RowModel rowModel = new RowModel(row); RowModel rowModel = new RowModel(row);
rowModel.addCell(new CellModel(Bytes.toBytes(column), rowModel.addCell(new CellModel(Bytes.toBytes(column),
Bytes.toBytes(value))); Bytes.toBytes(value)));
@ -170,14 +181,14 @@ public class TestRowResource extends HBaseRESTClusterTestBase {
cellSetModel.addRow(rowModel); cellSetModel.addRow(rowModel);
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
marshaller.marshal(cellSetModel, writer); marshaller.marshal(cellSetModel, writer);
Response response = client.put(url, MIMETYPE_XML, Response response = client.put(url, Constants.MIMETYPE_XML,
Bytes.toBytes(writer.toString())); Bytes.toBytes(writer.toString()));
Thread.yield(); Thread.yield();
return response; return response;
} }
void checkValueXML(String table, String row, String column, String value) private static void checkValueXML(String table, String row, String column,
throws IOException, JAXBException { String value) throws IOException, JAXBException {
Response response = getValueXML(table, row, column); Response response = getValueXML(table, row, column);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
CellSetModel cellSet = (CellSetModel) CellSetModel cellSet = (CellSetModel)
@ -188,8 +199,8 @@ public class TestRowResource extends HBaseRESTClusterTestBase {
assertEquals(Bytes.toString(cell.getValue()), value); assertEquals(Bytes.toString(cell.getValue()), value);
} }
void checkValueXML(String url, String table, String row, String column, private static void checkValueXML(String url, String table, String row,
String value) throws IOException, JAXBException { String column, String value) throws IOException, JAXBException {
Response response = getValueXML(url); Response response = getValueXML(url);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
CellSetModel cellSet = (CellSetModel) CellSetModel cellSet = (CellSetModel)
@ -200,8 +211,8 @@ public class TestRowResource extends HBaseRESTClusterTestBase {
assertEquals(Bytes.toString(cell.getValue()), value); assertEquals(Bytes.toString(cell.getValue()), value);
} }
Response putValuePB(String table, String row, String column, String value) private static Response putValuePB(String table, String row, String column,
throws IOException { String value) throws IOException {
StringBuilder path = new StringBuilder(); StringBuilder path = new StringBuilder();
path.append('/'); path.append('/');
path.append(table); path.append(table);
@ -212,21 +223,21 @@ public class TestRowResource extends HBaseRESTClusterTestBase {
return putValuePB(path.toString(), table, row, column, value); return putValuePB(path.toString(), table, row, column, value);
} }
Response putValuePB(String url, String table, String row, String column, private static Response putValuePB(String url, String table, String row,
String value) throws IOException { String column, String value) throws IOException {
RowModel rowModel = new RowModel(row); RowModel rowModel = new RowModel(row);
rowModel.addCell(new CellModel(Bytes.toBytes(column), rowModel.addCell(new CellModel(Bytes.toBytes(column),
Bytes.toBytes(value))); Bytes.toBytes(value)));
CellSetModel cellSetModel = new CellSetModel(); CellSetModel cellSetModel = new CellSetModel();
cellSetModel.addRow(rowModel); cellSetModel.addRow(rowModel);
Response response = client.put(url, MIMETYPE_PROTOBUF, Response response = client.put(url, Constants.MIMETYPE_PROTOBUF,
cellSetModel.createProtobufOutput()); cellSetModel.createProtobufOutput());
Thread.yield(); Thread.yield();
return response; return response;
} }
void checkValuePB(String table, String row, String column, String value) private static void checkValuePB(String table, String row, String column,
throws IOException { String value) throws IOException {
Response response = getValuePB(table, row, column); Response response = getValuePB(table, row, column);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
CellSetModel cellSet = new CellSetModel(); CellSetModel cellSet = new CellSetModel();
@ -237,19 +248,8 @@ public class TestRowResource extends HBaseRESTClusterTestBase {
assertEquals(Bytes.toString(cell.getValue()), value); assertEquals(Bytes.toString(cell.getValue()), value);
} }
void checkValuePB(String url, String table, String row, String column, @Test
String value) throws IOException { public void testDelete() throws IOException, JAXBException {
Response response = getValuePB(url);
assertEquals(response.getCode(), 200);
CellSetModel cellSet = new CellSetModel();
cellSet.getObjectFromMessage(response.getBody());
RowModel rowModel = cellSet.getRows().get(0);
CellModel cell = rowModel.getCells().get(0);
assertEquals(Bytes.toString(cell.getColumn()), column);
assertEquals(Bytes.toString(cell.getValue()), value);
}
void doTestDelete() throws IOException, JAXBException {
Response response; Response response;
response = putValueXML(TABLE, ROW_1, COLUMN_1, VALUE_1); response = putValueXML(TABLE, ROW_1, COLUMN_1, VALUE_1);
@ -273,7 +273,8 @@ public class TestRowResource extends HBaseRESTClusterTestBase {
assertEquals(response.getCode(), 404); assertEquals(response.getCode(), 404);
} }
void doTestSingleCellGetPutXML() throws IOException, JAXBException { @Test
public void testSingleCellGetPutXML() throws IOException, JAXBException {
Response response = getValueXML(TABLE, ROW_1, COLUMN_1); Response response = getValueXML(TABLE, ROW_1, COLUMN_1);
assertEquals(response.getCode(), 404); assertEquals(response.getCode(), 404);
@ -288,7 +289,8 @@ public class TestRowResource extends HBaseRESTClusterTestBase {
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
} }
void doTestSingleCellGetPutPB() throws IOException, JAXBException { @Test
public void testSingleCellGetPutPB() throws IOException, JAXBException {
Response response = getValuePB(TABLE, ROW_1, COLUMN_1); Response response = getValuePB(TABLE, ROW_1, COLUMN_1);
assertEquals(response.getCode(), 404); assertEquals(response.getCode(), 404);
@ -307,14 +309,15 @@ public class TestRowResource extends HBaseRESTClusterTestBase {
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
} }
void doTestSingleCellGetPutBinary() throws IOException { @Test
public void testSingleCellGetPutBinary() throws IOException {
final String path = "/" + TABLE + "/" + ROW_3 + "/" + COLUMN_1; final String path = "/" + TABLE + "/" + ROW_3 + "/" + COLUMN_1;
final byte[] body = Bytes.toBytes(VALUE_3); final byte[] body = Bytes.toBytes(VALUE_3);
Response response = client.put(path, MIMETYPE_BINARY, body); Response response = client.put(path, Constants.MIMETYPE_BINARY, body);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
Thread.yield(); Thread.yield();
response = client.get(path, MIMETYPE_BINARY); response = client.get(path, Constants.MIMETYPE_BINARY);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
assertTrue(Bytes.equals(response.getBody(), body)); assertTrue(Bytes.equals(response.getBody(), body));
boolean foundTimestampHeader = false; boolean foundTimestampHeader = false;
@ -330,19 +333,21 @@ public class TestRowResource extends HBaseRESTClusterTestBase {
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
} }
void doTestSingleCellGetJSON() throws IOException, JAXBException { @Test
public void testSingleCellGetJSON() throws IOException, JAXBException {
final String path = "/" + TABLE + "/" + ROW_4 + "/" + COLUMN_1; final String path = "/" + TABLE + "/" + ROW_4 + "/" + COLUMN_1;
Response response = client.put(path, MIMETYPE_BINARY, Response response = client.put(path, Constants.MIMETYPE_BINARY,
Bytes.toBytes(VALUE_4)); Bytes.toBytes(VALUE_4));
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
Thread.yield(); Thread.yield();
response = client.get(path, MIMETYPE_JSON); response = client.get(path, Constants.MIMETYPE_JSON);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
response = deleteRow(TABLE, ROW_4); response = deleteRow(TABLE, ROW_4);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
} }
public void doTestURLEncodedKey() throws IOException, JAXBException { @Test
public void testURLEncodedKey() throws IOException, JAXBException {
String urlKey = "http://example.com/foo"; String urlKey = "http://example.com/foo";
StringBuilder path = new StringBuilder(); StringBuilder path = new StringBuilder();
path.append('/'); path.append('/');
@ -358,18 +363,23 @@ public class TestRowResource extends HBaseRESTClusterTestBase {
checkValueXML(path.toString(), TABLE, urlKey, COLUMN_1, VALUE_1); checkValueXML(path.toString(), TABLE, urlKey, COLUMN_1, VALUE_1);
} }
public void doTestNoSuchCF() throws IOException, JAXBException { @Test
public void testNoSuchCF() throws IOException, JAXBException {
final String goodPath = "/" + TABLE + "/" + ROW_1 + "/" + CFA+":"; final String goodPath = "/" + TABLE + "/" + ROW_1 + "/" + CFA+":";
final String badPath = "/" + TABLE + "/" + ROW_1 + "/" + "BAD"; final String badPath = "/" + TABLE + "/" + ROW_1 + "/" + "BAD";
Response response = client.post(goodPath, MIMETYPE_BINARY, Response response = client.post(goodPath, Constants.MIMETYPE_BINARY,
Bytes.toBytes(VALUE_1)); Bytes.toBytes(VALUE_1));
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
assertEquals(client.get(goodPath, MIMETYPE_BINARY).getCode(), 200); assertEquals(client.get(goodPath, Constants.MIMETYPE_BINARY).getCode(),
assertEquals(client.get(badPath, MIMETYPE_BINARY).getCode(), 404); 200);
assertEquals(client.get(goodPath, MIMETYPE_BINARY).getCode(), 200); assertEquals(client.get(badPath, Constants.MIMETYPE_BINARY).getCode(),
404);
assertEquals(client.get(goodPath, Constants.MIMETYPE_BINARY).getCode(),
200);
} }
void doTestMultiCellGetPutXML() throws IOException, JAXBException { @Test
public void testMultiCellGetPutXML() throws IOException, JAXBException {
String path = "/" + TABLE + "/fakerow"; // deliberate nonexistent row String path = "/" + TABLE + "/fakerow"; // deliberate nonexistent row
CellSetModel cellSetModel = new CellSetModel(); CellSetModel cellSetModel = new CellSetModel();
@ -387,12 +397,12 @@ public class TestRowResource extends HBaseRESTClusterTestBase {
cellSetModel.addRow(rowModel); cellSetModel.addRow(rowModel);
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
marshaller.marshal(cellSetModel, writer); marshaller.marshal(cellSetModel, writer);
Response response = client.put(path, MIMETYPE_XML, Response response = client.put(path, Constants.MIMETYPE_XML,
Bytes.toBytes(writer.toString())); Bytes.toBytes(writer.toString()));
Thread.yield(); Thread.yield();
// make sure the fake row was not actually created // make sure the fake row was not actually created
response = client.get(path, MIMETYPE_XML); response = client.get(path, Constants.MIMETYPE_XML);
assertEquals(response.getCode(), 404); assertEquals(response.getCode(), 404);
// check that all of the values were created // check that all of the values were created
@ -407,7 +417,8 @@ public class TestRowResource extends HBaseRESTClusterTestBase {
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
} }
void doTestMultiCellGetPutPB() throws IOException { @Test
public void testMultiCellGetPutPB() throws IOException {
String path = "/" + TABLE + "/fakerow"; // deliberate nonexistent row String path = "/" + TABLE + "/fakerow"; // deliberate nonexistent row
CellSetModel cellSetModel = new CellSetModel(); CellSetModel cellSetModel = new CellSetModel();
@ -423,12 +434,12 @@ public class TestRowResource extends HBaseRESTClusterTestBase {
rowModel.addCell(new CellModel(Bytes.toBytes(COLUMN_2), rowModel.addCell(new CellModel(Bytes.toBytes(COLUMN_2),
Bytes.toBytes(VALUE_4))); Bytes.toBytes(VALUE_4)));
cellSetModel.addRow(rowModel); cellSetModel.addRow(rowModel);
Response response = client.put(path, MIMETYPE_PROTOBUF, Response response = client.put(path, Constants.MIMETYPE_PROTOBUF,
cellSetModel.createProtobufOutput()); cellSetModel.createProtobufOutput());
Thread.yield(); Thread.yield();
// make sure the fake row was not actually created // make sure the fake row was not actually created
response = client.get(path, MIMETYPE_PROTOBUF); response = client.get(path, Constants.MIMETYPE_PROTOBUF);
assertEquals(response.getCode(), 404); assertEquals(response.getCode(), 404);
// check that all of the values were created // check that all of the values were created
@ -442,16 +453,4 @@ public class TestRowResource extends HBaseRESTClusterTestBase {
response = deleteRow(TABLE, ROW_2); response = deleteRow(TABLE, ROW_2);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
} }
public void testRowResource() throws Exception {
doTestDelete();
doTestSingleCellGetPutXML();
doTestSingleCellGetPutPB();
doTestSingleCellGetPutBinary();
doTestSingleCellGetJSON();
doTestURLEncodedKey();
doTestNoSuchCF();
doTestMultiCellGetPutXML();
doTestMultiCellGetPutPB();
}
} }

View File

@ -32,6 +32,7 @@ import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller; import javax.xml.bind.Unmarshaller;
import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.Header;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.KeyValue;
@ -47,28 +48,34 @@ import org.apache.hadoop.hbase.rest.model.RowModel;
import org.apache.hadoop.hbase.rest.model.ScannerModel; import org.apache.hadoop.hbase.rest.model.ScannerModel;
import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Bytes;
public class TestScannerResource extends HBaseRESTClusterTestBase { import static org.junit.Assert.*;
static final String TABLE = "TestScannerResource"; import org.junit.AfterClass;
static final String NONEXISTENT_TABLE = "ThisTableDoesNotExist"; import org.junit.BeforeClass;
static final String CFA = "a"; import org.junit.Test;
static final String CFB = "b";
static final String COLUMN_1 = CFA + ":1";
static final String COLUMN_2 = CFB + ":2";
static int expectedRows1; public class TestScannerResource {
static int expectedRows2; private static final String TABLE = "TestScannerResource";
private static final String NONEXISTENT_TABLE = "ThisTableDoesNotExist";
private static final String CFA = "a";
private static final String CFB = "b";
private static final String COLUMN_1 = CFA + ":1";
private static final String COLUMN_2 = CFB + ":2";
Client client; private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
JAXBContext context; private static final HBaseRESTTestingUtility REST_TEST_UTIL =
Marshaller marshaller; new HBaseRESTTestingUtility(TEST_UTIL.getConfiguration());
Unmarshaller unmarshaller; private static Client client;
HBaseAdmin admin; private static JAXBContext context;
private static Marshaller marshaller;
private static Unmarshaller unmarshaller;
private static int expectedRows1;
private static int expectedRows2;
int insertData(String tableName, String column, double prob) private static int insertData(String tableName, String column, double prob)
throws IOException { throws IOException {
Random rng = new Random(); Random rng = new Random();
int count = 0; int count = 0;
HTable table = new HTable(conf, tableName); HTable table = new HTable(TEST_UTIL.getConfiguration(), tableName);
byte[] k = new byte[3]; byte[] k = new byte[3];
byte [][] famAndQf = KeyValue.parseColumn(Bytes.toBytes(column)); byte [][] famAndQf = KeyValue.parseColumn(Bytes.toBytes(column));
for (byte b1 = 'a'; b1 < 'z'; b1++) { for (byte b1 = 'a'; b1 < 'z'; b1++) {
@ -90,36 +97,7 @@ public class TestScannerResource extends HBaseRESTClusterTestBase {
return count; return count;
} }
@Override private static int countCellSet(CellSetModel model) {
protected void setUp() throws Exception {
super.setUp();
context = JAXBContext.newInstance(
CellModel.class,
CellSetModel.class,
RowModel.class,
ScannerModel.class);
marshaller = context.createMarshaller();
unmarshaller = context.createUnmarshaller();
client = new Client(new Cluster().add("localhost", testServletPort));
admin = new HBaseAdmin(conf);
if (admin.tableExists(TABLE)) {
return;
}
HTableDescriptor htd = new HTableDescriptor(TABLE);
htd.addFamily(new HColumnDescriptor(CFA));
htd.addFamily(new HColumnDescriptor(CFB));
admin.createTable(htd);
expectedRows1 = insertData(TABLE, COLUMN_1, 1.0);
expectedRows2 = insertData(TABLE, COLUMN_2, 0.5);
}
@Override
protected void tearDown() throws Exception {
client.shutdown();
super.tearDown();
}
int countCellSet(CellSetModel model) {
int count = 0; int count = 0;
Iterator<RowModel> rows = model.getRows().iterator(); Iterator<RowModel> rows = model.getRows().iterator();
while (rows.hasNext()) { while (rows.hasNext()) {
@ -133,7 +111,72 @@ public class TestScannerResource extends HBaseRESTClusterTestBase {
return count; return count;
} }
void doTestSimpleScannerXML() throws IOException, JAXBException { private static int fullTableScan(ScannerModel model) throws IOException {
model.setBatch(100);
Response response = client.put("/" + TABLE + "/scanner",
Constants.MIMETYPE_PROTOBUF, model.createProtobufOutput());
assertEquals(response.getCode(), 201);
String scannerURI = response.getLocation();
assertNotNull(scannerURI);
int count = 0;
while (true) {
response = client.get(scannerURI, Constants.MIMETYPE_PROTOBUF);
assertTrue(response.getCode() == 200 || response.getCode() == 204);
if (response.getCode() == 200) {
CellSetModel cellSet = new CellSetModel();
cellSet.getObjectFromMessage(response.getBody());
Iterator<RowModel> rows = cellSet.getRows().iterator();
while (rows.hasNext()) {
RowModel row = rows.next();
Iterator<CellModel> cells = row.getCells().iterator();
while (cells.hasNext()) {
cells.next();
count++;
}
}
} else {
break;
}
}
// delete the scanner
response = client.delete(scannerURI);
assertEquals(response.getCode(), 200);
return count;
}
@BeforeClass
public static void setUpBeforeClass() throws Exception {
TEST_UTIL.startMiniCluster(3);
REST_TEST_UTIL.startServletContainer();
client = new Client(new Cluster().add("localhost",
REST_TEST_UTIL.getServletPort()));
context = JAXBContext.newInstance(
CellModel.class,
CellSetModel.class,
RowModel.class,
ScannerModel.class);
marshaller = context.createMarshaller();
unmarshaller = context.createUnmarshaller();
HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
if (admin.tableExists(TABLE)) {
return;
}
HTableDescriptor htd = new HTableDescriptor(TABLE);
htd.addFamily(new HColumnDescriptor(CFA));
htd.addFamily(new HColumnDescriptor(CFB));
admin.createTable(htd);
expectedRows1 = insertData(TABLE, COLUMN_1, 1.0);
expectedRows2 = insertData(TABLE, COLUMN_2, 0.5);
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
REST_TEST_UTIL.shutdownServletContainer();
TEST_UTIL.shutdownMiniCluster();
}
@Test
public void testSimpleScannerXML() throws IOException, JAXBException {
final int BATCH_SIZE = 5; final int BATCH_SIZE = 5;
// new scanner // new scanner
ScannerModel model = new ScannerModel(); ScannerModel model = new ScannerModel();
@ -142,14 +185,14 @@ public class TestScannerResource extends HBaseRESTClusterTestBase {
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
marshaller.marshal(model, writer); marshaller.marshal(model, writer);
byte[] body = Bytes.toBytes(writer.toString()); byte[] body = Bytes.toBytes(writer.toString());
Response response = client.put("/" + TABLE + "/scanner", MIMETYPE_XML, Response response = client.put("/" + TABLE + "/scanner",
body); Constants.MIMETYPE_XML, body);
assertEquals(response.getCode(), 201); assertEquals(response.getCode(), 201);
String scannerURI = response.getLocation(); String scannerURI = response.getLocation();
assertNotNull(scannerURI); assertNotNull(scannerURI);
// get a cell set // get a cell set
response = client.get(scannerURI, MIMETYPE_XML); response = client.get(scannerURI, Constants.MIMETYPE_XML);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
CellSetModel cellSet = (CellSetModel) CellSetModel cellSet = (CellSetModel)
unmarshaller.unmarshal(new ByteArrayInputStream(response.getBody())); unmarshaller.unmarshal(new ByteArrayInputStream(response.getBody()));
@ -161,20 +204,21 @@ public class TestScannerResource extends HBaseRESTClusterTestBase {
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
} }
void doTestSimpleScannerPB() throws IOException { @Test
public void testSimpleScannerPB() throws IOException {
final int BATCH_SIZE = 10; final int BATCH_SIZE = 10;
// new scanner // new scanner
ScannerModel model = new ScannerModel(); ScannerModel model = new ScannerModel();
model.setBatch(BATCH_SIZE); model.setBatch(BATCH_SIZE);
model.addColumn(Bytes.toBytes(COLUMN_1)); model.addColumn(Bytes.toBytes(COLUMN_1));
Response response = client.put("/" + TABLE + "/scanner", Response response = client.put("/" + TABLE + "/scanner",
MIMETYPE_PROTOBUF, model.createProtobufOutput()); Constants.MIMETYPE_PROTOBUF, model.createProtobufOutput());
assertEquals(response.getCode(), 201); assertEquals(response.getCode(), 201);
String scannerURI = response.getLocation(); String scannerURI = response.getLocation();
assertNotNull(scannerURI); assertNotNull(scannerURI);
// get a cell set // get a cell set
response = client.get(scannerURI, MIMETYPE_PROTOBUF); response = client.get(scannerURI, Constants.MIMETYPE_PROTOBUF);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
CellSetModel cellSet = new CellSetModel(); CellSetModel cellSet = new CellSetModel();
cellSet.getObjectFromMessage(response.getBody()); cellSet.getObjectFromMessage(response.getBody());
@ -186,19 +230,20 @@ public class TestScannerResource extends HBaseRESTClusterTestBase {
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
} }
void doTestSimpleScannerBinary() throws IOException { @Test
public void testSimpleScannerBinary() throws IOException {
// new scanner // new scanner
ScannerModel model = new ScannerModel(); ScannerModel model = new ScannerModel();
model.setBatch(1); model.setBatch(1);
model.addColumn(Bytes.toBytes(COLUMN_1)); model.addColumn(Bytes.toBytes(COLUMN_1));
Response response = client.put("/" + TABLE + "/scanner", Response response = client.put("/" + TABLE + "/scanner",
MIMETYPE_PROTOBUF, model.createProtobufOutput()); Constants.MIMETYPE_PROTOBUF, model.createProtobufOutput());
assertEquals(response.getCode(), 201); assertEquals(response.getCode(), 201);
String scannerURI = response.getLocation(); String scannerURI = response.getLocation();
assertNotNull(scannerURI); assertNotNull(scannerURI);
// get a cell // get a cell
response = client.get(scannerURI, MIMETYPE_BINARY); response = client.get(scannerURI, Constants.MIMETYPE_BINARY);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
// verify that data was returned // verify that data was returned
assertTrue(response.getBody().length > 0); assertTrue(response.getBody().length > 0);
@ -223,40 +268,8 @@ public class TestScannerResource extends HBaseRESTClusterTestBase {
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
} }
int fullTableScan(ScannerModel model) throws IOException { @Test
model.setBatch(100); public void testFullTableScan() throws IOException {
Response response = client.put("/" + TABLE + "/scanner",
MIMETYPE_PROTOBUF, model.createProtobufOutput());
assertEquals(response.getCode(), 201);
String scannerURI = response.getLocation();
assertNotNull(scannerURI);
int count = 0;
while (true) {
response = client.get(scannerURI, MIMETYPE_PROTOBUF);
assertTrue(response.getCode() == 200 || response.getCode() == 204);
if (response.getCode() == 200) {
CellSetModel cellSet = new CellSetModel();
cellSet.getObjectFromMessage(response.getBody());
Iterator<RowModel> rows = cellSet.getRows().iterator();
while (rows.hasNext()) {
RowModel row = rows.next();
Iterator<CellModel> cells = row.getCells().iterator();
while (cells.hasNext()) {
cells.next();
count++;
}
}
} else {
break;
}
}
// delete the scanner
response = client.delete(scannerURI);
assertEquals(response.getCode(), 200);
return count;
}
void doTestFullTableScan() throws IOException {
ScannerModel model = new ScannerModel(); ScannerModel model = new ScannerModel();
model.addColumn(Bytes.toBytes(COLUMN_1)); model.addColumn(Bytes.toBytes(COLUMN_1));
assertEquals(fullTableScan(model), expectedRows1); assertEquals(fullTableScan(model), expectedRows1);
@ -266,21 +279,14 @@ public class TestScannerResource extends HBaseRESTClusterTestBase {
assertEquals(fullTableScan(model), expectedRows2); assertEquals(fullTableScan(model), expectedRows2);
} }
void doTestTableDoesNotExist() throws IOException, JAXBException { @Test
public void testTableDoesNotExist() throws IOException, JAXBException {
ScannerModel model = new ScannerModel(); ScannerModel model = new ScannerModel();
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
marshaller.marshal(model, writer); marshaller.marshal(model, writer);
byte[] body = Bytes.toBytes(writer.toString()); byte[] body = Bytes.toBytes(writer.toString());
Response response = client.put("/" + NONEXISTENT_TABLE + Response response = client.put("/" + NONEXISTENT_TABLE +
"/scanner", MIMETYPE_XML, body); "/scanner", Constants.MIMETYPE_XML, body);
assertEquals(response.getCode(), 404); assertEquals(response.getCode(), 404);
} }
public void testScannerResource() throws Exception {
doTestSimpleScannerXML();
doTestSimpleScannerPB();
doTestSimpleScannerBinary();
doTestFullTableScan();
doTestTableDoesNotExist();
}
} }

View File

@ -33,6 +33,7 @@ import javax.xml.bind.Unmarshaller;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.HTableDescriptor;
@ -66,48 +67,59 @@ import org.apache.hadoop.hbase.rest.model.RowModel;
import org.apache.hadoop.hbase.rest.model.ScannerModel; import org.apache.hadoop.hbase.rest.model.ScannerModel;
import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Bytes;
public class TestScannersWithFilters extends HBaseRESTClusterTestBase { import static org.junit.Assert.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
static final Log LOG = LogFactory.getLog(TestScannersWithFilters.class); public class TestScannersWithFilters {
static final byte [][] ROWS_ONE = { private static final Log LOG = LogFactory.getLog(TestScannersWithFilters.class);
private static final String TABLE = "TestScannersWithFilters";
private static final byte [][] ROWS_ONE = {
Bytes.toBytes("testRowOne-0"), Bytes.toBytes("testRowOne-1"), Bytes.toBytes("testRowOne-0"), Bytes.toBytes("testRowOne-1"),
Bytes.toBytes("testRowOne-2"), Bytes.toBytes("testRowOne-3") Bytes.toBytes("testRowOne-2"), Bytes.toBytes("testRowOne-3")
}; };
static final byte [][] ROWS_TWO = { private static final byte [][] ROWS_TWO = {
Bytes.toBytes("testRowTwo-0"), Bytes.toBytes("testRowTwo-1"), Bytes.toBytes("testRowTwo-0"), Bytes.toBytes("testRowTwo-1"),
Bytes.toBytes("testRowTwo-2"), Bytes.toBytes("testRowTwo-3") Bytes.toBytes("testRowTwo-2"), Bytes.toBytes("testRowTwo-3")
}; };
static final byte [][] FAMILIES = { private static final byte [][] FAMILIES = {
Bytes.toBytes("testFamilyOne"), Bytes.toBytes("testFamilyTwo") Bytes.toBytes("testFamilyOne"), Bytes.toBytes("testFamilyTwo")
}; };
static final byte [][] QUALIFIERS_ONE = { private static final byte [][] QUALIFIERS_ONE = {
Bytes.toBytes("testQualifierOne-0"), Bytes.toBytes("testQualifierOne-1"), Bytes.toBytes("testQualifierOne-0"), Bytes.toBytes("testQualifierOne-1"),
Bytes.toBytes("testQualifierOne-2"), Bytes.toBytes("testQualifierOne-3") Bytes.toBytes("testQualifierOne-2"), Bytes.toBytes("testQualifierOne-3")
}; };
static final byte [][] QUALIFIERS_TWO = { private static final byte [][] QUALIFIERS_TWO = {
Bytes.toBytes("testQualifierTwo-0"), Bytes.toBytes("testQualifierTwo-1"), Bytes.toBytes("testQualifierTwo-0"), Bytes.toBytes("testQualifierTwo-1"),
Bytes.toBytes("testQualifierTwo-2"), Bytes.toBytes("testQualifierTwo-3") Bytes.toBytes("testQualifierTwo-2"), Bytes.toBytes("testQualifierTwo-3")
}; };
static final byte [][] VALUES = { private static final byte [][] VALUES = {
Bytes.toBytes("testValueOne"), Bytes.toBytes("testValueTwo") Bytes.toBytes("testValueOne"), Bytes.toBytes("testValueTwo")
}; };
Client client; private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
JAXBContext context; private static final HBaseRESTTestingUtility REST_TEST_UTIL =
Marshaller marshaller; new HBaseRESTTestingUtility(TEST_UTIL.getConfiguration());
Unmarshaller unmarshaller; private static Client client;
long numRows = ROWS_ONE.length + ROWS_TWO.length; private static JAXBContext context;
long colsPerRow = FAMILIES.length * QUALIFIERS_ONE.length; private static Marshaller marshaller;
private static Unmarshaller unmarshaller;
private static long numRows = ROWS_ONE.length + ROWS_TWO.length;
private static long colsPerRow = FAMILIES.length * QUALIFIERS_ONE.length;
@Override @BeforeClass
protected void setUp() throws Exception { public static void setUpBeforeClass() throws Exception {
super.setUp(); TEST_UTIL.startMiniCluster(3);
REST_TEST_UTIL.startServletContainer();
context = JAXBContext.newInstance( context = JAXBContext.newInstance(
CellModel.class, CellModel.class,
CellSetModel.class, CellSetModel.class,
@ -115,14 +127,15 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
ScannerModel.class); ScannerModel.class);
marshaller = context.createMarshaller(); marshaller = context.createMarshaller();
unmarshaller = context.createUnmarshaller(); unmarshaller = context.createUnmarshaller();
client = new Client(new Cluster().add("localhost", testServletPort)); client = new Client(new Cluster().add("localhost",
HBaseAdmin admin = new HBaseAdmin(conf); REST_TEST_UTIL.getServletPort()));
if (!admin.tableExists(getName())) { HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
HTableDescriptor htd = new HTableDescriptor(getName()); if (!admin.tableExists(TABLE)) {
HTableDescriptor htd = new HTableDescriptor(TABLE);
htd.addFamily(new HColumnDescriptor(FAMILIES[0])); htd.addFamily(new HColumnDescriptor(FAMILIES[0]));
htd.addFamily(new HColumnDescriptor(FAMILIES[1])); htd.addFamily(new HColumnDescriptor(FAMILIES[1]));
admin.createTable(htd); admin.createTable(htd);
HTable table = new HTable(conf, getName()); HTable table = new HTable(TEST_UTIL.getConfiguration(), TABLE);
// Insert first half // Insert first half
for(byte [] ROW : ROWS_ONE) { for(byte [] ROW : ROWS_ONE) {
Put p = new Put(ROW); Put p = new Put(ROW);
@ -187,13 +200,13 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
} }
} }
@Override @AfterClass
protected void tearDown() throws Exception { public static void tearDownAfterClass() throws Exception {
client.shutdown(); REST_TEST_UTIL.shutdownServletContainer();
super.tearDown(); TEST_UTIL.shutdownMiniCluster();
} }
void verifyScan(Scan s, long expectedRows, long expectedKeys) private static void verifyScan(Scan s, long expectedRows, long expectedKeys)
throws Exception { throws Exception {
ScannerModel model = ScannerModel.fromScan(s); ScannerModel model = ScannerModel.fromScan(s);
model.setBatch(Integer.MAX_VALUE); // fetch it all at once model.setBatch(Integer.MAX_VALUE); // fetch it all at once
@ -201,14 +214,14 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
marshaller.marshal(model, writer); marshaller.marshal(model, writer);
LOG.debug(writer.toString()); LOG.debug(writer.toString());
byte[] body = Bytes.toBytes(writer.toString()); byte[] body = Bytes.toBytes(writer.toString());
Response response = client.put("/" + getName() + "/scanner", MIMETYPE_XML, Response response = client.put("/" + TABLE + "/scanner",
body); Constants.MIMETYPE_XML, body);
assertEquals(response.getCode(), 201); assertEquals(response.getCode(), 201);
String scannerURI = response.getLocation(); String scannerURI = response.getLocation();
assertNotNull(scannerURI); assertNotNull(scannerURI);
// get a cell set // get a cell set
response = client.get(scannerURI, MIMETYPE_XML); response = client.get(scannerURI, Constants.MIMETYPE_XML);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
CellSetModel cells = (CellSetModel) CellSetModel cells = (CellSetModel)
unmarshaller.unmarshal(new ByteArrayInputStream(response.getBody())); unmarshaller.unmarshal(new ByteArrayInputStream(response.getBody()));
@ -227,21 +240,22 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
} }
void verifyScanFull(Scan s, KeyValue [] kvs) throws Exception { private static void verifyScanFull(Scan s, KeyValue [] kvs)
throws Exception {
ScannerModel model = ScannerModel.fromScan(s); ScannerModel model = ScannerModel.fromScan(s);
model.setBatch(Integer.MAX_VALUE); // fetch it all at once model.setBatch(Integer.MAX_VALUE); // fetch it all at once
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
marshaller.marshal(model, writer); marshaller.marshal(model, writer);
LOG.debug(writer.toString()); LOG.debug(writer.toString());
byte[] body = Bytes.toBytes(writer.toString()); byte[] body = Bytes.toBytes(writer.toString());
Response response = client.put("/" + getName() + "/scanner", MIMETYPE_XML, Response response = client.put("/" + TABLE + "/scanner",
body); Constants.MIMETYPE_XML, body);
assertEquals(response.getCode(), 201); assertEquals(response.getCode(), 201);
String scannerURI = response.getLocation(); String scannerURI = response.getLocation();
assertNotNull(scannerURI); assertNotNull(scannerURI);
// get a cell set // get a cell set
response = client.get(scannerURI, MIMETYPE_XML); response = client.get(scannerURI, Constants.MIMETYPE_XML);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
CellSetModel cellSet = (CellSetModel) CellSetModel cellSet = (CellSetModel)
unmarshaller.unmarshal(new ByteArrayInputStream(response.getBody())); unmarshaller.unmarshal(new ByteArrayInputStream(response.getBody()));
@ -279,22 +293,22 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
kvs.length, idx); kvs.length, idx);
} }
void verifyScanNoEarlyOut(Scan s, long expectedRows, long expectedKeys) private static void verifyScanNoEarlyOut(Scan s, long expectedRows,
throws Exception { long expectedKeys) throws Exception {
ScannerModel model = ScannerModel.fromScan(s); ScannerModel model = ScannerModel.fromScan(s);
model.setBatch(Integer.MAX_VALUE); // fetch it all at once model.setBatch(Integer.MAX_VALUE); // fetch it all at once
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
marshaller.marshal(model, writer); marshaller.marshal(model, writer);
LOG.debug(writer.toString()); LOG.debug(writer.toString());
byte[] body = Bytes.toBytes(writer.toString()); byte[] body = Bytes.toBytes(writer.toString());
Response response = client.put("/" + getName() + "/scanner", MIMETYPE_XML, Response response = client.put("/" + TABLE + "/scanner",
body); Constants.MIMETYPE_XML, body);
assertEquals(response.getCode(), 201); assertEquals(response.getCode(), 201);
String scannerURI = response.getLocation(); String scannerURI = response.getLocation();
assertNotNull(scannerURI); assertNotNull(scannerURI);
// get a cell set // get a cell set
response = client.get(scannerURI, MIMETYPE_XML); response = client.get(scannerURI, Constants.MIMETYPE_XML);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
CellSetModel cellSet = (CellSetModel) CellSetModel cellSet = (CellSetModel)
unmarshaller.unmarshal(new ByteArrayInputStream(response.getBody())); unmarshaller.unmarshal(new ByteArrayInputStream(response.getBody()));
@ -320,10 +334,11 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
" rows", expectedRows, j); " rows", expectedRows, j);
} }
void doTestNoFilter() throws Exception { @Test
public void testNoFilter() throws Exception {
// No filter // No filter
long expectedRows = this.numRows; long expectedRows = numRows;
long expectedKeys = this.colsPerRow; long expectedKeys = colsPerRow;
// Both families // Both families
Scan s = new Scan(); Scan s = new Scan();
@ -335,16 +350,18 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
verifyScan(s, expectedRows, expectedKeys/2); verifyScan(s, expectedRows, expectedKeys/2);
} }
void doTestPrefixFilter() throws Exception { @Test
public void testPrefixFilter() throws Exception {
// Grab rows from group one (half of total) // Grab rows from group one (half of total)
long expectedRows = this.numRows / 2; long expectedRows = numRows / 2;
long expectedKeys = this.colsPerRow; long expectedKeys = colsPerRow;
Scan s = new Scan(); Scan s = new Scan();
s.setFilter(new PrefixFilter(Bytes.toBytes("testRowOne"))); s.setFilter(new PrefixFilter(Bytes.toBytes("testRowOne")));
verifyScan(s, expectedRows, expectedKeys); verifyScan(s, expectedRows, expectedKeys);
} }
void doTestPageFilter() throws Exception { @Test
public void testPageFilter() throws Exception {
// KVs in first 6 rows // KVs in first 6 rows
KeyValue [] expectedKVs = { KeyValue [] expectedKVs = {
// testRowOne-0 // testRowOne-0
@ -393,7 +410,7 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
// Grab all 6 rows // Grab all 6 rows
long expectedRows = 6; long expectedRows = 6;
long expectedKeys = this.colsPerRow; long expectedKeys = colsPerRow;
Scan s = new Scan(); Scan s = new Scan();
s.setFilter(new PageFilter(expectedRows)); s.setFilter(new PageFilter(expectedRows));
verifyScan(s, expectedRows, expectedKeys); verifyScan(s, expectedRows, expectedKeys);
@ -402,7 +419,7 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
// Grab first 4 rows (6 cols per row) // Grab first 4 rows (6 cols per row)
expectedRows = 4; expectedRows = 4;
expectedKeys = this.colsPerRow; expectedKeys = colsPerRow;
s = new Scan(); s = new Scan();
s.setFilter(new PageFilter(expectedRows)); s.setFilter(new PageFilter(expectedRows));
verifyScan(s, expectedRows, expectedKeys); verifyScan(s, expectedRows, expectedKeys);
@ -411,7 +428,7 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
// Grab first 2 rows // Grab first 2 rows
expectedRows = 2; expectedRows = 2;
expectedKeys = this.colsPerRow; expectedKeys = colsPerRow;
s = new Scan(); s = new Scan();
s.setFilter(new PageFilter(expectedRows)); s.setFilter(new PageFilter(expectedRows));
verifyScan(s, expectedRows, expectedKeys); verifyScan(s, expectedRows, expectedKeys);
@ -420,7 +437,7 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
// Grab first row // Grab first row
expectedRows = 1; expectedRows = 1;
expectedKeys = this.colsPerRow; expectedKeys = colsPerRow;
s = new Scan(); s = new Scan();
s.setFilter(new PageFilter(expectedRows)); s.setFilter(new PageFilter(expectedRows));
verifyScan(s, expectedRows, expectedKeys); verifyScan(s, expectedRows, expectedKeys);
@ -428,18 +445,19 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
verifyScanFull(s, Arrays.copyOf(expectedKVs, 6)); verifyScanFull(s, Arrays.copyOf(expectedKVs, 6));
} }
void doTestInclusiveStopFilter() throws Exception { @Test
public void testInclusiveStopFilter() throws Exception {
// Grab rows from group one // Grab rows from group one
// If we just use start/stop row, we get total/2 - 1 rows // If we just use start/stop row, we get total/2 - 1 rows
long expectedRows = (this.numRows / 2) - 1; long expectedRows = (numRows / 2) - 1;
long expectedKeys = this.colsPerRow; long expectedKeys = colsPerRow;
Scan s = new Scan(Bytes.toBytes("testRowOne-0"), Scan s = new Scan(Bytes.toBytes("testRowOne-0"),
Bytes.toBytes("testRowOne-3")); Bytes.toBytes("testRowOne-3"));
verifyScan(s, expectedRows, expectedKeys); verifyScan(s, expectedRows, expectedKeys);
// Now use start row with inclusive stop filter // Now use start row with inclusive stop filter
expectedRows = this.numRows / 2; expectedRows = numRows / 2;
s = new Scan(Bytes.toBytes("testRowOne-0")); s = new Scan(Bytes.toBytes("testRowOne-0"));
s.setFilter(new InclusiveStopFilter(Bytes.toBytes("testRowOne-3"))); s.setFilter(new InclusiveStopFilter(Bytes.toBytes("testRowOne-3")));
verifyScan(s, expectedRows, expectedKeys); verifyScan(s, expectedRows, expectedKeys);
@ -447,23 +465,23 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
// Grab rows from group two // Grab rows from group two
// If we just use start/stop row, we get total/2 - 1 rows // If we just use start/stop row, we get total/2 - 1 rows
expectedRows = (this.numRows / 2) - 1; expectedRows = (numRows / 2) - 1;
expectedKeys = this.colsPerRow; expectedKeys = colsPerRow;
s = new Scan(Bytes.toBytes("testRowTwo-0"), s = new Scan(Bytes.toBytes("testRowTwo-0"),
Bytes.toBytes("testRowTwo-3")); Bytes.toBytes("testRowTwo-3"));
verifyScan(s, expectedRows, expectedKeys); verifyScan(s, expectedRows, expectedKeys);
// Now use start row with inclusive stop filter // Now use start row with inclusive stop filter
expectedRows = this.numRows / 2; expectedRows = numRows / 2;
s = new Scan(Bytes.toBytes("testRowTwo-0")); s = new Scan(Bytes.toBytes("testRowTwo-0"));
s.setFilter(new InclusiveStopFilter(Bytes.toBytes("testRowTwo-3"))); s.setFilter(new InclusiveStopFilter(Bytes.toBytes("testRowTwo-3")));
verifyScan(s, expectedRows, expectedKeys); verifyScan(s, expectedRows, expectedKeys);
} }
void doTestQualifierFilter() throws Exception { @Test
public void testQualifierFilter() throws Exception {
// Match two keys (one from each family) in half the rows // Match two keys (one from each family) in half the rows
long expectedRows = this.numRows / 2; long expectedRows = numRows / 2;
long expectedKeys = 2; long expectedKeys = 2;
Filter f = new QualifierFilter(CompareOp.EQUAL, Filter f = new QualifierFilter(CompareOp.EQUAL,
new BinaryComparator(Bytes.toBytes("testQualifierOne-2"))); new BinaryComparator(Bytes.toBytes("testQualifierOne-2")));
@ -473,7 +491,7 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
// Match keys less than same qualifier // Match keys less than same qualifier
// Expect only two keys (one from each family) in half the rows // Expect only two keys (one from each family) in half the rows
expectedRows = this.numRows / 2; expectedRows = numRows / 2;
expectedKeys = 2; expectedKeys = 2;
f = new QualifierFilter(CompareOp.LESS, f = new QualifierFilter(CompareOp.LESS,
new BinaryComparator(Bytes.toBytes("testQualifierOne-2"))); new BinaryComparator(Bytes.toBytes("testQualifierOne-2")));
@ -483,7 +501,7 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
// Match keys less than or equal // Match keys less than or equal
// Expect four keys (two from each family) in half the rows // Expect four keys (two from each family) in half the rows
expectedRows = this.numRows / 2; expectedRows = numRows / 2;
expectedKeys = 4; expectedKeys = 4;
f = new QualifierFilter(CompareOp.LESS_OR_EQUAL, f = new QualifierFilter(CompareOp.LESS_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("testQualifierOne-2"))); new BinaryComparator(Bytes.toBytes("testQualifierOne-2")));
@ -494,7 +512,7 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
// Match keys not equal // Match keys not equal
// Expect four keys (two from each family) // Expect four keys (two from each family)
// Only look in first group of rows // Only look in first group of rows
expectedRows = this.numRows / 2; expectedRows = numRows / 2;
expectedKeys = 4; expectedKeys = 4;
f = new QualifierFilter(CompareOp.NOT_EQUAL, f = new QualifierFilter(CompareOp.NOT_EQUAL,
new BinaryComparator(Bytes.toBytes("testQualifierOne-2"))); new BinaryComparator(Bytes.toBytes("testQualifierOne-2")));
@ -505,7 +523,7 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
// Match keys greater or equal // Match keys greater or equal
// Expect four keys (two from each family) // Expect four keys (two from each family)
// Only look in first group of rows // Only look in first group of rows
expectedRows = this.numRows / 2; expectedRows = numRows / 2;
expectedKeys = 4; expectedKeys = 4;
f = new QualifierFilter(CompareOp.GREATER_OR_EQUAL, f = new QualifierFilter(CompareOp.GREATER_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("testQualifierOne-2"))); new BinaryComparator(Bytes.toBytes("testQualifierOne-2")));
@ -516,7 +534,7 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
// Match keys greater // Match keys greater
// Expect two keys (one from each family) // Expect two keys (one from each family)
// Only look in first group of rows // Only look in first group of rows
expectedRows = this.numRows / 2; expectedRows = numRows / 2;
expectedKeys = 2; expectedKeys = 2;
f = new QualifierFilter(CompareOp.GREATER, f = new QualifierFilter(CompareOp.GREATER,
new BinaryComparator(Bytes.toBytes("testQualifierOne-2"))); new BinaryComparator(Bytes.toBytes("testQualifierOne-2")));
@ -615,11 +633,12 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
}; };
verifyScanFull(s, kvs); verifyScanFull(s, kvs);
} }
void doTestRowFilter() throws Exception { @Test
public void testRowFilter() throws Exception {
// Match a single row, all keys // Match a single row, all keys
long expectedRows = 1; long expectedRows = 1;
long expectedKeys = this.colsPerRow; long expectedKeys = colsPerRow;
Filter f = new RowFilter(CompareOp.EQUAL, Filter f = new RowFilter(CompareOp.EQUAL,
new BinaryComparator(Bytes.toBytes("testRowOne-2"))); new BinaryComparator(Bytes.toBytes("testRowOne-2")));
Scan s = new Scan(); Scan s = new Scan();
@ -628,7 +647,7 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
// Match a two rows, one from each group, using regex // Match a two rows, one from each group, using regex
expectedRows = 2; expectedRows = 2;
expectedKeys = this.colsPerRow; expectedKeys = colsPerRow;
f = new RowFilter(CompareOp.EQUAL, f = new RowFilter(CompareOp.EQUAL,
new RegexStringComparator("testRow.+-2")); new RegexStringComparator("testRow.+-2"));
s = new Scan(); s = new Scan();
@ -638,7 +657,7 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
// Match rows less than // Match rows less than
// Expect all keys in one row // Expect all keys in one row
expectedRows = 1; expectedRows = 1;
expectedKeys = this.colsPerRow; expectedKeys = colsPerRow;
f = new RowFilter(CompareOp.LESS, f = new RowFilter(CompareOp.LESS,
new BinaryComparator(Bytes.toBytes("testRowOne-2"))); new BinaryComparator(Bytes.toBytes("testRowOne-2")));
s = new Scan(); s = new Scan();
@ -648,7 +667,7 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
// Match rows less than or equal // Match rows less than or equal
// Expect all keys in two rows // Expect all keys in two rows
expectedRows = 2; expectedRows = 2;
expectedKeys = this.colsPerRow; expectedKeys = colsPerRow;
f = new RowFilter(CompareOp.LESS_OR_EQUAL, f = new RowFilter(CompareOp.LESS_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("testRowOne-2"))); new BinaryComparator(Bytes.toBytes("testRowOne-2")));
s = new Scan(); s = new Scan();
@ -657,8 +676,8 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
// Match rows not equal // Match rows not equal
// Expect all keys in all but one row // Expect all keys in all but one row
expectedRows = this.numRows - 1; expectedRows = numRows - 1;
expectedKeys = this.colsPerRow; expectedKeys = colsPerRow;
f = new RowFilter(CompareOp.NOT_EQUAL, f = new RowFilter(CompareOp.NOT_EQUAL,
new BinaryComparator(Bytes.toBytes("testRowOne-2"))); new BinaryComparator(Bytes.toBytes("testRowOne-2")));
s = new Scan(); s = new Scan();
@ -667,8 +686,8 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
// Match keys greater or equal // Match keys greater or equal
// Expect all keys in all but one row // Expect all keys in all but one row
expectedRows = this.numRows - 1; expectedRows = numRows - 1;
expectedKeys = this.colsPerRow; expectedKeys = colsPerRow;
f = new RowFilter(CompareOp.GREATER_OR_EQUAL, f = new RowFilter(CompareOp.GREATER_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("testRowOne-2"))); new BinaryComparator(Bytes.toBytes("testRowOne-2")));
s = new Scan(); s = new Scan();
@ -677,8 +696,8 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
// Match keys greater // Match keys greater
// Expect all keys in all but two rows // Expect all keys in all but two rows
expectedRows = this.numRows - 2; expectedRows = numRows - 2;
expectedKeys = this.colsPerRow; expectedKeys = colsPerRow;
f = new RowFilter(CompareOp.GREATER, f = new RowFilter(CompareOp.GREATER,
new BinaryComparator(Bytes.toBytes("testRowOne-2"))); new BinaryComparator(Bytes.toBytes("testRowOne-2")));
s = new Scan(); s = new Scan();
@ -758,11 +777,12 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
}; };
verifyScanFull(s, kvs); verifyScanFull(s, kvs);
} }
void doTestValueFilter() throws Exception { @Test
public void testValueFilter() throws Exception {
// Match group one rows // Match group one rows
long expectedRows = this.numRows / 2; long expectedRows = numRows / 2;
long expectedKeys = this.colsPerRow; long expectedKeys = colsPerRow;
Filter f = new ValueFilter(CompareOp.EQUAL, Filter f = new ValueFilter(CompareOp.EQUAL,
new BinaryComparator(Bytes.toBytes("testValueOne"))); new BinaryComparator(Bytes.toBytes("testValueOne")));
Scan s = new Scan(); Scan s = new Scan();
@ -770,27 +790,27 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
verifyScanNoEarlyOut(s, expectedRows, expectedKeys); verifyScanNoEarlyOut(s, expectedRows, expectedKeys);
// Match group two rows // Match group two rows
expectedRows = this.numRows / 2; expectedRows = numRows / 2;
expectedKeys = this.colsPerRow; expectedKeys = colsPerRow;
f = new ValueFilter(CompareOp.EQUAL, f = new ValueFilter(CompareOp.EQUAL,
new BinaryComparator(Bytes.toBytes("testValueTwo"))); new BinaryComparator(Bytes.toBytes("testValueTwo")));
s = new Scan(); s = new Scan();
s.setFilter(f); s.setFilter(f);
verifyScanNoEarlyOut(s, expectedRows, expectedKeys); verifyScanNoEarlyOut(s, expectedRows, expectedKeys);
// Match all values using regex // Match all values using regex
expectedRows = this.numRows; expectedRows = numRows;
expectedKeys = this.colsPerRow; expectedKeys = colsPerRow;
f = new ValueFilter(CompareOp.EQUAL, f = new ValueFilter(CompareOp.EQUAL,
new RegexStringComparator("testValue((One)|(Two))")); new RegexStringComparator("testValue((One)|(Two))"));
s = new Scan(); s = new Scan();
s.setFilter(f); s.setFilter(f);
verifyScanNoEarlyOut(s, expectedRows, expectedKeys); verifyScanNoEarlyOut(s, expectedRows, expectedKeys);
// Match values less than // Match values less than
// Expect group one rows // Expect group one rows
expectedRows = this.numRows / 2; expectedRows = numRows / 2;
expectedKeys = this.colsPerRow; expectedKeys = colsPerRow;
f = new ValueFilter(CompareOp.LESS, f = new ValueFilter(CompareOp.LESS,
new BinaryComparator(Bytes.toBytes("testValueTwo"))); new BinaryComparator(Bytes.toBytes("testValueTwo")));
s = new Scan(); s = new Scan();
@ -799,8 +819,8 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
// Match values less than or equal // Match values less than or equal
// Expect all rows // Expect all rows
expectedRows = this.numRows; expectedRows = numRows;
expectedKeys = this.colsPerRow; expectedKeys = colsPerRow;
f = new ValueFilter(CompareOp.LESS_OR_EQUAL, f = new ValueFilter(CompareOp.LESS_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("testValueTwo"))); new BinaryComparator(Bytes.toBytes("testValueTwo")));
s = new Scan(); s = new Scan();
@ -809,8 +829,8 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
// Match values less than or equal // Match values less than or equal
// Expect group one rows // Expect group one rows
expectedRows = this.numRows / 2; expectedRows = numRows / 2;
expectedKeys = this.colsPerRow; expectedKeys = colsPerRow;
f = new ValueFilter(CompareOp.LESS_OR_EQUAL, f = new ValueFilter(CompareOp.LESS_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("testValueOne"))); new BinaryComparator(Bytes.toBytes("testValueOne")));
s = new Scan(); s = new Scan();
@ -819,8 +839,8 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
// Match values not equal // Match values not equal
// Expect half the rows // Expect half the rows
expectedRows = this.numRows / 2; expectedRows = numRows / 2;
expectedKeys = this.colsPerRow; expectedKeys = colsPerRow;
f = new ValueFilter(CompareOp.NOT_EQUAL, f = new ValueFilter(CompareOp.NOT_EQUAL,
new BinaryComparator(Bytes.toBytes("testValueOne"))); new BinaryComparator(Bytes.toBytes("testValueOne")));
s = new Scan(); s = new Scan();
@ -829,8 +849,8 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
// Match values greater or equal // Match values greater or equal
// Expect all rows // Expect all rows
expectedRows = this.numRows; expectedRows = numRows;
expectedKeys = this.colsPerRow; expectedKeys = colsPerRow;
f = new ValueFilter(CompareOp.GREATER_OR_EQUAL, f = new ValueFilter(CompareOp.GREATER_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("testValueOne"))); new BinaryComparator(Bytes.toBytes("testValueOne")));
s = new Scan(); s = new Scan();
@ -839,8 +859,8 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
// Match values greater // Match values greater
// Expect half rows // Expect half rows
expectedRows = this.numRows / 2; expectedRows = numRows / 2;
expectedKeys = this.colsPerRow; expectedKeys = colsPerRow;
f = new ValueFilter(CompareOp.GREATER, f = new ValueFilter(CompareOp.GREATER,
new BinaryComparator(Bytes.toBytes("testValueOne"))); new BinaryComparator(Bytes.toBytes("testValueOne")));
s = new Scan(); s = new Scan();
@ -880,8 +900,9 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
}; };
verifyScanFull(s, kvs); verifyScanFull(s, kvs);
} }
void doTestSkipFilter() throws Exception { @Test
public void testSkipFilter() throws Exception {
// Test for qualifier regex: "testQualifierOne-2" // Test for qualifier regex: "testQualifierOne-2"
// Should only get rows from second group, and all keys // Should only get rows from second group, and all keys
Filter f = new SkipFilter(new QualifierFilter(CompareOp.NOT_EQUAL, Filter f = new SkipFilter(new QualifierFilter(CompareOp.NOT_EQUAL,
@ -914,8 +935,9 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
}; };
verifyScanFull(s, kvs); verifyScanFull(s, kvs);
} }
void doTestFilterList() throws Exception { @Test
public void testFilterList() throws Exception {
// Test getting a single row, single key using Row, Qualifier, and Value // Test getting a single row, single key using Row, Qualifier, and Value
// regular expression and substring filters // regular expression and substring filters
// Use must pass all // Use must pass all
@ -947,10 +969,11 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
f = new FilterList(Operator.MUST_PASS_ONE, filters); f = new FilterList(Operator.MUST_PASS_ONE, filters);
s = new Scan(); s = new Scan();
s.setFilter(f); s.setFilter(f);
verifyScanNoEarlyOut(s, this.numRows, this.colsPerRow); verifyScanNoEarlyOut(s, numRows, colsPerRow);
} }
void doTestFirstKeyOnlyFilter() throws Exception { @Test
public void testFirstKeyOnlyFilter() throws Exception {
Scan s = new Scan(); Scan s = new Scan();
s.setFilter(new FirstKeyOnlyFilter()); s.setFilter(new FirstKeyOnlyFilter());
// Expected KVs, the first KV from each of the remaining 6 rows // Expected KVs, the first KV from each of the remaining 6 rows
@ -964,17 +987,4 @@ public class TestScannersWithFilters extends HBaseRESTClusterTestBase {
}; };
verifyScanFull(s, kvs); verifyScanFull(s, kvs);
} }
public void testScannersWithFilters() throws Exception {
doTestNoFilter();
doTestPrefixFilter();
doTestPageFilter();
doTestInclusiveStopFilter();
doTestQualifierFilter();
doTestRowFilter();
doTestValueFilter();
doTestSkipFilter();
doTestFilterList();
doTestFirstKeyOnlyFilter();
}
} }

View File

@ -27,6 +27,7 @@ import java.io.StringWriter;
import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.rest.client.Client; import org.apache.hadoop.hbase.rest.client.Client;
import org.apache.hadoop.hbase.rest.client.Cluster; import org.apache.hadoop.hbase.rest.client.Cluster;
@ -36,59 +37,70 @@ import org.apache.hadoop.hbase.rest.model.TableSchemaModel;
import org.apache.hadoop.hbase.rest.model.TestTableSchemaModel; import org.apache.hadoop.hbase.rest.model.TestTableSchemaModel;
import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Bytes;
public class TestSchemaResource extends HBaseRESTClusterTestBase { import static org.junit.Assert.*;
static String TABLE1 = "TestSchemaResource1"; import org.junit.AfterClass;
static String TABLE2 = "TestSchemaResource2"; import org.junit.BeforeClass;
import org.junit.Test;
Client client; public class TestSchemaResource {
JAXBContext context; private static String TABLE1 = "TestSchemaResource1";
HBaseAdmin admin; private static String TABLE2 = "TestSchemaResource2";
@Override private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
protected void setUp() throws Exception { private static final HBaseRESTTestingUtility REST_TEST_UTIL =
super.setUp(); new HBaseRESTTestingUtility(TEST_UTIL.getConfiguration());
private static Client client;
private static JAXBContext context;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
TEST_UTIL.startMiniCluster(3);
REST_TEST_UTIL.startServletContainer();
client = new Client(new Cluster().add("localhost",
REST_TEST_UTIL.getServletPort()));
context = JAXBContext.newInstance( context = JAXBContext.newInstance(
ColumnSchemaModel.class, ColumnSchemaModel.class,
TableSchemaModel.class); TableSchemaModel.class);
admin = new HBaseAdmin(conf);
client = new Client(new Cluster().add("localhost", testServletPort));
} }
@Override @AfterClass
protected void tearDown() throws Exception { public static void tearDownAfterClass() throws Exception {
client.shutdown(); REST_TEST_UTIL.shutdownServletContainer();
super.tearDown(); TEST_UTIL.shutdownMiniCluster();
} }
byte[] toXML(TableSchemaModel model) throws JAXBException { private static byte[] toXML(TableSchemaModel model) throws JAXBException {
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
context.createMarshaller().marshal(model, writer); context.createMarshaller().marshal(model, writer);
return Bytes.toBytes(writer.toString()); return Bytes.toBytes(writer.toString());
} }
TableSchemaModel fromXML(byte[] content) throws JAXBException { private static TableSchemaModel fromXML(byte[] content)
throws JAXBException {
return (TableSchemaModel) context.createUnmarshaller() return (TableSchemaModel) context.createUnmarshaller()
.unmarshal(new ByteArrayInputStream(content)); .unmarshal(new ByteArrayInputStream(content));
} }
void doTestTableCreateAndDeleteXML() throws IOException, JAXBException { @Test
public void testTableCreateAndDeleteXML() throws IOException, JAXBException {
String schemaPath = "/" + TABLE1 + "/schema"; String schemaPath = "/" + TABLE1 + "/schema";
TableSchemaModel model; TableSchemaModel model;
Response response; Response response;
HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
assertFalse(admin.tableExists(TABLE1)); assertFalse(admin.tableExists(TABLE1));
// create the table // create the table
model = TestTableSchemaModel.buildTestModel(TABLE1); model = TestTableSchemaModel.buildTestModel(TABLE1);
TestTableSchemaModel.checkModel(model, TABLE1); TestTableSchemaModel.checkModel(model, TABLE1);
response = client.put(schemaPath, MIMETYPE_XML, toXML(model)); response = client.put(schemaPath, Constants.MIMETYPE_XML, toXML(model));
assertEquals(response.getCode(), 201); assertEquals(response.getCode(), 201);
// make sure HBase concurs, and wait for the table to come online // make sure HBase concurs, and wait for the table to come online
admin.enableTable(TABLE1); admin.enableTable(TABLE1);
// retrieve the schema and validate it // retrieve the schema and validate it
response = client.get(schemaPath, MIMETYPE_XML); response = client.get(schemaPath, Constants.MIMETYPE_XML);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
model = fromXML(response.getBody()); model = fromXML(response.getBody());
TestTableSchemaModel.checkModel(model, TABLE1); TestTableSchemaModel.checkModel(model, TABLE1);
@ -100,11 +112,13 @@ public class TestSchemaResource extends HBaseRESTClusterTestBase {
assertFalse(admin.tableExists(TABLE1)); assertFalse(admin.tableExists(TABLE1));
} }
void doTestTableCreateAndDeletePB() throws IOException, JAXBException { @Test
public void testTableCreateAndDeletePB() throws IOException, JAXBException {
String schemaPath = "/" + TABLE2 + "/schema"; String schemaPath = "/" + TABLE2 + "/schema";
TableSchemaModel model; TableSchemaModel model;
Response response; Response response;
HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
assertFalse(admin.tableExists(TABLE2)); assertFalse(admin.tableExists(TABLE2));
// create the table // create the table
@ -130,9 +144,4 @@ public class TestSchemaResource extends HBaseRESTClusterTestBase {
// make sure HBase concurs // make sure HBase concurs
assertFalse(admin.tableExists(TABLE2)); assertFalse(admin.tableExists(TABLE2));
} }
public void testSchemaResource() throws Exception {
doTestTableCreateAndDeleteXML();
doTestTableCreateAndDeletePB();
}
} }

View File

@ -26,34 +26,29 @@ import java.io.IOException;
import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.rest.client.Client; import org.apache.hadoop.hbase.rest.client.Client;
import org.apache.hadoop.hbase.rest.client.Cluster; import org.apache.hadoop.hbase.rest.client.Cluster;
import org.apache.hadoop.hbase.rest.client.Response; import org.apache.hadoop.hbase.rest.client.Response;
import org.apache.hadoop.hbase.rest.model.StorageClusterStatusModel; import org.apache.hadoop.hbase.rest.model.StorageClusterStatusModel;
import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Bytes;
public class TestStatusResource extends HBaseRESTClusterTestBase { import static org.junit.Assert.*;
static final byte[] ROOT_REGION_NAME = Bytes.toBytes("-ROOT-,,0"); import org.junit.AfterClass;
static final byte[] META_REGION_NAME = Bytes.toBytes(".META.,,1"); import org.junit.BeforeClass;
import org.junit.Test;
Client client; public class TestStatusResource {
JAXBContext context; private static final byte[] ROOT_REGION_NAME = Bytes.toBytes("-ROOT-,,0");
private static final byte[] META_REGION_NAME = Bytes.toBytes(".META.,,1");
private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
private static final HBaseRESTTestingUtility REST_TEST_UTIL =
new HBaseRESTTestingUtility(TEST_UTIL.getConfiguration());
private static Client client;
private static JAXBContext context;
@Override private static void validate(StorageClusterStatusModel model) {
protected void setUp() throws Exception {
super.setUp();
context = JAXBContext.newInstance(
StorageClusterStatusModel.class);
client = new Client(new Cluster().add("localhost", testServletPort));
}
@Override
protected void tearDown() throws Exception {
client.shutdown();
super.tearDown();
}
void validate(StorageClusterStatusModel model) {
assertNotNull(model); assertNotNull(model);
assertTrue(model.getRegions() >= 2); assertTrue(model.getRegions() >= 2);
assertTrue(model.getRequests() >= 0); assertTrue(model.getRequests() >= 0);
@ -80,25 +75,38 @@ public class TestStatusResource extends HBaseRESTClusterTestBase {
assertTrue(foundMeta); assertTrue(foundMeta);
} }
void doTestGetClusterStatusXML() throws IOException, JAXBException { @BeforeClass
Response response = client.get("/status/cluster", MIMETYPE_XML); public static void setUpBeforeClass() throws Exception {
TEST_UTIL.startMiniCluster(2); // some tests depend on having only 2 RS
REST_TEST_UTIL.startServletContainer();
client = new Client(new Cluster().add("localhost",
REST_TEST_UTIL.getServletPort()));
context = JAXBContext.newInstance(StorageClusterStatusModel.class);
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
REST_TEST_UTIL.shutdownServletContainer();
TEST_UTIL.shutdownMiniCluster();
}
@Test
public void testGetClusterStatusXML() throws IOException, JAXBException {
Response response = client.get("/status/cluster", Constants.MIMETYPE_XML);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
StorageClusterStatusModel model = (StorageClusterStatusModel) StorageClusterStatusModel model = (StorageClusterStatusModel)
context.createUnmarshaller().unmarshal( context.createUnmarshaller().unmarshal(
new ByteArrayInputStream(response.getBody())); new ByteArrayInputStream(response.getBody()));
validate(model); validate(model);
} }
void doTestGetClusterStatusPB() throws IOException { @Test
Response response = client.get("/status/cluster", MIMETYPE_PROTOBUF); public void testGetClusterStatusPB() throws IOException {
Response response = client.get("/status/cluster",
Constants.MIMETYPE_PROTOBUF);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
StorageClusterStatusModel model = new StorageClusterStatusModel(); StorageClusterStatusModel model = new StorageClusterStatusModel();
model.getObjectFromMessage(response.getBody()); model.getObjectFromMessage(response.getBody());
validate(model); validate(model);
} }
public void testStatusResource() throws Exception {
doTestGetClusterStatusXML();
doTestGetClusterStatusPB();
}
} }

View File

@ -31,6 +31,7 @@ import javax.xml.bind.JAXBException;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HRegionInfo; import org.apache.hadoop.hbase.HRegionInfo;
import org.apache.hadoop.hbase.HServerAddress; import org.apache.hadoop.hbase.HServerAddress;
@ -49,35 +50,44 @@ import org.apache.hadoop.hbase.rest.model.TableRegionModel;
import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.util.StringUtils; import org.apache.hadoop.util.StringUtils;
public class TestTableResource extends HBaseRESTClusterTestBase { import static org.junit.Assert.*;
static final Log LOG = LogFactory.getLog(TestTableResource.class); import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
static String TABLE = "TestTableResource"; public class TestTableResource {
static String COLUMN_FAMILY = "test"; private static final Log LOG = LogFactory.getLog(TestTableResource.class);
static String COLUMN = COLUMN_FAMILY + ":qualifier";
static Map<HRegionInfo,HServerAddress> regionMap;
Client client; private static String TABLE = "TestTableResource";
JAXBContext context; private static String COLUMN_FAMILY = "test";
HBaseAdmin admin; private static String COLUMN = COLUMN_FAMILY + ":qualifier";
private static Map<HRegionInfo,HServerAddress> regionMap;
@Override private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
protected void setUp() throws Exception { private static final HBaseRESTTestingUtility REST_TEST_UTIL =
super.setUp(); new HBaseRESTTestingUtility(TEST_UTIL.getConfiguration());
private static Client client;
private static JAXBContext context;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
TEST_UTIL.startMiniCluster(3);
REST_TEST_UTIL.startServletContainer();
client = new Client(new Cluster().add("localhost",
REST_TEST_UTIL.getServletPort()));
context = JAXBContext.newInstance( context = JAXBContext.newInstance(
TableModel.class, TableModel.class,
TableInfoModel.class, TableInfoModel.class,
TableListModel.class, TableListModel.class,
TableRegionModel.class); TableRegionModel.class);
client = new Client(new Cluster().add("localhost", testServletPort)); HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
admin = new HBaseAdmin(conf);
if (admin.tableExists(TABLE)) { if (admin.tableExists(TABLE)) {
return; return;
} }
HTableDescriptor htd = new HTableDescriptor(TABLE); HTableDescriptor htd = new HTableDescriptor(TABLE);
htd.addFamily(new HColumnDescriptor(COLUMN_FAMILY)); htd.addFamily(new HColumnDescriptor(COLUMN_FAMILY));
admin.createTable(htd); admin.createTable(htd);
HTable table = new HTable(conf, TABLE); HTable table = new HTable(TEST_UTIL.getConfiguration(), TABLE);
byte[] k = new byte[3]; byte[] k = new byte[3];
byte [][] famAndQf = KeyValue.parseColumn(Bytes.toBytes(COLUMN)); byte [][] famAndQf = KeyValue.parseColumn(Bytes.toBytes(COLUMN));
for (byte b1 = 'a'; b1 < 'z'; b1++) { for (byte b1 = 'a'; b1 < 'z'; b1++) {
@ -112,13 +122,13 @@ public class TestTableResource extends HBaseRESTClusterTestBase {
LOG.info("regions: " + regionMap); LOG.info("regions: " + regionMap);
} }
@Override @AfterClass
protected void tearDown() throws Exception { public static void tearDownAfterClass() throws Exception {
client.shutdown(); REST_TEST_UTIL.shutdownServletContainer();
super.tearDown(); TEST_UTIL.shutdownMiniCluster();
} }
void checkTableList(TableListModel model) { private static void checkTableList(TableListModel model) {
boolean found = false; boolean found = false;
Iterator<TableModel> tables = model.getTables().iterator(); Iterator<TableModel> tables = model.getTables().iterator();
assertTrue(tables.hasNext()); assertTrue(tables.hasNext());
@ -132,34 +142,7 @@ public class TestTableResource extends HBaseRESTClusterTestBase {
assertTrue(found); assertTrue(found);
} }
void doTestTableListText() throws IOException { void checkTableInfo(TableInfoModel model) {
Response response = client.get("/", MIMETYPE_TEXT);
assertEquals(response.getCode(), 200);
}
void doTestTableListXML() throws IOException, JAXBException {
Response response = client.get("/", MIMETYPE_XML);
assertEquals(response.getCode(), 200);
TableListModel model = (TableListModel)
context.createUnmarshaller()
.unmarshal(new ByteArrayInputStream(response.getBody()));
checkTableList(model);
}
void doTestTableListJSON() throws IOException {
Response response = client.get("/", MIMETYPE_JSON);
assertEquals(response.getCode(), 200);
}
void doTestTableListPB() throws IOException, JAXBException {
Response response = client.get("/", MIMETYPE_PROTOBUF);
assertEquals(response.getCode(), 200);
TableListModel model = new TableListModel();
model.getObjectFromMessage(response.getBody());
checkTableList(model);
}
public void checkTableInfo(TableInfoModel model) {
assertEquals(model.getName(), TABLE); assertEquals(model.getName(), TABLE);
Iterator<TableRegionModel> regions = model.getRegions().iterator(); Iterator<TableRegionModel> regions = model.getRegions().iterator();
assertTrue(regions.hasNext()); assertTrue(regions.hasNext());
@ -188,13 +171,48 @@ public class TestTableResource extends HBaseRESTClusterTestBase {
} }
} }
void doTestTableInfoText() throws IOException { @Test
Response response = client.get("/" + TABLE + "/regions", MIMETYPE_TEXT); public void testTableListText() throws IOException {
Response response = client.get("/", Constants.MIMETYPE_TEXT);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
} }
void doTestTableInfoXML() throws IOException, JAXBException { @Test
Response response = client.get("/" + TABLE + "/regions", MIMETYPE_XML); public void testTableListXML() throws IOException, JAXBException {
Response response = client.get("/", Constants.MIMETYPE_XML);
assertEquals(response.getCode(), 200);
TableListModel model = (TableListModel)
context.createUnmarshaller()
.unmarshal(new ByteArrayInputStream(response.getBody()));
checkTableList(model);
}
@Test
public void testTableListJSON() throws IOException {
Response response = client.get("/", Constants.MIMETYPE_JSON);
assertEquals(response.getCode(), 200);
}
@Test
public void testTableListPB() throws IOException, JAXBException {
Response response = client.get("/", Constants.MIMETYPE_PROTOBUF);
assertEquals(response.getCode(), 200);
TableListModel model = new TableListModel();
model.getObjectFromMessage(response.getBody());
checkTableList(model);
}
@Test
public void testTableInfoText() throws IOException {
Response response = client.get("/" + TABLE + "/regions",
Constants.MIMETYPE_TEXT);
assertEquals(response.getCode(), 200);
}
@Test
public void testTableInfoXML() throws IOException, JAXBException {
Response response = client.get("/" + TABLE + "/regions",
Constants.MIMETYPE_XML);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
TableInfoModel model = (TableInfoModel) TableInfoModel model = (TableInfoModel)
context.createUnmarshaller() context.createUnmarshaller()
@ -202,28 +220,20 @@ public class TestTableResource extends HBaseRESTClusterTestBase {
checkTableInfo(model); checkTableInfo(model);
} }
void doTestTableInfoJSON() throws IOException { @Test
Response response = client.get("/" + TABLE + "/regions", MIMETYPE_JSON); public void testTableInfoJSON() throws IOException {
Response response = client.get("/" + TABLE + "/regions",
Constants.MIMETYPE_JSON);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
} }
void doTestTableInfoPB() throws IOException, JAXBException { @Test
Response response = public void testTableInfoPB() throws IOException, JAXBException {
client.get("/" + TABLE + "/regions", MIMETYPE_PROTOBUF); Response response = client.get("/" + TABLE + "/regions",
Constants.MIMETYPE_PROTOBUF);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
TableInfoModel model = new TableInfoModel(); TableInfoModel model = new TableInfoModel();
model.getObjectFromMessage(response.getBody()); model.getObjectFromMessage(response.getBody());
checkTableInfo(model); checkTableInfo(model);
} }
public void testTableResource() throws Exception {
doTestTableListText();
doTestTableListXML();
doTestTableListJSON();
doTestTableListPB();
doTestTableInfoText();
doTestTableInfoXML();
doTestTableInfoJSON();
doTestTableInfoPB();
}
} }

View File

@ -20,6 +20,7 @@
package org.apache.hadoop.hbase.rest; package org.apache.hadoop.hbase.rest;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.Get;
@ -31,25 +32,34 @@ import org.apache.hadoop.hbase.rest.client.Cluster;
import org.apache.hadoop.hbase.rest.client.Response; import org.apache.hadoop.hbase.rest.client.Response;
import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Bytes;
public class BROKE_TestTransform extends HBaseRESTClusterTestBase { import static org.junit.Assert.*;
static final String TABLE = "TestTransform"; import org.junit.AfterClass;
static final String CFA = "a"; import org.junit.BeforeClass;
static final String CFB = "b"; import org.junit.Test;
static final String COLUMN_1 = CFA + ":1";
static final String COLUMN_2 = CFB + ":2";
static final String ROW_1 = "testrow1";
static final byte[] VALUE_1 = Bytes.toBytes("testvalue1");
static final byte[] VALUE_2 = Bytes.toBytes("testvalue2");
static final byte[] VALUE_2_BASE64 = Bytes.toBytes("dGVzdHZhbHVlMg==");
Client client; public class TestTransform {
HBaseAdmin admin; private static final String TABLE = "TestTransform";
private static final String CFA = "a";
private static final String CFB = "b";
private static final String COLUMN_1 = CFA + ":1";
private static final String COLUMN_2 = CFB + ":2";
private static final String ROW_1 = "testrow1";
private static final byte[] VALUE_1 = Bytes.toBytes("testvalue1");
private static final byte[] VALUE_2 = Bytes.toBytes("testvalue2");
private static final byte[] VALUE_2_BASE64 = Bytes.toBytes("dGVzdHZhbHVlMg==");
@Override private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
protected void setUp() throws Exception { private static final HBaseRESTTestingUtility REST_TEST_UTIL =
super.setUp(); new HBaseRESTTestingUtility(TEST_UTIL.getConfiguration());
client = new Client(new Cluster().add("localhost", testServletPort)); private static Client client;
admin = new HBaseAdmin(conf);
@BeforeClass
public static void setUpBeforeClass() throws Exception {
TEST_UTIL.startMiniCluster(3);
REST_TEST_UTIL.startServletContainer();
client = new Client(new Cluster().add("localhost",
REST_TEST_UTIL.getServletPort()));
HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
if (admin.tableExists(TABLE)) { if (admin.tableExists(TABLE)) {
return; return;
} }
@ -61,22 +71,23 @@ public class BROKE_TestTransform extends HBaseRESTClusterTestBase {
admin.createTable(htd); admin.createTable(htd);
} }
@Override @AfterClass
protected void tearDown() throws Exception { public static void tearDownAfterClass() throws Exception {
client.shutdown(); REST_TEST_UTIL.shutdownServletContainer();
super.tearDown(); TEST_UTIL.shutdownMiniCluster();
} }
@Test
public void testTransform() throws Exception { public void testTransform() throws Exception {
String path1 = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1; String path1 = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
String path2 = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_2; String path2 = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_2;
// store value 1 // store value 1
Response response = client.put(path1, MIMETYPE_BINARY, VALUE_1); Response response = client.put(path1, Constants.MIMETYPE_BINARY, VALUE_1);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
// store value 2 (stargate should transform into base64) // store value 2 (stargate should transform into base64)
response = client.put(path2, MIMETYPE_BINARY, VALUE_2); response = client.put(path2, Constants.MIMETYPE_BINARY, VALUE_2);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
// get the table contents directly // get the table contents directly
@ -96,7 +107,7 @@ public class BROKE_TestTransform extends HBaseRESTClusterTestBase {
table.close(); table.close();
// stargate should decode the transformed value back to original bytes // stargate should decode the transformed value back to original bytes
response = client.get(path2, MIMETYPE_BINARY); response = client.get(path2, Constants.MIMETYPE_BINARY);
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
value = response.getBody(); value = response.getBody();
assertTrue(Bytes.equals(value, VALUE_2)); assertTrue(Bytes.equals(value, VALUE_2));

View File

@ -28,6 +28,7 @@ import javax.xml.bind.JAXBException;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.rest.client.Client; import org.apache.hadoop.hbase.rest.client.Client;
import org.apache.hadoop.hbase.rest.client.Cluster; import org.apache.hadoop.hbase.rest.client.Cluster;
import org.apache.hadoop.hbase.rest.client.Response; import org.apache.hadoop.hbase.rest.client.Response;
@ -35,34 +36,40 @@ import org.apache.hadoop.hbase.rest.model.StorageClusterVersionModel;
import org.apache.hadoop.hbase.rest.model.VersionModel; import org.apache.hadoop.hbase.rest.model.VersionModel;
import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Bytes;
import static org.junit.Assert.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.sun.jersey.spi.container.servlet.ServletContainer; import com.sun.jersey.spi.container.servlet.ServletContainer;
public class TestVersionResource extends HBaseRESTClusterTestBase { public class TestVersionResource {
static final Log LOG = LogFactory.getLog(TestVersionResource.class); private static final Log LOG = LogFactory.getLog(TestVersionResource.class);
Client client; private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
JAXBContext context; private static final HBaseRESTTestingUtility REST_TEST_UTIL =
new HBaseRESTTestingUtility(TEST_UTIL.getConfiguration());
private static Client client;
private static JAXBContext context;
public TestVersionResource() throws JAXBException { @BeforeClass
super(); public static void setUpBeforeClass() throws Exception {
TEST_UTIL.startMiniCluster(3);
REST_TEST_UTIL.startServletContainer();
client = new Client(new Cluster().add("localhost",
REST_TEST_UTIL.getServletPort()));
context = JAXBContext.newInstance( context = JAXBContext.newInstance(
VersionModel.class, VersionModel.class,
StorageClusterVersionModel.class); StorageClusterVersionModel.class);
} }
@Override @AfterClass
protected void setUp() throws Exception { public static void tearDownAfterClass() throws Exception {
super.setUp(); REST_TEST_UTIL.shutdownServletContainer();
client = new Client(new Cluster().add("localhost", testServletPort)); TEST_UTIL.shutdownMiniCluster();
} }
@Override private static void validate(VersionModel model) {
protected void tearDown() throws Exception {
client.shutdown();
super.tearDown();
}
void validate(VersionModel model) {
assertNotNull(model); assertNotNull(model);
assertNotNull(model.getRESTVersion()); assertNotNull(model.getRESTVersion());
assertEquals(model.getRESTVersion(), RESTServlet.VERSION_STRING); assertEquals(model.getRESTVersion(), RESTServlet.VERSION_STRING);
@ -83,8 +90,9 @@ public class TestVersionResource extends HBaseRESTClusterTestBase {
.getImplementationVersion()); .getImplementationVersion());
} }
void doTestGetStargateVersionText() throws IOException { @Test
Response response = client.get("/version", MIMETYPE_TEXT); public void testGetStargateVersionText() throws IOException {
Response response = client.get("/version", Constants.MIMETYPE_TEXT);
assertTrue(response.getCode() == 200); assertTrue(response.getCode() == 200);
String body = Bytes.toString(response.getBody()); String body = Bytes.toString(response.getBody());
assertTrue(body.length() > 0); assertTrue(body.length() > 0);
@ -99,8 +107,9 @@ public class TestVersionResource extends HBaseRESTClusterTestBase {
.getImplementationVersion())); .getImplementationVersion()));
} }
void doTestGetStargateVersionXML() throws IOException, JAXBException { @Test
Response response = client.get("/version", MIMETYPE_XML); public void testGetStargateVersionXML() throws IOException, JAXBException {
Response response = client.get("/version", Constants.MIMETYPE_XML);
assertTrue(response.getCode() == 200); assertTrue(response.getCode() == 200);
VersionModel model = (VersionModel) VersionModel model = (VersionModel)
context.createUnmarshaller().unmarshal( context.createUnmarshaller().unmarshal(
@ -109,13 +118,15 @@ public class TestVersionResource extends HBaseRESTClusterTestBase {
LOG.info("success retrieving Stargate version as XML"); LOG.info("success retrieving Stargate version as XML");
} }
void doTestGetStargateVersionJSON() throws IOException { @Test
Response response = client.get("/version", MIMETYPE_JSON); public void testGetStargateVersionJSON() throws IOException {
Response response = client.get("/version", Constants.MIMETYPE_JSON);
assertTrue(response.getCode() == 200); assertTrue(response.getCode() == 200);
} }
void doTestGetStargateVersionPB() throws IOException { @Test
Response response = client.get("/version", MIMETYPE_PROTOBUF); public void testGetStargateVersionPB() throws IOException {
Response response = client.get("/version", Constants.MIMETYPE_PROTOBUF);
assertTrue(response.getCode() == 200); assertTrue(response.getCode() == 200);
VersionModel model = new VersionModel(); VersionModel model = new VersionModel();
model.getObjectFromMessage(response.getBody()); model.getObjectFromMessage(response.getBody());
@ -123,14 +134,17 @@ public class TestVersionResource extends HBaseRESTClusterTestBase {
LOG.info("success retrieving Stargate version as protobuf"); LOG.info("success retrieving Stargate version as protobuf");
} }
void doTestGetStorageClusterVersionText() throws IOException { @Test
Response response = client.get("/version/cluster", MIMETYPE_TEXT); public void testGetStorageClusterVersionText() throws IOException {
Response response = client.get("/version/cluster",
Constants.MIMETYPE_TEXT);
assertTrue(response.getCode() == 200); assertTrue(response.getCode() == 200);
} }
void doTestGetStorageClusterVersionXML() throws IOException, @Test
public void testGetStorageClusterVersionXML() throws IOException,
JAXBException { JAXBException {
Response response = client.get("/version/cluster", MIMETYPE_XML); Response response = client.get("/version/cluster",Constants.MIMETYPE_XML);
assertTrue(response.getCode() == 200); assertTrue(response.getCode() == 200);
StorageClusterVersionModel clusterVersionModel = StorageClusterVersionModel clusterVersionModel =
(StorageClusterVersionModel) (StorageClusterVersionModel)
@ -141,18 +155,9 @@ public class TestVersionResource extends HBaseRESTClusterTestBase {
LOG.info("success retrieving storage cluster version as XML"); LOG.info("success retrieving storage cluster version as XML");
} }
void doTestGetStorageClusterVersionJSON() throws IOException { @Test
Response response = client.get("/version/cluster", MIMETYPE_JSON); public void doTestGetStorageClusterVersionJSON() throws IOException {
Response response = client.get("/version/cluster", Constants.MIMETYPE_JSON);
assertTrue(response.getCode() == 200); assertTrue(response.getCode() == 200);
} }
public void testVersionResource() throws Exception {
doTestGetStargateVersionText();
doTestGetStargateVersionXML();
doTestGetStargateVersionJSON();
doTestGetStargateVersionPB();
doTestGetStorageClusterVersionText();
doTestGetStorageClusterVersionXML();
doTestGetStorageClusterVersionJSON();
}
} }

View File

@ -20,18 +20,24 @@
package org.apache.hadoop.hbase.rest.client; package org.apache.hadoop.hbase.rest.client;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.rest.HBaseRESTClusterTestBase; import org.apache.hadoop.hbase.rest.HBaseRESTTestingUtility;
import org.apache.hadoop.hbase.rest.client.Client; import org.apache.hadoop.hbase.rest.client.Client;
import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Bytes;
public class TestRemoteAdmin extends HBaseRESTClusterTestBase { import static org.junit.Assert.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
static final String TABLE_1 = "TestRemoteAdmin_Table_1"; public class TestRemoteAdmin {
static final String TABLE_2 = "TestRemoteAdmin_Table_2";
static final byte[] COLUMN_1 = Bytes.toBytes("a"); private static final String TABLE_1 = "TestRemoteAdmin_Table_1";
private static final String TABLE_2 = "TestRemoteAdmin_Table_2";
private static final byte[] COLUMN_1 = Bytes.toBytes("a");
static final HTableDescriptor DESC_1; static final HTableDescriptor DESC_1;
static { static {
@ -44,17 +50,20 @@ public class TestRemoteAdmin extends HBaseRESTClusterTestBase {
DESC_2.addFamily(new HColumnDescriptor(COLUMN_1)); DESC_2.addFamily(new HColumnDescriptor(COLUMN_1));
} }
Client client; private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
HBaseAdmin localAdmin; private static final HBaseRESTTestingUtility REST_TEST_UTIL =
RemoteAdmin remoteAdmin; new HBaseRESTTestingUtility(TEST_UTIL.getConfiguration());
private static HBaseAdmin localAdmin;
private static RemoteAdmin remoteAdmin;
@Override @BeforeClass
protected void setUp() throws Exception { public static void setUpBeforeClass() throws Exception {
super.setUp(); TEST_UTIL.startMiniCluster(3);
localAdmin = new HBaseAdmin(conf); REST_TEST_UTIL.startServletContainer();
localAdmin = TEST_UTIL.getHBaseAdmin();
remoteAdmin = new RemoteAdmin(new Client( remoteAdmin = new RemoteAdmin(new Client(
new Cluster().add("localhost", testServletPort)), new Cluster().add("localhost", REST_TEST_UTIL.getServletPort())),
conf); TEST_UTIL.getConfiguration());
if (localAdmin.tableExists(TABLE_1)) { if (localAdmin.tableExists(TABLE_1)) {
localAdmin.disableTable(TABLE_1); localAdmin.disableTable(TABLE_1);
localAdmin.deleteTable(TABLE_1); localAdmin.deleteTable(TABLE_1);
@ -64,17 +73,20 @@ public class TestRemoteAdmin extends HBaseRESTClusterTestBase {
} }
} }
@Override @AfterClass
protected void tearDown() throws Exception { public static void tearDownAfterClass() throws Exception {
super.tearDown(); REST_TEST_UTIL.shutdownServletContainer();
TEST_UTIL.shutdownMiniCluster();
} }
@Test
public void testCreateTable() throws Exception { public void testCreateTable() throws Exception {
assertFalse(remoteAdmin.isTableAvailable(TABLE_1)); assertFalse(remoteAdmin.isTableAvailable(TABLE_1));
remoteAdmin.createTable(DESC_1); remoteAdmin.createTable(DESC_1);
assertTrue(remoteAdmin.isTableAvailable(TABLE_1)); assertTrue(remoteAdmin.isTableAvailable(TABLE_1));
} }
@Test
public void testDeleteTable() throws Exception { public void testDeleteTable() throws Exception {
assertTrue(remoteAdmin.isTableAvailable(TABLE_2)); assertTrue(remoteAdmin.isTableAvailable(TABLE_2));
remoteAdmin.deleteTable(TABLE_2); remoteAdmin.deleteTable(TABLE_2);

View File

@ -26,6 +26,7 @@ import java.util.List;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.KeyValue;
@ -37,51 +38,57 @@ import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.rest.HBaseRESTClusterTestBase; import org.apache.hadoop.hbase.rest.HBaseRESTTestingUtility;
import org.apache.hadoop.hbase.rest.client.Client; import org.apache.hadoop.hbase.rest.client.Client;
import org.apache.hadoop.hbase.rest.client.Cluster; import org.apache.hadoop.hbase.rest.client.Cluster;
import org.apache.hadoop.hbase.rest.client.RemoteHTable; import org.apache.hadoop.hbase.rest.client.RemoteHTable;
import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Bytes;
public class TestRemoteTable extends HBaseRESTClusterTestBase { import static org.junit.Assert.*;
private static final Log LOG = LogFactory.getLog(HBaseRESTClusterTestBase.class); import org.junit.AfterClass;
static final String TABLE = "TestRemoteTable"; import org.junit.BeforeClass;
static final byte[] ROW_1 = Bytes.toBytes("testrow1"); import org.junit.Test;
static final byte[] ROW_2 = Bytes.toBytes("testrow2");
static final byte[] ROW_3 = Bytes.toBytes("testrow3");
static final byte[] ROW_4 = Bytes.toBytes("testrow4");
static final byte[] COLUMN_1 = Bytes.toBytes("a");
static final byte[] COLUMN_2 = Bytes.toBytes("b");
static final byte[] COLUMN_3 = Bytes.toBytes("c");
static final byte[] QUALIFIER_1 = Bytes.toBytes("1");
static final byte[] QUALIFIER_2 = Bytes.toBytes("2");
static final byte[] QUALIFIER_3 = Bytes.toBytes("3");
static final byte[] VALUE_1 = Bytes.toBytes("testvalue1");
static final byte[] VALUE_2 = Bytes.toBytes("testvalue2");
static final byte[] VALUE_3 = Bytes.toBytes("testvalue3");
static final long ONE_HOUR = 60 * 60 * 1000; public class TestRemoteTable {
static final long TS_2 = System.currentTimeMillis(); private static final Log LOG = LogFactory.getLog(TestRemoteTable.class);
static final long TS_1 = TS_2 - ONE_HOUR; private static final String TABLE = "TestRemoteTable";
private static final byte[] ROW_1 = Bytes.toBytes("testrow1");
private static final byte[] ROW_2 = Bytes.toBytes("testrow2");
private static final byte[] ROW_3 = Bytes.toBytes("testrow3");
private static final byte[] ROW_4 = Bytes.toBytes("testrow4");
private static final byte[] COLUMN_1 = Bytes.toBytes("a");
private static final byte[] COLUMN_2 = Bytes.toBytes("b");
private static final byte[] COLUMN_3 = Bytes.toBytes("c");
private static final byte[] QUALIFIER_1 = Bytes.toBytes("1");
private static final byte[] QUALIFIER_2 = Bytes.toBytes("2");
private static final byte[] VALUE_1 = Bytes.toBytes("testvalue1");
private static final byte[] VALUE_2 = Bytes.toBytes("testvalue2");
Client client; private static final long ONE_HOUR = 60 * 60 * 1000;
HBaseAdmin admin; private static final long TS_2 = System.currentTimeMillis();
RemoteHTable remoteTable; private static final long TS_1 = TS_2 - ONE_HOUR;
@Override private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
protected void setUp() throws Exception { private static final HBaseRESTTestingUtility REST_TEST_UTIL =
super.setUp(); new HBaseRESTTestingUtility(TEST_UTIL.getConfiguration());
private static RemoteHTable remoteTable;
admin = new HBaseAdmin(conf);
LOG.info("Admin Connection=" + admin.getConnection() + ", " + admin.getConnection().getZooKeeperWatcher()); @BeforeClass
public static void setUpBeforeClass() throws Exception {
TEST_UTIL.startMiniCluster(3);
REST_TEST_UTIL.startServletContainer();
HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
LOG.info("Admin Connection=" + admin.getConnection() + ", " +
admin.getConnection().getZooKeeperWatcher());
if (!admin.tableExists(TABLE)) { if (!admin.tableExists(TABLE)) {
HTableDescriptor htd = new HTableDescriptor(TABLE); HTableDescriptor htd = new HTableDescriptor(TABLE);
htd.addFamily(new HColumnDescriptor(COLUMN_1)); htd.addFamily(new HColumnDescriptor(COLUMN_1));
htd.addFamily(new HColumnDescriptor(COLUMN_2)); htd.addFamily(new HColumnDescriptor(COLUMN_2));
htd.addFamily(new HColumnDescriptor(COLUMN_3)); htd.addFamily(new HColumnDescriptor(COLUMN_3));
admin.createTable(htd); admin.createTable(htd);
HTable table = new HTable(conf, TABLE); HTable table = new HTable(TEST_UTIL.getConfiguration(), TABLE);
LOG.info("Table connection=" + table.getConnection() + ", " + admin.getConnection().getZooKeeperWatcher()); LOG.info("Table connection=" + table.getConnection() + ", " +
admin.getConnection().getZooKeeperWatcher());
Put put = new Put(ROW_1); Put put = new Put(ROW_1);
put.add(COLUMN_1, QUALIFIER_1, TS_2, VALUE_1); put.add(COLUMN_1, QUALIFIER_1, TS_2, VALUE_1);
table.put(put); table.put(put);
@ -93,21 +100,26 @@ public class TestRemoteTable extends HBaseRESTClusterTestBase {
table.flushCommits(); table.flushCommits();
} }
remoteTable = new RemoteHTable( remoteTable = new RemoteHTable(
new Client(new Cluster().add("localhost", testServletPort)), new Client(new Cluster().add("localhost",
conf, TABLE, null); REST_TEST_UTIL.getServletPort())),
TEST_UTIL.getConfiguration(), TABLE, null);
} }
@Override @AfterClass
protected void tearDown() throws Exception { public static void tearDownAfterClass() throws Exception {
remoteTable.close(); remoteTable.close();
super.tearDown(); REST_TEST_UTIL.shutdownServletContainer();
TEST_UTIL.shutdownMiniCluster();
} }
@Test
public void testGetTableDescriptor() throws IOException { public void testGetTableDescriptor() throws IOException {
HTableDescriptor local = new HTable(conf, TABLE).getTableDescriptor(); HTableDescriptor local = new HTable(TEST_UTIL.getConfiguration(),
TABLE).getTableDescriptor();
assertEquals(remoteTable.getTableDescriptor(), local); assertEquals(remoteTable.getTableDescriptor(), local);
} }
@Test
public void testGet() throws IOException { public void testGet() throws IOException {
Get get = new Get(ROW_1); Get get = new Get(ROW_1);
Result result = remoteTable.get(get); Result result = remoteTable.get(get);
@ -210,6 +222,7 @@ public class TestRemoteTable extends HBaseRESTClusterTestBase {
assertEquals(2, count); assertEquals(2, count);
} }
@Test
public void testPut() throws IOException { public void testPut() throws IOException {
Put put = new Put(ROW_3); Put put = new Put(ROW_3);
put.add(COLUMN_1, QUALIFIER_1, VALUE_1); put.add(COLUMN_1, QUALIFIER_1, VALUE_1);