1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.rest;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertNotNull;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.IOException;
26 import java.io.StringWriter;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.List;
30
31 import javax.xml.bind.JAXBContext;
32 import javax.xml.bind.JAXBException;
33
34 import org.apache.commons.httpclient.Header;
35
36 import org.apache.hadoop.conf.Configuration;
37 import org.apache.hadoop.hbase.HBaseTestingUtility;
38 import org.apache.hadoop.hbase.testclassification.MediumTests;
39 import org.apache.hadoop.hbase.TableName;
40 import org.apache.hadoop.hbase.client.Admin;
41 import org.apache.hadoop.hbase.rest.client.Client;
42 import org.apache.hadoop.hbase.rest.client.Cluster;
43 import org.apache.hadoop.hbase.rest.client.Response;
44 import org.apache.hadoop.hbase.rest.model.ColumnSchemaModel;
45 import org.apache.hadoop.hbase.rest.model.TableSchemaModel;
46 import org.apache.hadoop.hbase.rest.model.TestTableSchemaModel;
47 import org.apache.hadoop.hbase.util.Bytes;
48
49 import org.junit.AfterClass;
50 import org.junit.BeforeClass;
51 import org.junit.Test;
52 import org.junit.experimental.categories.Category;
53 import org.junit.runner.RunWith;
54 import org.junit.runners.Parameterized;
55
56 @Category(MediumTests.class)
57 @RunWith(Parameterized.class)
58 public class TestSchemaResource {
59 private static String TABLE1 = "TestSchemaResource1";
60 private static String TABLE2 = "TestSchemaResource2";
61
62 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
63 private static final HBaseRESTTestingUtility REST_TEST_UTIL =
64 new HBaseRESTTestingUtility();
65 private static Client client;
66 private static JAXBContext context;
67 private static Configuration conf;
68 private static TestTableSchemaModel testTableSchemaModel;
69 private static Header extraHdr = null;
70
71 private static boolean csrfEnabled = true;
72
73 @Parameterized.Parameters
74 public static Collection<Object[]> data() {
75 List<Object[]> params = new ArrayList<Object[]>();
76 params.add(new Object[] {Boolean.TRUE});
77 params.add(new Object[] {Boolean.FALSE});
78 return params;
79 }
80
81 public TestSchemaResource(Boolean csrf) {
82 csrfEnabled = csrf;
83 }
84
85 @BeforeClass
86 public static void setUpBeforeClass() throws Exception {
87 conf = TEST_UTIL.getConfiguration();
88 conf.setBoolean(RESTServer.REST_CSRF_ENABLED_KEY, csrfEnabled);
89 extraHdr = new Header(RESTServer.REST_CSRF_CUSTOM_HEADER_DEFAULT, "");
90 TEST_UTIL.startMiniCluster();
91 REST_TEST_UTIL.startServletContainer(conf);
92 client = new Client(new Cluster().add("localhost",
93 REST_TEST_UTIL.getServletPort()));
94 testTableSchemaModel = new TestTableSchemaModel();
95 context = JAXBContext.newInstance(
96 ColumnSchemaModel.class,
97 TableSchemaModel.class);
98 }
99
100 @AfterClass
101 public static void tearDownAfterClass() throws Exception {
102 REST_TEST_UTIL.shutdownServletContainer();
103 TEST_UTIL.shutdownMiniCluster();
104 }
105
106 private static byte[] toXML(TableSchemaModel model) throws JAXBException {
107 StringWriter writer = new StringWriter();
108 context.createMarshaller().marshal(model, writer);
109 return Bytes.toBytes(writer.toString());
110 }
111
112 private static TableSchemaModel fromXML(byte[] content)
113 throws JAXBException {
114 return (TableSchemaModel) context.createUnmarshaller()
115 .unmarshal(new ByteArrayInputStream(content));
116 }
117
118 @Test
119 public void testTableCreateAndDeleteXML() throws IOException, JAXBException {
120 String schemaPath = "/" + TABLE1 + "/schema";
121 TableSchemaModel model;
122 Response response;
123
124 Admin admin = TEST_UTIL.getHBaseAdmin();
125 assertFalse(admin.tableExists(TableName.valueOf(TABLE1)));
126
127
128 model = testTableSchemaModel.buildTestModel(TABLE1);
129 testTableSchemaModel.checkModel(model, TABLE1);
130 if (csrfEnabled) {
131
132 response = client.put(schemaPath, Constants.MIMETYPE_XML, toXML(model));
133 assertEquals(400, response.getCode());
134 }
135
136 response = client.put(schemaPath, Constants.MIMETYPE_XML, toXML(model), extraHdr);
137 assertEquals(201, response.getCode());
138
139
140 conf.set("hbase.rest.readonly", "true");
141 response = client.put(schemaPath, Constants.MIMETYPE_XML, toXML(model), extraHdr);
142 assertEquals(403, response.getCode());
143
144
145 response = client.get(schemaPath, Constants.MIMETYPE_XML);
146 assertEquals(200, response.getCode());
147 assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
148 model = fromXML(response.getBody());
149 testTableSchemaModel.checkModel(model, TABLE1);
150
151
152 response = client.get(schemaPath, Constants.MIMETYPE_JSON);
153 assertEquals(200, response.getCode());
154 assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
155 model = testTableSchemaModel.fromJSON(Bytes.toString(response.getBody()));
156 testTableSchemaModel.checkModel(model, TABLE1);
157
158 if (csrfEnabled) {
159
160 response = client.delete(schemaPath);
161 assertEquals(400, response.getCode());
162 }
163
164
165 response = client.delete(schemaPath, extraHdr);
166 assertEquals(403, response.getCode());
167
168
169 conf.set("hbase.rest.readonly", "false");
170
171
172 response = client.delete(schemaPath, extraHdr);
173 assertEquals(200, response.getCode());
174 assertFalse(admin.tableExists(TableName.valueOf(TABLE1)));
175 }
176
177 @Test
178 public void testTableCreateAndDeletePB() throws IOException {
179 String schemaPath = "/" + TABLE2 + "/schema";
180 TableSchemaModel model;
181 Response response;
182
183 Admin admin = TEST_UTIL.getHBaseAdmin();
184 assertFalse(admin.tableExists(TableName.valueOf(TABLE2)));
185
186
187 model = testTableSchemaModel.buildTestModel(TABLE2);
188 testTableSchemaModel.checkModel(model, TABLE2);
189
190 if (csrfEnabled) {
191
192 response = client.put(schemaPath, Constants.MIMETYPE_PROTOBUF, model.createProtobufOutput());
193 assertEquals(400, response.getCode());
194 }
195 response = client.put(schemaPath, Constants.MIMETYPE_PROTOBUF,
196 model.createProtobufOutput(), extraHdr);
197 assertEquals(201, response.getCode());
198
199
200 conf.set("hbase.rest.readonly", "true");
201 response = client.put(schemaPath, Constants.MIMETYPE_PROTOBUF,
202 model.createProtobufOutput(), extraHdr);
203 assertNotNull(extraHdr);
204 assertEquals(403, response.getCode());
205
206
207 response = client.get(schemaPath, Constants.MIMETYPE_PROTOBUF);
208 assertEquals(200, response.getCode());
209 assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
210 model = new TableSchemaModel();
211 model.getObjectFromMessage(response.getBody());
212 testTableSchemaModel.checkModel(model, TABLE2);
213
214
215 response = client.get(schemaPath, Constants.MIMETYPE_PROTOBUF_IETF);
216 assertEquals(200, response.getCode());
217 assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type"));
218 model = new TableSchemaModel();
219 model.getObjectFromMessage(response.getBody());
220 testTableSchemaModel.checkModel(model, TABLE2);
221
222 if (csrfEnabled) {
223
224 response = client.delete(schemaPath);
225 assertEquals(400, response.getCode());
226 }
227
228
229 response = client.delete(schemaPath, extraHdr);
230 assertEquals(403, response.getCode());
231
232
233 conf.set("hbase.rest.readonly", "false");
234
235
236 response = client.delete(schemaPath, extraHdr);
237 assertEquals(200, response.getCode());
238 assertFalse(admin.tableExists(TableName.valueOf(TABLE2)));
239 }
240 }