diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/client/RemoteHTable.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/client/RemoteHTable.java index d170ac381cc..1fbf56a66a0 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/client/RemoteHTable.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/client/RemoteHTable.java @@ -124,7 +124,7 @@ public class RemoteHTable implements HTableInterface { } } } - if (startTime != 0 && endTime != Long.MAX_VALUE) { + if (startTime >= 0 && endTime != Long.MAX_VALUE) { sb.append('/'); sb.append(startTime); if (startTime != endTime) { diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/CellModel.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/CellModel.java index 5eb60eb92b8..4736e30fed0 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/CellModel.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/CellModel.java @@ -22,6 +22,8 @@ package org.apache.hadoop.hbase.rest.model; import java.io.IOException; import java.io.Serializable; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlValue; @@ -33,6 +35,7 @@ import org.apache.hadoop.hbase.rest.ProtobufMessageHandler; import org.apache.hadoop.hbase.rest.protobuf.generated.CellMessage.Cell; import com.google.protobuf.ByteString; +import org.codehaus.jackson.annotate.JsonProperty; /** * Representation of a cell. A cell is a single value associated a column and @@ -54,12 +57,21 @@ import com.google.protobuf.ByteString; * */ @XmlRootElement(name="Cell") +@XmlAccessorType(XmlAccessType.FIELD) @InterfaceAudience.Private public class CellModel implements ProtobufMessageHandler, Serializable { private static final long serialVersionUID = 1L; - private long timestamp = HConstants.LATEST_TIMESTAMP; + @JsonProperty("column") + @XmlAttribute private byte[] column; + + @JsonProperty("timestamp") + @XmlAttribute + private long timestamp = HConstants.LATEST_TIMESTAMP; + + @JsonProperty("$") + @XmlValue private byte[] value; /** @@ -123,7 +135,6 @@ public class CellModel implements ProtobufMessageHandler, Serializable { /** * @return the column */ - @XmlAttribute public byte[] getColumn() { return column; } @@ -146,7 +157,6 @@ public class CellModel implements ProtobufMessageHandler, Serializable { /** * @return the timestamp */ - @XmlAttribute public long getTimestamp() { return timestamp; } @@ -161,7 +171,6 @@ public class CellModel implements ProtobufMessageHandler, Serializable { /** * @return the value */ - @XmlValue public byte[] getValue() { return value; } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/CellSetModel.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/CellSetModel.java index bdfe3bf716b..03c7b9c9846 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/CellSetModel.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/CellSetModel.java @@ -24,6 +24,8 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlElement; @@ -69,11 +71,13 @@ import com.google.protobuf.ByteString; * */ @XmlRootElement(name="CellSet") +@XmlAccessorType(XmlAccessType.FIELD) @InterfaceAudience.Private public class CellSetModel implements Serializable, ProtobufMessageHandler { private static final long serialVersionUID = 1L; - + + @XmlElement(name="Row") private List rows; /** @@ -102,7 +106,6 @@ public class CellSetModel implements Serializable, ProtobufMessageHandler { /** * @return the rows */ - @XmlElement(name="Row") public List getRows() { return rows; } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/ColumnSchemaModel.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/ColumnSchemaModel.java index ed50639f03b..e7dc05d032e 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/ColumnSchemaModel.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/ColumnSchemaModel.java @@ -31,6 +31,8 @@ import javax.xml.namespace.QName; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HConstants; +import org.codehaus.jackson.annotate.JsonAnyGetter; +import org.codehaus.jackson.annotate.JsonAnySetter; /** * Representation of a column family schema. @@ -67,6 +69,7 @@ public class ColumnSchemaModel implements Serializable { * @param name the attribute name * @param value the attribute value */ + @JsonAnySetter public void addAttribute(String name, Object value) { attrs.put(new QName(name), value); } @@ -92,6 +95,7 @@ public class ColumnSchemaModel implements Serializable { * @return the map for holding unspecified (user) attributes */ @XmlAnyAttribute + @JsonAnyGetter public Map getAny() { return attrs; } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/RowModel.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/RowModel.java index 03d11b27b4b..a38e5dd2243 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/RowModel.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/RowModel.java @@ -24,12 +24,15 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.hbase.rest.ProtobufMessageHandler; +import org.codehaus.jackson.annotate.JsonProperty; /** * Representation of a row. A row is a related set of cells, grouped by common @@ -47,13 +50,20 @@ import org.apache.hadoop.hbase.rest.ProtobufMessageHandler; * */ @XmlRootElement(name="Row") +@XmlAccessorType(XmlAccessType.FIELD) @InterfaceAudience.Private public class RowModel implements ProtobufMessageHandler, Serializable { private static final long serialVersionUID = 1L; + @JsonProperty("key") + @XmlAttribute private byte[] key; + + @JsonProperty("Cell") + @XmlElement(name="Cell") private List cells = new ArrayList(); + /** * Default constructor */ @@ -106,7 +116,6 @@ public class RowModel implements ProtobufMessageHandler, Serializable { /** * @return the row key */ - @XmlAttribute public byte[] getKey() { return key; } @@ -121,7 +130,6 @@ public class RowModel implements ProtobufMessageHandler, Serializable { /** * @return the cells */ - @XmlElement(name="Cell") public List getCells() { return cells; } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/StorageClusterVersionModel.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/StorageClusterVersionModel.java index 29ca515069e..df50a36a5cf 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/StorageClusterVersionModel.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/StorageClusterVersionModel.java @@ -19,6 +19,8 @@ package org.apache.hadoop.hbase.rest.model; +import org.codehaus.jackson.annotate.JsonValue; + import java.io.Serializable; import javax.xml.bind.annotation.XmlRootElement; @@ -60,8 +62,17 @@ public class StorageClusterVersionModel implements Serializable { /* (non-Javadoc) * @see java.lang.Object#toString() */ + @JsonValue @Override public String toString() { return version; } + + //needed for jackson deserialization + private static StorageClusterVersionModel valueOf(String value) { + StorageClusterVersionModel versionModel + = new StorageClusterVersionModel(); + versionModel.setVersion(value); + return versionModel; + } } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/TableSchemaModel.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/TableSchemaModel.java index 592e31a5f22..7812c602017 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/TableSchemaModel.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/model/TableSchemaModel.java @@ -43,6 +43,9 @@ import org.apache.hadoop.hbase.rest.ProtobufMessageHandler; import org.apache.hadoop.hbase.rest.protobuf.generated.ColumnSchemaMessage.ColumnSchema; import org.apache.hadoop.hbase.rest.protobuf.generated.TableSchemaMessage.TableSchema; import org.apache.hadoop.hbase.util.Bytes; +import org.codehaus.jackson.annotate.JsonAnyGetter; +import org.codehaus.jackson.annotate.JsonAnySetter; +import org.codehaus.jackson.annotate.JsonIgnore; /** * A representation of HBase table descriptors. @@ -107,6 +110,7 @@ public class TableSchemaModel implements Serializable, ProtobufMessageHandler { * @param name attribute name * @param value attribute value */ + @JsonAnySetter public void addAttribute(String name, Object value) { attrs.put(new QName(name), value); } @@ -151,6 +155,7 @@ public class TableSchemaModel implements Serializable, ProtobufMessageHandler { * @return the map for holding unspecified (user) attributes */ @XmlAnyAttribute + @JsonAnyGetter public Map getAny() { return attrs; } @@ -337,6 +342,7 @@ public class TableSchemaModel implements Serializable, ProtobufMessageHandler { /** * @return a table descriptor */ + @JsonIgnore public HTableDescriptor getTableDescriptor() { HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(getName())); for (Map.Entry e: getAny().entrySet()) { diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/provider/JacksonProvider.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/provider/JacksonProvider.java new file mode 100644 index 00000000000..7791d024ad7 --- /dev/null +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/rest/provider/JacksonProvider.java @@ -0,0 +1,31 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.rest.provider; + +import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider; + +import javax.ws.rs.ext.Provider; + +//create a class in the defined resource package name +//so it gets activated +//Use jackson to take care of json +//since it has better support for object +//deserializaiton and less clunky to deal with +@Provider +public class JacksonProvider extends JacksonJaxbJsonProvider { +} diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/TestSchemaResource.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/TestSchemaResource.java index a25cd2072f5..8397d0dfebc 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/TestSchemaResource.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/TestSchemaResource.java @@ -56,6 +56,7 @@ public class TestSchemaResource { private static Client client; private static JAXBContext context; private static Configuration conf; + private static TestTableSchemaModel testTableSchemaModel; @BeforeClass public static void setUpBeforeClass() throws Exception { @@ -64,6 +65,7 @@ public class TestSchemaResource { REST_TEST_UTIL.startServletContainer(conf); client = new Client(new Cluster().add("localhost", REST_TEST_UTIL.getServletPort())); + testTableSchemaModel = new TestTableSchemaModel(); context = JAXBContext.newInstance( ColumnSchemaModel.class, TableSchemaModel.class); @@ -97,8 +99,8 @@ public class TestSchemaResource { assertFalse(admin.tableExists(TABLE1)); // create the table - model = TestTableSchemaModel.buildTestModel(TABLE1); - TestTableSchemaModel.checkModel(model, TABLE1); + model = testTableSchemaModel.buildTestModel(TABLE1); + testTableSchemaModel.checkModel(model, TABLE1); response = client.put(schemaPath, Constants.MIMETYPE_XML, toXML(model)); assertEquals(response.getCode(), 201); @@ -112,7 +114,14 @@ public class TestSchemaResource { assertEquals(response.getCode(), 200); assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type")); model = fromXML(response.getBody()); - TestTableSchemaModel.checkModel(model, TABLE1); + testTableSchemaModel.checkModel(model, TABLE1); + + // with json retrieve the schema and validate it + response = client.get(schemaPath, Constants.MIMETYPE_JSON); + assertEquals(response.getCode(), 200); + assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type")); + model = testTableSchemaModel.fromJSON(Bytes.toString(response.getBody())); + testTableSchemaModel.checkModel(model, TABLE1); // delete the table client.delete(schemaPath); @@ -134,8 +143,8 @@ public class TestSchemaResource { assertFalse(admin.tableExists(TABLE2)); // create the table - model = TestTableSchemaModel.buildTestModel(TABLE2); - TestTableSchemaModel.checkModel(model, TABLE2); + model = testTableSchemaModel.buildTestModel(TABLE2); + testTableSchemaModel.checkModel(model, TABLE2); response = client.put(schemaPath, Constants.MIMETYPE_PROTOBUF, model.createProtobufOutput()); assertEquals(response.getCode(), 201); @@ -152,7 +161,7 @@ public class TestSchemaResource { assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type")); model = new TableSchemaModel(); model.getObjectFromMessage(response.getBody()); - TestTableSchemaModel.checkModel(model, TABLE2); + testTableSchemaModel.checkModel(model, TABLE2); // retrieve the schema and validate it with alternate pbuf type response = client.get(schemaPath, Constants.MIMETYPE_PROTOBUF_IETF); @@ -160,7 +169,7 @@ public class TestSchemaResource { assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type")); model = new TableSchemaModel(); model.getObjectFromMessage(response.getBody()); - TestTableSchemaModel.checkModel(model, TABLE2); + testTableSchemaModel.checkModel(model, TABLE2); // delete the table client.delete(schemaPath); diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/TestStatusResource.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/TestStatusResource.java index 0a7b9a6c16a..c9aa1915816 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/TestStatusResource.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/TestStatusResource.java @@ -53,7 +53,7 @@ public class TestStatusResource { private static void validate(StorageClusterStatusModel model) { assertNotNull(model); - assertTrue(model.getRegions() >= 1); + assertTrue(model.getRegions() + ">= 1", model.getRegions() >= 1); assertTrue(model.getRequests() >= 0); assertTrue(model.getAverageLoad() >= 0.0); assertNotNull(model.getLiveNodes()); diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestCellModel.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestCellModel.java index 77400406463..170dfabb667 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestCellModel.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestCellModel.java @@ -26,6 +26,7 @@ import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; +import com.sun.jersey.api.json.JSONJAXBContext; import org.apache.hadoop.hbase.SmallTests; import org.apache.hadoop.hbase.util.Base64; import org.apache.hadoop.hbase.util.Bytes; @@ -34,28 +35,25 @@ import junit.framework.TestCase; import org.junit.experimental.categories.Category; @Category(SmallTests.class) -public class TestCellModel extends TestCase { +public class TestCellModel extends TestModelBase { private static final long TIMESTAMP = 1245219839331L; private static final byte[] COLUMN = Bytes.toBytes("testcolumn"); private static final byte[] VALUE = Bytes.toBytes("testvalue"); - private static final String AS_XML = - "" + - "dGVzdHZhbHVl"; + public TestCellModel() throws Exception { + super(CellModel.class); + AS_XML = + "dGVzdHZhbHVl"; + AS_PB = + "Egp0ZXN0Y29sdW1uGOO6i+eeJCIJdGVzdHZhbHVl"; - private static final String AS_PB = - "Egp0ZXN0Y29sdW1uGOO6i+eeJCIJdGVzdHZhbHVl"; - - private JAXBContext context; - - public TestCellModel() throws JAXBException { - super(); - context = JAXBContext.newInstance(CellModel.class); + AS_JSON = + "{\"column\":\"dGVzdGNvbHVtbg==\",\"timestamp\":1245219839331,\"$\":\"dGVzdHZhbHVl\"}"; } - private CellModel buildTestModel() { + protected CellModel buildTestModel() { CellModel model = new CellModel(); model.setColumn(COLUMN); model.setTimestamp(TIMESTAMP); @@ -63,29 +61,7 @@ public class TestCellModel extends TestCase { return model; } - @SuppressWarnings("unused") - private String toXML(CellModel model) throws JAXBException { - StringWriter writer = new StringWriter(); - context.createMarshaller().marshal(model, writer); - return writer.toString(); - } - - private CellModel fromXML(String xml) throws JAXBException { - return (CellModel) - context.createUnmarshaller().unmarshal(new StringReader(xml)); - } - - @SuppressWarnings("unused") - private byte[] toPB(CellModel model) { - return model.createProtobufOutput(); - } - - private CellModel fromPB(String pb) throws IOException { - return (CellModel) - new CellModel().getObjectFromMessage(Base64.decode(AS_PB)); - } - - private void checkModel(CellModel model) { + protected void checkModel(CellModel model) { assertTrue(Bytes.equals(model.getColumn(), COLUMN)); assertTrue(Bytes.equals(model.getValue(), VALUE)); assertTrue(model.hasUserTimestamp()); diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestCellSetModel.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestCellSetModel.java index 848c0aa9768..716da14d05f 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestCellSetModel.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestCellSetModel.java @@ -35,7 +35,7 @@ import junit.framework.TestCase; import org.junit.experimental.categories.Category; @Category(SmallTests.class) -public class TestCellSetModel extends TestCase { +public class TestCellSetModel extends TestModelBase { private static final byte[] ROW1 = Bytes.toBytes("testrow1"); private static final byte[] COLUMN1 = Bytes.toBytes("testcolumn1"); @@ -49,36 +49,46 @@ public class TestCellSetModel extends TestCase { private static final byte[] VALUE3 = Bytes.toBytes("testvalue3"); private static final long TIMESTAMP3 = 1245393318192L; - private static final String AS_XML = - "" + - "" + - "" + - "dGVzdHZhbHVlMQ==" + - "" + - "" + - "" + - "dGVzdHZhbHVlMg==" + - "" + - "dGVzdHZhbHVlMw==" + - "" + - ""; + public TestCellSetModel() throws Exception { + super(CellSetModel.class); + AS_XML = + "" + + "" + + "" + + "dGVzdHZhbHVlMQ==" + + "" + + "" + + "" + + "dGVzdHZhbHVlMg==" + + "" + + "dGVzdHZhbHVlMw==" + + "" + + ""; - private static final String AS_PB = - "CiwKCHRlc3Ryb3cxEiASC3Rlc3Rjb2x1bW4xGOO6i+eeJCIKdGVzdHZhbHVlMQpOCgh0ZXN0cm93" + - "MRIgEgt0ZXN0Y29sdW1uMhjHyc7wniQiCnRlc3R2YWx1ZTISIBILdGVzdGNvbHVtbjMYsOLnuZ8k" + - "Igp0ZXN0dmFsdWUz"; + AS_PB = + "CiwKCHRlc3Ryb3cxEiASC3Rlc3Rjb2x1bW4xGOO6i+eeJCIKdGVzdHZhbHVlMQpOCgh0ZXN0cm93" + + "MRIgEgt0ZXN0Y29sdW1uMhjHyc7wniQiCnRlc3R2YWx1ZTISIBILdGVzdGNvbHVtbjMYsOLnuZ8k" + + "Igp0ZXN0dmFsdWUz"; - private JAXBContext context; + AS_XML = + "" + + "" + + "dGVzdHZhbHVlMQ==" + + "" + + "dGVzdHZhbHVlMg==" + + "dGVzdHZhbHVlMw==" + + ""; - public TestCellSetModel() throws JAXBException { - super(); - context = JAXBContext.newInstance( - CellModel.class, - CellSetModel.class, - RowModel.class); + AS_JSON = + "{\"Row\":[{\"key\":\"dGVzdHJvdzE=\"," + + "\"Cell\":[{\"column\":\"dGVzdGNvbHVtbjE=\",\"timestamp\":1245219839331," + + "\"$\":\"dGVzdHZhbHVlMQ==\"}]},{\"key\":\"dGVzdHJvdzE=\"," + + "\"Cell\":[{\"column\":\"dGVzdGNvbHVtbjI=\",\"timestamp\":1245239813319," + + "\"$\":\"dGVzdHZhbHVlMg==\"},{\"column\":\"dGVzdGNvbHVtbjM=\"," + + "\"timestamp\":1245393318192,\"$\":\"dGVzdHZhbHVlMw==\"}]}]}"; } - private CellSetModel buildTestModel() { + protected CellSetModel buildTestModel() { CellSetModel model = new CellSetModel(); RowModel row; row = new RowModel(); @@ -93,29 +103,7 @@ public class TestCellSetModel extends TestCase { return model; } - @SuppressWarnings("unused") - private String toXML(CellSetModel model) throws JAXBException { - StringWriter writer = new StringWriter(); - context.createMarshaller().marshal(model, writer); - return writer.toString(); - } - - private CellSetModel fromXML(String xml) throws JAXBException { - return (CellSetModel) - context.createUnmarshaller().unmarshal(new StringReader(xml)); - } - - @SuppressWarnings("unused") - private byte[] toPB(CellSetModel model) { - return model.createProtobufOutput(); - } - - private CellSetModel fromPB(String pb) throws IOException { - return (CellSetModel) - new CellSetModel().getObjectFromMessage(Base64.decode(AS_PB)); - } - - private void checkModel(CellSetModel model) { + protected void checkModel(CellSetModel model) { Iterator rows = model.getRows().iterator(); RowModel row = rows.next(); assertTrue(Bytes.equals(ROW1, row.getKey())); diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestColumnSchemaModel.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestColumnSchemaModel.java index 8e55e0b4392..cb022d12c05 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestColumnSchemaModel.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestColumnSchemaModel.java @@ -27,10 +27,12 @@ import javax.xml.bind.JAXBException; import junit.framework.TestCase; import org.apache.hadoop.hbase.SmallTests; +import org.junit.Ignore; +import org.junit.Test; import org.junit.experimental.categories.Category; @Category(SmallTests.class) -public class TestColumnSchemaModel extends TestCase { +public class TestColumnSchemaModel extends TestModelBase { protected static final String COLUMN_NAME = "testcolumn"; protected static final boolean BLOCKCACHE = true; @@ -41,24 +43,20 @@ public class TestColumnSchemaModel extends TestCase { protected static final int TTL = 86400; protected static final int VERSIONS = 1; - protected static final String AS_XML = - ""; + public TestColumnSchemaModel() throws Exception { + super(ColumnSchemaModel.class); + AS_XML = + ""; - private JAXBContext context; - - public TestColumnSchemaModel() throws JAXBException { - super(); - context = JAXBContext.newInstance(ColumnSchemaModel.class); + AS_JSON = + "{\"name\":\"testcolumn\",\"BLOCKSIZE\":\"16384\",\"BLOOMFILTER\":\"NONE\"," + + "\"BLOCKCACHE\":\"true\",\"COMPRESSION\":\"GZ\",\"VERSIONS\":\"1\"," + + "\"TTL\":\"86400\",\"IN_MEMORY\":\"false\"}"; } - protected static ColumnSchemaModel buildTestModel() { + protected ColumnSchemaModel buildTestModel() { ColumnSchemaModel model = new ColumnSchemaModel(); model.setName(COLUMN_NAME); model.__setBlockcache(BLOCKCACHE); @@ -71,19 +69,7 @@ public class TestColumnSchemaModel extends TestCase { return model; } - @SuppressWarnings("unused") - private String toXML(ColumnSchemaModel model) throws JAXBException { - StringWriter writer = new StringWriter(); - context.createMarshaller().marshal(model, writer); - return writer.toString(); - } - - private ColumnSchemaModel fromXML(String xml) throws JAXBException { - return (ColumnSchemaModel) - context.createUnmarshaller().unmarshal(new StringReader(xml)); - } - - protected static void checkModel(ColumnSchemaModel model) { + protected void checkModel(ColumnSchemaModel model) { assertEquals(model.getName(), COLUMN_NAME); assertEquals(model.__getBlockcache(), BLOCKCACHE); assertEquals(model.__getBlocksize(), BLOCKSIZE); @@ -94,13 +80,7 @@ public class TestColumnSchemaModel extends TestCase { assertEquals(model.__getVersions(), VERSIONS); } - public void testBuildModel() throws Exception { - checkModel(buildTestModel()); + public void testFromPB() throws Exception { } - - public void testFromXML() throws Exception { - checkModel(fromXML(AS_XML)); - } - } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestModelBase.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestModelBase.java new file mode 100644 index 00000000000..5143cb3ca98 --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestModelBase.java @@ -0,0 +1,143 @@ +/* + * Copyright 2010 The Apache Software Foundation + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase.rest.model; + +import com.sun.jersey.api.json.JSONJAXBContext; +import junit.framework.TestCase; +import org.apache.hadoop.hbase.SmallTests; +import org.apache.hadoop.hbase.rest.ProtobufMessageHandler; +import org.apache.hadoop.hbase.rest.provider.JAXBContextResolver; +import org.apache.hadoop.hbase.util.Base64; +import org.apache.hadoop.hbase.util.Bytes; +import org.codehaus.jackson.JsonGenerationException; +import org.codehaus.jackson.JsonParseException; +import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider; +import org.codehaus.jackson.map.AnnotationIntrospector; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.map.SerializationConfig; +import org.codehaus.jackson.map.introspect.JacksonAnnotationIntrospector; +import org.codehaus.jackson.node.ObjectNode; +import org.codehaus.jackson.xc.JaxbAnnotationIntrospector; +import org.junit.experimental.categories.Category; + +import javax.ws.rs.core.MediaType; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; +import java.lang.reflect.InvocationTargetException; + +@Category(SmallTests.class) +public abstract class TestModelBase extends TestCase { + + protected String AS_XML; + + protected String AS_PB; + + protected String AS_JSON; + + protected JAXBContext context; + + protected Class clazz; + + protected ObjectMapper mapper; + + protected TestModelBase(Class clazz) throws Exception { + super(); + this.clazz = clazz; + context = new JAXBContextResolver().getContext(clazz); + mapper = new JacksonJaxbJsonProvider().locateMapper(clazz, + MediaType.APPLICATION_JSON_TYPE); + } + + protected abstract T buildTestModel(); + + @SuppressWarnings("unused") + protected String toXML(T model) throws JAXBException { + StringWriter writer = new StringWriter(); + context.createMarshaller().marshal(model, writer); + return writer.toString(); + } + + protected String toJSON(T model) throws JAXBException, IOException { + StringWriter writer = new StringWriter(); + mapper.writeValue(writer, model); +// original marshaller, uncomment this and comment mapper to verify backward compatibility +// ((JSONJAXBContext)context).createJSONMarshaller().marshallToJSON(model, writer); + return writer.toString(); + } + + public T fromJSON(String json) throws JAXBException, IOException { + return (T) + mapper.readValue(json, clazz); + } + + public T fromXML(String xml) throws JAXBException { + return (T) + context.createUnmarshaller().unmarshal(new StringReader(xml)); + } + + @SuppressWarnings("unused") + protected byte[] toPB(ProtobufMessageHandler model) { + return model.createProtobufOutput(); + } + + protected T fromPB(String pb) throws + Exception { + return (T)clazz.getMethod("getObjectFromMessage", byte[].class).invoke( + clazz.newInstance(), + Base64.decode(AS_PB)); + } + + protected abstract void checkModel(T model); + + public void testBuildModel() throws Exception { + checkModel(buildTestModel()); + } + + public void testFromPB() throws Exception { + checkModel(fromPB(AS_PB)); + } + + public void testFromXML() throws Exception { + checkModel(fromXML(AS_XML)); + } + + public void testToXML() throws Exception { + assertEquals(AS_XML, toXML(buildTestModel())); + } + + public void testToJSON() throws Exception { + try { + ObjectNode expObj = mapper.readValue(AS_JSON, ObjectNode.class); + ObjectNode actObj = mapper.readValue(toJSON(buildTestModel()), ObjectNode.class); + assertEquals(expObj, actObj); + } catch(Exception e) { + assertEquals(AS_JSON, toJSON(buildTestModel())); + } + } + + public void testFromJSON() throws Exception { + checkModel(fromJSON(AS_JSON)); + } +} + diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestRowModel.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestRowModel.java index fef51a8836f..e0068c832c5 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestRowModel.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestRowModel.java @@ -33,48 +33,34 @@ import junit.framework.TestCase; import org.junit.experimental.categories.Category; @Category(SmallTests.class) -public class TestRowModel extends TestCase { +public class TestRowModel extends TestModelBase { private static final byte[] ROW1 = Bytes.toBytes("testrow1"); private static final byte[] COLUMN1 = Bytes.toBytes("testcolumn1"); private static final byte[] VALUE1 = Bytes.toBytes("testvalue1"); private static final long TIMESTAMP1 = 1245219839331L; - private static final String AS_XML = - "" + - "" + - "dGVzdHZhbHVlMQ==" + - ""; - private JAXBContext context; - public TestRowModel() throws JAXBException { - super(); - context = JAXBContext.newInstance( - CellModel.class, - RowModel.class); + public TestRowModel() throws Exception { + super(RowModel.class); + AS_XML = + "" + + "dGVzdHZhbHVlMQ=="; + + AS_JSON = + "{\"key\":\"dGVzdHJvdzE=\",\"Cell\":[{\"column\":\"dGVzdGNvbHVtbjE=\"," + + "\"timestamp\":1245219839331,\"$\":\"dGVzdHZhbHVlMQ==\"}]}"; } - private RowModel buildTestModel() { + protected RowModel buildTestModel() { RowModel model = new RowModel(); model.setKey(ROW1); model.addCell(new CellModel(COLUMN1, TIMESTAMP1, VALUE1)); return model; } - @SuppressWarnings("unused") - private String toXML(RowModel model) throws JAXBException { - StringWriter writer = new StringWriter(); - context.createMarshaller().marshal(model, writer); - return writer.toString(); - } - - private RowModel fromXML(String xml) throws JAXBException { - return (RowModel) - context.createUnmarshaller().unmarshal(new StringReader(xml)); - } - - private void checkModel(RowModel model) { + protected void checkModel(RowModel model) { assertTrue(Bytes.equals(ROW1, model.getKey())); Iterator cells = model.getCells().iterator(); CellModel cell = cells.next(); @@ -85,13 +71,9 @@ public class TestRowModel extends TestCase { assertFalse(cells.hasNext()); } - public void testBuildModel() throws Exception { - checkModel(buildTestModel()); + @Override + public void testFromPB() throws Exception { + //do nothing row model has no PB } - - public void testFromXML() throws Exception { - checkModel(fromXML(AS_XML)); - } - } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestScannerModel.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestScannerModel.java index adc8af62bfc..2e2b304db96 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestScannerModel.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestScannerModel.java @@ -34,7 +34,7 @@ import junit.framework.TestCase; import org.junit.experimental.categories.Category; @Category(SmallTests.class) -public class TestScannerModel extends TestCase { +public class TestScannerModel extends TestModelBase { private static final byte[] START_ROW = Bytes.toBytes("abracadabra"); private static final byte[] END_ROW = Bytes.toBytes("zzyzx"); private static final byte[] COLUMN1 = Bytes.toBytes("column1"); @@ -44,29 +44,25 @@ public class TestScannerModel extends TestCase { private static final int CACHING = 1000; private static final int BATCH = 100; - private static final String AS_XML = - "" + - "Y29sdW1uMQ==" + - "Y29sdW1uMjpmb28=" + - ""; + public TestScannerModel() throws Exception { + super(ScannerModel.class); + AS_XML = + "" + + "" + + "Y29sdW1uMQ==Y29sdW1uMjpmb28="; - private static final String AS_PB = - "CgthYnJhY2FkYWJyYRIFenp5engaB2NvbHVtbjEaC2NvbHVtbjI6Zm9" + - "vIGQo47qL554kMLDi57mfJDj/////B0joBw=="; + AS_JSON = + "{\"batch\":100,\"caching\":1000,\"endRow\":\"enp5eng=\",\"endTime\":1245393318192,"+ + "\"maxVersions\":2147483647,\"startRow\":\"YWJyYWNhZGFicmE=\",\"startTime\":1245219839331,"+ + "\"column\":[\"Y29sdW1uMQ==\",\"Y29sdW1uMjpmb28=\"]}"; - private JAXBContext context; - - public TestScannerModel() throws JAXBException { - super(); - context = JAXBContext.newInstance(ScannerModel.class); + AS_PB = + "CgthYnJhY2FkYWJyYRIFenp5engaB2NvbHVtbjEaC2NvbHVtbjI6Zm9vIGQo47qL554kMLDi57mf" + + "JDj/////B0joBw=="; } - private ScannerModel buildTestModel() { + protected ScannerModel buildTestModel() { ScannerModel model = new ScannerModel(); model.setStartRow(START_ROW); model.setEndRow(END_ROW); @@ -79,29 +75,7 @@ public class TestScannerModel extends TestCase { return model; } - @SuppressWarnings("unused") - private String toXML(ScannerModel model) throws JAXBException { - StringWriter writer = new StringWriter(); - context.createMarshaller().marshal(model, writer); - return writer.toString(); - } - - private ScannerModel fromXML(String xml) throws JAXBException { - return (ScannerModel) - context.createUnmarshaller().unmarshal(new StringReader(xml)); - } - - @SuppressWarnings("unused") - private byte[] toPB(ScannerModel model) { - return model.createProtobufOutput(); - } - - private ScannerModel fromPB(String pb) throws IOException { - return (ScannerModel) - new ScannerModel().getObjectFromMessage(Base64.decode(AS_PB)); - } - - private void checkModel(ScannerModel model) { + protected void checkModel(ScannerModel model) { assertTrue(Bytes.equals(model.getStartRow(), START_ROW)); assertTrue(Bytes.equals(model.getEndRow(), END_ROW)); boolean foundCol1 = false, foundCol2 = false; @@ -120,16 +94,5 @@ public class TestScannerModel extends TestCase { assertEquals(model.getCaching(), CACHING); } - public void testBuildModel() throws Exception { - checkModel(buildTestModel()); - } - - public void testFromXML() throws Exception { - checkModel(fromXML(AS_XML)); - } - - public void testFromPB() throws Exception { - checkModel(fromPB(AS_PB)); - } } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestStorageClusterStatusModel.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestStorageClusterStatusModel.java index 964d3dbd888..0f852ca53f7 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestStorageClusterStatusModel.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestStorageClusterStatusModel.java @@ -19,60 +19,62 @@ package org.apache.hadoop.hbase.rest.model; -import java.io.IOException; -import java.io.StringReader; -import java.io.StringWriter; import java.util.Iterator; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; - import org.apache.hadoop.hbase.SmallTests; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.util.Base64; import org.apache.hadoop.hbase.util.Bytes; -import junit.framework.TestCase; import org.junit.experimental.categories.Category; @Category(SmallTests.class) -public class TestStorageClusterStatusModel extends TestCase { +public class TestStorageClusterStatusModel extends TestModelBase { - private static final String AS_XML = - "" + - "" + - "" + - "" + - "" + - ""+ - ""; + public TestStorageClusterStatusModel() throws Exception { + super(StorageClusterStatusModel.class); - private static final String AS_PB = - "Cj8KBXRlc3QxEOO6i+eeJBgAIIABKIAIMicKDWhiYXNlOnJvb3QsLDAQARgBIAAoADAAOAFAAkgB"+ - "UAFYAWABaAEKSwoFdGVzdDIQ/pKx8J4kGAAggAQogAgyMwoZaGJhc2U6bWV0YSwsMTI0NjAwMDA0"+ + AS_XML = + "" + + "" + + "" + + "" + + "" + + "" + + "" + + ""; + + AS_PB = + "Cj8KBXRlc3QxEOO6i+eeJBgAIIABKIAIMicKDWhiYXNlOnJvb3QsLDAQARgBIAAoADAAOAFAAkgB" + + "UAFYAWABaAEKSwoFdGVzdDIQ/pKx8J4kGAAggAQogAgyMwoZaGJhc2U6bWV0YSwsMTI0NjAwMDA0" + "MzcyNBABGAEgACgAMAA4AUACSAFQAVgBYAFoARgCIAApAAAAAAAA8D8="; - - private JAXBContext context; - public TestStorageClusterStatusModel() throws JAXBException { - super(); - context = JAXBContext.newInstance(StorageClusterStatusModel.class); + //Using jackson will break json backward compatibilty for this representation + //but the original one was broken as it would only print one Node element + //so the format itself was broken + AS_JSON = + "{\"regions\":2,\"requests\":0,\"averageLoad\":1.0,\"LiveNodes\":[{\"name\":\"test1\"," + + "\"Region\":[{\"name\":\"aGJhc2U6cm9vdCwsMA==\",\"stores\":1,\"storefiles\":1," + + "\"storefileSizeMB\":0,\"memstoreSizeMB\":0,\"storefileIndexSizeMB\":0," + + "\"readRequestsCount\":1,\"writeRequestsCount\":2,\"rootIndexSizeKB\":1," + + "\"totalStaticIndexSizeKB\":1,\"totalStaticBloomSizeKB\":1,\"totalCompactingKVs\":1," + + "\"currentCompactedKVs\":1}],\"requests\":0,\"startCode\":1245219839331," + + "\"heapSizeMB\":128,\"maxHeapSizeMB\":1024},{\"name\":\"test2\"," + + "\"Region\":[{\"name\":\"aGJhc2U6bWV0YSwsMTI0NjAwMDA0MzcyNA==\",\"stores\":1," + + "\"storefiles\":1,\"storefileSizeMB\":0,\"memstoreSizeMB\":0,\"storefileIndexSizeMB\":0," + + "\"readRequestsCount\":1,\"writeRequestsCount\":2,\"rootIndexSizeKB\":1," + + "\"totalStaticIndexSizeKB\":1,\"totalStaticBloomSizeKB\":1,\"totalCompactingKVs\":1," + + "\"currentCompactedKVs\":1}],\"requests\":0,\"startCode\":1245239331198," + + "\"heapSizeMB\":512,\"maxHeapSizeMB\":1024}],\"DeadNodes\":[]}"; } - private StorageClusterStatusModel buildTestModel() { + protected StorageClusterStatusModel buildTestModel() { StorageClusterStatusModel model = new StorageClusterStatusModel(); model.setRegions(2); model.setRequests(0); @@ -85,29 +87,7 @@ public class TestStorageClusterStatusModel extends TestCase { return model; } - @SuppressWarnings("unused") - private String toXML(StorageClusterStatusModel model) throws JAXBException { - StringWriter writer = new StringWriter(); - context.createMarshaller().marshal(model, writer); - return writer.toString(); - } - - private StorageClusterStatusModel fromXML(String xml) throws JAXBException { - return (StorageClusterStatusModel) - context.createUnmarshaller().unmarshal(new StringReader(xml)); - } - - @SuppressWarnings("unused") - private byte[] toPB(StorageClusterStatusModel model) { - return model.createProtobufOutput(); - } - - private StorageClusterStatusModel fromPB(String pb) throws IOException { - return (StorageClusterStatusModel) - new StorageClusterStatusModel().getObjectFromMessage(Base64.decode(AS_PB)); - } - - private void checkModel(StorageClusterStatusModel model) { + protected void checkModel(StorageClusterStatusModel model) { assertEquals(model.getRegions(), 2); assertEquals(model.getRequests(), 0); assertEquals(model.getAverageLoad(), 1.0); @@ -161,18 +141,5 @@ public class TestStorageClusterStatusModel extends TestCase { assertFalse(regions.hasNext()); assertFalse(nodes.hasNext()); } - - public void testBuildModel() throws Exception { - checkModel(buildTestModel()); - } - - public void testFromXML() throws Exception { - checkModel(fromXML(AS_XML)); - } - - public void testFromPB() throws Exception { - checkModel(fromPB(AS_PB)); - } - } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestStorageClusterVersionModel.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestStorageClusterVersionModel.java index 91395a8f746..bd4fa1f6cb7 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestStorageClusterVersionModel.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestStorageClusterVersionModel.java @@ -30,48 +30,31 @@ import org.apache.hadoop.hbase.SmallTests; import org.junit.experimental.categories.Category; @Category(SmallTests.class) -public class TestStorageClusterVersionModel extends TestCase { +public class TestStorageClusterVersionModel extends TestModelBase { private static final String VERSION = "0.0.1-testing"; - private static final String AS_XML = - "" + VERSION + ""; + public TestStorageClusterVersionModel() throws Exception { + super(StorageClusterVersionModel.class); + AS_XML = + ""+ + "" + VERSION + ""; - private JAXBContext context; - - public TestStorageClusterVersionModel() throws JAXBException { - super(); - context = JAXBContext.newInstance(StorageClusterVersionModel.class); + AS_JSON = "\"0.0.1-testing\""; } - private StorageClusterVersionModel buildTestModel() { + protected StorageClusterVersionModel buildTestModel() { StorageClusterVersionModel model = new StorageClusterVersionModel(); model.setVersion(VERSION); return model; } - @SuppressWarnings("unused") - private String toXML(StorageClusterVersionModel model) throws JAXBException { - StringWriter writer = new StringWriter(); - context.createMarshaller().marshal(model, writer); - return writer.toString(); - } - - private StorageClusterVersionModel fromXML(String xml) throws JAXBException { - return (StorageClusterVersionModel) - context.createUnmarshaller().unmarshal(new StringReader(xml)); - } - - private void checkModel(StorageClusterVersionModel model) { + protected void checkModel(StorageClusterVersionModel model) { assertEquals(model.getVersion(), VERSION); } - public void testBuildModel() throws Exception { - checkModel(buildTestModel()); + @Override + public void testFromPB() throws Exception { + //ignore test no pb } - - public void testFromXML() throws Exception { - checkModel(fromXML(AS_XML)); - } - } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestTableInfoModel.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestTableInfoModel.java index d76f6521bc0..dadb9adefcb 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestTableInfoModel.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestTableInfoModel.java @@ -35,65 +35,41 @@ import junit.framework.TestCase; import org.junit.experimental.categories.Category; @Category(SmallTests.class) -public class TestTableInfoModel extends TestCase { +public class TestTableInfoModel extends TestModelBase { private static final String TABLE = "testtable"; private static final byte[] START_KEY = Bytes.toBytes("abracadbra"); private static final byte[] END_KEY = Bytes.toBytes("zzyzx"); private static final long ID = 8731042424L; private static final String LOCATION = "testhost:9876"; - - private static final String AS_XML = - "" + - "" + - ""; - private static final String AS_PB = - "Cgl0ZXN0dGFibGUSSQofdGVzdHRhYmxlLGFicmFjYWRicmEsODczMTA0MjQyNBIKYWJyYWNhZGJy" + - "YRoFenp5engg+MSkwyAqDXRlc3Rob3N0Ojk4NzY="; + public TestTableInfoModel() throws Exception { + super(TableInfoModel.class); + AS_XML = + ""; - private JAXBContext context; + AS_PB = + "Cgl0ZXN0dGFibGUSSQofdGVzdHRhYmxlLGFicmFjYWRicmEsODczMTA0MjQyNBIKYWJyYWNhZGJy" + + "YRoFenp5engg+MSkwyAqDXRlc3Rob3N0Ojk4NzY="; - public TestTableInfoModel() throws JAXBException { - super(); - context = JAXBContext.newInstance( - TableInfoModel.class, - TableRegionModel.class); + AS_JSON = + "{\"name\":\"testtable\",\"Region\":[{\"endKey\":\"enp5eng=\",\"id\":8731042424," + + "\"location\":\"testhost:9876\",\"" + + "name\":\"testtable,abracadbra,8731042424.ad9860f031282c46ed431d7af8f94aca.\",\"" + + "startKey\":\"YWJyYWNhZGJyYQ==\"}]}"; } - private TableInfoModel buildTestModel() { + protected TableInfoModel buildTestModel() { TableInfoModel model = new TableInfoModel(); model.setName(TABLE); model.add(new TableRegionModel(TABLE, ID, START_KEY, END_KEY, LOCATION)); return model; } - @SuppressWarnings("unused") - private String toXML(TableInfoModel model) throws JAXBException { - StringWriter writer = new StringWriter(); - context.createMarshaller().marshal(model, writer); - return writer.toString(); - } - - private TableInfoModel fromXML(String xml) throws JAXBException { - return (TableInfoModel) - context.createUnmarshaller().unmarshal(new StringReader(xml)); - } - - @SuppressWarnings("unused") - private byte[] toPB(TableInfoModel model) { - return model.createProtobufOutput(); - } - - private TableInfoModel fromPB(String pb) throws IOException { - return (TableInfoModel) - new TableInfoModel().getObjectFromMessage(Base64.decode(AS_PB)); - } - - private void checkModel(TableInfoModel model) { + protected void checkModel(TableInfoModel model) { assertEquals(model.getName(), TABLE); Iterator regions = model.getRegions().iterator(); TableRegionModel region = regions.next(); diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestTableListModel.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestTableListModel.java index 6f19d905e5a..4cb91946a2a 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestTableListModel.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestTableListModel.java @@ -34,27 +34,24 @@ import junit.framework.TestCase; import org.junit.experimental.categories.Category; @Category(SmallTests.class) -public class TestTableListModel extends TestCase { +public class TestTableListModel extends TestModelBase { private static final String TABLE1 = "table1"; private static final String TABLE2 = "table2"; private static final String TABLE3 = "table3"; - - private static final String AS_XML = - "
" + - "
"; - private static final String AS_PB = "CgZ0YWJsZTEKBnRhYmxlMgoGdGFibGUz"; + public TestTableListModel() throws Exception { + super(TableListModel.class); + AS_XML = + "
"; - private JAXBContext context; + AS_PB = "CgZ0YWJsZTEKBnRhYmxlMgoGdGFibGUz"; - public TestTableListModel() throws JAXBException { - super(); - context = JAXBContext.newInstance( - TableListModel.class, - TableModel.class); + AS_JSON = + "{\"table\":[{\"name\":\"table1\"},{\"name\":\"table2\"},{\"name\":\"table3\"}]}"; } - private TableListModel buildTestModel() { + protected TableListModel buildTestModel() { TableListModel model = new TableListModel(); model.add(new TableModel(TABLE1)); model.add(new TableModel(TABLE2)); @@ -62,29 +59,7 @@ public class TestTableListModel extends TestCase { return model; } - @SuppressWarnings("unused") - private String toXML(TableListModel model) throws JAXBException { - StringWriter writer = new StringWriter(); - context.createMarshaller().marshal(model, writer); - return writer.toString(); - } - - private TableListModel fromXML(String xml) throws JAXBException { - return (TableListModel) - context.createUnmarshaller().unmarshal(new StringReader(xml)); - } - - @SuppressWarnings("unused") - private byte[] toPB(TableListModel model) { - return model.createProtobufOutput(); - } - - private TableListModel fromPB(String pb) throws IOException { - return (TableListModel) - new TableListModel().getObjectFromMessage(Base64.decode(AS_PB)); - } - - private void checkModel(TableListModel model) { + protected void checkModel(TableListModel model) { Iterator tables = model.getTables().iterator(); TableModel table = tables.next(); assertEquals(table.getName(), TABLE1); @@ -94,18 +69,5 @@ public class TestTableListModel extends TestCase { assertEquals(table.getName(), TABLE3); assertFalse(tables.hasNext()); } - - public void testBuildModel() throws Exception { - checkModel(buildTestModel()); - } - - public void testFromXML() throws Exception { - checkModel(fromXML(AS_XML)); - } - - public void testFromPB() throws Exception { - checkModel(fromPB(AS_PB)); - } - } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestTableRegionModel.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestTableRegionModel.java index 12b8684aebb..5c4b1a9384d 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestTableRegionModel.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestTableRegionModel.java @@ -32,46 +32,35 @@ import junit.framework.TestCase; import org.junit.experimental.categories.Category; @Category(SmallTests.class) -public class TestTableRegionModel extends TestCase { +public class TestTableRegionModel extends TestModelBase { private static final String TABLE = "testtable"; private static final byte[] START_KEY = Bytes.toBytes("abracadbra"); private static final byte[] END_KEY = Bytes.toBytes("zzyzx"); private static final long ID = 8731042424L; private static final String LOCATION = "testhost:9876"; - private static final String AS_XML = - ""; + public TestTableRegionModel() throws Exception { + super(TableRegionModel.class); - private JAXBContext context; + AS_XML = + ""; - public TestTableRegionModel() throws JAXBException { - super(); - context = JAXBContext.newInstance(TableRegionModel.class); + AS_JSON = + "{\"endKey\":\"enp5eng=\",\"id\":8731042424,\"location\":\"testhost:9876\"," + + "\"name\":\"testtable,abracadbra,8731042424.ad9860f031282c46ed431d7af8f94aca.\",\"" + + "startKey\":\"YWJyYWNhZGJyYQ==\"}"; } - private TableRegionModel buildTestModel() { + protected TableRegionModel buildTestModel() { TableRegionModel model = new TableRegionModel(TABLE, ID, START_KEY, END_KEY, LOCATION); return model; } - @SuppressWarnings("unused") - private String toXML(TableRegionModel model) throws JAXBException { - StringWriter writer = new StringWriter(); - context.createMarshaller().marshal(model, writer); - return writer.toString(); - } - - private TableRegionModel fromXML(String xml) throws JAXBException { - return (TableRegionModel) - context.createUnmarshaller().unmarshal(new StringReader(xml)); - } - - private void checkModel(TableRegionModel model) { + protected void checkModel(TableRegionModel model) { assertTrue(Bytes.equals(model.getStartKey(), START_KEY)); assertTrue(Bytes.equals(model.getEndKey(), END_KEY)); assertEquals(model.getId(), ID); @@ -81,10 +70,6 @@ public class TestTableRegionModel extends TestCase { ".ad9860f031282c46ed431d7af8f94aca."); } - public void testBuildModel() throws Exception { - checkModel(buildTestModel()); - } - public void testGetName() { TableRegionModel model = buildTestModel(); String modelName = model.getName(); @@ -100,9 +85,9 @@ public class TestTableRegionModel extends TestCase { assertEquals(name, model.getName()); } - public void testFromXML() throws Exception { - checkModel(fromXML(AS_XML)); + @Override + public void testFromPB() throws Exception { + //no pb ignore } - } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestTableSchemaModel.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestTableSchemaModel.java index 7ec9f644ffb..b725f7bc507 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestTableSchemaModel.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestTableSchemaModel.java @@ -30,81 +30,66 @@ import javax.xml.bind.JAXBException; import org.apache.hadoop.hbase.SmallTests; import org.apache.hadoop.hbase.util.Base64; +import org.apache.hadoop.hbase.rest.model.TableSchemaModel; + import junit.framework.TestCase; import org.junit.experimental.categories.Category; @Category(SmallTests.class) -public class TestTableSchemaModel extends TestCase { +public class TestTableSchemaModel extends TestModelBase { public static final String TABLE_NAME = "testTable"; private static final boolean IS_META = false; private static final boolean IS_ROOT = false; private static final boolean READONLY = false; - private static final String AS_XML = - "" + - TestColumnSchemaModel.AS_XML + - ""; - - private static final String AS_PB = - "Cgl0ZXN0VGFibGUSEAoHSVNfTUVUQRIFZmFsc2USEAoHSVNfUk9PVBIFZmFsc2USEQoIUkVBRE9O" + - "TFkSBWZhbHNlGpcBCgp0ZXN0Y29sdW1uEhIKCUJMT0NLU0laRRIFMTYzODQSEwoLQkxPT01GSUxU" + - "RVISBE5PTkUSEgoKQkxPQ0tDQUNIRRIEdHJ1ZRIRCgtDT01QUkVTU0lPThICR1oSDQoIVkVSU0lP" + - "TlMSATESDAoDVFRMEgU4NjQwMBISCglJTl9NRU1PUlkSBWZhbHNlGICjBSABKgJHWigA"; + TestColumnSchemaModel testColumnSchemaModel; private JAXBContext context; - public TestTableSchemaModel() throws JAXBException { - super(); - context = JAXBContext.newInstance( - ColumnSchemaModel.class, - TableSchemaModel.class); + public TestTableSchemaModel() throws Exception { + super(TableSchemaModel.class); + testColumnSchemaModel = new TestColumnSchemaModel(); + + AS_XML = + "" + + "" + + "" + + ""; + + AS_PB = + "Cgl0ZXN0VGFibGUSEAoHSVNfTUVUQRIFZmFsc2USEAoHSVNfUk9PVBIFZmFsc2USEQoIUkVBRE9O" + + "TFkSBWZhbHNlGpcBCgp0ZXN0Y29sdW1uEhIKCUJMT0NLU0laRRIFMTYzODQSEwoLQkxPT01GSUxU" + + "RVISBE5PTkUSEgoKQkxPQ0tDQUNIRRIEdHJ1ZRIRCgtDT01QUkVTU0lPThICR1oSDQoIVkVSU0lP" + + "TlMSATESDAoDVFRMEgU4NjQwMBISCglJTl9NRU1PUlkSBWZhbHNlGICjBSABKgJHWigA"; + + AS_JSON = + "{\"name\":\"testTable\",\"IS_META\":\"false\",\"IS_ROOT\":\"false\"," + + "\"READONLY\":\"false\",\"ColumnSchema\":[{\"name\":\"testcolumn\"," + + "\"BLOCKSIZE\":\"16384\",\"BLOOMFILTER\":\"NONE\",\"BLOCKCACHE\":\"true\"," + + "\"COMPRESSION\":\"GZ\",\"VERSIONS\":\"1\",\"TTL\":\"86400\",\"IN_MEMORY\":\"false\"}]}"; } - public static TableSchemaModel buildTestModel() { + protected TableSchemaModel buildTestModel() { return buildTestModel(TABLE_NAME); } - public static TableSchemaModel buildTestModel(String name) { + public TableSchemaModel buildTestModel(String name) { TableSchemaModel model = new TableSchemaModel(); model.setName(name); model.__setIsMeta(IS_META); model.__setIsRoot(IS_ROOT); model.__setReadOnly(READONLY); - model.addColumnFamily(TestColumnSchemaModel.buildTestModel()); + model.addColumnFamily(testColumnSchemaModel.buildTestModel()); return model; } - @SuppressWarnings("unused") - private String toXML(TableSchemaModel model) throws JAXBException { - StringWriter writer = new StringWriter(); - context.createMarshaller().marshal(model, writer); - return writer.toString(); - } - - private TableSchemaModel fromXML(String xml) throws JAXBException { - return (TableSchemaModel) - context.createUnmarshaller().unmarshal(new StringReader(xml)); - } - - @SuppressWarnings("unused") - private byte[] toPB(TableSchemaModel model) { - return model.createProtobufOutput(); - } - - private TableSchemaModel fromPB(String pb) throws IOException { - return (TableSchemaModel) - new TableSchemaModel().getObjectFromMessage(Base64.decode(AS_PB)); - } - - public static void checkModel(TableSchemaModel model) { + protected void checkModel(TableSchemaModel model) { checkModel(model, TABLE_NAME); } - public static void checkModel(TableSchemaModel model, String tableName) { + public void checkModel(TableSchemaModel model, String tableName) { assertEquals(model.getName(), tableName); assertEquals(model.__getIsMeta(), IS_META); assertEquals(model.__getIsRoot(), IS_ROOT); @@ -112,7 +97,7 @@ public class TestTableSchemaModel extends TestCase { Iterator families = model.getColumns().iterator(); assertTrue(families.hasNext()); ColumnSchemaModel family = families.next(); - TestColumnSchemaModel.checkModel(family); + testColumnSchemaModel.checkModel(family); assertFalse(families.hasNext()); } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestVersionModel.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestVersionModel.java index 273be992403..553bb356933 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestVersionModel.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/rest/model/TestVersionModel.java @@ -33,7 +33,7 @@ import junit.framework.TestCase; import org.junit.experimental.categories.Category; @Category(SmallTests.class) -public class TestVersionModel extends TestCase { +public class TestVersionModel extends TestModelBase { private static final String REST_VERSION = "0.0.1"; private static final String OS_VERSION = "Linux 2.6.18-128.1.6.el5.centos.plusxen amd64"; @@ -42,25 +42,24 @@ public class TestVersionModel extends TestCase { private static final String JETTY_VERSION = "6.1.14"; private static final String JERSEY_VERSION = "1.1.0-ea"; - private static final String AS_XML = - ""; + public TestVersionModel() throws Exception { + super(VersionModel.class); + AS_XML = + ""; - private static final String AS_PB = - "CgUwLjAuMRInU3VuIE1pY3Jvc3lzdGVtcyBJbmMuIDEuNi4wXzEzLTExLjMtYjAyGi1MaW51eCAy" + - "LjYuMTgtMTI4LjEuNi5lbDUuY2VudG9zLnBsdXN4ZW4gYW1kNjQiBjYuMS4xNCoIMS4xLjAtZWE="; + AS_PB = + "CgUwLjAuMRInU3VuIE1pY3Jvc3lzdGVtcyBJbmMuIDEuNi4wXzEzLTExLjMtYjAyGi1MaW51eCAy" + + "LjYuMTgtMTI4LjEuNi5lbDUuY2VudG9zLnBsdXN4ZW4gYW1kNjQiBjYuMS4xNCoIMS4xLjAtZWE="; - private JAXBContext context; - - public TestVersionModel() throws JAXBException { - super(); - context = JAXBContext.newInstance(VersionModel.class); + AS_JSON = + "{\"JVM\":\"Sun Microsystems Inc. 1.6.0_13-11.3-b02\",\"Jersey\":\"1.1.0-ea\"," + + "\"OS\":\"Linux 2.6.18-128.1.6.el5.centos.plusxen amd64\",\"" + + "REST\":\"0.0.1\",\"Server\":\"6.1.14\"}"; } - private VersionModel buildTestModel() { + protected VersionModel buildTestModel() { VersionModel model = new VersionModel(); model.setRESTVersion(REST_VERSION); model.setOSVersion(OS_VERSION); @@ -70,47 +69,12 @@ public class TestVersionModel extends TestCase { return model; } - @SuppressWarnings("unused") - private String toXML(VersionModel model) throws JAXBException { - StringWriter writer = new StringWriter(); - context.createMarshaller().marshal(model, writer); - return writer.toString(); - } - - private VersionModel fromXML(String xml) throws JAXBException { - return (VersionModel) - context.createUnmarshaller().unmarshal(new StringReader(xml)); - } - - @SuppressWarnings("unused") - private byte[] toPB(VersionModel model) { - return model.createProtobufOutput(); - } - - private VersionModel fromPB(String pb) throws IOException { - return (VersionModel) - new VersionModel().getObjectFromMessage(Base64.decode(AS_PB)); - } - - private void checkModel(VersionModel model) { + protected void checkModel(VersionModel model) { assertEquals(model.getRESTVersion(), REST_VERSION); assertEquals(model.getOSVersion(), OS_VERSION); assertEquals(model.getJVMVersion(), JVM_VERSION); assertEquals(model.getServerVersion(), JETTY_VERSION); assertEquals(model.getJerseyVersion(), JERSEY_VERSION); } - - public void testBuildModel() throws Exception { - checkModel(buildTestModel()); - } - - public void testFromXML() throws Exception { - checkModel(fromXML(AS_XML)); - } - - public void testFromPB() throws Exception { - checkModel(fromPB(AS_PB)); - } - }