1   /*
2    * Copyright 2010 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  
21  package org.apache.hadoop.hbase.rest;
22  
23  import org.apache.hadoop.hbase.HBaseTestingUtility;
24  import org.apache.hadoop.hbase.HColumnDescriptor;
25  import org.apache.hadoop.hbase.HTableDescriptor;
26  import org.apache.hadoop.hbase.client.Get;
27  import org.apache.hadoop.hbase.client.HBaseAdmin;
28  import org.apache.hadoop.hbase.client.HTable;
29  import org.apache.hadoop.hbase.client.Result;
30  import org.apache.hadoop.hbase.rest.client.Client;
31  import org.apache.hadoop.hbase.rest.client.Cluster;
32  import org.apache.hadoop.hbase.rest.client.Response;
33  import org.apache.hadoop.hbase.util.Bytes;
34  
35  import static org.junit.Assert.*;
36  import org.junit.AfterClass;
37  import org.junit.BeforeClass;
38  import org.junit.Test;
39  
40  public class TestTransform {
41    private static final String TABLE = "TestTransform";
42    private static final String CFA = "a";
43    private static final String CFB = "b";
44    private static final String COLUMN_1 = CFA + ":1";
45    private static final String COLUMN_2 = CFB + ":2";
46    private static final String ROW_1 = "testrow1";
47    private static final byte[] VALUE_1 = Bytes.toBytes("testvalue1");
48    private static final byte[] VALUE_2 = Bytes.toBytes("testvalue2");
49    private static final byte[] VALUE_2_BASE64 = Bytes.toBytes("dGVzdHZhbHVlMg==");
50  
51    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
52    private static final HBaseRESTTestingUtility REST_TEST_UTIL = 
53      new HBaseRESTTestingUtility();
54    private static Client client;
55  
56    @BeforeClass
57    public static void setUpBeforeClass() throws Exception {
58      TEST_UTIL.startMiniCluster(3);
59      REST_TEST_UTIL.startServletContainer(TEST_UTIL.getConfiguration());
60      client = new Client(new Cluster().add("localhost", 
61        REST_TEST_UTIL.getServletPort()));
62      HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
63      if (admin.tableExists(TABLE)) {
64        return;
65      }
66      HTableDescriptor htd = new HTableDescriptor(TABLE);
67      htd.addFamily(new HColumnDescriptor(CFA));
68      HColumnDescriptor cfB = new HColumnDescriptor(CFB);
69      cfB.setValue("Transform$1", "*:Base64");
70      htd.addFamily(cfB);
71      admin.createTable(htd);
72    }
73  
74    @AfterClass
75    public static void tearDownAfterClass() throws Exception {
76      REST_TEST_UTIL.shutdownServletContainer();
77      TEST_UTIL.shutdownMiniCluster();
78    }
79  
80    @Test
81    public void testTransform() throws Exception {
82      String path1 = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
83      String path2 = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_2;
84  
85      // store value 1
86      Response response = client.put(path1, Constants.MIMETYPE_BINARY, VALUE_1);
87      assertEquals(response.getCode(), 200);
88  
89      // store value 2 (stargate should transform into base64)
90      response = client.put(path2, Constants.MIMETYPE_BINARY, VALUE_2);
91      assertEquals(response.getCode(), 200);
92  
93      // get the table contents directly
94      HTable table = new HTable(TEST_UTIL.getConfiguration(), TABLE);
95      Get get = new Get(Bytes.toBytes(ROW_1));
96      get.addFamily(Bytes.toBytes(CFA));
97      get.addFamily(Bytes.toBytes(CFB));
98      Result result = table.get(get);
99      // value 1 should not be transformed
100     byte[] value = result.getValue(Bytes.toBytes(CFA), Bytes.toBytes("1"));
101     assertNotNull(value);
102     assertTrue(Bytes.equals(value, VALUE_1));
103     // value 2 should have been base64 encoded
104     value = result.getValue(Bytes.toBytes(CFB), Bytes.toBytes("2"));
105     assertNotNull(value);
106     assertTrue(Bytes.equals(value, VALUE_2_BASE64));
107     table.close();
108 
109     // stargate should decode the transformed value back to original bytes
110     response = client.get(path2, Constants.MIMETYPE_BINARY);
111     assertEquals(response.getCode(), 200);
112     value = response.getBody();
113     assertTrue(Bytes.equals(value, VALUE_2));
114   }
115 }