1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
26 import org.apache.avro.Schema;
27 import org.apache.avro.generic.GenericArray;
28 import org.apache.avro.generic.GenericData;
29 import org.apache.hadoop.hbase.HBaseTestingUtility;
30 import org.apache.hadoop.hbase.avro.generated.AColumn;
31 import org.apache.hadoop.hbase.avro.generated.AColumnValue;
32 import org.apache.hadoop.hbase.avro.generated.AFamilyDescriptor;
33 import org.apache.hadoop.hbase.avro.generated.AGet;
34 import org.apache.hadoop.hbase.avro.generated.APut;
35 import org.apache.hadoop.hbase.avro.generated.ATableDescriptor;
36 import org.apache.hadoop.hbase.util.Bytes;
37 import org.apache.hadoop.hbase.util.Threads;
38 import org.junit.After;
39 import org.junit.AfterClass;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43
44
45
46
47
48 public class TestAvroServer {
49 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
50
51
52
53 private static ByteBuffer tableAname = ByteBuffer.wrap(Bytes.toBytes("tableA"));
54 private static ByteBuffer tableBname = ByteBuffer.wrap(Bytes.toBytes("tableB"));
55 private static ByteBuffer familyAname = ByteBuffer.wrap(Bytes.toBytes("FamilyA"));
56 private static ByteBuffer qualifierAname = ByteBuffer.wrap(Bytes.toBytes("QualifierA"));
57 private static ByteBuffer rowAname = ByteBuffer.wrap(Bytes.toBytes("RowA"));
58 private static ByteBuffer valueA = ByteBuffer.wrap(Bytes.toBytes("ValueA"));
59
60
61
62
63 @BeforeClass
64 public static void setUpBeforeClass() throws Exception {
65 TEST_UTIL.startMiniCluster(3);
66 }
67
68
69
70
71 @AfterClass
72 public static void tearDownAfterClass() throws Exception {
73 TEST_UTIL.shutdownMiniCluster();
74 }
75
76
77
78
79 @Before
80 public void setUp() throws Exception {
81
82 }
83
84
85
86
87 @After
88 public void tearDown() throws Exception {
89
90 }
91
92
93
94
95
96
97 @Test (timeout=300000)
98 public void testTableAdminAndMetadata() throws Exception {
99 AvroServer.HBaseImpl impl =
100 new AvroServer.HBaseImpl(TEST_UTIL.getConfiguration());
101
102 assertEquals(impl.listTables().size(), 0);
103
104 ATableDescriptor tableA = new ATableDescriptor();
105 tableA.name = tableAname;
106 impl.createTable(tableA);
107 assertEquals(impl.listTables().size(), 1);
108 assertTrue(impl.isTableEnabled(tableAname));
109 assertTrue(impl.tableExists(tableAname));
110
111 ATableDescriptor tableB = new ATableDescriptor();
112 tableB.name = tableBname;
113 impl.createTable(tableB);
114 assertEquals(impl.listTables().size(), 2);
115
116 impl.disableTable(tableBname);
117 assertFalse(impl.isTableEnabled(tableBname));
118
119 impl.deleteTable(tableBname);
120 assertEquals(impl.listTables().size(), 1);
121
122 impl.disableTable(tableAname);
123 assertFalse(impl.isTableEnabled(tableAname));
124
125 tableA.maxFileSize = 123456L;
126 impl.modifyTable(tableAname, tableA);
127
128 while(impl.describeTable(tableAname).maxFileSize != 123456L) Threads.sleep(100);
129 assertEquals(123456L, (long) impl.describeTable(tableAname).maxFileSize);
130
131
132
133
134
135
136 impl.deleteTable(tableAname);
137 }
138
139
140
141
142
143
144 @Test
145 public void testFamilyAdminAndMetadata() throws Exception {
146 AvroServer.HBaseImpl impl =
147 new AvroServer.HBaseImpl(TEST_UTIL.getConfiguration());
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
176
177
178
179 @Test
180 public void testDML() throws Exception {
181 AvroServer.HBaseImpl impl =
182 new AvroServer.HBaseImpl(TEST_UTIL.getConfiguration());
183
184 ATableDescriptor tableA = new ATableDescriptor();
185 tableA.name = tableAname;
186 AFamilyDescriptor familyA = new AFamilyDescriptor();
187 familyA.name = familyAname;
188 Schema familyArraySchema = Schema.createArray(AFamilyDescriptor.SCHEMA$);
189 GenericArray<AFamilyDescriptor> families = new GenericData.Array<AFamilyDescriptor>(1, familyArraySchema);
190 families.add(familyA);
191 tableA.families = families;
192 impl.createTable(tableA);
193 assertEquals(impl.describeTable(tableAname).families.size(), 1);
194
195 AGet getA = new AGet();
196 getA.row = rowAname;
197 Schema columnsSchema = Schema.createArray(AColumn.SCHEMA$);
198 GenericArray<AColumn> columns = new GenericData.Array<AColumn>(1, columnsSchema);
199 AColumn column = new AColumn();
200 column.family = familyAname;
201 column.qualifier = qualifierAname;
202 columns.add(column);
203 getA.columns = columns;
204
205 assertFalse(impl.exists(tableAname, getA));
206
207 APut putA = new APut();
208 putA.row = rowAname;
209 Schema columnValuesSchema = Schema.createArray(AColumnValue.SCHEMA$);
210 GenericArray<AColumnValue> columnValues = new GenericData.Array<AColumnValue>(1, columnValuesSchema);
211 AColumnValue acv = new AColumnValue();
212 acv.family = familyAname;
213 acv.qualifier = qualifierAname;
214 acv.value = valueA;
215 columnValues.add(acv);
216 putA.columnValues = columnValues;
217
218 impl.put(tableAname, putA);
219 assertTrue(impl.exists(tableAname, getA));
220
221 assertEquals(impl.get(tableAname, getA).entries.size(), 1);
222
223 impl.disableTable(tableAname);
224 impl.deleteTable(tableAname);
225 }
226 }