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