1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.rest;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.IOException;
25 import java.io.StringWriter;
26
27 import javax.xml.bind.JAXBContext;
28 import javax.xml.bind.JAXBException;
29
30 import org.apache.hadoop.hbase.client.HBaseAdmin;
31 import org.apache.hadoop.hbase.rest.client.Client;
32 import org.apache.hadoop.hbase.rest.client.Cluster;
33 import org.apache.hadoop.hbase.rest.client.Response;
34 import org.apache.hadoop.hbase.rest.model.ColumnSchemaModel;
35 import org.apache.hadoop.hbase.rest.model.TableSchemaModel;
36 import org.apache.hadoop.hbase.rest.model.TestTableSchemaModel;
37 import org.apache.hadoop.hbase.util.Bytes;
38
39 public class TestSchemaResource extends HBaseRESTClusterTestBase {
40 static String TABLE1 = "TestSchemaResource1";
41 static String TABLE2 = "TestSchemaResource2";
42
43 Client client;
44 JAXBContext context;
45 HBaseAdmin admin;
46
47 @Override
48 protected void setUp() throws Exception {
49 super.setUp();
50 context = JAXBContext.newInstance(
51 ColumnSchemaModel.class,
52 TableSchemaModel.class);
53 admin = new HBaseAdmin(conf);
54 client = new Client(new Cluster().add("localhost", testServletPort));
55 }
56
57 @Override
58 protected void tearDown() throws Exception {
59 client.shutdown();
60 super.tearDown();
61 }
62
63 byte[] toXML(TableSchemaModel model) throws JAXBException {
64 StringWriter writer = new StringWriter();
65 context.createMarshaller().marshal(model, writer);
66 return Bytes.toBytes(writer.toString());
67 }
68
69 TableSchemaModel fromXML(byte[] content) throws JAXBException {
70 return (TableSchemaModel) context.createUnmarshaller()
71 .unmarshal(new ByteArrayInputStream(content));
72 }
73
74 void doTestTableCreateAndDeleteXML() throws IOException, JAXBException {
75 String schemaPath = "/" + TABLE1 + "/schema";
76 TableSchemaModel model;
77 Response response;
78
79 assertFalse(admin.tableExists(TABLE1));
80
81
82 model = TestTableSchemaModel.buildTestModel(TABLE1);
83 TestTableSchemaModel.checkModel(model, TABLE1);
84 response = client.put(schemaPath, MIMETYPE_XML, toXML(model));
85 assertEquals(response.getCode(), 201);
86
87
88 admin.enableTable(TABLE1);
89
90
91 response = client.get(schemaPath, MIMETYPE_XML);
92 assertEquals(response.getCode(), 200);
93 model = fromXML(response.getBody());
94 TestTableSchemaModel.checkModel(model, TABLE1);
95
96
97 client.delete(schemaPath);
98
99
100 assertFalse(admin.tableExists(TABLE1));
101 }
102
103 void doTestTableCreateAndDeletePB() throws IOException, JAXBException {
104 String schemaPath = "/" + TABLE2 + "/schema";
105 TableSchemaModel model;
106 Response response;
107
108 assertFalse(admin.tableExists(TABLE2));
109
110
111 model = TestTableSchemaModel.buildTestModel(TABLE2);
112 TestTableSchemaModel.checkModel(model, TABLE2);
113 response = client.put(schemaPath, Constants.MIMETYPE_PROTOBUF,
114 model.createProtobufOutput());
115 assertEquals(response.getCode(), 201);
116
117
118 admin.enableTable(TABLE2);
119
120
121 response = client.get(schemaPath, Constants.MIMETYPE_PROTOBUF);
122 assertEquals(response.getCode(), 200);
123 model = new TableSchemaModel();
124 model.getObjectFromMessage(response.getBody());
125 TestTableSchemaModel.checkModel(model, TABLE2);
126
127
128 client.delete(schemaPath);
129
130
131 assertFalse(admin.tableExists(TABLE2));
132 }
133
134 public void testSchemaResource() throws Exception {
135 doTestTableCreateAndDeleteXML();
136 doTestTableCreateAndDeletePB();
137 }
138 }