Revert "HBASE-26523 Upgrade hbase-thirdparty dependency to 4.0.0 (#3910)"
[hbase.git] / hbase-rest / src / test / java / org / apache / hadoop / hbase / rest / TestMultiRowResource.java
blobcf1ef2ea17bd632f9cdfe82d0c94b988625396aa
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.rest;
20 import static org.junit.Assert.assertEquals;
22 import com.fasterxml.jackson.databind.ObjectMapper;
23 import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
25 import java.io.IOException;
26 import java.util.Collection;
27 import javax.ws.rs.core.MediaType;
28 import javax.xml.bind.JAXBContext;
29 import javax.xml.bind.Marshaller;
30 import javax.xml.bind.Unmarshaller;
32 import org.apache.hadoop.conf.Configuration;
33 import org.apache.hadoop.hbase.HBaseClassTestRule;
34 import org.apache.hadoop.hbase.HBaseCommonTestingUtil;
35 import org.apache.hadoop.hbase.HBaseTestingUtil;
36 import org.apache.hadoop.hbase.TableName;
37 import org.apache.hadoop.hbase.client.Admin;
38 import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
39 import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
40 import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
41 import org.apache.hadoop.hbase.rest.client.Client;
42 import org.apache.hadoop.hbase.rest.client.Cluster;
43 import org.apache.hadoop.hbase.rest.client.Response;
44 import org.apache.hadoop.hbase.rest.model.CellModel;
45 import org.apache.hadoop.hbase.rest.model.CellSetModel;
46 import org.apache.hadoop.hbase.rest.model.RowModel;
47 import org.apache.hadoop.hbase.testclassification.MediumTests;
48 import org.apache.hadoop.hbase.testclassification.RestTests;
49 import org.apache.hadoop.hbase.util.Bytes;
51 import org.apache.http.Header;
52 import org.apache.http.message.BasicHeader;
54 import org.junit.AfterClass;
55 import org.junit.BeforeClass;
56 import org.junit.ClassRule;
57 import org.junit.Test;
58 import org.junit.experimental.categories.Category;
59 import org.junit.runner.RunWith;
60 import org.junit.runners.Parameterized;
62 @Category({RestTests.class, MediumTests.class})
63 @RunWith(Parameterized.class)
64 public class TestMultiRowResource {
65 @ClassRule
66 public static final HBaseClassTestRule CLASS_RULE =
67 HBaseClassTestRule.forClass(TestMultiRowResource.class);
69 private static final TableName TABLE = TableName.valueOf("TestRowResource");
70 private static final String CFA = "a";
71 private static final String CFB = "b";
72 private static final String COLUMN_1 = CFA + ":1";
73 private static final String COLUMN_2 = CFB + ":2";
74 private static final String ROW_1 = "testrow5";
75 private static final String VALUE_1 = "testvalue5";
76 private static final String ROW_2 = "testrow6";
77 private static final String VALUE_2 = "testvalue6";
79 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
80 private static final HBaseRESTTestingUtility REST_TEST_UTIL = new HBaseRESTTestingUtility();
82 private static Client client;
83 private static JAXBContext context;
84 private static Marshaller marshaller;
85 private static Unmarshaller unmarshaller;
86 private static Configuration conf;
88 private static Header extraHdr = null;
89 private static boolean csrfEnabled = true;
91 @Parameterized.Parameters
92 public static Collection<Object[]> data() {
93 return HBaseCommonTestingUtil.BOOLEAN_PARAMETERIZED;
96 public TestMultiRowResource(Boolean csrf) {
97 csrfEnabled = csrf;
100 @BeforeClass
101 public static void setUpBeforeClass() throws Exception {
102 conf = TEST_UTIL.getConfiguration();
103 conf.setBoolean(RESTServer.REST_CSRF_ENABLED_KEY, csrfEnabled);
104 if (csrfEnabled) {
105 conf.set(RESTServer.REST_CSRF_BROWSER_USERAGENTS_REGEX_KEY, ".*");
107 extraHdr = new BasicHeader(RESTServer.REST_CSRF_CUSTOM_HEADER_DEFAULT, "");
108 TEST_UTIL.startMiniCluster();
109 REST_TEST_UTIL.startServletContainer(conf);
110 context = JAXBContext.newInstance(
111 CellModel.class,
112 CellSetModel.class,
113 RowModel.class);
114 marshaller = context.createMarshaller();
115 unmarshaller = context.createUnmarshaller();
116 client = new Client(new Cluster().add("localhost", REST_TEST_UTIL.getServletPort()));
117 Admin admin = TEST_UTIL.getAdmin();
118 if (admin.tableExists(TABLE)) {
119 return;
121 TableDescriptorBuilder tableDescriptorBuilder =
122 TableDescriptorBuilder.newBuilder(TABLE);
123 ColumnFamilyDescriptor columnFamilyDescriptor =
124 ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(CFA)).build();
125 tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
126 columnFamilyDescriptor =
127 ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(CFB)).build();
128 tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
129 admin.createTable(tableDescriptorBuilder.build());
132 @AfterClass
133 public static void tearDownAfterClass() throws Exception {
134 REST_TEST_UTIL.shutdownServletContainer();
135 TEST_UTIL.shutdownMiniCluster();
138 @Test
139 public void testMultiCellGetJSON() throws IOException {
140 String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
141 String row_6_url = "/" + TABLE + "/" + ROW_2 + "/" + COLUMN_2;
143 StringBuilder path = new StringBuilder();
144 path.append("/");
145 path.append(TABLE);
146 path.append("/multiget/?row=");
147 path.append(ROW_1);
148 path.append("&row=");
149 path.append(ROW_2);
151 if (csrfEnabled) {
152 Response response = client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1));
153 assertEquals(400, response.getCode());
156 client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1), extraHdr);
157 client.post(row_6_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_2), extraHdr);
159 Response response = client.get(path.toString(), Constants.MIMETYPE_JSON);
160 assertEquals(200, response.getCode());
161 assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
163 client.delete(row_5_url, extraHdr);
164 client.delete(row_6_url, extraHdr);
167 @Test
168 public void testMultiCellGetXML() throws IOException {
169 String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
170 String row_6_url = "/" + TABLE + "/" + ROW_2 + "/" + COLUMN_2;
172 StringBuilder path = new StringBuilder();
173 path.append("/");
174 path.append(TABLE);
175 path.append("/multiget/?row=");
176 path.append(ROW_1);
177 path.append("&row=");
178 path.append(ROW_2);
180 client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1), extraHdr);
181 client.post(row_6_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_2), extraHdr);
183 Response response = client.get(path.toString(), Constants.MIMETYPE_XML);
184 assertEquals(200, response.getCode());
185 assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
187 client.delete(row_5_url, extraHdr);
188 client.delete(row_6_url, extraHdr);
191 @Test
192 public void testMultiCellGetWithColsJSON() throws IOException {
193 String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
194 String row_6_url = "/" + TABLE + "/" + ROW_2 + "/" + COLUMN_2;
196 StringBuilder path = new StringBuilder();
197 path.append("/");
198 path.append(TABLE);
199 path.append("/multiget");
200 path.append("/" + COLUMN_1 + "," + CFB);
201 path.append("?row=");
202 path.append(ROW_1);
203 path.append("&row=");
204 path.append(ROW_2);
206 client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1), extraHdr);
207 client.post(row_6_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_2), extraHdr);
209 Response response = client.get(path.toString(), Constants.MIMETYPE_JSON);
210 assertEquals(200, response.getCode());
211 ObjectMapper mapper = new JacksonJaxbJsonProvider().locateMapper(CellSetModel.class,
212 MediaType.APPLICATION_JSON_TYPE);
213 CellSetModel cellSet = mapper.readValue(response.getBody(), CellSetModel.class);
214 assertEquals(2, cellSet.getRows().size());
215 assertEquals(ROW_1, Bytes.toString(cellSet.getRows().get(0).getKey()));
216 assertEquals(VALUE_1, Bytes.toString(cellSet.getRows().get(0).getCells().get(0).getValue()));
217 assertEquals(ROW_2, Bytes.toString(cellSet.getRows().get(1).getKey()));
218 assertEquals(VALUE_2, Bytes.toString(cellSet.getRows().get(1).getCells().get(0).getValue()));
220 client.delete(row_5_url, extraHdr);
221 client.delete(row_6_url, extraHdr);
224 @Test
225 public void testMultiCellGetJSONNotFound() throws IOException {
226 String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
228 StringBuilder path = new StringBuilder();
229 path.append("/");
230 path.append(TABLE);
231 path.append("/multiget/?row=");
232 path.append(ROW_1);
233 path.append("&row=");
234 path.append(ROW_2);
236 client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1), extraHdr);
237 Response response = client.get(path.toString(), Constants.MIMETYPE_JSON);
238 assertEquals(200, response.getCode());
239 ObjectMapper mapper = new JacksonJaxbJsonProvider().locateMapper(CellSetModel.class,
240 MediaType.APPLICATION_JSON_TYPE);
241 CellSetModel cellSet = (CellSetModel) mapper.readValue(response.getBody(), CellSetModel.class);
242 assertEquals(1, cellSet.getRows().size());
243 assertEquals(ROW_1, Bytes.toString(cellSet.getRows().get(0).getKey()));
244 assertEquals(VALUE_1, Bytes.toString(cellSet.getRows().get(0).getCells().get(0).getValue()));
245 client.delete(row_5_url, extraHdr);
248 @Test
249 public void testMultiCellGetWithColsInQueryPathJSON() throws IOException {
250 String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
251 String row_6_url = "/" + TABLE + "/" + ROW_2 + "/" + COLUMN_2;
253 StringBuilder path = new StringBuilder();
254 path.append("/");
255 path.append(TABLE);
256 path.append("/multiget/?row=");
257 path.append(ROW_1);
258 path.append("/");
259 path.append(COLUMN_1);
260 path.append("&row=");
261 path.append(ROW_2);
262 path.append("/");
263 path.append(COLUMN_1);
265 client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1), extraHdr);
266 client.post(row_6_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_2), extraHdr);
268 Response response = client.get(path.toString(), Constants.MIMETYPE_JSON);
269 assertEquals(200, response.getCode());
270 ObjectMapper mapper = new JacksonJaxbJsonProvider().locateMapper(
271 CellSetModel.class, MediaType.APPLICATION_JSON_TYPE);
272 CellSetModel cellSet = mapper.readValue(response.getBody(), CellSetModel.class);
273 assertEquals(1, cellSet.getRows().size());
274 assertEquals(ROW_1, Bytes.toString(cellSet.getRows().get(0).getKey()));
275 assertEquals(VALUE_1, Bytes.toString(cellSet.getRows().get(0).getCells().get(0).getValue()));
277 client.delete(row_5_url, extraHdr);
278 client.delete(row_6_url, extraHdr);