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.conf.Configuration;
24 import org.apache.hadoop.hbase.HBaseTestingUtility;
25 import org.apache.hadoop.hbase.HColumnDescriptor;
26 import org.apache.hadoop.hbase.HTableDescriptor;
27 import org.apache.hadoop.hbase.client.HBaseAdmin;
28 import org.apache.hadoop.hbase.rest.client.Client;
29 import org.apache.hadoop.hbase.rest.client.Cluster;
30 import org.apache.hadoop.hbase.rest.client.Response;
31 import org.apache.hadoop.hbase.rest.model.CellModel;
32 import org.apache.hadoop.hbase.rest.model.CellSetModel;
33 import org.apache.hadoop.hbase.rest.model.RowModel;
34 import org.apache.hadoop.hbase.util.Bytes;
35 import org.junit.AfterClass;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38
39 import javax.xml.bind.JAXBContext;
40 import javax.xml.bind.JAXBException;
41 import javax.xml.bind.Marshaller;
42 import javax.xml.bind.Unmarshaller;
43 import java.io.IOException;
44
45 import static org.junit.Assert.assertEquals;
46
47
48 public class TestMultiRowResource {
49
50 private static final String TABLE = "TestRowResource";
51 private static final String CFA = "a";
52 private static final String CFB = "b";
53 private static final String COLUMN_1 = CFA + ":1";
54 private static final String COLUMN_2 = CFB + ":2";
55 private static final String ROW_1 = "testrow5";
56 private static final String VALUE_1 = "testvalue5";
57 private static final String ROW_2 = "testrow6";
58 private static final String VALUE_2 = "testvalue6";
59
60
61 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
62 private static final HBaseRESTTestingUtility REST_TEST_UTIL = new HBaseRESTTestingUtility();
63
64 private static Client client;
65 private static JAXBContext context;
66 private static Marshaller marshaller;
67 private static Unmarshaller unmarshaller;
68 private static Configuration conf;
69
70
71 @BeforeClass
72 public static void setUpBeforeClass() throws Exception {
73 conf = TEST_UTIL.getConfiguration();
74 TEST_UTIL.startMiniCluster();
75 REST_TEST_UTIL.startServletContainer(conf);
76 context = JAXBContext.newInstance(
77 CellModel.class,
78 CellSetModel.class,
79 RowModel.class);
80 marshaller = context.createMarshaller();
81 unmarshaller = context.createUnmarshaller();
82 client = new Client(new Cluster().add("localhost", REST_TEST_UTIL.getServletPort()));
83 HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
84 if (admin.tableExists(TABLE)) {
85 return;
86 }
87 HTableDescriptor htd = new HTableDescriptor(TABLE);
88 htd.addFamily(new HColumnDescriptor(CFA));
89 htd.addFamily(new HColumnDescriptor(CFB));
90 admin.createTable(htd);
91 }
92
93 @AfterClass
94 public static void tearDownAfterClass() throws Exception {
95 REST_TEST_UTIL.shutdownServletContainer();
96 TEST_UTIL.shutdownMiniCluster();
97 }
98
99
100 @Test
101 public void testMultiCellGetJSON() throws IOException, JAXBException {
102 String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
103 String row_6_url = "/" + TABLE + "/" + ROW_2 + "/" + COLUMN_2;
104
105
106 StringBuilder path = new StringBuilder();
107 path.append("/");
108 path.append(TABLE);
109 path.append("/multiget/?row=");
110 path.append(ROW_1);
111 path.append("&row=");
112 path.append(ROW_2);
113
114 client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1));
115 client.post(row_6_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_2));
116
117
118 Response response = client.get(path.toString(), Constants.MIMETYPE_JSON);
119 assertEquals(response.getCode(), 200);
120
121 client.delete(row_5_url);
122 client.delete(row_6_url);
123
124 }
125
126 @Test
127 public void testMultiCellGetXML() throws IOException, JAXBException {
128 String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
129 String row_6_url = "/" + TABLE + "/" + ROW_2 + "/" + COLUMN_2;
130
131
132 StringBuilder path = new StringBuilder();
133 path.append("/");
134 path.append(TABLE);
135 path.append("/multiget/?row=");
136 path.append(ROW_1);
137 path.append("&row=");
138 path.append(ROW_2);
139
140 client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1));
141 client.post(row_6_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_2));
142
143
144 Response response = client.get(path.toString(), Constants.MIMETYPE_XML);
145 assertEquals(response.getCode(), 200);
146
147 client.delete(row_5_url);
148 client.delete(row_6_url);
149
150 }
151
152 @Test
153 public void testMultiCellGetJSONNotFound() throws IOException {
154 String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
155
156 StringBuilder path = new StringBuilder();
157 path.append("/");
158 path.append(TABLE);
159 path.append("/multiget/?row=");
160 path.append(ROW_1);
161 path.append("&row=");
162 path.append(ROW_2);
163
164 client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1));
165
166 Response response = client.get(path.toString(), Constants.MIMETYPE_JSON);
167
168 assertEquals(response.getCode(), 404);
169
170 }
171 }