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