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