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.HBaseTestingUtility;
24  import org.apache.hadoop.hbase.HColumnDescriptor;
25  import org.apache.hadoop.hbase.HTableDescriptor;
26  import org.apache.hadoop.hbase.client.HBaseAdmin;
27  import org.apache.hadoop.hbase.rest.HBaseRESTTestingUtility;
28  import org.apache.hadoop.hbase.rest.client.Client;
29  import org.apache.hadoop.hbase.util.Bytes;
30  
31  import static org.junit.Assert.*;
32  import org.junit.AfterClass;
33  import org.junit.BeforeClass;
34  import org.junit.Test;
35  
36  public class TestRemoteAdmin {
37  
38    private static final String TABLE_1 = "TestRemoteAdmin_Table_1";
39    private static final String TABLE_2 = "TestRemoteAdmin_Table_2";
40    private static final byte[] COLUMN_1 = Bytes.toBytes("a");
41  
42    static final HTableDescriptor DESC_1;
43    static {
44      DESC_1 = new HTableDescriptor(TABLE_1);
45      DESC_1.addFamily(new HColumnDescriptor(COLUMN_1));
46    }
47    static final HTableDescriptor DESC_2;
48    static {
49      DESC_2 = new HTableDescriptor(TABLE_2);
50      DESC_2.addFamily(new HColumnDescriptor(COLUMN_1));
51    }
52  
53    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
54    private static final HBaseRESTTestingUtility REST_TEST_UTIL = 
55      new HBaseRESTTestingUtility();
56    private static HBaseAdmin localAdmin;
57    private static RemoteAdmin remoteAdmin;
58  
59    @BeforeClass
60    public static void setUpBeforeClass() throws Exception {
61      TEST_UTIL.startMiniCluster(3);
62      REST_TEST_UTIL.startServletContainer(TEST_UTIL.getConfiguration());
63      localAdmin = TEST_UTIL.getHBaseAdmin();
64      remoteAdmin = new RemoteAdmin(new Client(
65        new Cluster().add("localhost", REST_TEST_UTIL.getServletPort())),
66        TEST_UTIL.getConfiguration());
67      if (localAdmin.tableExists(TABLE_1)) {
68        localAdmin.disableTable(TABLE_1);
69        localAdmin.deleteTable(TABLE_1);
70      }
71      if (!localAdmin.tableExists(TABLE_2)) {
72        localAdmin.createTable(DESC_2);
73      }
74    }
75  
76    @AfterClass
77    public static void tearDownAfterClass() throws Exception {
78      REST_TEST_UTIL.shutdownServletContainer();
79      TEST_UTIL.shutdownMiniCluster();
80    }
81  
82    @Test
83    public void testCreateTable() throws Exception {
84      assertFalse(remoteAdmin.isTableAvailable(TABLE_1));
85      remoteAdmin.createTable(DESC_1);
86      assertTrue(remoteAdmin.isTableAvailable(TABLE_1));
87    }
88  
89    @Test
90    public void testDeleteTable() throws Exception {
91      assertTrue(remoteAdmin.isTableAvailable(TABLE_2));
92      remoteAdmin.deleteTable(TABLE_2);
93      assertFalse(remoteAdmin.isTableAvailable(TABLE_2));
94    }
95  }