HBASE-22002 Remove the deprecated methods in Admin interface
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / client / TestAsyncNamespaceAdminApi.java
blob8de32b86a3471d33f466685262c4e84c880e2e1a
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.client;
20 import static org.apache.hadoop.hbase.client.AsyncProcess.START_LOG_ERRORS_AFTER_COUNT_KEY;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
25 import java.util.concurrent.Callable;
26 import org.apache.hadoop.hbase.HBaseClassTestRule;
27 import org.apache.hadoop.hbase.HColumnDescriptor;
28 import org.apache.hadoop.hbase.HConstants;
29 import org.apache.hadoop.hbase.HTableDescriptor;
30 import org.apache.hadoop.hbase.NamespaceDescriptor;
31 import org.apache.hadoop.hbase.NamespaceExistException;
32 import org.apache.hadoop.hbase.NamespaceNotFoundException;
33 import org.apache.hadoop.hbase.TableName;
34 import org.apache.hadoop.hbase.testclassification.ClientTests;
35 import org.apache.hadoop.hbase.testclassification.LargeTests;
36 import org.junit.BeforeClass;
37 import org.junit.ClassRule;
38 import org.junit.Test;
39 import org.junit.experimental.categories.Category;
40 import org.junit.runner.RunWith;
41 import org.junit.runners.Parameterized;
43 /**
44 * Class to test asynchronous namespace admin operations.
46 @RunWith(Parameterized.class)
47 @Category({ LargeTests.class, ClientTests.class })
48 public class TestAsyncNamespaceAdminApi extends TestAsyncAdminBase {
50 @ClassRule
51 public static final HBaseClassTestRule CLASS_RULE =
52 HBaseClassTestRule.forClass(TestAsyncNamespaceAdminApi.class);
54 private String prefix = "TestNamespace";
56 @BeforeClass
57 public static void setUpBeforeClass() throws Exception {
58 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_RPC_TIMEOUT_KEY, 60000);
59 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT, 120000);
60 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);
61 TEST_UTIL.getConfiguration().setInt(START_LOG_ERRORS_AFTER_COUNT_KEY, 0);
62 TEST_UTIL.startMiniCluster(1);
63 ASYNC_CONN = ConnectionFactory.createAsyncConnection(TEST_UTIL.getConfiguration()).get();
64 LOG.info("Done initializing cluster");
67 @Test
68 public void testCreateAndDelete() throws Exception {
69 String testName = "testCreateAndDelete";
70 String nsName = prefix + "_" + testName;
72 // create namespace and verify
73 admin.createNamespace(NamespaceDescriptor.create(nsName).build()).join();
74 assertEquals(3, admin.listNamespaceDescriptors().get().size());
75 // delete namespace and verify
76 admin.deleteNamespace(nsName).join();
77 assertEquals(2, admin.listNamespaceDescriptors().get().size());
80 @Test
81 public void testDeleteReservedNS() throws Exception {
82 boolean exceptionCaught = false;
83 try {
84 admin.deleteNamespace(NamespaceDescriptor.DEFAULT_NAMESPACE_NAME_STR).join();
85 } catch (Exception exp) {
86 LOG.warn(exp.toString(), exp);
87 exceptionCaught = true;
88 } finally {
89 assertTrue(exceptionCaught);
92 try {
93 admin.deleteNamespace(NamespaceDescriptor.SYSTEM_NAMESPACE_NAME_STR).join();
94 } catch (Exception exp) {
95 LOG.warn(exp.toString(), exp);
96 exceptionCaught = true;
97 } finally {
98 assertTrue(exceptionCaught);
102 @Test
103 public void testNamespaceOperations() throws Exception {
104 admin.createNamespace(NamespaceDescriptor.create(prefix + "ns1").build()).join();
105 admin.createNamespace(NamespaceDescriptor.create(prefix + "ns2").build()).join();
107 // create namespace that already exists
108 runWithExpectedException(new Callable<Void>() {
109 @Override
110 public Void call() throws Exception {
111 admin.createNamespace(NamespaceDescriptor.create(prefix + "ns1").build()).join();
112 return null;
114 }, NamespaceExistException.class);
116 // create a table in non-existing namespace
117 runWithExpectedException(new Callable<Void>() {
118 @Override
119 public Void call() throws Exception {
120 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("non_existing_namespace",
121 "table1"));
122 htd.addFamily(new HColumnDescriptor("family1"));
123 admin.createTable(htd).join();
124 return null;
126 }, NamespaceNotFoundException.class);
128 // get descriptor for existing namespace
129 NamespaceDescriptor ns1 = admin.getNamespaceDescriptor(prefix + "ns1").get();
130 assertEquals(prefix + "ns1", ns1.getName());
132 // get descriptor for non-existing namespace
133 runWithExpectedException(new Callable<NamespaceDescriptor>() {
134 @Override
135 public NamespaceDescriptor call() throws Exception {
136 return admin.getNamespaceDescriptor("non_existing_namespace").get();
138 }, NamespaceNotFoundException.class);
140 // delete descriptor for existing namespace
141 admin.deleteNamespace(prefix + "ns2").join();
143 // delete descriptor for non-existing namespace
144 runWithExpectedException(new Callable<Void>() {
145 @Override
146 public Void call() throws Exception {
147 admin.deleteNamespace("non_existing_namespace").join();
148 return null;
150 }, NamespaceNotFoundException.class);
152 // modify namespace descriptor for existing namespace
153 ns1 = admin.getNamespaceDescriptor(prefix + "ns1").get();
154 ns1.setConfiguration("foo", "bar");
155 admin.modifyNamespace(ns1).join();
156 ns1 = admin.getNamespaceDescriptor(prefix + "ns1").get();
157 assertEquals("bar", ns1.getConfigurationValue("foo"));
159 // modify namespace descriptor for non-existing namespace
160 runWithExpectedException(new Callable<Void>() {
161 @Override
162 public Void call() throws Exception {
163 admin.modifyNamespace(NamespaceDescriptor.create("non_existing_namespace").build()).join();
164 return null;
166 }, NamespaceNotFoundException.class);
168 admin.deleteNamespace(prefix + "ns1").join();
171 private static <V, E> void runWithExpectedException(Callable<V> callable, Class<E> exceptionClass) {
172 try {
173 callable.call();
174 } catch (Exception ex) {
175 LOG.info("Get exception is " + ex);
176 assertEquals(exceptionClass, ex.getCause().getClass());
177 return;
179 fail("Should have thrown exception " + exceptionClass);