1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.client;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import org.junit.Test;
25
26 import java.io.IOException;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.apache.hadoop.hbase.util.Bytes;
32
33 import org.codehaus.jackson.map.ObjectMapper;
34
35
36
37
38
39 public class TestOperation {
40 private static byte [] ROW = Bytes.toBytes("testRow");
41 private static byte [] FAMILY = Bytes.toBytes("testFamily");
42 private static byte [] QUALIFIER = Bytes.toBytes("testQualifier");
43 private static byte [] VALUE = Bytes.toBytes("testValue");
44
45 private static ObjectMapper mapper = new ObjectMapper();
46
47
48
49
50
51
52 @Test
53 public void testOperationJSON()
54 throws IOException {
55
56 Scan scan = new Scan(ROW);
57 scan.addColumn(FAMILY, QUALIFIER);
58
59 String json = scan.toJSON();
60 Map<String, Object> parsedJSON = mapper.readValue(json, HashMap.class);
61
62 assertEquals("startRow incorrect in Scan.toJSON()",
63 Bytes.toStringBinary(ROW), parsedJSON.get("startRow"));
64
65 List familyInfo = (List) ((Map) parsedJSON.get("families")).get(
66 Bytes.toStringBinary(FAMILY));
67 assertNotNull("Family absent in Scan.toJSON()", familyInfo);
68 assertEquals("Qualifier absent in Scan.toJSON()", 1, familyInfo.size());
69 assertEquals("Qualifier incorrect in Scan.toJSON()",
70 Bytes.toStringBinary(QUALIFIER),
71 familyInfo.get(0));
72
73
74 Get get = new Get(ROW);
75 get.addColumn(FAMILY, QUALIFIER);
76
77 json = get.toJSON();
78 parsedJSON = mapper.readValue(json, HashMap.class);
79
80 assertEquals("row incorrect in Get.toJSON()",
81 Bytes.toStringBinary(ROW), parsedJSON.get("row"));
82
83 familyInfo = (List) ((Map) parsedJSON.get("families")).get(
84 Bytes.toStringBinary(FAMILY));
85 assertNotNull("Family absent in Get.toJSON()", familyInfo);
86 assertEquals("Qualifier absent in Get.toJSON()", 1, familyInfo.size());
87 assertEquals("Qualifier incorrect in Get.toJSON()",
88 Bytes.toStringBinary(QUALIFIER),
89 familyInfo.get(0));
90
91
92 Put put = new Put(ROW);
93 put.add(FAMILY, QUALIFIER, VALUE);
94
95 json = put.toJSON();
96 parsedJSON = mapper.readValue(json, HashMap.class);
97
98 assertEquals("row absent in Put.toJSON()",
99 Bytes.toStringBinary(ROW), parsedJSON.get("row"));
100
101 familyInfo = (List) ((Map) parsedJSON.get("families")).get(
102 Bytes.toStringBinary(FAMILY));
103 assertNotNull("Family absent in Put.toJSON()", familyInfo);
104 assertEquals("KeyValue absent in Put.toJSON()", 1, familyInfo.size());
105 Map kvMap = (Map) familyInfo.get(0);
106 assertEquals("Qualifier incorrect in Put.toJSON()",
107 Bytes.toStringBinary(QUALIFIER),
108 kvMap.get("qualifier"));
109 assertEquals("Value length incorrect in Put.toJSON()",
110 VALUE.length, kvMap.get("vlen"));
111
112
113 Delete delete = new Delete(ROW);
114 delete.deleteColumn(FAMILY, QUALIFIER);
115
116 json = delete.toJSON();
117 parsedJSON = mapper.readValue(json, HashMap.class);
118
119 assertEquals("row absent in Delete.toJSON()",
120 Bytes.toStringBinary(ROW), parsedJSON.get("row"));
121
122 familyInfo = (List) ((Map) parsedJSON.get("families")).get(
123 Bytes.toStringBinary(FAMILY));
124 assertNotNull("Family absent in Delete.toJSON()", familyInfo);
125 assertEquals("KeyValue absent in Delete.toJSON()", 1, familyInfo.size());
126 kvMap = (Map) familyInfo.get(0);
127 assertEquals("Qualifier incorrect in Delete.toJSON()",
128 Bytes.toStringBinary(QUALIFIER), kvMap.get("qualifier"));
129 }
130 }