View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hbase.archetypes.exemplars.client;
20  
21  import java.io.IOException;
22  import org.apache.hadoop.hbase.HBaseTestingUtility;
23  import org.apache.hadoop.hbase.NamespaceDescriptor;
24  import org.apache.hadoop.hbase.client.Admin;
25  import org.apache.hadoop.hbase.client.Get;
26  import org.apache.hadoop.hbase.client.Put;
27  import org.apache.hadoop.hbase.client.Result;
28  import org.apache.hadoop.hbase.client.Table;
29  import org.apache.hadoop.hbase.testclassification.MediumTests;
30  import org.apache.hadoop.hbase.util.Bytes;
31  import org.junit.AfterClass;
32  import static org.junit.Assert.assertEquals;
33  import org.junit.BeforeClass;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  /**
38   * Unit testing for HelloHBase.
39   */
40  @Category(MediumTests.class)
41  public class TestHelloHBase {
42  
43    private static final HBaseTestingUtility TEST_UTIL
44            = new HBaseTestingUtility();
45  
46    @BeforeClass
47    public static void beforeClass() throws Exception {
48      TEST_UTIL.startMiniCluster(1);
49    }
50  
51    @AfterClass
52    public static void afterClass() throws Exception {
53      TEST_UTIL.shutdownMiniCluster();
54    }
55  
56    @Test
57    public void testNamespaceExists() throws Exception {
58      final String NONEXISTENT_NAMESPACE = "xyzpdq_nonexistent";
59      final String EXISTING_NAMESPACE = "pdqxyz_myExistingNamespace";
60      boolean exists;
61      Admin admin = TEST_UTIL.getHBaseAdmin();
62  
63      exists = HelloHBase.namespaceExists(admin, NONEXISTENT_NAMESPACE);
64      assertEquals("#namespaceExists failed: found nonexistent namespace.",
65              false, exists);
66  
67      admin.createNamespace
68          (NamespaceDescriptor.create(EXISTING_NAMESPACE).build());
69      exists = HelloHBase.namespaceExists(admin, EXISTING_NAMESPACE);
70      assertEquals("#namespaceExists failed: did NOT find existing namespace.",
71              true, exists);
72      admin.deleteNamespace(EXISTING_NAMESPACE);
73    }
74  
75    @Test
76    public void testCreateNamespaceAndTable() throws Exception {
77      Admin admin = TEST_UTIL.getHBaseAdmin();
78      HelloHBase.createNamespaceAndTable(admin);
79  
80      boolean namespaceExists
81              = HelloHBase.namespaceExists(admin, HelloHBase.MY_NAMESPACE_NAME);
82      assertEquals("#createNamespaceAndTable failed to create namespace.",
83              true, namespaceExists);
84  
85      boolean tableExists = admin.tableExists(HelloHBase.MY_TABLE_NAME);
86      assertEquals("#createNamespaceAndTable failed to create table.",
87              true, tableExists);
88  
89      admin.disableTable(HelloHBase.MY_TABLE_NAME);
90      admin.deleteTable(HelloHBase.MY_TABLE_NAME);
91      admin.deleteNamespace(HelloHBase.MY_NAMESPACE_NAME);
92    }
93  
94    @Test
95    public void testPutRowToTable() throws IOException {
96      Admin admin = TEST_UTIL.getHBaseAdmin();
97      admin.createNamespace
98          (NamespaceDescriptor.create(HelloHBase.MY_NAMESPACE_NAME).build());
99      Table table
100             = TEST_UTIL.createTable
101                 (HelloHBase.MY_TABLE_NAME, HelloHBase.MY_COLUMN_FAMILY_NAME);
102 
103     HelloHBase.putRowToTable(table);
104     Result row = table.get(new Get(HelloHBase.MY_ROW_ID));
105     assertEquals("#putRowToTable failed to store row.", false, row.isEmpty());
106 
107     TEST_UTIL.deleteTable(HelloHBase.MY_TABLE_NAME);
108     admin.deleteNamespace(HelloHBase.MY_NAMESPACE_NAME);
109   }
110 
111   @Test
112   public void testDeleteRow() throws IOException {
113     Admin admin = TEST_UTIL.getHBaseAdmin();
114     admin.createNamespace
115         (NamespaceDescriptor.create(HelloHBase.MY_NAMESPACE_NAME).build());
116     Table table
117             = TEST_UTIL.createTable
118                 (HelloHBase.MY_TABLE_NAME, HelloHBase.MY_COLUMN_FAMILY_NAME);
119 
120     table.put(new Put(HelloHBase.MY_ROW_ID).
121             addColumn(HelloHBase.MY_COLUMN_FAMILY_NAME,
122                     HelloHBase.MY_FIRST_COLUMN_QUALIFIER,
123                     Bytes.toBytes("xyz")));
124     HelloHBase.deleteRow(table);
125     Result row = table.get(new Get(HelloHBase.MY_ROW_ID));
126     assertEquals("#deleteRow failed to delete row.", true, row.isEmpty());
127 
128     TEST_UTIL.deleteTable(HelloHBase.MY_TABLE_NAME);
129     admin.deleteNamespace(HelloHBase.MY_NAMESPACE_NAME);
130   }
131 }