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.hbase.archetypes.exemplars.shaded_client;
019
020import static org.junit.Assert.assertEquals;
021
022import java.io.IOException;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseTestingUtil;
025import org.apache.hadoop.hbase.NamespaceDescriptor;
026import org.apache.hadoop.hbase.client.Admin;
027import org.apache.hadoop.hbase.client.Get;
028import org.apache.hadoop.hbase.client.Put;
029import org.apache.hadoop.hbase.client.Result;
030import org.apache.hadoop.hbase.client.Table;
031import org.apache.hadoop.hbase.testclassification.MediumTests;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.junit.AfterClass;
034import org.junit.BeforeClass;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038
039/**
040 * Unit testing for HelloHBase.
041 */
042@Category(MediumTests.class)
043public class TestHelloHBase {
044
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047    HBaseClassTestRule.forClass(TestHelloHBase.class);
048
049  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
050
051  @BeforeClass
052  public static void beforeClass() throws Exception {
053    TEST_UTIL.startMiniCluster(1);
054  }
055
056  @AfterClass
057  public static void afterClass() throws Exception {
058    TEST_UTIL.shutdownMiniCluster();
059  }
060
061  @Test
062  public void testNamespaceExists() throws Exception {
063    final String NONEXISTENT_NAMESPACE = "xyzpdq_nonexistent";
064    final String EXISTING_NAMESPACE = "pdqxyz_myExistingNamespace";
065    boolean exists;
066    Admin admin = TEST_UTIL.getAdmin();
067
068    exists = HelloHBase.namespaceExists(admin, NONEXISTENT_NAMESPACE);
069    assertEquals("#namespaceExists failed: found nonexistent namespace.", false, exists);
070
071    admin.createNamespace(NamespaceDescriptor.create(EXISTING_NAMESPACE).build());
072    exists = HelloHBase.namespaceExists(admin, EXISTING_NAMESPACE);
073    assertEquals("#namespaceExists failed: did NOT find existing namespace.", true, exists);
074    admin.deleteNamespace(EXISTING_NAMESPACE);
075  }
076
077  @Test
078  public void testCreateNamespaceAndTable() throws Exception {
079    Admin admin = TEST_UTIL.getAdmin();
080    HelloHBase.createNamespaceAndTable(admin);
081
082    boolean namespaceExists = HelloHBase.namespaceExists(admin, HelloHBase.MY_NAMESPACE_NAME);
083    assertEquals("#createNamespaceAndTable failed to create namespace.", true, namespaceExists);
084
085    boolean tableExists = admin.tableExists(HelloHBase.MY_TABLE_NAME);
086    assertEquals("#createNamespaceAndTable failed to create table.", true, tableExists);
087
088    admin.disableTable(HelloHBase.MY_TABLE_NAME);
089    admin.deleteTable(HelloHBase.MY_TABLE_NAME);
090    admin.deleteNamespace(HelloHBase.MY_NAMESPACE_NAME);
091  }
092
093  @Test
094  public void testPutRowToTable() throws IOException {
095    Admin admin = TEST_UTIL.getAdmin();
096    admin.createNamespace(NamespaceDescriptor.create(HelloHBase.MY_NAMESPACE_NAME).build());
097    Table table = TEST_UTIL.createTable(HelloHBase.MY_TABLE_NAME, HelloHBase.MY_COLUMN_FAMILY_NAME);
098
099    HelloHBase.putRowToTable(table);
100    Result row = table.get(new Get(HelloHBase.MY_ROW_ID));
101    assertEquals("#putRowToTable failed to store row.", false, row.isEmpty());
102
103    TEST_UTIL.deleteTable(HelloHBase.MY_TABLE_NAME);
104    admin.deleteNamespace(HelloHBase.MY_NAMESPACE_NAME);
105  }
106
107  @Test
108  public void testDeleteRow() throws IOException {
109    Admin admin = TEST_UTIL.getAdmin();
110    admin.createNamespace(NamespaceDescriptor.create(HelloHBase.MY_NAMESPACE_NAME).build());
111    Table table = TEST_UTIL.createTable(HelloHBase.MY_TABLE_NAME, HelloHBase.MY_COLUMN_FAMILY_NAME);
112
113    table.put(new Put(HelloHBase.MY_ROW_ID).addColumn(HelloHBase.MY_COLUMN_FAMILY_NAME,
114      HelloHBase.MY_FIRST_COLUMN_QUALIFIER, Bytes.toBytes("xyz")));
115    HelloHBase.deleteRow(table);
116    Result row = table.get(new Get(HelloHBase.MY_ROW_ID));
117    assertEquals("#deleteRow failed to delete row.", true, row.isEmpty());
118
119    TEST_UTIL.deleteTable(HelloHBase.MY_TABLE_NAME);
120    admin.deleteNamespace(HelloHBase.MY_NAMESPACE_NAME);
121  }
122}