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.hadoop.conf.Configuration;
23  import org.apache.hadoop.hbase.*;
24  import org.apache.hadoop.hbase.client.HBaseAdmin;
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.util.Bytes;
32  import org.junit.AfterClass;
33  import org.junit.BeforeClass;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  import javax.xml.bind.JAXBContext;
38  import javax.xml.bind.JAXBException;
39  import javax.xml.bind.Marshaller;
40  import javax.xml.bind.Unmarshaller;
41  import java.io.IOException;
42  
43  import static org.junit.Assert.assertEquals;
44  
45  
46  @Category(MediumTests.class)
47  public class TestMultiRowResource {
48  
49    private static final String TABLE = "TestRowResource";
50    private static final String CFA = "a";
51    private static final String CFB = "b";
52    private static final String COLUMN_1 = CFA + ":1";
53    private static final String COLUMN_2 = CFB + ":2";
54    private static final String ROW_1 = "testrow5";
55    private static final String VALUE_1 = "testvalue5";
56    private static final String ROW_2 = "testrow6";
57    private static final String VALUE_2 = "testvalue6";
58  
59  
60    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
61    private static final HBaseRESTTestingUtility REST_TEST_UTIL = new HBaseRESTTestingUtility();
62  
63    private static Client client;
64    private static JAXBContext context;
65    private static Marshaller marshaller;
66    private static Unmarshaller unmarshaller;
67    private static Configuration conf;
68  
69  
70    @BeforeClass
71    public static void setUpBeforeClass() throws Exception {
72      conf = TEST_UTIL.getConfiguration();
73      TEST_UTIL.startMiniCluster();
74      REST_TEST_UTIL.startServletContainer(conf);
75      context = JAXBContext.newInstance(
76              CellModel.class,
77              CellSetModel.class,
78              RowModel.class);
79      marshaller = context.createMarshaller();
80      unmarshaller = context.createUnmarshaller();
81      client = new Client(new Cluster().add("localhost", REST_TEST_UTIL.getServletPort()));
82      HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
83      if (admin.tableExists(TABLE)) {
84        return;
85      }
86      HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(TABLE));
87      htd.addFamily(new HColumnDescriptor(CFA));
88      htd.addFamily(new HColumnDescriptor(CFB));
89      admin.createTable(htd);
90    }
91  
92    @AfterClass
93    public static void tearDownAfterClass() throws Exception {
94      REST_TEST_UTIL.shutdownServletContainer();
95      TEST_UTIL.shutdownMiniCluster();
96    }
97  
98  
99    @Test
100   public void testMultiCellGetJSON() throws IOException, JAXBException {
101     String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
102     String row_6_url = "/" + TABLE + "/" + ROW_2 + "/" + COLUMN_2;
103 
104 
105     StringBuilder path = new StringBuilder();
106     path.append("/");
107     path.append(TABLE);
108     path.append("/multiget/?row=");
109     path.append(ROW_1);
110     path.append("&row=");
111     path.append(ROW_2);
112 
113     client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1));
114     client.post(row_6_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_2));
115 
116 
117     Response response = client.get(path.toString(), Constants.MIMETYPE_JSON);
118     assertEquals(response.getCode(), 200);
119     assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
120 
121     client.delete(row_5_url);
122     client.delete(row_6_url);
123 
124   }
125 
126   @Test
127   public void testMultiCellGetXML() throws IOException, JAXBException {
128     String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
129     String row_6_url = "/" + TABLE + "/" + ROW_2 + "/" + COLUMN_2;
130 
131 
132     StringBuilder path = new StringBuilder();
133     path.append("/");
134     path.append(TABLE);
135     path.append("/multiget/?row=");
136     path.append(ROW_1);
137     path.append("&row=");
138     path.append(ROW_2);
139 
140     client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1));
141     client.post(row_6_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_2));
142 
143 
144     Response response = client.get(path.toString(), Constants.MIMETYPE_XML);
145     assertEquals(response.getCode(), 200);
146     assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
147 
148     client.delete(row_5_url);
149     client.delete(row_6_url);
150 
151   }
152 
153   @Test
154   public void testMultiCellGetJSONNotFound() throws IOException {
155     String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
156 
157     StringBuilder path = new StringBuilder();
158     path.append("/");
159     path.append(TABLE);
160     path.append("/multiget/?row=");
161     path.append(ROW_1);
162     path.append("&row=");
163     path.append(ROW_2);
164 
165     client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1));
166 
167     Response response = client.get(path.toString(), Constants.MIMETYPE_JSON);
168 
169     assertEquals(response.getCode(), 404);
170 
171   }
172 
173 }
174