1   /**
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
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   * Run tests that use the funtionality of the Operation superclass for 
37   * Puts, Gets, Deletes, Scans, and MultiPuts.
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     * Test the client Operations' JSON encoding to ensure that produced JSON is 
49     * parseable and that the details are present and not corrupted.
50     * @throws IOException
51     */
52    @Test
53    public void testOperationJSON()
54        throws IOException {
55      // produce a Scan Operation
56      Scan scan = new Scan(ROW);
57      scan.addColumn(FAMILY, QUALIFIER);
58      // get its JSON representation, and parse it
59      String json = scan.toJSON();
60      Map<String, Object> parsedJSON = mapper.readValue(json, HashMap.class);
61      // check for the row
62      assertEquals("startRow incorrect in Scan.toJSON()",
63          Bytes.toStringBinary(ROW), parsedJSON.get("startRow"));
64      // check for the family and the qualifier.
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      // produce a Get Operation
74      Get get = new Get(ROW);
75      get.addColumn(FAMILY, QUALIFIER);
76      // get its JSON representation, and parse it
77      json = get.toJSON();
78      parsedJSON = mapper.readValue(json, HashMap.class);
79      // check for the row
80      assertEquals("row incorrect in Get.toJSON()",
81          Bytes.toStringBinary(ROW), parsedJSON.get("row"));
82      // check for the family and the qualifier.
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      // produce a Put operation
92      Put put = new Put(ROW);
93      put.add(FAMILY, QUALIFIER, VALUE);
94      // get its JSON representation, and parse it
95      json = put.toJSON();
96      parsedJSON = mapper.readValue(json, HashMap.class);
97      // check for the row
98      assertEquals("row absent in Put.toJSON()",
99          Bytes.toStringBinary(ROW), parsedJSON.get("row"));
100     // check for the family and the qualifier.
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     // produce a Delete operation
113     Delete delete = new Delete(ROW);
114     delete.deleteColumn(FAMILY, QUALIFIER);
115     // get its JSON representation, and parse it
116     json = delete.toJSON();
117     parsedJSON = mapper.readValue(json, HashMap.class);
118     // check for the row
119     assertEquals("row absent in Delete.toJSON()",
120         Bytes.toStringBinary(ROW), parsedJSON.get("row"));
121     // check for the family and the qualifier.
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 }