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