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.conf.Configuration;
31 import org.apache.hadoop.hbase.HBaseTestingUtility;
32 import org.apache.hadoop.hbase.client.HBaseAdmin;
33 import org.apache.hadoop.hbase.rest.client.Client;
34 import org.apache.hadoop.hbase.rest.client.Cluster;
35 import org.apache.hadoop.hbase.rest.client.Response;
36 import org.apache.hadoop.hbase.rest.model.ColumnSchemaModel;
37 import org.apache.hadoop.hbase.rest.model.TableSchemaModel;
38 import org.apache.hadoop.hbase.rest.model.TestTableSchemaModel;
39 import org.apache.hadoop.hbase.util.Bytes;
40
41 import static org.junit.Assert.*;
42 import org.junit.AfterClass;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45
46 public class TestSchemaResource {
47 private static String TABLE1 = "TestSchemaResource1";
48 private static String TABLE2 = "TestSchemaResource2";
49
50 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
51 private static final HBaseRESTTestingUtility REST_TEST_UTIL =
52 new HBaseRESTTestingUtility();
53 private static Client client;
54 private static JAXBContext context;
55 private static Configuration conf;
56
57 @BeforeClass
58 public static void setUpBeforeClass() throws Exception {
59 conf = TEST_UTIL.getConfiguration();
60 TEST_UTIL.startMiniCluster(3);
61 REST_TEST_UTIL.startServletContainer(conf);
62 client = new Client(new Cluster().add("localhost",
63 REST_TEST_UTIL.getServletPort()));
64 context = JAXBContext.newInstance(
65 ColumnSchemaModel.class,
66 TableSchemaModel.class);
67 }
68
69 @AfterClass
70 public static void tearDownAfterClass() throws Exception {
71 REST_TEST_UTIL.shutdownServletContainer();
72 TEST_UTIL.shutdownMiniCluster();
73 }
74
75 private static byte[] toXML(TableSchemaModel model) throws JAXBException {
76 StringWriter writer = new StringWriter();
77 context.createMarshaller().marshal(model, writer);
78 return Bytes.toBytes(writer.toString());
79 }
80
81 private static TableSchemaModel fromXML(byte[] content)
82 throws JAXBException {
83 return (TableSchemaModel) context.createUnmarshaller()
84 .unmarshal(new ByteArrayInputStream(content));
85 }
86
87 @Test
88 public void testTableCreateAndDeleteXML() throws IOException, JAXBException {
89 String schemaPath = "/" + TABLE1 + "/schema";
90 TableSchemaModel model;
91 Response response;
92
93 HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
94 assertFalse(admin.tableExists(TABLE1));
95
96
97 model = TestTableSchemaModel.buildTestModel(TABLE1);
98 TestTableSchemaModel.checkModel(model, TABLE1);
99 response = client.put(schemaPath, Constants.MIMETYPE_XML, toXML(model));
100 assertEquals(response.getCode(), 201);
101
102
103 conf.set("hbase.rest.readonly", "true");
104 response = client.put(schemaPath, Constants.MIMETYPE_XML, toXML(model));
105 assertEquals(response.getCode(), 403);
106
107
108 admin.enableTable(TABLE1);
109
110
111 response = client.get(schemaPath, Constants.MIMETYPE_XML);
112 assertEquals(response.getCode(), 200);
113 model = fromXML(response.getBody());
114 TestTableSchemaModel.checkModel(model, TABLE1);
115
116
117 client.delete(schemaPath);
118
119
120 assertFalse(admin.tableExists(TABLE1));
121
122
123 conf.set("hbase.rest.readonly", "false");
124 }
125
126 @Test
127 public void testTableCreateAndDeletePB() throws IOException, JAXBException {
128 String schemaPath = "/" + TABLE2 + "/schema";
129 TableSchemaModel model;
130 Response response;
131
132 HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
133 assertFalse(admin.tableExists(TABLE2));
134
135
136 model = TestTableSchemaModel.buildTestModel(TABLE2);
137 TestTableSchemaModel.checkModel(model, TABLE2);
138 response = client.put(schemaPath, Constants.MIMETYPE_PROTOBUF,
139 model.createProtobufOutput());
140 assertEquals(response.getCode(), 201);
141
142
143 conf.set("hbase.rest.readonly", "true");
144 response = client.put(schemaPath, Constants.MIMETYPE_PROTOBUF,
145 model.createProtobufOutput());
146 assertEquals(response.getCode(), 403);
147
148
149 admin.enableTable(TABLE2);
150
151
152 response = client.get(schemaPath, Constants.MIMETYPE_PROTOBUF);
153 assertEquals(response.getCode(), 200);
154 model = new TableSchemaModel();
155 model.getObjectFromMessage(response.getBody());
156 TestTableSchemaModel.checkModel(model, TABLE2);
157
158
159 client.delete(schemaPath);
160
161
162 assertFalse(admin.tableExists(TABLE2));
163
164
165 conf.set("hbase.rest.readonly", "false");
166 }
167 }