View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest;
21  
22  import static org.junit.Assert.assertEquals;
23  
24  import com.fasterxml.jackson.databind.ObjectMapper;
25  import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
26  
27  import java.io.IOException;
28  import java.util.ArrayList;
29  import java.util.Collection;
30  import java.util.List;
31  import javax.ws.rs.core.MediaType;
32  import javax.xml.bind.JAXBContext;
33  import javax.xml.bind.JAXBException;
34  
35  import org.apache.commons.httpclient.Header;
36  import org.apache.hadoop.conf.Configuration;
37  import org.apache.hadoop.hbase.*;
38  import org.apache.hadoop.hbase.client.Admin;
39  import org.apache.hadoop.hbase.rest.client.Client;
40  import org.apache.hadoop.hbase.rest.client.Cluster;
41  import org.apache.hadoop.hbase.rest.client.Response;
42  import org.apache.hadoop.hbase.rest.model.CellModel;
43  import org.apache.hadoop.hbase.rest.model.CellSetModel;
44  import org.apache.hadoop.hbase.rest.model.RowModel;
45  import org.apache.hadoop.hbase.rest.provider.JacksonProvider;
46  import org.apache.hadoop.hbase.testclassification.MediumTests;
47  import org.apache.hadoop.hbase.util.Bytes;
48  
49  import org.junit.AfterClass;
50  import org.junit.BeforeClass;
51  import org.junit.Test;
52  import org.junit.experimental.categories.Category;
53  import org.junit.runner.RunWith;
54  import org.junit.runners.Parameterized;
55  
56  import javax.ws.rs.core.MediaType;
57  import javax.xml.bind.JAXBContext;
58  import javax.xml.bind.JAXBException;
59  import java.io.IOException;
60  import java.util.ArrayList;
61  import java.util.Collection;
62  import java.util.List;
63  
64  @Category(MediumTests.class)
65  @RunWith(Parameterized.class)
66  public class TestMultiRowResource {
67  
68    private static final TableName TABLE = TableName.valueOf("TestRowResource");
69    private static final String CFA = "a";
70    private static final String CFB = "b";
71    private static final String COLUMN_1 = CFA + ":1";
72    private static final String COLUMN_2 = CFB + ":2";
73    private static final String ROW_1 = "testrow5";
74    private static final String VALUE_1 = "testvalue5";
75    private static final String ROW_2 = "testrow6";
76    private static final String VALUE_2 = "testvalue6";
77  
78  
79    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
80    private static final HBaseRESTTestingUtility REST_TEST_UTIL = new HBaseRESTTestingUtility();
81  
82    private static Client client;
83    @SuppressWarnings("unused")
84    private static JAXBContext context;
85    private static Configuration conf;
86  
87    private static Header extraHdr = null;
88    private static boolean csrfEnabled = true;
89  
90    @Parameterized.Parameters
91    public static Collection<Object[]> data() {
92      List<Object[]> params = new ArrayList<Object[]>();
93      params.add(new Object[] {Boolean.TRUE});
94      params.add(new Object[] {Boolean.FALSE});
95      return params;
96    }
97  
98    public TestMultiRowResource(Boolean csrf) {
99      csrfEnabled = csrf;
100   }
101 
102 
103   @BeforeClass
104   public static void setUpBeforeClass() throws Exception {
105     conf = TEST_UTIL.getConfiguration();
106     conf.setBoolean(RESTServer.REST_CSRF_ENABLED_KEY, csrfEnabled);
107     extraHdr = new Header(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     client = new Client(new Cluster().add("localhost", REST_TEST_UTIL.getServletPort()));
115     Admin admin = TEST_UTIL.getHBaseAdmin();
116     if (admin.tableExists(TABLE)) {
117       return;
118     }
119     HTableDescriptor htd = new HTableDescriptor(TABLE);
120     htd.addFamily(new HColumnDescriptor(CFA));
121     htd.addFamily(new HColumnDescriptor(CFB));
122     admin.createTable(htd);
123   }
124 
125   @AfterClass
126   public static void tearDownAfterClass() throws Exception {
127     REST_TEST_UTIL.shutdownServletContainer();
128     TEST_UTIL.shutdownMiniCluster();
129   }
130 
131 
132   @Test
133   public void testMultiCellGetJSON() throws IOException, JAXBException {
134     String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
135     String row_6_url = "/" + TABLE + "/" + ROW_2 + "/" + COLUMN_2;
136 
137 
138     StringBuilder path = new StringBuilder();
139     path.append("/");
140     path.append(TABLE);
141     path.append("/multiget/?row=");
142     path.append(ROW_1);
143     path.append("&row=");
144     path.append(ROW_2);
145 
146     if (csrfEnabled) {
147       Response response = client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1));
148       assertEquals(400, response.getCode());
149     }
150 
151     client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1), extraHdr);
152     client.post(row_6_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_2), extraHdr);
153 
154 
155     Response response = client.get(path.toString(), Constants.MIMETYPE_JSON);
156     assertEquals(200, response.getCode());
157     assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
158 
159     client.delete(row_5_url, extraHdr);
160     client.delete(row_6_url, extraHdr);
161 
162   }
163 
164   @Test
165   public void testMultiCellGetXML() throws IOException, JAXBException {
166     String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
167     String row_6_url = "/" + TABLE + "/" + ROW_2 + "/" + COLUMN_2;
168 
169 
170     StringBuilder path = new StringBuilder();
171     path.append("/");
172     path.append(TABLE);
173     path.append("/multiget/?row=");
174     path.append(ROW_1);
175     path.append("&row=");
176     path.append(ROW_2);
177 
178     client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1), extraHdr);
179     client.post(row_6_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_2), extraHdr);
180 
181 
182     Response response = client.get(path.toString(), Constants.MIMETYPE_XML);
183     assertEquals(200, response.getCode());
184     assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
185 
186     client.delete(row_5_url, extraHdr);
187     client.delete(row_6_url, extraHdr);
188 
189   }
190 
191   @Test
192   public void testMultiCellGetWithColsJSON() throws IOException, JAXBException {
193     String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
194     String row_6_url = "/" + TABLE + "/" + ROW_2 + "/" + COLUMN_2;
195 
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);
205 
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);
208 
209     Response response = client.get(path.toString(), Constants.MIMETYPE_JSON);
210     assertEquals(200, response.getCode());
211     ObjectMapper mapper =
212         new JacksonProvider().locateMapper(CellSetModel.class, MediaType.APPLICATION_JSON_TYPE);
213     CellSetModel cellSet = (CellSetModel) 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()));
219 
220     client.delete(row_5_url, extraHdr);
221     client.delete(row_6_url, extraHdr);
222 
223   }
224 
225   @Test
226   public void testMultiCellGetJSONNotFound() throws IOException, JAXBException {
227     String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
228 
229     StringBuilder path = new StringBuilder();
230     path.append("/");
231     path.append(TABLE);
232     path.append("/multiget/?row=");
233     path.append(ROW_1);
234     path.append("&row=");
235     path.append(ROW_2);
236 
237     client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1), extraHdr);
238     Response response = client.get(path.toString(), Constants.MIMETYPE_JSON);
239     assertEquals(200, response.getCode());
240     ObjectMapper mapper = new JacksonProvider().locateMapper(CellSetModel.class,
241       MediaType.APPLICATION_JSON_TYPE);
242     CellSetModel cellSet = (CellSetModel) mapper.readValue(response.getBody(), CellSetModel.class);
243     assertEquals(1, cellSet.getRows().size());
244     assertEquals(ROW_1, Bytes.toString(cellSet.getRows().get(0).getKey()));
245     assertEquals(VALUE_1, Bytes.toString(cellSet.getRows().get(0).getCells().get(0).getValue()));
246     client.delete(row_5_url, extraHdr);
247   }
248 
249 }
250