1   /** 
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.avro;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.nio.ByteBuffer;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import org.apache.hadoop.hbase.HBaseClusterTestCase;
29  import org.apache.hadoop.hbase.HBaseTestingUtility;
30  import org.apache.hadoop.hbase.util.Bytes;
31  import org.junit.After;
32  import org.junit.AfterClass;
33  import org.junit.Before;
34  import org.junit.BeforeClass;
35  import org.junit.Ignore;
36  import org.junit.Test;
37  
38  import org.apache.avro.Schema;
39  import org.apache.avro.generic.GenericArray;
40  import org.apache.avro.generic.GenericData;
41  
42  import org.apache.hadoop.hbase.avro.generated.AColumn;
43  import org.apache.hadoop.hbase.avro.generated.AColumnValue;
44  import org.apache.hadoop.hbase.avro.generated.AFamilyDescriptor;
45  import org.apache.hadoop.hbase.avro.generated.AGet;
46  import org.apache.hadoop.hbase.avro.generated.APut;
47  import org.apache.hadoop.hbase.avro.generated.AResult;
48  import org.apache.hadoop.hbase.avro.generated.ATableDescriptor;
49  
50  /**
51   * Unit testing for AvroServer.HBaseImpl, a part of the
52   * org.apache.hadoop.hbase.avro package.
53   */
54  public class TestAvroServer {
55    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
56  
57    // Static names for tables, columns, rows, and values
58    // TODO(hammer): Better style to define these in test method?
59    private static ByteBuffer tableAname = ByteBuffer.wrap(Bytes.toBytes("tableA"));
60    private static ByteBuffer tableBname = ByteBuffer.wrap(Bytes.toBytes("tableB"));
61    private static ByteBuffer familyAname = ByteBuffer.wrap(Bytes.toBytes("FamilyA"));
62    private static ByteBuffer qualifierAname = ByteBuffer.wrap(Bytes.toBytes("QualifierA"));
63    private static ByteBuffer rowAname = ByteBuffer.wrap(Bytes.toBytes("RowA"));
64    private static ByteBuffer valueA = ByteBuffer.wrap(Bytes.toBytes("ValueA"));
65  
66    /**
67     * @throws java.lang.Exception
68     */
69    @BeforeClass
70    public static void setUpBeforeClass() throws Exception {
71      TEST_UTIL.startMiniCluster(3);
72    }
73  
74    /**
75     * @throws java.lang.Exception
76     */
77    @AfterClass
78    public static void tearDownAfterClass() throws Exception {
79      TEST_UTIL.shutdownMiniCluster();
80    }
81  
82    /**
83     * @throws java.lang.Exception
84     */
85    @Before
86    public void setUp() throws Exception {
87      // Nothing to do.
88    }
89  
90    /**
91     * @throws java.lang.Exception
92     */
93    @After
94    public void tearDown() throws Exception {
95      // Nothing to do.
96    }
97  
98    /**
99     * Tests for creating, enabling, disabling, modifying, and deleting tables.
100    *
101    * @throws Exception
102    */
103   @Test
104   public void testTableAdminAndMetadata() throws Exception {
105     AvroServer.HBaseImpl impl = new AvroServer.HBaseImpl();
106 
107     assertEquals(impl.listTables().size(), 0);
108 
109     ATableDescriptor tableA = new ATableDescriptor();
110     tableA.name = tableAname;
111     impl.createTable(tableA);
112     assertEquals(impl.listTables().size(), 1);
113     assertTrue(impl.isTableEnabled(tableAname));
114     assertTrue(impl.tableExists(tableAname));
115 
116     ATableDescriptor tableB = new ATableDescriptor();
117     tableB.name = tableBname;
118     impl.createTable(tableB);
119     assertEquals(impl.listTables().size(), 2);
120 
121     impl.disableTable(tableBname);
122     assertFalse(impl.isTableEnabled(tableBname));
123 
124     impl.deleteTable(tableBname);
125     assertEquals(impl.listTables().size(), 1);
126 
127     impl.disableTable(tableAname);
128     assertFalse(impl.isTableEnabled(tableAname));
129 
130     tableA.maxFileSize = 123456L;
131     impl.modifyTable(tableAname, tableA);
132     assertEquals((long) impl.describeTable(tableAname).maxFileSize, 123456L);
133 
134     impl.enableTable(tableAname);
135     assertTrue(impl.isTableEnabled(tableAname));
136     impl.disableTable(tableAname);
137     impl.deleteTable(tableAname);
138   }
139 
140   /**
141    * Tests for creating, modifying, and deleting column families.
142    *
143    * @throws Exception
144    */
145   @Test
146   public void testFamilyAdminAndMetadata() throws Exception {
147     AvroServer.HBaseImpl impl = new AvroServer.HBaseImpl();
148 
149     ATableDescriptor tableA = new ATableDescriptor();
150     tableA.name = tableAname;
151     AFamilyDescriptor familyA = new AFamilyDescriptor();
152     familyA.name = familyAname;
153     Schema familyArraySchema = Schema.createArray(AFamilyDescriptor.SCHEMA$);
154     GenericArray<AFamilyDescriptor> families = new GenericData.Array<AFamilyDescriptor>(1, familyArraySchema);
155     families.add(familyA);
156     tableA.families = families;
157     impl.createTable(tableA);    
158     assertEquals(impl.describeTable(tableAname).families.size(), 1);
159 
160     impl.disableTable(tableAname);
161     assertFalse(impl.isTableEnabled(tableAname));
162 
163     familyA.maxVersions = 123456;
164     impl.modifyFamily(tableAname, familyAname, familyA);
165     assertEquals((int) impl.describeFamily(tableAname, familyAname).maxVersions, 123456);
166 
167     impl.deleteFamily(tableAname, familyAname);
168     assertEquals(impl.describeTable(tableAname).families.size(), 0);
169 
170     impl.disableTable(tableAname);
171     impl.deleteTable(tableAname);
172   }
173 
174   /**
175    * Tests for adding, reading, and deleting data.
176    *
177    * @throws Exception
178    */
179   @Test
180   public void testDML() throws Exception {
181     AvroServer.HBaseImpl impl = new AvroServer.HBaseImpl();
182 
183     ATableDescriptor tableA = new ATableDescriptor();
184     tableA.name = tableAname;
185     AFamilyDescriptor familyA = new AFamilyDescriptor();
186     familyA.name = familyAname;
187     Schema familyArraySchema = Schema.createArray(AFamilyDescriptor.SCHEMA$);
188     GenericArray<AFamilyDescriptor> families = new GenericData.Array<AFamilyDescriptor>(1, familyArraySchema);
189     families.add(familyA);
190     tableA.families = families;
191     impl.createTable(tableA);
192     assertEquals(impl.describeTable(tableAname).families.size(), 1);
193 
194     AGet getA = new AGet();
195     getA.row = rowAname;
196     Schema columnsSchema = Schema.createArray(AColumn.SCHEMA$);
197     GenericArray<AColumn> columns = new GenericData.Array<AColumn>(1, columnsSchema);
198     AColumn column = new AColumn();
199     column.family = familyAname;
200     column.qualifier = qualifierAname;
201     columns.add(column);
202     getA.columns = columns;
203    
204     assertFalse(impl.exists(tableAname, getA));
205 
206     APut putA = new APut();
207     putA.row = rowAname;
208     Schema columnValuesSchema = Schema.createArray(AColumnValue.SCHEMA$);
209     GenericArray<AColumnValue> columnValues = new GenericData.Array<AColumnValue>(1, columnValuesSchema);
210     AColumnValue acv = new AColumnValue();
211     acv.family = familyAname;
212     acv.qualifier = qualifierAname;
213     acv.value = valueA;
214     columnValues.add(acv);
215     putA.columnValues = columnValues;
216 
217     impl.put(tableAname, putA);
218     assertTrue(impl.exists(tableAname, getA));
219 
220     assertEquals(impl.get(tableAname, getA).entries.size(), 1);
221 
222     impl.disableTable(tableAname);
223     impl.deleteTable(tableAname);
224   }
225 }