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
.AsyncConnectionConfiguration
.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
.HConstants
;
28 import org
.apache
.hadoop
.hbase
.NamespaceDescriptor
;
29 import org
.apache
.hadoop
.hbase
.NamespaceExistException
;
30 import org
.apache
.hadoop
.hbase
.NamespaceNotFoundException
;
31 import org
.apache
.hadoop
.hbase
.TableName
;
32 import org
.apache
.hadoop
.hbase
.testclassification
.ClientTests
;
33 import org
.apache
.hadoop
.hbase
.testclassification
.LargeTests
;
34 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
35 import org
.junit
.BeforeClass
;
36 import org
.junit
.ClassRule
;
37 import org
.junit
.Test
;
38 import org
.junit
.experimental
.categories
.Category
;
39 import org
.junit
.runner
.RunWith
;
40 import org
.junit
.runners
.Parameterized
;
43 * Class to test asynchronous namespace admin operations.
45 @RunWith(Parameterized
.class)
46 @Category({ LargeTests
.class, ClientTests
.class })
47 public class TestAsyncNamespaceAdminApi
extends TestAsyncAdminBase
{
50 public static final HBaseClassTestRule CLASS_RULE
=
51 HBaseClassTestRule
.forClass(TestAsyncNamespaceAdminApi
.class);
53 private String prefix
= "TestNamespace";
56 public static void setUpBeforeClass() throws Exception
{
57 TEST_UTIL
.getConfiguration().setInt(HConstants
.HBASE_RPC_TIMEOUT_KEY
, 60000);
58 TEST_UTIL
.getConfiguration().setInt(HConstants
.HBASE_CLIENT_OPERATION_TIMEOUT
, 120000);
59 TEST_UTIL
.getConfiguration().setInt(HConstants
.HBASE_CLIENT_RETRIES_NUMBER
, 2);
60 TEST_UTIL
.getConfiguration().setInt(START_LOG_ERRORS_AFTER_COUNT_KEY
, 0);
61 TEST_UTIL
.startMiniCluster(1);
62 ASYNC_CONN
= ConnectionFactory
.createAsyncConnection(TEST_UTIL
.getConfiguration()).get();
63 LOG
.info("Done initializing cluster");
67 public void testCreateAndDelete() throws Exception
{
68 String testName
= "testCreateAndDelete";
69 String nsName
= prefix
+ "_" + testName
;
71 // create namespace and verify
72 admin
.createNamespace(NamespaceDescriptor
.create(nsName
).build()).join();
73 assertEquals(3, admin
.listNamespaces().get().size());
74 assertEquals(3, admin
.listNamespaceDescriptors().get().size());
75 // delete namespace and verify
76 admin
.deleteNamespace(nsName
).join();
77 assertEquals(2, admin
.listNamespaces().get().size());
78 assertEquals(2, admin
.listNamespaceDescriptors().get().size());
82 public void testDeleteReservedNS() throws Exception
{
83 boolean exceptionCaught
= false;
85 admin
.deleteNamespace(NamespaceDescriptor
.DEFAULT_NAMESPACE_NAME_STR
).join();
86 } catch (Exception exp
) {
87 LOG
.warn(exp
.toString(), exp
);
88 exceptionCaught
= true;
90 assertTrue(exceptionCaught
);
94 admin
.deleteNamespace(NamespaceDescriptor
.SYSTEM_NAMESPACE_NAME_STR
).join();
95 } catch (Exception exp
) {
96 LOG
.warn(exp
.toString(), exp
);
97 exceptionCaught
= true;
99 assertTrue(exceptionCaught
);
104 public void testNamespaceOperations() throws Exception
{
105 admin
.createNamespace(NamespaceDescriptor
.create(prefix
+ "ns1").build()).join();
106 admin
.createNamespace(NamespaceDescriptor
.create(prefix
+ "ns2").build()).join();
108 // create namespace that already exists
109 runWithExpectedException(new Callable
<Void
>() {
111 public Void
call() throws Exception
{
112 admin
.createNamespace(NamespaceDescriptor
.create(prefix
+ "ns1").build()).join();
115 }, NamespaceExistException
.class);
117 // create a table in non-existing namespace
118 runWithExpectedException(new Callable
<Void
>() {
120 public Void
call() throws Exception
{
121 TableDescriptorBuilder tableDescriptorBuilder
=
122 TableDescriptorBuilder
.newBuilder(TableName
.valueOf("non_existing_namespace",
124 ColumnFamilyDescriptor columnFamilyDescriptor
=
125 ColumnFamilyDescriptorBuilder
.newBuilder(Bytes
.toBytes("family1")).build();
126 tableDescriptorBuilder
.setColumnFamily(columnFamilyDescriptor
);
127 admin
.createTable(tableDescriptorBuilder
.build()).join();
130 }, NamespaceNotFoundException
.class);
132 // get descriptor for existing namespace
133 NamespaceDescriptor ns1
= admin
.getNamespaceDescriptor(prefix
+ "ns1").get();
134 assertEquals(prefix
+ "ns1", ns1
.getName());
136 // get descriptor for non-existing namespace
137 runWithExpectedException(new Callable
<NamespaceDescriptor
>() {
139 public NamespaceDescriptor
call() throws Exception
{
140 return admin
.getNamespaceDescriptor("non_existing_namespace").get();
142 }, NamespaceNotFoundException
.class);
144 // delete descriptor for existing namespace
145 admin
.deleteNamespace(prefix
+ "ns2").join();
147 // delete descriptor for non-existing namespace
148 runWithExpectedException(new Callable
<Void
>() {
150 public Void
call() throws Exception
{
151 admin
.deleteNamespace("non_existing_namespace").join();
154 }, NamespaceNotFoundException
.class);
156 // modify namespace descriptor for existing namespace
157 ns1
= admin
.getNamespaceDescriptor(prefix
+ "ns1").get();
158 ns1
.setConfiguration("foo", "bar");
159 admin
.modifyNamespace(ns1
).join();
160 ns1
= admin
.getNamespaceDescriptor(prefix
+ "ns1").get();
161 assertEquals("bar", ns1
.getConfigurationValue("foo"));
163 // modify namespace descriptor for non-existing namespace
164 runWithExpectedException(new Callable
<Void
>() {
166 public Void
call() throws Exception
{
167 admin
.modifyNamespace(NamespaceDescriptor
.create("non_existing_namespace").build()).join();
170 }, NamespaceNotFoundException
.class);
172 admin
.deleteNamespace(prefix
+ "ns1").join();
175 private static <V
, E
> void runWithExpectedException(Callable
<V
> callable
, Class
<E
> exceptionClass
) {
178 } catch (Exception ex
) {
179 LOG
.info("Get exception is " + ex
);
180 assertEquals(exceptionClass
, ex
.getCause().getClass());
183 fail("Should have thrown exception " + exceptionClass
);