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.rest;
21
22 import org.apache.hadoop.conf.Configuration;
23 import org.apache.hadoop.hbase.*;
24 import org.apache.hadoop.hbase.client.Admin;
25 import org.apache.hadoop.hbase.rest.client.Client;
26 import org.apache.hadoop.hbase.rest.client.Cluster;
27 import org.apache.hadoop.hbase.rest.client.Response;
28 import org.apache.hadoop.hbase.rest.model.CellModel;
29 import org.apache.hadoop.hbase.rest.model.CellSetModel;
30 import org.apache.hadoop.hbase.rest.model.RowModel;
31 import org.apache.hadoop.hbase.rest.provider.JacksonProvider;
32 import org.apache.hadoop.hbase.testclassification.MediumTests;
33 import org.apache.hadoop.hbase.util.Bytes;
34 import org.codehaus.jackson.map.ObjectMapper;
35 import org.junit.AfterClass;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.junit.experimental.categories.Category;
39
40 import javax.ws.rs.core.MediaType;
41 import javax.xml.bind.JAXBContext;
42 import javax.xml.bind.JAXBException;
43 import javax.xml.bind.Marshaller;
44 import javax.xml.bind.Unmarshaller;
45 import java.io.IOException;
46
47 import static org.junit.Assert.assertEquals;
48
49
50 @Category(MediumTests.class)
51 public class TestMultiRowResource {
52
53 private static final TableName TABLE = TableName.valueOf("TestRowResource");
54 private static final String CFA = "a";
55 private static final String CFB = "b";
56 private static final String COLUMN_1 = CFA + ":1";
57 private static final String COLUMN_2 = CFB + ":2";
58 private static final String ROW_1 = "testrow5";
59 private static final String VALUE_1 = "testvalue5";
60 private static final String ROW_2 = "testrow6";
61 private static final String VALUE_2 = "testvalue6";
62
63
64 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
65 private static final HBaseRESTTestingUtility REST_TEST_UTIL = new HBaseRESTTestingUtility();
66
67 private static Client client;
68 private static JAXBContext context;
69 private static Marshaller marshaller;
70 private static Unmarshaller unmarshaller;
71 private static Configuration conf;
72
73
74 @BeforeClass
75 public static void setUpBeforeClass() throws Exception {
76 conf = TEST_UTIL.getConfiguration();
77 TEST_UTIL.startMiniCluster();
78 REST_TEST_UTIL.startServletContainer(conf);
79 context = JAXBContext.newInstance(
80 CellModel.class,
81 CellSetModel.class,
82 RowModel.class);
83 marshaller = context.createMarshaller();
84 unmarshaller = context.createUnmarshaller();
85 client = new Client(new Cluster().add("localhost", REST_TEST_UTIL.getServletPort()));
86 Admin admin = TEST_UTIL.getHBaseAdmin();
87 if (admin.tableExists(TABLE)) {
88 return;
89 }
90 HTableDescriptor htd = new HTableDescriptor(TABLE);
91 htd.addFamily(new HColumnDescriptor(CFA));
92 htd.addFamily(new HColumnDescriptor(CFB));
93 admin.createTable(htd);
94 }
95
96 @AfterClass
97 public static void tearDownAfterClass() throws Exception {
98 REST_TEST_UTIL.shutdownServletContainer();
99 TEST_UTIL.shutdownMiniCluster();
100 }
101
102
103 @Test
104 public void testMultiCellGetJSON() throws IOException, JAXBException {
105 String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
106 String row_6_url = "/" + TABLE + "/" + ROW_2 + "/" + COLUMN_2;
107
108
109 StringBuilder path = new StringBuilder();
110 path.append("/");
111 path.append(TABLE);
112 path.append("/multiget/?row=");
113 path.append(ROW_1);
114 path.append("&row=");
115 path.append(ROW_2);
116
117 client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1));
118 client.post(row_6_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_2));
119
120
121 Response response = client.get(path.toString(), Constants.MIMETYPE_JSON);
122 assertEquals(response.getCode(), 200);
123 assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
124
125 client.delete(row_5_url);
126 client.delete(row_6_url);
127
128 }
129
130 @Test
131 public void testMultiCellGetXML() throws IOException, JAXBException {
132 String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
133 String row_6_url = "/" + TABLE + "/" + ROW_2 + "/" + COLUMN_2;
134
135
136 StringBuilder path = new StringBuilder();
137 path.append("/");
138 path.append(TABLE);
139 path.append("/multiget/?row=");
140 path.append(ROW_1);
141 path.append("&row=");
142 path.append(ROW_2);
143
144 client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1));
145 client.post(row_6_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_2));
146
147
148 Response response = client.get(path.toString(), Constants.MIMETYPE_XML);
149 assertEquals(response.getCode(), 200);
150 assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
151
152 client.delete(row_5_url);
153 client.delete(row_6_url);
154
155 }
156
157 @Test
158 public void testMultiCellGetWithColsJSON() throws IOException, JAXBException {
159 String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
160 String row_6_url = "/" + TABLE + "/" + ROW_2 + "/" + COLUMN_2;
161
162 StringBuilder path = new StringBuilder();
163 path.append("/");
164 path.append(TABLE);
165 path.append("/multiget");
166 path.append("/" + COLUMN_1 + "," + CFB);
167 path.append("?row=");
168 path.append(ROW_1);
169 path.append("&row=");
170 path.append(ROW_2);
171
172 client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1));
173 client.post(row_6_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_2));
174
175 Response response = client.get(path.toString(), Constants.MIMETYPE_JSON);
176 assertEquals(response.getCode(), 200);
177 ObjectMapper mapper =
178 new JacksonProvider().locateMapper(CellSetModel.class, MediaType.APPLICATION_JSON_TYPE);
179 CellSetModel cellSet = (CellSetModel) mapper.readValue(response.getBody(), CellSetModel.class);
180 assertEquals(2, cellSet.getRows().size());
181 assertEquals(ROW_1, Bytes.toString(cellSet.getRows().get(0).getKey()));
182 assertEquals(VALUE_1, Bytes.toString(cellSet.getRows().get(0).getCells().get(0).getValue()));
183 assertEquals(ROW_2, Bytes.toString(cellSet.getRows().get(1).getKey()));
184 assertEquals(VALUE_2, Bytes.toString(cellSet.getRows().get(1).getCells().get(0).getValue()));
185
186 client.delete(row_5_url);
187 client.delete(row_6_url);
188
189 }
190
191 @Test
192 public void testMultiCellGetJSONNotFound() throws IOException, JAXBException {
193 String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
194
195 StringBuilder path = new StringBuilder();
196 path.append("/");
197 path.append(TABLE);
198 path.append("/multiget/?row=");
199 path.append(ROW_1);
200 path.append("&row=");
201 path.append(ROW_2);
202
203 client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1));
204 Response response = client.get(path.toString(), Constants.MIMETYPE_JSON);
205 assertEquals(response.getCode(), 200);
206 ObjectMapper mapper = new JacksonProvider().locateMapper(CellSetModel.class,
207 MediaType.APPLICATION_JSON_TYPE);
208 CellSetModel cellSet = (CellSetModel) mapper.readValue(response.getBody(), CellSetModel.class);
209 assertEquals(1, cellSet.getRows().size());
210 assertEquals(ROW_1, Bytes.toString(cellSet.getRows().get(0).getKey()));
211 assertEquals(VALUE_1, Bytes.toString(cellSet.getRows().get(0).getCells().get(0).getValue()));
212 client.delete(row_5_url);
213 }
214
215 }
216