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.client; 019 020import static org.junit.Assert.assertEquals; 021 022import java.io.IOException; 023import org.apache.hadoop.hbase.HBaseClassTestRule; 024import org.apache.hadoop.hbase.HBaseTestingUtility; 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 HBaseTestingUtility TEST_UTIL 050 = new HBaseTestingUtility(); 051 052 @BeforeClass 053 public static void beforeClass() throws Exception { 054 TEST_UTIL.startMiniCluster(1); 055 } 056 057 @AfterClass 058 public static void afterClass() throws Exception { 059 TEST_UTIL.shutdownMiniCluster(); 060 } 061 062 @Test 063 public void testNamespaceExists() throws Exception { 064 final String NONEXISTENT_NAMESPACE = "xyzpdq_nonexistent"; 065 final String EXISTING_NAMESPACE = "pdqxyz_myExistingNamespace"; 066 boolean exists; 067 Admin admin = TEST_UTIL.getAdmin(); 068 069 exists = HelloHBase.namespaceExists(admin, NONEXISTENT_NAMESPACE); 070 assertEquals("#namespaceExists failed: found nonexistent namespace.", 071 false, exists); 072 073 admin.createNamespace(NamespaceDescriptor.create(EXISTING_NAMESPACE).build()); 074 exists = HelloHBase.namespaceExists(admin, EXISTING_NAMESPACE); 075 assertEquals("#namespaceExists failed: did NOT find existing namespace.", 076 true, exists); 077 admin.deleteNamespace(EXISTING_NAMESPACE); 078 } 079 080 @Test 081 public void testCreateNamespaceAndTable() throws Exception { 082 Admin admin = TEST_UTIL.getAdmin(); 083 HelloHBase.createNamespaceAndTable(admin); 084 085 boolean namespaceExists 086 = HelloHBase.namespaceExists(admin, HelloHBase.MY_NAMESPACE_NAME); 087 assertEquals("#createNamespaceAndTable failed to create namespace.", 088 true, namespaceExists); 089 090 boolean tableExists = admin.tableExists(HelloHBase.MY_TABLE_NAME); 091 assertEquals("#createNamespaceAndTable failed to create table.", 092 true, tableExists); 093 094 admin.disableTable(HelloHBase.MY_TABLE_NAME); 095 admin.deleteTable(HelloHBase.MY_TABLE_NAME); 096 admin.deleteNamespace(HelloHBase.MY_NAMESPACE_NAME); 097 } 098 099 @Test 100 public void testPutRowToTable() throws IOException { 101 Admin admin = TEST_UTIL.getAdmin(); 102 admin.createNamespace(NamespaceDescriptor.create(HelloHBase.MY_NAMESPACE_NAME).build()); 103 Table table 104 = TEST_UTIL.createTable(HelloHBase.MY_TABLE_NAME, HelloHBase.MY_COLUMN_FAMILY_NAME); 105 106 HelloHBase.putRowToTable(table); 107 Result row = table.get(new Get(HelloHBase.MY_ROW_ID)); 108 assertEquals("#putRowToTable failed to store row.", false, row.isEmpty()); 109 110 TEST_UTIL.deleteTable(HelloHBase.MY_TABLE_NAME); 111 admin.deleteNamespace(HelloHBase.MY_NAMESPACE_NAME); 112 } 113 114 @Test 115 public void testDeleteRow() throws IOException { 116 Admin admin = TEST_UTIL.getAdmin(); 117 admin.createNamespace(NamespaceDescriptor.create(HelloHBase.MY_NAMESPACE_NAME).build()); 118 Table table 119 = TEST_UTIL.createTable(HelloHBase.MY_TABLE_NAME, HelloHBase.MY_COLUMN_FAMILY_NAME); 120 121 table.put(new Put(HelloHBase.MY_ROW_ID). 122 addColumn(HelloHBase.MY_COLUMN_FAMILY_NAME, 123 HelloHBase.MY_FIRST_COLUMN_QUALIFIER, 124 Bytes.toBytes("xyz"))); 125 HelloHBase.deleteRow(table); 126 Result row = table.get(new Get(HelloHBase.MY_ROW_ID)); 127 assertEquals("#deleteRow failed to delete row.", true, row.isEmpty()); 128 129 TEST_UTIL.deleteTable(HelloHBase.MY_TABLE_NAME); 130 admin.deleteNamespace(HelloHBase.MY_NAMESPACE_NAME); 131 } 132}