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.MediumTests;
33 import org.apache.hadoop.hbase.client.HBaseAdmin;
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
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 context = JAXBContext.newInstance(
69 ColumnSchemaModel.class,
70 TableSchemaModel.class);
71 }
72
73 @AfterClass
74 public static void tearDownAfterClass() throws Exception {
75 REST_TEST_UTIL.shutdownServletContainer();
76 TEST_UTIL.shutdownMiniCluster();
77 }
78
79 private static byte[] toXML(TableSchemaModel model) throws JAXBException {
80 StringWriter writer = new StringWriter();
81 context.createMarshaller().marshal(model, writer);
82 return Bytes.toBytes(writer.toString());
83 }
84
85 private static TableSchemaModel fromXML(byte[] content)
86 throws JAXBException {
87 return (TableSchemaModel) context.createUnmarshaller()
88 .unmarshal(new ByteArrayInputStream(content));
89 }
90
91 @Test
92 public void testTableCreateAndDeleteXML() throws IOException, JAXBException {
93 String schemaPath = "/" + TABLE1 + "/schema";
94 TableSchemaModel model;
95 Response response;
96
97 HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
98 assertFalse(admin.tableExists(TABLE1));
99
100
101 model = TestTableSchemaModel.buildTestModel(TABLE1);
102 TestTableSchemaModel.checkModel(model, TABLE1);
103 response = client.put(schemaPath, Constants.MIMETYPE_XML, toXML(model));
104 assertEquals(response.getCode(), 201);
105
106
107 conf.set("hbase.rest.readonly", "true");
108 response = client.put(schemaPath, Constants.MIMETYPE_XML, toXML(model));
109 assertEquals(response.getCode(), 403);
110
111
112 response = client.get(schemaPath, Constants.MIMETYPE_XML);
113 assertEquals(response.getCode(), 200);
114 assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
115 model = fromXML(response.getBody());
116 TestTableSchemaModel.checkModel(model, TABLE1);
117
118
119 response = client.delete(schemaPath);
120 assertEquals(response.getCode(), 403);
121
122
123 conf.set("hbase.rest.readonly", "false");
124
125
126 response = client.delete(schemaPath);
127 assertEquals(response.getCode(), 200);
128 assertFalse(admin.tableExists(TABLE1));
129 }
130
131 @Test
132 public void testTableCreateAndDeletePB() throws IOException, JAXBException {
133 String schemaPath = "/" + TABLE2 + "/schema";
134 TableSchemaModel model;
135 Response response;
136
137 HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
138 assertFalse(admin.tableExists(TABLE2));
139
140
141 model = TestTableSchemaModel.buildTestModel(TABLE2);
142 TestTableSchemaModel.checkModel(model, TABLE2);
143 response = client.put(schemaPath, Constants.MIMETYPE_PROTOBUF,
144 model.createProtobufOutput());
145 assertEquals(response.getCode(), 201);
146
147
148 conf.set("hbase.rest.readonly", "true");
149 response = client.put(schemaPath, Constants.MIMETYPE_PROTOBUF,
150 model.createProtobufOutput());
151 assertEquals(response.getCode(), 403);
152
153
154 response = client.get(schemaPath, Constants.MIMETYPE_PROTOBUF);
155 assertEquals(response.getCode(), 200);
156 assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
157 model = new TableSchemaModel();
158 model.getObjectFromMessage(response.getBody());
159 TestTableSchemaModel.checkModel(model, TABLE2);
160
161
162 response = client.get(schemaPath, Constants.MIMETYPE_PROTOBUF_IETF);
163 assertEquals(response.getCode(), 200);
164 assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type"));
165 model = new TableSchemaModel();
166 model.getObjectFromMessage(response.getBody());
167 TestTableSchemaModel.checkModel(model, TABLE2);
168
169
170 response = client.delete(schemaPath);
171 assertEquals(response.getCode(), 403);
172
173
174 conf.set("hbase.rest.readonly", "false");
175
176
177 response = client.delete(schemaPath);
178 assertEquals(response.getCode(), 200);
179 assertFalse(admin.tableExists(TABLE2));
180 }
181
182 @org.junit.Rule
183 public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
184 new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
185 }
186