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