|
69 | 69 |
|
70 | 70 | import org.apache.hbase.thirdparty.com.google.common.reflect.TypeToken;
|
71 | 71 | import org.apache.hbase.thirdparty.com.google.gson.Gson;
|
| 72 | +import org.apache.hbase.thirdparty.com.google.gson.GsonBuilder; |
| 73 | +import org.apache.hbase.thirdparty.com.google.gson.LongSerializationPolicy; |
| 74 | +import org.apache.hbase.thirdparty.com.google.gson.ToNumberPolicy; |
72 | 75 |
|
73 | 76 | /**
|
74 | 77 | * Run tests that use the functionality of the Operation superclass for Puts, Gets, Deletes, Scans,
|
@@ -345,6 +348,101 @@ public void testOperationJSON() throws IOException {
|
345 | 348 | kvMap.get("qualifier"));
|
346 | 349 | }
|
347 | 350 |
|
| 351 | + /** |
| 352 | + * Test the client Scan Operations' JSON encoding to ensure that produced JSON is parseable and |
| 353 | + * that the details are present and not corrupted. |
| 354 | + * @throws IOException if the JSON conversion fails |
| 355 | + */ |
| 356 | + @Test |
| 357 | + public void testScanOperationToJSON() throws IOException { |
| 358 | + // produce a Scan Operation |
| 359 | + Scan scan = new Scan().withStartRow(ROW, true); |
| 360 | + scan.addColumn(FAMILY, QUALIFIER); |
| 361 | + scan.withStopRow(ROW, true); |
| 362 | + scan.readVersions(5); |
| 363 | + scan.setBatch(10); |
| 364 | + scan.setAllowPartialResults(true); |
| 365 | + scan.setMaxResultsPerColumnFamily(3); |
| 366 | + scan.setRowOffsetPerColumnFamily(8); |
| 367 | + scan.setCaching(20); |
| 368 | + scan.setMaxResultSize(50); |
| 369 | + scan.setCacheBlocks(true); |
| 370 | + scan.setReversed(true); |
| 371 | + scan.setTimeRange(1000, 2000); |
| 372 | + scan.setAsyncPrefetch(true); |
| 373 | + scan.setMvccReadPoint(123); |
| 374 | + scan.setLimit(5); |
| 375 | + scan.setReadType(Scan.ReadType.PREAD); |
| 376 | + scan.setNeedCursorResult(true); |
| 377 | + scan.setFilter(SCV_FILTER); |
| 378 | + scan.setReplicaId(1); |
| 379 | + scan.setConsistency(Consistency.STRONG); |
| 380 | + scan.setLoadColumnFamiliesOnDemand(true); |
| 381 | + scan.setColumnFamilyTimeRange(FAMILY, 2000, 3000); |
| 382 | + scan.setPriority(10); |
| 383 | + |
| 384 | + // get its JSON representation, and parse it |
| 385 | + String json = scan.toJSON(); |
| 386 | + Type typeOfHashMap = new TypeToken<Map<String, Object>>() { |
| 387 | + }.getType(); |
| 388 | + Gson gson = new GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING) |
| 389 | + .setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE).create(); |
| 390 | + Map<String, Object> parsedJSON = gson.fromJson(json, typeOfHashMap); |
| 391 | + // check for the row |
| 392 | + assertEquals("startRow incorrect in Scan.toJSON()", Bytes.toStringBinary(ROW), |
| 393 | + parsedJSON.get("startRow")); |
| 394 | + // check for the family and the qualifier. |
| 395 | + List familyInfo = (List) ((Map) parsedJSON.get("families")).get(Bytes.toStringBinary(FAMILY)); |
| 396 | + assertNotNull("Family absent in Scan.toJSON()", familyInfo); |
| 397 | + assertEquals("Qualifier absent in Scan.toJSON()", 1, familyInfo.size()); |
| 398 | + assertEquals("Qualifier incorrect in Scan.toJSON()", Bytes.toStringBinary(QUALIFIER), |
| 399 | + familyInfo.get(0)); |
| 400 | + assertEquals("stopRow incorrect in Scan.toJSON()", Bytes.toStringBinary(ROW), |
| 401 | + parsedJSON.get("stopRow")); |
| 402 | + assertEquals("includeStartRow incorrect in Scan.toJSON()", true, |
| 403 | + parsedJSON.get("includeStartRow")); |
| 404 | + assertEquals("includeStopRow incorrect in Scan.toJSON()", true, |
| 405 | + parsedJSON.get("includeStopRow")); |
| 406 | + assertEquals("maxVersions incorrect in Scan.toJSON()", 5L, parsedJSON.get("maxVersions")); |
| 407 | + assertEquals("batch incorrect in Scan.toJSON()", 10L, parsedJSON.get("batch")); |
| 408 | + assertEquals("allowPartialResults incorrect in Scan.toJSON()", true, |
| 409 | + parsedJSON.get("allowPartialResults")); |
| 410 | + assertEquals("storeLimit incorrect in Scan.toJSON()", 3L, parsedJSON.get("storeLimit")); |
| 411 | + assertEquals("storeOffset incorrect in Scan.toJSON()", 8L, parsedJSON.get("storeOffset")); |
| 412 | + assertEquals("caching incorrect in Scan.toJSON()", 20L, parsedJSON.get("caching")); |
| 413 | + assertEquals("maxResultSize incorrect in Scan.toJSON()", "50", parsedJSON.get("maxResultSize")); |
| 414 | + assertEquals("cacheBlocks incorrect in Scan.toJSON()", true, parsedJSON.get("cacheBlocks")); |
| 415 | + assertEquals("reversed incorrect in Scan.toJSON()", true, parsedJSON.get("reversed")); |
| 416 | + List trList = (List) parsedJSON.get("timeRange"); |
| 417 | + assertEquals("timeRange incorrect in Scan.toJSON()", 2, trList.size()); |
| 418 | + assertEquals("timeRange incorrect in Scan.toJSON()", "1000", trList.get(0)); |
| 419 | + assertEquals("timeRange incorrect in Scan.toJSON()", "2000", trList.get(1)); |
| 420 | + |
| 421 | + assertEquals("asyncPrefetch incorrect in Scan.toJSON()", true, parsedJSON.get("asyncPrefetch")); |
| 422 | + assertEquals("mvccReadPoint incorrect in Scan.toJSON()", "123", |
| 423 | + parsedJSON.get("mvccReadPoint")); |
| 424 | + assertEquals("limit incorrect in Scan.toJSON()", 5L, parsedJSON.get("limit")); |
| 425 | + assertEquals("readType incorrect in Scan.toJSON()", "PREAD", parsedJSON.get("readType")); |
| 426 | + assertEquals("needCursorResult incorrect in Scan.toJSON()", true, |
| 427 | + parsedJSON.get("needCursorResult")); |
| 428 | + |
| 429 | + Map colFamTimeRange = (Map) parsedJSON.get("colFamTimeRangeMap"); |
| 430 | + assertEquals("colFamTimeRangeMap incorrect in Scan.toJSON()", 1L, colFamTimeRange.size()); |
| 431 | + List testFamily = (List) colFamTimeRange.get("testFamily"); |
| 432 | + assertEquals("colFamTimeRangeMap incorrect in Scan.toJSON()", 2L, testFamily.size()); |
| 433 | + assertEquals("colFamTimeRangeMap incorrect in Scan.toJSON()", "2000", testFamily.get(0)); |
| 434 | + assertEquals("colFamTimeRangeMap incorrect in Scan.toJSON()", "3000", testFamily.get(1)); |
| 435 | + |
| 436 | + assertEquals("targetReplicaId incorrect in Scan.toJSON()", 1L, |
| 437 | + parsedJSON.get("targetReplicaId")); |
| 438 | + assertEquals("consistency incorrect in Scan.toJSON()", "STRONG", parsedJSON.get("consistency")); |
| 439 | + assertEquals("loadColumnFamiliesOnDemand incorrect in Scan.toJSON()", true, |
| 440 | + parsedJSON.get("loadColumnFamiliesOnDemand")); |
| 441 | + |
| 442 | + assertEquals("priority incorrect in Scan.toJSON()", 10L, parsedJSON.get("priority")); |
| 443 | + |
| 444 | + } |
| 445 | + |
348 | 446 | @Test
|
349 | 447 | public void testPutCreationWithByteBuffer() {
|
350 | 448 | Put p = new Put(ROW);
|
|
0 commit comments