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  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertTrue;
24  
25  import com.fasterxml.jackson.core.JsonFactory;
26  import com.fasterxml.jackson.core.JsonParser;
27  import com.fasterxml.jackson.core.JsonToken;
28  import com.fasterxml.jackson.databind.ObjectMapper;
29  
30  import java.io.DataInputStream;
31  import java.io.EOFException;
32  import java.io.IOException;
33  import java.io.InputStream;
34  import java.io.Serializable;
35  import java.net.URLEncoder;
36  import java.nio.charset.StandardCharsets;
37  import java.util.ArrayList;
38  import java.util.Collections;
39  import java.util.List;
40  
41  import javax.ws.rs.core.MediaType;
42  import javax.xml.bind.JAXBContext;
43  import javax.xml.bind.JAXBException;
44  import javax.xml.bind.Unmarshaller;
45  import javax.xml.bind.annotation.XmlAccessType;
46  import javax.xml.bind.annotation.XmlAccessorType;
47  import javax.xml.bind.annotation.XmlElement;
48  import javax.xml.bind.annotation.XmlRootElement;
49  import javax.xml.parsers.SAXParserFactory;
50  import javax.xml.stream.XMLStreamException;
51  
52  import org.apache.hadoop.conf.Configuration;
53  import org.apache.hadoop.hbase.HBaseTestingUtility;
54  import org.apache.hadoop.hbase.HColumnDescriptor;
55  import org.apache.hadoop.hbase.HTableDescriptor;
56  import org.apache.hadoop.hbase.testclassification.MediumTests;
57  import org.apache.hadoop.hbase.TableName;
58  import org.apache.hadoop.hbase.client.Admin;
59  import org.apache.hadoop.hbase.filter.Filter;
60  import org.apache.hadoop.hbase.filter.ParseFilter;
61  import org.apache.hadoop.hbase.filter.PrefixFilter;
62  import org.apache.hadoop.hbase.rest.client.Client;
63  import org.apache.hadoop.hbase.rest.client.Cluster;
64  import org.apache.hadoop.hbase.rest.client.Response;
65  import org.apache.hadoop.hbase.rest.model.CellModel;
66  import org.apache.hadoop.hbase.rest.model.CellSetModel;
67  import org.apache.hadoop.hbase.rest.model.RowModel;
68  import org.apache.hadoop.hbase.rest.provider.JacksonProvider;
69  import org.apache.hadoop.hbase.util.Bytes;
70  import org.junit.AfterClass;
71  import org.junit.BeforeClass;
72  import org.junit.Test;
73  import org.junit.experimental.categories.Category;
74  import org.xml.sax.InputSource;
75  import org.xml.sax.XMLReader;
76  
77  @Category(MediumTests.class)
78  public class TestTableScan {
79  
80    private static final TableName TABLE = TableName.valueOf("TestScanResource");
81    private static final String CFA = "a";
82    private static final String CFB = "b";
83    private static final String COLUMN_1 = CFA + ":1";
84    private static final String COLUMN_2 = CFB + ":2";
85    private static final String COLUMN_EMPTY = CFA + ":";
86    private static Client client;
87    private static int expectedRows1;
88    private static int expectedRows2;
89    private static int expectedRows3;
90    private static Configuration conf;
91  
92    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
93    private static final HBaseRESTTestingUtility REST_TEST_UTIL =
94      new HBaseRESTTestingUtility();
95  
96    @BeforeClass
97    public static void setUpBeforeClass() throws Exception {
98      conf = TEST_UTIL.getConfiguration();
99      conf.set(Constants.CUSTOM_FILTERS, "CustomFilter:" + CustomFilter.class.getName()); 
100     TEST_UTIL.startMiniCluster();
101     REST_TEST_UTIL.startServletContainer(conf);
102     client = new Client(new Cluster().add("localhost",
103       REST_TEST_UTIL.getServletPort()));
104     Admin admin = TEST_UTIL.getHBaseAdmin();
105     if (!admin.tableExists(TABLE)) {
106       HTableDescriptor htd = new HTableDescriptor(TABLE);
107       htd.addFamily(new HColumnDescriptor(CFA));
108       htd.addFamily(new HColumnDescriptor(CFB));
109       admin.createTable(htd);
110       expectedRows1 = TestScannerResource.insertData(conf, TABLE, COLUMN_1, 1.0);
111       expectedRows2 = TestScannerResource.insertData(conf, TABLE, COLUMN_2, 0.5);
112       expectedRows3 = TestScannerResource.insertData(conf, TABLE, COLUMN_EMPTY, 1.0);
113     }
114   }
115 
116   @AfterClass
117   public static void tearDownAfterClass() throws Exception {
118     TEST_UTIL.getHBaseAdmin().disableTable(TABLE);
119     TEST_UTIL.getHBaseAdmin().deleteTable(TABLE);
120     REST_TEST_UTIL.shutdownServletContainer();
121     TEST_UTIL.shutdownMiniCluster();
122   }
123 
124   @Test
125   public void testSimpleScannerXML() throws IOException, JAXBException, XMLStreamException {
126     // Test scanning particular columns
127     StringBuilder builder = new StringBuilder();
128     builder.append("/*");
129     builder.append("?");
130     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
131     builder.append("&");
132     builder.append(Constants.SCAN_LIMIT + "=10");
133     Response response = client.get("/" + TABLE + builder.toString(),
134       Constants.MIMETYPE_XML);
135     assertEquals(200, response.getCode());
136     assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
137     JAXBContext ctx = JAXBContext.newInstance(CellSetModel.class);
138     Unmarshaller ush = ctx.createUnmarshaller();
139     CellSetModel model = (CellSetModel) ush.unmarshal(response.getStream());
140     int count = TestScannerResource.countCellSet(model);
141     assertEquals(10, count);
142     checkRowsNotNull(model);
143 
144     //Test with no limit.
145     builder = new StringBuilder();
146     builder.append("/*");
147     builder.append("?");
148     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
149     response = client.get("/" + TABLE + builder.toString(),
150       Constants.MIMETYPE_XML);
151     assertEquals(200, response.getCode());
152     assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type")); 
153     model = (CellSetModel) ush.unmarshal(response.getStream());
154     count = TestScannerResource.countCellSet(model);
155     assertEquals(expectedRows1, count);
156     checkRowsNotNull(model);
157 
158     //Test with start and end row.
159     builder = new StringBuilder();
160     builder.append("/*");
161     builder.append("?");
162     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
163     builder.append("&");
164     builder.append(Constants.SCAN_START_ROW + "=aaa");
165     builder.append("&");
166     builder.append(Constants.SCAN_END_ROW + "=aay");
167     response = client.get("/" + TABLE + builder.toString(),
168       Constants.MIMETYPE_XML);
169     assertEquals(200, response.getCode());
170     model = (CellSetModel) ush.unmarshal(response.getStream());
171     count = TestScannerResource.countCellSet(model);
172     RowModel startRow = model.getRows().get(0);
173     assertEquals("aaa", Bytes.toString(startRow.getKey()));
174     RowModel endRow = model.getRows().get(model.getRows().size() - 1);
175     assertEquals("aax", Bytes.toString(endRow.getKey()));
176     assertEquals(24, count);
177     checkRowsNotNull(model);
178 
179     //Test with start row and limit.
180     builder = new StringBuilder();
181     builder.append("/*");
182     builder.append("?");
183     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
184     builder.append("&");
185     builder.append(Constants.SCAN_START_ROW + "=aaa");
186     builder.append("&");
187     builder.append(Constants.SCAN_LIMIT + "=15");
188     response = client.get("/" + TABLE + builder.toString(),
189       Constants.MIMETYPE_XML);
190     assertEquals(200, response.getCode());
191     assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
192     model = (CellSetModel) ush.unmarshal(response.getStream());
193     startRow = model.getRows().get(0);
194     assertEquals("aaa", Bytes.toString(startRow.getKey()));
195     count = TestScannerResource.countCellSet(model);
196     assertEquals(15, count);
197     checkRowsNotNull(model);
198   }
199 
200   @Test
201   public void testSimpleScannerJson() throws IOException, JAXBException {
202     // Test scanning particular columns with limit.
203     StringBuilder builder = new StringBuilder();
204     builder.append("/*");
205     builder.append("?");
206     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
207     builder.append("&");
208     builder.append(Constants.SCAN_LIMIT + "=20");
209     Response response = client.get("/" + TABLE + builder.toString(),
210       Constants.MIMETYPE_JSON);
211     assertEquals(200, response.getCode());
212     assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
213     ObjectMapper mapper = new JacksonProvider()
214         .locateMapper(CellSetModel.class, MediaType.APPLICATION_JSON_TYPE);
215     CellSetModel model = mapper.readValue(response.getStream(), CellSetModel.class);
216     int count = TestScannerResource.countCellSet(model);
217     assertEquals(20, count);
218     checkRowsNotNull(model);
219 
220     //Test scanning with no limit.
221     builder = new StringBuilder();
222     builder.append("/*");
223     builder.append("?");
224     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_2);
225     response = client.get("/" + TABLE + builder.toString(),
226       Constants.MIMETYPE_JSON);
227     assertEquals(200, response.getCode());
228     assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
229     model = mapper.readValue(response.getStream(), CellSetModel.class);
230     count = TestScannerResource.countCellSet(model);
231     assertEquals(expectedRows2, count);
232     checkRowsNotNull(model);
233 
234     //Test with start row and end row.
235     builder = new StringBuilder();
236     builder.append("/*");
237     builder.append("?");
238     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
239     builder.append("&");
240     builder.append(Constants.SCAN_START_ROW + "=aaa");
241     builder.append("&");
242     builder.append(Constants.SCAN_END_ROW + "=aay");
243     response = client.get("/" + TABLE + builder.toString(),
244       Constants.MIMETYPE_JSON);
245     assertEquals(200, response.getCode());
246     model = mapper.readValue(response.getStream(), CellSetModel.class);
247     RowModel startRow = model.getRows().get(0);
248     assertEquals("aaa", Bytes.toString(startRow.getKey()));
249     RowModel endRow = model.getRows().get(model.getRows().size() - 1);
250     assertEquals("aax", Bytes.toString(endRow.getKey()));
251     count = TestScannerResource.countCellSet(model);
252     assertEquals(24, count);
253     checkRowsNotNull(model);
254   }
255 
256   /**
257    * An example to scan using listener in unmarshaller for XML.
258    * @throws Exception the exception
259    */
260   @Test
261   public void testScanUsingListenerUnmarshallerXML() throws Exception {
262     StringBuilder builder = new StringBuilder();
263     builder.append("/*");
264     builder.append("?");
265     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
266     builder.append("&");
267     builder.append(Constants.SCAN_LIMIT + "=10");
268     Response response = client.get("/" + TABLE + builder.toString(),
269       Constants.MIMETYPE_XML);
270     assertEquals(200, response.getCode());
271     assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
272     JAXBContext context = JAXBContext.newInstance(ClientSideCellSetModel.class, RowModel.class,
273       CellModel.class);
274     Unmarshaller unmarshaller = context.createUnmarshaller();
275 
276     final ClientSideCellSetModel.Listener listener = new ClientSideCellSetModel.Listener() {
277       @Override
278       public void handleRowModel(ClientSideCellSetModel helper, RowModel row) {
279         assertTrue(row.getKey() != null);
280         assertTrue(row.getCells().size() > 0);
281       }
282     };
283 
284     // install the callback on all ClientSideCellSetModel instances
285     unmarshaller.setListener(new Unmarshaller.Listener() {
286         @Override
287         public void beforeUnmarshal(Object target, Object parent) {
288             if (target instanceof ClientSideCellSetModel) {
289                 ((ClientSideCellSetModel) target).setCellSetModelListener(listener);
290             }
291         }
292 
293         @Override
294         public void afterUnmarshal(Object target, Object parent) {
295             if (target instanceof ClientSideCellSetModel) {
296                 ((ClientSideCellSetModel) target).setCellSetModelListener(null);
297             }
298         }
299     });
300 
301     // create a new XML parser
302     SAXParserFactory factory = SAXParserFactory.newInstance();
303     factory.setNamespaceAware(true);
304     XMLReader reader = factory.newSAXParser().getXMLReader();
305     reader.setContentHandler(unmarshaller.getUnmarshallerHandler());
306     assertFalse(ClientSideCellSetModel.listenerInvoked);
307     reader.parse(new InputSource(response.getStream()));
308     assertTrue(ClientSideCellSetModel.listenerInvoked);
309 
310   }
311 
312   @Test
313   public void testStreamingJSON() throws Exception {
314     // Test scanning particular columns with limit.
315     StringBuilder builder = new StringBuilder();
316     builder.append("/*");
317     builder.append("?");
318     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
319     builder.append("&");
320     builder.append(Constants.SCAN_LIMIT + "=20");
321     Response response = client.get("/" + TABLE + builder.toString(),
322       Constants.MIMETYPE_JSON);
323     assertEquals(200, response.getCode());
324     assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
325     ObjectMapper mapper = new JacksonProvider()
326         .locateMapper(CellSetModel.class, MediaType.APPLICATION_JSON_TYPE);
327     CellSetModel model = mapper.readValue(response.getStream(), CellSetModel.class);
328     int count = TestScannerResource.countCellSet(model);
329     assertEquals(20, count);
330     checkRowsNotNull(model);
331 
332     //Test scanning with no limit.
333     builder = new StringBuilder();
334     builder.append("/*");
335     builder.append("?");
336     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_2);
337     response = client.get("/" + TABLE + builder.toString(),
338       Constants.MIMETYPE_JSON);
339     assertEquals(200, response.getCode());
340     assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
341     model = mapper.readValue(response.getStream(), CellSetModel.class);
342     count = TestScannerResource.countCellSet(model);
343     assertEquals(expectedRows2, count);
344     checkRowsNotNull(model);
345 
346     //Test with start row and end row.
347     builder = new StringBuilder();
348     builder.append("/*");
349     builder.append("?");
350     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
351     builder.append("&");
352     builder.append(Constants.SCAN_START_ROW + "=aaa");
353     builder.append("&");
354     builder.append(Constants.SCAN_END_ROW + "=aay");
355     response = client.get("/" + TABLE + builder.toString(),
356       Constants.MIMETYPE_JSON);
357     assertEquals(200, response.getCode());
358 
359     count = 0;
360     JsonFactory jfactory = new JsonFactory(mapper);
361     JsonParser jParser = jfactory.createParser(response.getStream());
362     boolean found = false;
363     while (jParser.nextToken() != JsonToken.END_OBJECT) {
364       if(jParser.getCurrentToken() == JsonToken.START_OBJECT && found) {
365         RowModel row = jParser.readValueAs(RowModel.class);
366         assertNotNull(row.getKey());
367         for (int i = 0; i < row.getCells().size(); i++) {
368           if (count == 0) {
369             assertEquals("aaa", Bytes.toString(row.getKey()));
370           }
371           if (count == 23) {
372             assertEquals("aax", Bytes.toString(row.getKey()));
373           }
374           count++;
375         }
376         jParser.skipChildren();
377       } else {
378         found = jParser.getCurrentToken() == JsonToken.START_ARRAY;
379       }
380     }
381     assertEquals(24, count);
382   }
383 
384   @Test
385   public void testSimpleScannerProtobuf() throws Exception {
386     StringBuilder builder = new StringBuilder();
387     builder.append("/*");
388     builder.append("?");
389     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
390     builder.append("&");
391     builder.append(Constants.SCAN_LIMIT + "=15");
392     Response response = client.get("/" + TABLE + builder.toString(),
393       Constants.MIMETYPE_PROTOBUF);
394     assertEquals(200, response.getCode());
395     assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
396     int rowCount = readProtobufStream(response.getStream());
397     assertEquals(15, rowCount);
398 
399   //Test with start row and end row.
400     builder = new StringBuilder();
401     builder.append("/*");
402     builder.append("?");
403     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
404     builder.append("&");
405     builder.append(Constants.SCAN_START_ROW + "=aaa");
406     builder.append("&");
407     builder.append(Constants.SCAN_END_ROW + "=aay");
408     response = client.get("/" + TABLE + builder.toString(),
409       Constants.MIMETYPE_PROTOBUF);
410     assertEquals(200, response.getCode());
411     assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
412     rowCount = readProtobufStream(response.getStream());
413     assertEquals(24, rowCount);
414   }
415 
416   private void checkRowsNotNull(CellSetModel model) {
417     for (RowModel row: model.getRows()) {
418       assertTrue(row.getKey() != null);
419       assertTrue(row.getCells().size() > 0);
420     }
421   }
422 
423   /**
424    * Read protobuf stream.
425    * @param inputStream the input stream
426    * @return The number of rows in the cell set model.
427    * @throws IOException Signals that an I/O exception has occurred.
428    */
429   public int readProtobufStream(InputStream inputStream) throws IOException{
430     DataInputStream stream = new DataInputStream(inputStream);
431     CellSetModel model = null;
432     int rowCount = 0;
433     try {
434       while (true) {
435         byte[] lengthBytes = new byte[2];
436         int readBytes = stream.read(lengthBytes);
437         if (readBytes == -1) {
438           break;
439         }
440         assertEquals(2, readBytes);
441         int length = Bytes.toShort(lengthBytes);
442         byte[] cellset = new byte[length];
443         stream.read(cellset);
444         model = new CellSetModel();
445         model.getObjectFromMessage(cellset);
446         checkRowsNotNull(model);
447         rowCount = rowCount + TestScannerResource.countCellSet(model);
448       }
449     } catch (EOFException exp) {
450       exp.printStackTrace();
451     } finally {
452       stream.close();
453     }
454     return rowCount;
455   }
456 
457   @Test
458   public void testScanningUnknownColumnJson() throws IOException, JAXBException {
459     // Test scanning particular columns with limit.
460     StringBuilder builder = new StringBuilder();
461     builder.append("/*");
462     builder.append("?");
463     builder.append(Constants.SCAN_COLUMN + "=a:test");
464     Response response = client.get("/" + TABLE  + builder.toString(),
465       Constants.MIMETYPE_JSON);
466     assertEquals(200, response.getCode());
467     assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
468     ObjectMapper mapper = new JacksonProvider().locateMapper(CellSetModel.class,
469       MediaType.APPLICATION_JSON_TYPE);
470     CellSetModel model = mapper.readValue(response.getStream(), CellSetModel.class);
471     int count = TestScannerResource.countCellSet(model);
472     assertEquals(0, count);
473   }
474   
475   @Test
476   public void testSimpleFilter() throws IOException, JAXBException {
477     StringBuilder builder = new StringBuilder();
478     builder.append("/*");
479     builder.append("?");
480     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
481     builder.append("&");
482     builder.append(Constants.SCAN_START_ROW + "=aaa");
483     builder.append("&");
484     builder.append(Constants.SCAN_END_ROW + "=aay");
485     builder.append("&");
486     builder.append(Constants.SCAN_FILTER + "=" + URLEncoder.encode("PrefixFilter('aab')", "UTF-8"));
487     Response response =
488         client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_XML);
489     assertEquals(200, response.getCode());
490     JAXBContext ctx = JAXBContext.newInstance(CellSetModel.class);
491     Unmarshaller ush = ctx.createUnmarshaller();
492     CellSetModel model = (CellSetModel) ush.unmarshal(response.getStream());
493     int count = TestScannerResource.countCellSet(model);
494     assertEquals(1, count);
495     assertEquals("aab", new String(model.getRows().get(0).getCells().get(0).getValue(),
496       StandardCharsets.UTF_8));
497   }
498 
499   @Test
500   public void testQualifierAndPrefixFilters() throws IOException, JAXBException {
501     StringBuilder builder = new StringBuilder();
502     builder.append("/abc*");
503     builder.append("?");
504     builder.append(Constants.SCAN_FILTER + "="
505         + URLEncoder.encode("QualifierFilter(=,'binary:1')", "UTF-8"));
506     Response response = 
507         client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_XML);
508     assertEquals(200, response.getCode());
509     JAXBContext ctx = JAXBContext.newInstance(CellSetModel.class);
510     Unmarshaller ush = ctx.createUnmarshaller();
511     CellSetModel model = (CellSetModel) ush.unmarshal(response.getStream());
512     int count = TestScannerResource.countCellSet(model);
513     assertEquals(1, count);
514     assertEquals("abc", new String(model.getRows().get(0).getCells().get(0).getValue(),
515       StandardCharsets.UTF_8));
516   }
517 
518 
519   @Test
520   public void testCompoundFilter() throws IOException, JAXBException {
521     StringBuilder builder = new StringBuilder();
522     builder.append("/*");
523     builder.append("?");
524     builder.append(Constants.SCAN_FILTER + "="
525         + URLEncoder.encode("PrefixFilter('abc') AND QualifierFilter(=,'binary:1')", "UTF-8"));
526     Response response =
527         client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_XML);
528     assertEquals(200, response.getCode());
529     JAXBContext ctx = JAXBContext.newInstance(CellSetModel.class);
530     Unmarshaller ush = ctx.createUnmarshaller();
531     CellSetModel model = (CellSetModel) ush.unmarshal(response.getStream());
532     int count = TestScannerResource.countCellSet(model);
533     assertEquals(1, count);
534     assertEquals("abc", new String(model.getRows().get(0).getCells().get(0).getValue(),
535       StandardCharsets.UTF_8));
536   }
537 
538   @Test
539   public void testCustomFilter() throws IOException, JAXBException {
540     StringBuilder builder = new StringBuilder();
541     builder.append("/a*");
542     builder.append("?");
543     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
544     builder.append("&");
545     builder.append(Constants.SCAN_FILTER + "=" + URLEncoder.encode("CustomFilter('abc')", "UTF-8"));
546     Response response =
547         client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_XML);
548     assertEquals(200, response.getCode());
549     JAXBContext ctx = JAXBContext.newInstance(CellSetModel.class);
550     Unmarshaller ush = ctx.createUnmarshaller();
551     CellSetModel model = (CellSetModel) ush.unmarshal(response.getStream());
552     int count = TestScannerResource.countCellSet(model);
553     assertEquals(1, count);
554     assertEquals("abc", new String(model.getRows().get(0).getCells().get(0).getValue(),
555       StandardCharsets.UTF_8));
556   }
557   
558   @Test
559   public void testNegativeCustomFilter() throws IOException, JAXBException {
560     StringBuilder builder = new StringBuilder();
561     builder.append("/b*");
562     builder.append("?");
563     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
564     builder.append("&");
565     builder.append(Constants.SCAN_FILTER + "=" + URLEncoder.encode("CustomFilter('abc')", "UTF-8"));
566     Response response =
567         client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_XML);
568     assertEquals(200, response.getCode());
569     JAXBContext ctx = JAXBContext.newInstance(CellSetModel.class);
570     Unmarshaller ush = ctx.createUnmarshaller();
571     CellSetModel model = (CellSetModel) ush.unmarshal(response.getStream());
572     int count = TestScannerResource.countCellSet(model);
573     // Should return no rows as the filters conflict
574     assertEquals(0, count);
575   }
576 
577   @Test
578   public void testReversed() throws IOException, JAXBException {
579     StringBuilder builder = new StringBuilder();
580     builder.append("/*");
581     builder.append("?");
582     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
583     builder.append("&");
584     builder.append(Constants.SCAN_START_ROW + "=aaa");
585     builder.append("&");
586     builder.append(Constants.SCAN_END_ROW + "=aay");
587     Response response = client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_XML);
588     assertEquals(200, response.getCode());
589     JAXBContext ctx = JAXBContext.newInstance(CellSetModel.class);
590     Unmarshaller ush = ctx.createUnmarshaller();
591     CellSetModel model = (CellSetModel) ush.unmarshal(response.getStream());
592     int count = TestScannerResource.countCellSet(model);
593     assertEquals(24, count);
594     List<RowModel> rowModels = model.getRows().subList(1, count);
595 
596     //reversed
597     builder = new StringBuilder();
598     builder.append("/*");
599     builder.append("?");
600     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
601     builder.append("&");
602     builder.append(Constants.SCAN_START_ROW + "=aay");
603     builder.append("&");
604     builder.append(Constants.SCAN_END_ROW + "=aaa");
605     builder.append("&");
606     builder.append(Constants.SCAN_REVERSED + "=true");
607     response = client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_XML);
608     assertEquals(200, response.getCode());
609     model = (CellSetModel) ush.unmarshal(response.getStream());
610     count = TestScannerResource.countCellSet(model);
611     assertEquals(24, count);
612     List<RowModel> reversedRowModels = model.getRows().subList(1, count);
613 
614     Collections.reverse(reversedRowModels);
615     assertEquals(rowModels.size(), reversedRowModels.size());
616     for (int i = 0; i < rowModels.size(); i++) {
617       RowModel rowModel = rowModels.get(i);
618       RowModel reversedRowModel = reversedRowModels.get(i);
619 
620       assertEquals(new String(rowModel.getKey(), StandardCharsets.UTF_8),
621           new String(reversedRowModel.getKey(), StandardCharsets.UTF_8));
622       assertEquals(new String(rowModel.getCells().get(0).getValue(), StandardCharsets.UTF_8),
623           new String(reversedRowModel.getCells().get(0).getValue(), StandardCharsets.UTF_8));
624     }
625   }
626 
627   @Test
628   public void testColumnWithEmptyQualifier() throws IOException, JAXBException {
629     // Test scanning with empty qualifier
630     StringBuilder builder = new StringBuilder();
631     builder.append("/*");
632     builder.append("?");
633     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_EMPTY);
634     Response response = client.get("/" + TABLE + builder.toString(),
635         Constants.MIMETYPE_JSON);
636     assertEquals(200, response.getCode());
637     assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
638     ObjectMapper mapper = new JacksonProvider()
639         .locateMapper(CellSetModel.class, MediaType.APPLICATION_JSON_TYPE);
640     CellSetModel model = mapper.readValue(response.getStream(), CellSetModel.class);
641     int count = TestScannerResource.countCellSet(model);
642     assertEquals(expectedRows3, count);
643     checkRowsNotNull(model);
644     RowModel startRow = model.getRows().get(0);
645     assertEquals("aaa", Bytes.toString(startRow.getKey()));
646     assertEquals(1, startRow.getCells().size());
647 
648     // Test scanning with empty qualifier and normal qualifier
649     builder = new StringBuilder();
650     builder.append("/*");
651     builder.append("?");
652     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
653     builder.append("&");
654     builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_EMPTY);
655     response = client.get("/" + TABLE + builder.toString(),
656         Constants.MIMETYPE_JSON);
657     assertEquals(200, response.getCode());
658     assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
659     mapper = new JacksonProvider()
660         .locateMapper(CellSetModel.class, MediaType.APPLICATION_JSON_TYPE);
661     model = mapper.readValue(response.getStream(), CellSetModel.class);
662     count = TestScannerResource.countCellSet(model);
663     assertEquals(expectedRows1 + expectedRows3, count);
664     checkRowsNotNull(model);
665   }
666 
667   public static class CustomFilter extends PrefixFilter {
668     private byte[] key = null;
669 
670     public CustomFilter(byte[] key) {
671       super(key);
672     }
673     
674     @Override
675     public boolean filterRowKey(byte[] buffer, int offset, int length) {
676       int cmp = Bytes.compareTo(buffer, offset, length, this.key, 0, this.key.length);
677       return cmp != 0;
678     }
679 
680     public static Filter createFilterFromArguments(ArrayList<byte[]> filterArguments) {
681       byte[] prefix = ParseFilter.removeQuotesFromByteArray(filterArguments.get(0));
682       return new CustomFilter(prefix);
683     }
684   }
685 
686   /**
687    * The Class ClientSideCellSetModel which mimics cell set model, and contains listener to perform
688    * user defined operations on the row model.
689    */
690   @XmlRootElement(name = "CellSet")
691   @XmlAccessorType(XmlAccessType.FIELD)
692   public static class ClientSideCellSetModel implements Serializable {
693 
694     private static final long serialVersionUID = 1L;
695 
696     /**
697      * This list is not a real list; instead it will notify a listener whenever JAXB has
698      * unmarshalled the next row.
699      */
700     @XmlElement(name="Row")
701     private List<RowModel> row;
702 
703     static boolean listenerInvoked = false;
704 
705     /**
706      * Install a listener for row model on this object. If l is null, the listener
707      * is removed again.
708      */
709     public void setCellSetModelListener(final Listener l) {
710         row = (l == null) ? null : new ArrayList<RowModel>() {
711         private static final long serialVersionUID = 1L;
712             @Override
713             public boolean add(RowModel o) {
714                 l.handleRowModel(ClientSideCellSetModel.this, o);
715                 listenerInvoked = true;
716                 return false;
717             }
718         };
719     }
720 
721     /**
722      * This listener is invoked every time a new row model is unmarshalled.
723      */
724     public static interface Listener {
725         void handleRowModel(ClientSideCellSetModel helper, RowModel rowModel);
726     }
727   }
728 }
729 
730 
731