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.hbase.client.HBaseAdmin;
31  import org.apache.hadoop.hbase.rest.client.Client;
32  import org.apache.hadoop.hbase.rest.client.Cluster;
33  import org.apache.hadoop.hbase.rest.client.Response;
34  import org.apache.hadoop.hbase.rest.model.ColumnSchemaModel;
35  import org.apache.hadoop.hbase.rest.model.TableSchemaModel;
36  import org.apache.hadoop.hbase.rest.model.TestTableSchemaModel;
37  import org.apache.hadoop.hbase.util.Bytes;
38  
39  public class TestSchemaResource extends HBaseRESTClusterTestBase {
40    static String TABLE1 = "TestSchemaResource1";
41    static String TABLE2 = "TestSchemaResource2";
42  
43    Client client;
44    JAXBContext context;
45    HBaseAdmin admin;
46  
47    @Override
48    protected void setUp() throws Exception {
49      super.setUp();
50      context = JAXBContext.newInstance(
51          ColumnSchemaModel.class,
52          TableSchemaModel.class);
53      admin = new HBaseAdmin(conf);
54      client = new Client(new Cluster().add("localhost", testServletPort));
55    }
56  
57    @Override
58    protected void tearDown() throws Exception {
59      client.shutdown();
60      super.tearDown();
61    }
62  
63    byte[] toXML(TableSchemaModel model) throws JAXBException {
64      StringWriter writer = new StringWriter();
65      context.createMarshaller().marshal(model, writer);
66      return Bytes.toBytes(writer.toString());
67    }
68  
69    TableSchemaModel fromXML(byte[] content) throws JAXBException {
70      return (TableSchemaModel) context.createUnmarshaller()
71        .unmarshal(new ByteArrayInputStream(content));
72    }
73  
74    void doTestTableCreateAndDeleteXML() throws IOException, JAXBException {
75      String schemaPath = "/" + TABLE1 + "/schema";
76      TableSchemaModel model;
77      Response response;
78  
79      assertFalse(admin.tableExists(TABLE1));
80  
81      // create the table
82      model = TestTableSchemaModel.buildTestModel(TABLE1);
83      TestTableSchemaModel.checkModel(model, TABLE1);
84      response = client.put(schemaPath, MIMETYPE_XML, toXML(model));
85      assertEquals(response.getCode(), 201);
86  
87      // make sure HBase concurs, and wait for the table to come online
88      admin.enableTable(TABLE1);
89  
90      // retrieve the schema and validate it
91      response = client.get(schemaPath, MIMETYPE_XML);
92      assertEquals(response.getCode(), 200);
93      model = fromXML(response.getBody());
94      TestTableSchemaModel.checkModel(model, TABLE1);
95  
96      // delete the table
97      client.delete(schemaPath);
98  
99      // make sure HBase concurs
100     assertFalse(admin.tableExists(TABLE1));
101   }
102 
103   void doTestTableCreateAndDeletePB() throws IOException, JAXBException {
104     String schemaPath = "/" + TABLE2 + "/schema";
105     TableSchemaModel model;
106     Response response;
107 
108     assertFalse(admin.tableExists(TABLE2));
109 
110     // create the table
111     model = TestTableSchemaModel.buildTestModel(TABLE2);
112     TestTableSchemaModel.checkModel(model, TABLE2);
113     response = client.put(schemaPath, Constants.MIMETYPE_PROTOBUF,
114       model.createProtobufOutput());
115     assertEquals(response.getCode(), 201);
116 
117     // make sure HBase concurs, and wait for the table to come online
118     admin.enableTable(TABLE2);
119 
120     // retrieve the schema and validate it
121     response = client.get(schemaPath, Constants.MIMETYPE_PROTOBUF);
122     assertEquals(response.getCode(), 200);
123     model = new TableSchemaModel();
124     model.getObjectFromMessage(response.getBody());
125     TestTableSchemaModel.checkModel(model, TABLE2);
126 
127     // delete the table
128     client.delete(schemaPath);
129 
130     // make sure HBase concurs
131     assertFalse(admin.tableExists(TABLE2));
132   }
133 
134   public void testSchemaResource() throws Exception {
135     doTestTableCreateAndDeleteXML();
136     doTestTableCreateAndDeletePB();
137   }
138 }