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