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