1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
86 Response response = client.put(path1, Constants.MIMETYPE_BINARY, VALUE_1);
87 assertEquals(response.getCode(), 200);
88
89
90 response = client.put(path2, Constants.MIMETYPE_BINARY, VALUE_2);
91 assertEquals(response.getCode(), 200);
92
93
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
100 byte[] value = result.getValue(Bytes.toBytes(CFA), Bytes.toBytes("1"));
101 assertNotNull(value);
102 assertTrue(Bytes.equals(value, VALUE_1));
103
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
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 }