1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
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     // create the table
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     // recall the same put operation but in read-only mode
107     conf.set("hbase.rest.readonly", "true");
108     response = client.put(schemaPath, Constants.MIMETYPE_XML, toXML(model));
109     assertEquals(response.getCode(), 403);
110 
111     // retrieve the schema and validate it
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     // delete the table
119     client.delete(schemaPath);
120 
121     // make sure HBase concurs
122     assertFalse(admin.tableExists(TABLE1));
123 
124     // return read-only setting back to default
125     conf.set("hbase.rest.readonly", "false");
126   }
127 
128   @Test
129   public void testTableCreateAndDeletePB() throws IOException, JAXBException {
130     String schemaPath = "/" + TABLE2 + "/schema";
131     TableSchemaModel model;
132     Response response;
133 
134     HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
135     assertFalse(admin.tableExists(TABLE2));
136 
137     // create the table
138     model = TestTableSchemaModel.buildTestModel(TABLE2);
139     TestTableSchemaModel.checkModel(model, TABLE2);
140     response = client.put(schemaPath, Constants.MIMETYPE_PROTOBUF,
141       model.createProtobufOutput());
142     assertEquals(response.getCode(), 201);
143 
144     // recall the same put operation but in read-only mode
145     conf.set("hbase.rest.readonly", "true");
146     response = client.put(schemaPath, Constants.MIMETYPE_PROTOBUF,
147       model.createProtobufOutput());
148     assertEquals(response.getCode(), 403);
149 
150     // retrieve the schema and validate it
151     response = client.get(schemaPath, Constants.MIMETYPE_PROTOBUF);
152     assertEquals(response.getCode(), 200);
153     assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
154     model = new TableSchemaModel();
155     model.getObjectFromMessage(response.getBody());
156     TestTableSchemaModel.checkModel(model, TABLE2);
157 
158     // retrieve the schema and validate it with alternate pbuf type
159     response = client.get(schemaPath, Constants.MIMETYPE_PROTOBUF_IETF);
160     assertEquals(response.getCode(), 200);
161     assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type"));
162     model = new TableSchemaModel();
163     model.getObjectFromMessage(response.getBody());
164     TestTableSchemaModel.checkModel(model, TABLE2);
165 
166     // delete the table
167     client.delete(schemaPath);
168 
169     // make sure HBase concurs
170     assertFalse(admin.tableExists(TABLE2));
171 
172     // return read-only setting back to default
173     conf.set("hbase.rest.readonly", "false");
174   }
175 
176   @org.junit.Rule
177   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
178     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
179 }
180