001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import static org.apache.hadoop.hbase.client.AsyncConnectionConfiguration.START_LOG_ERRORS_AFTER_COUNT_KEY;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertTrue;
023import static org.junit.Assert.fail;
024
025import java.util.concurrent.Callable;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.hadoop.hbase.NamespaceDescriptor;
029import org.apache.hadoop.hbase.NamespaceExistException;
030import org.apache.hadoop.hbase.NamespaceNotFoundException;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.testclassification.ClientTests;
033import org.apache.hadoop.hbase.testclassification.LargeTests;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.junit.BeforeClass;
036import org.junit.ClassRule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039import org.junit.runner.RunWith;
040import org.junit.runners.Parameterized;
041
042/**
043 * Class to test asynchronous namespace admin operations.
044 */
045@RunWith(Parameterized.class)
046@Category({ LargeTests.class, ClientTests.class })
047public class TestAsyncNamespaceAdminApi extends TestAsyncAdminBase {
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051    HBaseClassTestRule.forClass(TestAsyncNamespaceAdminApi.class);
052
053  private String prefix = "TestNamespace";
054
055  @BeforeClass
056  public static void setUpBeforeClass() throws Exception {
057    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_RPC_TIMEOUT_KEY, 60000);
058    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT, 120000);
059    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);
060    TEST_UTIL.getConfiguration().setInt(START_LOG_ERRORS_AFTER_COUNT_KEY, 0);
061    TEST_UTIL.startMiniCluster(1);
062    ASYNC_CONN = ConnectionFactory.createAsyncConnection(TEST_UTIL.getConfiguration()).get();
063    LOG.info("Done initializing cluster");
064  }
065
066  @Test
067  public void testCreateAndDelete() throws Exception {
068    String testName = "testCreateAndDelete";
069    String nsName = prefix + "_" + testName;
070
071    // create namespace and verify
072    admin.createNamespace(NamespaceDescriptor.create(nsName).build()).join();
073    assertEquals(3, admin.listNamespaces().get().size());
074    assertEquals(3, admin.listNamespaceDescriptors().get().size());
075    // delete namespace and verify
076    admin.deleteNamespace(nsName).join();
077    assertEquals(2, admin.listNamespaces().get().size());
078    assertEquals(2, admin.listNamespaceDescriptors().get().size());
079  }
080
081  @Test
082  public void testDeleteReservedNS() throws Exception {
083    boolean exceptionCaught = false;
084    try {
085      admin.deleteNamespace(NamespaceDescriptor.DEFAULT_NAMESPACE_NAME_STR).join();
086    } catch (Exception exp) {
087      LOG.warn(exp.toString(), exp);
088      exceptionCaught = true;
089    } finally {
090      assertTrue(exceptionCaught);
091    }
092
093    try {
094      admin.deleteNamespace(NamespaceDescriptor.SYSTEM_NAMESPACE_NAME_STR).join();
095    } catch (Exception exp) {
096      LOG.warn(exp.toString(), exp);
097      exceptionCaught = true;
098    } finally {
099      assertTrue(exceptionCaught);
100    }
101  }
102
103  @Test
104  public void testNamespaceOperations() throws Exception {
105    admin.createNamespace(NamespaceDescriptor.create(prefix + "ns1").build()).join();
106    admin.createNamespace(NamespaceDescriptor.create(prefix + "ns2").build()).join();
107
108    // create namespace that already exists
109    runWithExpectedException(new Callable<Void>() {
110      @Override
111      public Void call() throws Exception {
112        admin.createNamespace(NamespaceDescriptor.create(prefix + "ns1").build()).join();
113        return null;
114      }
115    }, NamespaceExistException.class);
116
117    // create a table in non-existing namespace
118    runWithExpectedException(new Callable<Void>() {
119      @Override
120      public Void call() throws Exception {
121        TableDescriptorBuilder tableDescriptorBuilder =
122          TableDescriptorBuilder.newBuilder(TableName.valueOf("non_existing_namespace", "table1"));
123        ColumnFamilyDescriptor columnFamilyDescriptor =
124          ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("family1")).build();
125        tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
126        admin.createTable(tableDescriptorBuilder.build()).join();
127        return null;
128      }
129    }, NamespaceNotFoundException.class);
130
131    // get descriptor for existing namespace
132    NamespaceDescriptor ns1 = admin.getNamespaceDescriptor(prefix + "ns1").get();
133    assertEquals(prefix + "ns1", ns1.getName());
134
135    // get descriptor for non-existing namespace
136    runWithExpectedException(new Callable<NamespaceDescriptor>() {
137      @Override
138      public NamespaceDescriptor call() throws Exception {
139        return admin.getNamespaceDescriptor("non_existing_namespace").get();
140      }
141    }, NamespaceNotFoundException.class);
142
143    // delete descriptor for existing namespace
144    admin.deleteNamespace(prefix + "ns2").join();
145
146    // delete descriptor for non-existing namespace
147    runWithExpectedException(new Callable<Void>() {
148      @Override
149      public Void call() throws Exception {
150        admin.deleteNamespace("non_existing_namespace").join();
151        return null;
152      }
153    }, NamespaceNotFoundException.class);
154
155    // modify namespace descriptor for existing namespace
156    ns1 = admin.getNamespaceDescriptor(prefix + "ns1").get();
157    ns1.setConfiguration("foo", "bar");
158    admin.modifyNamespace(ns1).join();
159    ns1 = admin.getNamespaceDescriptor(prefix + "ns1").get();
160    assertEquals("bar", ns1.getConfigurationValue("foo"));
161
162    // modify namespace descriptor for non-existing namespace
163    runWithExpectedException(new Callable<Void>() {
164      @Override
165      public Void call() throws Exception {
166        admin.modifyNamespace(NamespaceDescriptor.create("non_existing_namespace").build()).join();
167        return null;
168      }
169    }, NamespaceNotFoundException.class);
170
171    admin.deleteNamespace(prefix + "ns1").join();
172  }
173
174  private static <V, E> void runWithExpectedException(Callable<V> callable,
175    Class<E> exceptionClass) {
176    try {
177      callable.call();
178    } catch (Exception ex) {
179      LOG.info("Get exception is " + ex);
180      assertEquals(exceptionClass, ex.getCause().getClass());
181      return;
182    }
183    fail("Should have thrown exception " + exceptionClass);
184  }
185}