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.client;
22  
23  import org.apache.hadoop.hbase.HColumnDescriptor;
24  import org.apache.hadoop.hbase.HTableDescriptor;
25  import org.apache.hadoop.hbase.client.HBaseAdmin;
26  import org.apache.hadoop.hbase.rest.HBaseRESTClusterTestBase;
27  import org.apache.hadoop.hbase.rest.client.Client;
28  import org.apache.hadoop.hbase.util.Bytes;
29  
30  public class TestRemoteAdmin extends HBaseRESTClusterTestBase {
31  
32    static final String TABLE_1 = "TestRemoteAdmin_Table_1";
33    static final String TABLE_2 = "TestRemoteAdmin_Table_2";
34    static final byte[] COLUMN_1 = Bytes.toBytes("a");
35  
36    static final HTableDescriptor DESC_1;
37    static {
38      DESC_1 = new HTableDescriptor(TABLE_1);
39      DESC_1.addFamily(new HColumnDescriptor(COLUMN_1));
40    }
41    static final HTableDescriptor DESC_2;
42    static {
43      DESC_2 = new HTableDescriptor(TABLE_2);
44      DESC_2.addFamily(new HColumnDescriptor(COLUMN_1));
45    }
46  
47    Client client;
48    HBaseAdmin localAdmin;
49    RemoteAdmin remoteAdmin;
50  
51    @Override
52    protected void setUp() throws Exception {
53      super.setUp();
54      localAdmin = new HBaseAdmin(conf);
55      remoteAdmin = new RemoteAdmin(new Client(
56          new Cluster().add("localhost", testServletPort)),
57        conf);
58      if (localAdmin.tableExists(TABLE_1)) {
59        localAdmin.disableTable(TABLE_1);
60        localAdmin.deleteTable(TABLE_1);
61      }
62      if (!localAdmin.tableExists(TABLE_2)) {
63        localAdmin.createTable(DESC_2);
64      }
65    }
66  
67    @Override
68    protected void tearDown() throws Exception {
69      super.tearDown();
70    }
71  
72    public void testCreateTable() throws Exception {
73      assertFalse(remoteAdmin.isTableAvailable(TABLE_1));
74      remoteAdmin.createTable(DESC_1);
75      assertTrue(remoteAdmin.isTableAvailable(TABLE_1));
76    }
77  
78    public void testDeleteTable() throws Exception {
79      assertTrue(remoteAdmin.isTableAvailable(TABLE_2));
80      remoteAdmin.deleteTable(TABLE_2);
81      assertFalse(remoteAdmin.isTableAvailable(TABLE_2));
82    }
83  }