View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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     // create the table
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     // recall the same put operation but in read-only mode
109     conf.set("hbase.rest.readonly", "true");
110     response = client.put(schemaPath, Constants.MIMETYPE_XML, toXML(model));
111     assertEquals(response.getCode(), 403);
112 
113     // retrieve the schema and validate it
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     // with json retrieve the schema and validate it
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     // test delete schema operation is forbidden in read-only mode
128     response = client.delete(schemaPath);
129     assertEquals(response.getCode(), 403);
130 
131     // return read-only setting back to default
132     conf.set("hbase.rest.readonly", "false");
133 
134     // delete the table and make sure HBase concurs
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     // create the table
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     // recall the same put operation but in read-only mode
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     // retrieve the schema and validate it
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     // retrieve the schema and validate it with alternate pbuf type
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     // test delete schema operation is forbidden in read-only mode
179     response = client.delete(schemaPath);
180     assertEquals(response.getCode(), 403);
181 
182     // return read-only setting back to default
183     conf.set("hbase.rest.readonly", "false");
184 
185     // delete the table and make sure HBase concurs
186     response = client.delete(schemaPath);
187     assertEquals(response.getCode(), 200);
188     assertFalse(admin.tableExists(TableName.valueOf(TABLE2)));
189   }
190 
191 }
192