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