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