1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.rest;
21
22 import java.io.ByteArrayInputStream;
23 import java.io.IOException;
24 import java.io.StringWriter;
25
26 import javax.xml.bind.JAXBContext;
27 import javax.xml.bind.JAXBException;
28
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.hbase.HBaseTestingUtility;
31 import org.apache.hadoop.hbase.testclassification.MediumTests;
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
43 import org.junit.AfterClass;
44 import org.junit.BeforeClass;
45 import org.junit.Test;
46 import org.junit.experimental.categories.Category;
47
48 @Category(MediumTests.class)
49 public class TestSchemaResource {
50 private static String TABLE1 = "TestSchemaResource1";
51 private static String TABLE2 = "TestSchemaResource2";
52
53 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
54 private static final HBaseRESTTestingUtility REST_TEST_UTIL =
55 new HBaseRESTTestingUtility();
56 private static Client client;
57 private static JAXBContext context;
58 private static Configuration conf;
59 private static TestTableSchemaModel testTableSchemaModel;
60
61 @BeforeClass
62 public static void setUpBeforeClass() throws Exception {
63 conf = TEST_UTIL.getConfiguration();
64 TEST_UTIL.startMiniCluster();
65 REST_TEST_UTIL.startServletContainer(conf);
66 client = new Client(new Cluster().add("localhost",
67 REST_TEST_UTIL.getServletPort()));
68 testTableSchemaModel = new TestTableSchemaModel();
69 context = JAXBContext.newInstance(
70 ColumnSchemaModel.class,
71 TableSchemaModel.class);
72 }
73
74 @AfterClass
75 public static void tearDownAfterClass() throws Exception {
76 REST_TEST_UTIL.shutdownServletContainer();
77 TEST_UTIL.shutdownMiniCluster();
78 }
79
80 private static byte[] toXML(TableSchemaModel model) throws JAXBException {
81 StringWriter writer = new StringWriter();
82 context.createMarshaller().marshal(model, writer);
83 return Bytes.toBytes(writer.toString());
84 }
85
86 private static TableSchemaModel fromXML(byte[] content)
87 throws JAXBException {
88 return (TableSchemaModel) context.createUnmarshaller()
89 .unmarshal(new ByteArrayInputStream(content));
90 }
91
92 @Test
93 public void testTableCreateAndDeleteXML() throws IOException, JAXBException {
94 String schemaPath = "/" + TABLE1 + "/schema";
95 TableSchemaModel model;
96 Response response;
97
98 HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
99 assertFalse(admin.tableExists(TABLE1));
100
101
102 model = testTableSchemaModel.buildTestModel(TABLE1);
103 testTableSchemaModel.checkModel(model, TABLE1);
104 response = client.put(schemaPath, Constants.MIMETYPE_XML, toXML(model));
105 assertEquals(response.getCode(), 201);
106
107
108 conf.set("hbase.rest.readonly", "true");
109 response = client.put(schemaPath, Constants.MIMETYPE_XML, toXML(model));
110 assertEquals(response.getCode(), 403);
111
112
113 response = client.get(schemaPath, Constants.MIMETYPE_XML);
114 assertEquals(response.getCode(), 200);
115 assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
116 model = fromXML(response.getBody());
117 testTableSchemaModel.checkModel(model, TABLE1);
118
119
120 response = client.get(schemaPath, Constants.MIMETYPE_JSON);
121 assertEquals(response.getCode(), 200);
122 assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
123 model = testTableSchemaModel.fromJSON(Bytes.toString(response.getBody()));
124 testTableSchemaModel.checkModel(model, TABLE1);
125
126
127 response = client.delete(schemaPath);
128 assertEquals(response.getCode(), 403);
129
130
131 conf.set("hbase.rest.readonly", "false");
132
133
134 response = client.delete(schemaPath);
135 assertEquals(response.getCode(), 200);
136 assertFalse(admin.tableExists(TABLE1));
137 }
138
139 @Test
140 public void testTableCreateAndDeletePB() throws IOException, JAXBException {
141 String schemaPath = "/" + TABLE2 + "/schema";
142 TableSchemaModel model;
143 Response response;
144
145 HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
146 assertFalse(admin.tableExists(TABLE2));
147
148
149 model = testTableSchemaModel.buildTestModel(TABLE2);
150 testTableSchemaModel.checkModel(model, TABLE2);
151 response = client.put(schemaPath, Constants.MIMETYPE_PROTOBUF,
152 model.createProtobufOutput());
153 assertEquals(response.getCode(), 201);
154
155
156 conf.set("hbase.rest.readonly", "true");
157 response = client.put(schemaPath, Constants.MIMETYPE_PROTOBUF,
158 model.createProtobufOutput());
159 assertEquals(response.getCode(), 403);
160
161
162 response = client.get(schemaPath, Constants.MIMETYPE_PROTOBUF);
163 assertEquals(response.getCode(), 200);
164 assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
165 model = new TableSchemaModel();
166 model.getObjectFromMessage(response.getBody());
167 testTableSchemaModel.checkModel(model, TABLE2);
168
169
170 response = client.get(schemaPath, Constants.MIMETYPE_PROTOBUF_IETF);
171 assertEquals(response.getCode(), 200);
172 assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type"));
173 model = new TableSchemaModel();
174 model.getObjectFromMessage(response.getBody());
175 testTableSchemaModel.checkModel(model, TABLE2);
176
177
178 response = client.delete(schemaPath);
179 assertEquals(response.getCode(), 403);
180
181
182 conf.set("hbase.rest.readonly", "false");
183
184
185 response = client.delete(schemaPath);
186 assertEquals(response.getCode(), 200);
187 assertFalse(admin.tableExists(TABLE2));
188 }
189
190 }
191