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 static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertFalse;
25  import static org.junit.Assert.assertTrue;
26  
27  import java.util.List;
28  
29  import org.apache.hadoop.hbase.ClusterStatus;
30  import org.apache.hadoop.hbase.HBaseTestingUtility;
31  import org.apache.hadoop.hbase.HColumnDescriptor;
32  import org.apache.hadoop.hbase.HTableDescriptor;
33  import org.apache.hadoop.hbase.MediumTests;
34  import org.apache.hadoop.hbase.rest.HBaseRESTTestingUtility;
35  import org.apache.hadoop.hbase.rest.model.StorageClusterStatusModel;
36  import org.apache.hadoop.hbase.rest.model.TableModel;
37  import org.apache.hadoop.hbase.rest.model.VersionModel;
38  import org.apache.hadoop.hbase.util.Bytes;
39  import org.junit.AfterClass;
40  import org.junit.BeforeClass;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  
44  @Category(MediumTests.class)
45  public class TestRemoteAdmin {
46    private static final HBaseTestingUtility TEST_UTIL =
47      new HBaseTestingUtility();
48    private static final HBaseRESTTestingUtility REST_TEST_UTIL =
49      new HBaseRESTTestingUtility();
50    private static final String TABLE_1 = "TestRemoteAdmin_Table_1";
51    private static final String TABLE_2 = TABLE_1 + System.currentTimeMillis();
52    private static final byte[] COLUMN_1 = Bytes.toBytes("a");
53    static final HTableDescriptor DESC_1 =  new HTableDescriptor(TABLE_1);
54    static final HTableDescriptor DESC_2 =  new HTableDescriptor(TABLE_2);
55    private static RemoteAdmin remoteAdmin;
56  
57    @BeforeClass
58    public static void setUpBeforeClass() throws Exception {
59      DESC_1.addFamily(new HColumnDescriptor(COLUMN_1));
60  
61      TEST_UTIL.startMiniCluster();
62      REST_TEST_UTIL.startServletContainer(TEST_UTIL.getConfiguration());
63  
64      remoteAdmin = new RemoteAdmin(new Client(
65        new Cluster().add("localhost", REST_TEST_UTIL.getServletPort())),
66        TEST_UTIL.getConfiguration());
67    }
68  
69    @AfterClass
70    public static void tearDownAfterClass() throws Exception {
71      REST_TEST_UTIL.shutdownServletContainer();
72      TEST_UTIL.shutdownMiniCluster();
73    }
74  
75    @Test
76    public void testCreateAnDeleteTable() throws Exception {
77      assertFalse(remoteAdmin.isTableAvailable(TABLE_1));
78      remoteAdmin.createTable(DESC_1);
79      assertTrue(remoteAdmin.isTableAvailable(TABLE_1));
80      remoteAdmin.deleteTable(TABLE_1);
81      assertFalse(remoteAdmin.isTableAvailable(TABLE_1));
82    }
83  
84    @Test
85    public void testGetRestVersion() throws Exception {
86  
87      VersionModel RETURNED_REST_VERSION = remoteAdmin.getRestVersion();
88      System.out.print("Returned version is: " + RETURNED_REST_VERSION);
89  
90      // Assert that it contains info about rest version, OS, JVM
91      assertTrue("Returned REST version did not contain info about rest.",
92          RETURNED_REST_VERSION.toString().contains("rest"));
93      assertTrue("Returned REST version did not contain info about the JVM.",
94          RETURNED_REST_VERSION.toString().contains("JVM"));
95      assertTrue("Returned REST version did not contain info about OS.",
96          RETURNED_REST_VERSION.toString().contains("OS"));
97    }
98  
99    @Test
100   public void testClusterVersion() throws Exception {
101     // testing the /version/cluster endpoint
102     final String HBASE_VERSION = TEST_UTIL.getHBaseCluster().getClusterStatus()
103         .getHBaseVersion();
104     assertEquals("Cluster status from REST API did not match. ", HBASE_VERSION,
105         remoteAdmin.getClusterVersion().getVersion());
106   }
107 
108   @Test
109   public void testClusterStatus() throws Exception {
110 
111     ClusterStatus status = TEST_UTIL.getHBaseClusterInterface()
112         .getClusterStatus();
113     StorageClusterStatusModel returnedStatus = remoteAdmin.getClusterStatus();
114     assertEquals(
115         "Region count from cluster status and returned status did not match up. ",
116         status.getRegionsCount(), returnedStatus.getRegions());
117     assertEquals(
118         "Dead server count from cluster status and returned status did not match up. ",
119         status.getDeadServers(), returnedStatus.getDeadNodes().size());
120   }
121 
122   @Test
123   public void testListTables() throws Exception {
124 
125     remoteAdmin.createTable(DESC_2);
126     List<TableModel> tableList = remoteAdmin.getTableList().getTables();
127     System.out.println("List of tables is: ");
128     boolean found = false;
129     for (TableModel tm : tableList) {
130 
131       if (tm.getName().equals(TABLE_2)) {
132         found = true;
133         break;
134       }
135     }
136     assertTrue("Table " + TABLE_2 + " was not found by get request to '/'",
137         found);
138   }
139 
140   @org.junit.Rule
141   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
142     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
143 }