View Javadoc

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.rest;
19  
20  import static org.junit.Assert.assertEquals;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.IOException;
24  import java.io.StringWriter;
25  
26  import javax.ws.rs.core.MediaType;
27  import javax.xml.bind.JAXBContext;
28  import javax.xml.bind.JAXBException;
29  import javax.xml.bind.Marshaller;
30  import javax.xml.bind.Unmarshaller;
31  
32  import org.apache.hadoop.conf.Configuration;
33  import org.apache.hadoop.hbase.HBaseTestingUtility;
34  import org.apache.hadoop.hbase.HColumnDescriptor;
35  import org.apache.hadoop.hbase.HTableDescriptor;
36  import org.apache.hadoop.hbase.TableName;
37  import org.apache.hadoop.hbase.client.HBaseAdmin;
38  import org.apache.hadoop.hbase.rest.client.Client;
39  import org.apache.hadoop.hbase.rest.client.Cluster;
40  import org.apache.hadoop.hbase.rest.client.Response;
41  import org.apache.hadoop.hbase.rest.model.CellModel;
42  import org.apache.hadoop.hbase.rest.model.CellSetModel;
43  import org.apache.hadoop.hbase.rest.model.RowModel;
44  import org.apache.hadoop.hbase.rest.provider.JacksonProvider;
45  import org.apache.hadoop.hbase.util.Bytes;
46  import org.codehaus.jackson.map.ObjectMapper;
47  import org.junit.After;
48  import org.junit.AfterClass;
49  import org.junit.Before;
50  import org.junit.BeforeClass;
51  
52  public class RowResourceBase {
53  
54    protected static final String TABLE = "TestRowResource";
55    protected static final String CFA = "a";
56    protected static final String CFB = "b";
57    protected static final String COLUMN_1 = CFA + ":1";
58    protected static final String COLUMN_2 = CFB + ":2";
59    protected static final String COLUMN_3 = CFA + ":";
60    protected static final String ROW_1 = "testrow1";
61    protected static final String VALUE_1 = "testvalue1";
62    protected static final String ROW_2 = "testrow2";
63    protected static final String VALUE_2 = "testvalue2";
64    protected static final String ROW_3 = "testrow3";
65    protected static final String VALUE_3 = "testvalue3";
66    protected static final String ROW_4 = "testrow4";
67    protected static final String VALUE_4 = "testvalue4";
68  
69    protected static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
70    protected static final HBaseRESTTestingUtility REST_TEST_UTIL =
71      new HBaseRESTTestingUtility();
72    protected static Client client;
73    protected static JAXBContext context;
74    protected static Marshaller xmlMarshaller;
75    protected static Unmarshaller xmlUnmarshaller;
76    protected static Configuration conf;
77    protected static ObjectMapper jsonMapper;
78  
79    @BeforeClass
80    public static void setUpBeforeClass() throws Exception {
81      conf = TEST_UTIL.getConfiguration();
82      TEST_UTIL.startMiniCluster(3);
83      REST_TEST_UTIL.startServletContainer(conf);
84      context = JAXBContext.newInstance(
85          CellModel.class,
86          CellSetModel.class,
87          RowModel.class);
88      xmlMarshaller = context.createMarshaller();
89      xmlUnmarshaller = context.createUnmarshaller();
90      jsonMapper = new JacksonProvider()
91      .locateMapper(CellSetModel.class, MediaType.APPLICATION_JSON_TYPE);
92      client = new Client(new Cluster().add("localhost",
93        REST_TEST_UTIL.getServletPort()));
94    }
95  
96    @AfterClass
97    public static void tearDownAfterClass() throws Exception {
98      REST_TEST_UTIL.shutdownServletContainer();
99      TEST_UTIL.shutdownMiniCluster();
100   }
101 
102   @Before
103   public void beforeMethod() throws Exception {
104     HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
105     if (admin.tableExists(TABLE)) {
106       TEST_UTIL.deleteTable(Bytes.toBytes(TABLE));
107     }
108     HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(TABLE));
109     htd.addFamily(new HColumnDescriptor(CFA));
110     htd.addFamily(new HColumnDescriptor(CFB));
111     admin.createTable(htd);
112   }
113 
114   @After
115   public void afterMethod() throws Exception {
116     HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
117     if (admin.tableExists(TABLE)) {
118       TEST_UTIL.deleteTable(Bytes.toBytes(TABLE));
119     }
120   }
121 
122   static Response putValuePB(String table, String row, String column,
123       String value) throws IOException {
124     StringBuilder path = new StringBuilder();
125     path.append('/');
126     path.append(table);
127     path.append('/');
128     path.append(row);
129     path.append('/');
130     path.append(column);
131     return putValuePB(path.toString(), table, row, column, value);
132   }
133 
134   static Response putValuePB(String url, String table, String row,
135       String column, String value) throws IOException {
136     RowModel rowModel = new RowModel(row);
137     rowModel.addCell(new CellModel(Bytes.toBytes(column),
138       Bytes.toBytes(value)));
139     CellSetModel cellSetModel = new CellSetModel();
140     cellSetModel.addRow(rowModel);
141     Response response = client.put(url, Constants.MIMETYPE_PROTOBUF,
142       cellSetModel.createProtobufOutput());
143     Thread.yield();
144     return response;
145   }
146 
147   protected static void checkValueXML(String url, String table, String row,
148       String column, String value) throws IOException, JAXBException {
149     Response response = getValueXML(url);
150     assertEquals(response.getCode(), 200);
151     assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
152     CellSetModel cellSet = (CellSetModel)
153       xmlUnmarshaller.unmarshal(new ByteArrayInputStream(response.getBody()));
154     RowModel rowModel = cellSet.getRows().get(0);
155     CellModel cell = rowModel.getCells().get(0);
156     assertEquals(Bytes.toString(cell.getColumn()), column);
157     assertEquals(Bytes.toString(cell.getValue()), value);
158   }
159 
160   protected static void checkValueXML(String table, String row, String column,
161       String value) throws IOException, JAXBException {
162     Response response = getValueXML(table, row, column);
163     assertEquals(response.getCode(), 200);
164     assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
165     CellSetModel cellSet = (CellSetModel)
166       xmlUnmarshaller.unmarshal(new ByteArrayInputStream(response.getBody()));
167     RowModel rowModel = cellSet.getRows().get(0);
168     CellModel cell = rowModel.getCells().get(0);
169     assertEquals(Bytes.toString(cell.getColumn()), column);
170     assertEquals(Bytes.toString(cell.getValue()), value);
171   }
172 
173   protected static Response getValuePB(String url) throws IOException {
174     Response response = client.get(url, Constants.MIMETYPE_PROTOBUF); 
175     return response;
176   }
177 
178   protected static Response putValueXML(String table, String row, String column,
179       String value) throws IOException, JAXBException {
180     StringBuilder path = new StringBuilder();
181     path.append('/');
182     path.append(table);
183     path.append('/');
184     path.append(row);
185     path.append('/');
186     path.append(column);
187     return putValueXML(path.toString(), table, row, column, value);
188   }
189 
190   protected static Response putValueXML(String url, String table, String row,
191       String column, String value) throws IOException, JAXBException {
192     RowModel rowModel = new RowModel(row);
193     rowModel.addCell(new CellModel(Bytes.toBytes(column),
194       Bytes.toBytes(value)));
195     CellSetModel cellSetModel = new CellSetModel();
196     cellSetModel.addRow(rowModel);
197     StringWriter writer = new StringWriter();
198     xmlMarshaller.marshal(cellSetModel, writer);
199     Response response = client.put(url, Constants.MIMETYPE_XML,
200       Bytes.toBytes(writer.toString()));
201     Thread.yield();
202     return response;
203   }
204 
205   protected static Response getValuePB(String table, String row, String column)
206       throws IOException {
207     StringBuilder path = new StringBuilder();
208     path.append('/');
209     path.append(table);
210     path.append('/');
211     path.append(row);
212     path.append('/');
213     path.append(column);
214     return getValuePB(path.toString());
215   }
216 
217   protected static void checkValuePB(String table, String row, String column,
218       String value) throws IOException {
219     Response response = getValuePB(table, row, column);
220     assertEquals(response.getCode(), 200);
221     assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
222     CellSetModel cellSet = new CellSetModel();
223     cellSet.getObjectFromMessage(response.getBody());
224     RowModel rowModel = cellSet.getRows().get(0);
225     CellModel cell = rowModel.getCells().get(0);
226     assertEquals(Bytes.toString(cell.getColumn()), column);
227     assertEquals(Bytes.toString(cell.getValue()), value);
228   }
229 
230   protected static Response checkAndPutValuePB(String url, String table,
231       String row, String column, String valueToCheck, String valueToPut)
232         throws IOException {
233     RowModel rowModel = new RowModel(row);
234     rowModel.addCell(new CellModel(Bytes.toBytes(column),
235       Bytes.toBytes(valueToPut)));
236     rowModel.addCell(new CellModel(Bytes.toBytes(column),
237       Bytes.toBytes(valueToCheck)));
238     CellSetModel cellSetModel = new CellSetModel();
239     cellSetModel.addRow(rowModel);
240     Response response = client.put(url, Constants.MIMETYPE_PROTOBUF,
241       cellSetModel.createProtobufOutput());
242     Thread.yield();
243     return response;
244   }
245 
246   protected static Response checkAndPutValuePB(String table, String row,
247       String column, String valueToCheck, String valueToPut) throws IOException {
248     StringBuilder path = new StringBuilder();
249     path.append('/');
250     path.append(table);
251     path.append('/');
252     path.append(row);
253     path.append("?check=put");
254     return checkAndPutValuePB(path.toString(), table, row, column,
255       valueToCheck, valueToPut);
256   }
257 
258   protected static Response checkAndPutValueXML(String url, String table,
259       String row, String column, String valueToCheck, String valueToPut)
260         throws IOException, JAXBException {
261     RowModel rowModel = new RowModel(row);
262     rowModel.addCell(new CellModel(Bytes.toBytes(column),
263       Bytes.toBytes(valueToPut)));
264     rowModel.addCell(new CellModel(Bytes.toBytes(column),
265       Bytes.toBytes(valueToCheck)));
266     CellSetModel cellSetModel = new CellSetModel();
267     cellSetModel.addRow(rowModel);
268     StringWriter writer = new StringWriter();
269     xmlMarshaller.marshal(cellSetModel, writer);
270     Response response = client.put(url, Constants.MIMETYPE_XML,
271       Bytes.toBytes(writer.toString()));
272     Thread.yield();
273     return response;
274   }
275 
276   protected static Response checkAndPutValueXML(String table, String row,
277       String column, String valueToCheck, String valueToPut)
278         throws IOException, JAXBException {
279     StringBuilder path = new StringBuilder();
280     path.append('/');
281     path.append(table);
282     path.append('/');
283     path.append(row);
284     path.append("?check=put");
285     return checkAndPutValueXML(path.toString(), table, row, column,
286       valueToCheck, valueToPut);
287   }
288 
289   protected static Response checkAndDeleteXML(String url, String table,
290       String row, String column, String valueToCheck)
291         throws IOException, JAXBException {
292     RowModel rowModel = new RowModel(row);
293     rowModel.addCell(new CellModel(Bytes.toBytes(column),
294       Bytes.toBytes(valueToCheck)));
295     CellSetModel cellSetModel = new CellSetModel();
296     cellSetModel.addRow(rowModel);
297     StringWriter writer = new StringWriter();
298     xmlMarshaller.marshal(cellSetModel, writer);
299     Response response = client.put(url, Constants.MIMETYPE_XML,
300       Bytes.toBytes(writer.toString()));
301     Thread.yield();
302     return response;
303   }
304 
305   protected static Response checkAndDeleteXML(String table, String row,
306       String column, String valueToCheck) throws IOException, JAXBException {
307     StringBuilder path = new StringBuilder();
308     path.append('/');
309     path.append(table);
310     path.append('/');
311     path.append(row);
312     path.append("?check=delete");
313     return checkAndDeleteXML(path.toString(), table, row, column, valueToCheck);
314   }
315 
316   protected static Response checkAndDeleteJson(String table, String row,
317       String column, String valueToCheck) throws IOException, JAXBException {
318     StringBuilder path = new StringBuilder();
319     path.append('/');
320     path.append(table);
321     path.append('/');
322     path.append(row);
323     path.append("?check=delete");
324     return checkAndDeleteJson(path.toString(), table, row, column, valueToCheck);
325   }
326 
327   protected static Response checkAndDeleteJson(String url, String table,
328       String row, String column, String valueToCheck)
329         throws IOException, JAXBException {
330     RowModel rowModel = new RowModel(row);
331     rowModel.addCell(new CellModel(Bytes.toBytes(column),
332       Bytes.toBytes(valueToCheck)));
333     CellSetModel cellSetModel = new CellSetModel();
334     cellSetModel.addRow(rowModel);
335     String jsonString = jsonMapper.writeValueAsString(cellSetModel);
336     Response response = client.put(url, Constants.MIMETYPE_JSON,
337       Bytes.toBytes(jsonString));
338     Thread.yield();
339     return response;
340   }
341 
342   protected static Response checkAndDeletePB(String table, String row,
343       String column, String value) throws IOException {
344     StringBuilder path = new StringBuilder();
345     path.append('/');
346     path.append(table);
347     path.append('/');
348     path.append(row);
349     path.append("?check=delete");
350     return checkAndDeleteValuePB(path.toString(), table, row, column, value);
351   }
352 
353   protected static Response checkAndDeleteValuePB(String url, String table,
354       String row, String column, String valueToCheck)
355       throws IOException {
356     RowModel rowModel = new RowModel(row);
357     rowModel.addCell(new CellModel(Bytes.toBytes(column), Bytes
358         .toBytes(valueToCheck)));
359     CellSetModel cellSetModel = new CellSetModel();
360     cellSetModel.addRow(rowModel);
361     Response response = client.put(url, Constants.MIMETYPE_PROTOBUF,
362         cellSetModel.createProtobufOutput());
363     Thread.yield();
364     return response;
365   }
366 
367   protected static Response getValueXML(String table, String startRow,
368       String endRow, String column) throws IOException {
369     StringBuilder path = new StringBuilder();
370     path.append('/');
371     path.append(table);
372     path.append('/');
373     path.append(startRow);
374     path.append(",");
375     path.append(endRow);
376     path.append('/');
377     path.append(column);
378     return getValueXML(path.toString());
379   }
380 
381   protected static Response getValueXML(String url) throws IOException {
382     Response response = client.get(url, Constants.MIMETYPE_XML);
383     return response;
384   }
385 
386   protected static Response getValueJson(String url) throws IOException {
387     Response response = client.get(url, Constants.MIMETYPE_JSON);
388     return response;
389   }
390 
391   protected static Response deleteValue(String table, String row, String column)
392       throws IOException {
393     StringBuilder path = new StringBuilder();
394     path.append('/');
395     path.append(table);
396     path.append('/');
397     path.append(row);
398     path.append('/');
399     path.append(column);
400     Response response = client.delete(path.toString());
401     Thread.yield();
402     return response;
403   }
404 
405   protected static Response getValueXML(String table, String row, String column)
406       throws IOException {
407     StringBuilder path = new StringBuilder();
408     path.append('/');
409     path.append(table);
410     path.append('/');
411     path.append(row);
412     path.append('/');
413     path.append(column);
414     return getValueXML(path.toString());
415   }
416 
417   protected static Response deleteRow(String table, String row)
418       throws IOException {
419     StringBuilder path = new StringBuilder();
420     path.append('/');
421     path.append(table);
422     path.append('/');
423     path.append(row);
424     Response response = client.delete(path.toString());
425     Thread.yield();
426     return response;
427   }
428 
429   protected static Response getValueJson(String table, String row,
430       String column) throws IOException {
431     StringBuilder path = new StringBuilder();
432     path.append('/');
433     path.append(table);
434     path.append('/');
435     path.append(row);
436     path.append('/');
437     path.append(column);
438     return getValueJson(path.toString());
439   }
440 
441   protected static void checkValueJSON(String table, String row, String column,
442       String value) throws IOException, JAXBException {
443     Response response = getValueJson(table, row, column);
444     assertEquals(response.getCode(), 200);
445     assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
446     ObjectMapper mapper = new JacksonProvider()
447     .locateMapper(CellSetModel.class, MediaType.APPLICATION_JSON_TYPE);
448     CellSetModel cellSet = mapper.readValue(response.getBody(), CellSetModel.class);
449     RowModel rowModel = cellSet.getRows().get(0);
450     CellModel cell = rowModel.getCells().get(0);
451     assertEquals(Bytes.toString(cell.getColumn()), column);
452     assertEquals(Bytes.toString(cell.getValue()), value);
453   }
454 
455   protected static Response putValueJson(String table, String row, String column,
456       String value) throws IOException, JAXBException {
457     StringBuilder path = new StringBuilder();
458     path.append('/');
459     path.append(table);
460     path.append('/');
461     path.append(row);
462     path.append('/');
463     path.append(column);
464     return putValueJson(path.toString(), table, row, column, value);
465   }
466 
467   protected static Response putValueJson(String url, String table, String row, String column,
468       String value) throws IOException, JAXBException {
469     RowModel rowModel = new RowModel(row);
470     rowModel.addCell(new CellModel(Bytes.toBytes(column),
471       Bytes.toBytes(value)));
472     CellSetModel cellSetModel = new CellSetModel();
473     cellSetModel.addRow(rowModel);
474     String jsonString = jsonMapper.writeValueAsString(cellSetModel);
475     Response response = client.put(url, Constants.MIMETYPE_JSON,
476       Bytes.toBytes(jsonString));
477     Thread.yield();
478     return response;
479   }
480 
481 }