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",
123            "table1"));
124        ColumnFamilyDescriptor columnFamilyDescriptor =
125          ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("family1")).build();
126        tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
127        admin.createTable(tableDescriptorBuilder.build()).join();
128        return null;
129      }
130    }, NamespaceNotFoundException.class);
131
132    // get descriptor for existing namespace
133    NamespaceDescriptor ns1 = admin.getNamespaceDescriptor(prefix + "ns1").get();
134    assertEquals(prefix + "ns1", ns1.getName());
135
136    // get descriptor for non-existing namespace
137    runWithExpectedException(new Callable<NamespaceDescriptor>() {
138      @Override
139      public NamespaceDescriptor call() throws Exception {
140        return admin.getNamespaceDescriptor("non_existing_namespace").get();
141      }
142    }, NamespaceNotFoundException.class);
143
144    // delete descriptor for existing namespace
145    admin.deleteNamespace(prefix + "ns2").join();
146
147    // delete descriptor for non-existing namespace
148    runWithExpectedException(new Callable<Void>() {
149      @Override
150      public Void call() throws Exception {
151        admin.deleteNamespace("non_existing_namespace").join();
152        return null;
153      }
154    }, NamespaceNotFoundException.class);
155
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"));
162
163    // modify namespace descriptor for non-existing namespace
164    runWithExpectedException(new Callable<Void>() {
165      @Override
166      public Void call() throws Exception {
167        admin.modifyNamespace(NamespaceDescriptor.create("non_existing_namespace").build()).join();
168        return null;
169      }
170    }, NamespaceNotFoundException.class);
171
172    admin.deleteNamespace(prefix + "ns1").join();
173  }
174
175  private static <V, E> void runWithExpectedException(Callable<V> callable, 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}