2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.rest
;
20 import static org
.junit
.Assert
.assertEquals
;
21 import static org
.junit
.Assert
.assertFalse
;
23 import java
.io
.ByteArrayInputStream
;
24 import java
.io
.IOException
;
25 import javax
.xml
.bind
.JAXBContext
;
26 import javax
.xml
.bind
.JAXBException
;
27 import org
.apache
.hadoop
.conf
.Configuration
;
28 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
29 import org
.apache
.hadoop
.hbase
.HBaseTestingUtil
;
30 import org
.apache
.hadoop
.hbase
.NamespaceDescriptor
;
31 import org
.apache
.hadoop
.hbase
.client
.Admin
;
32 import org
.apache
.hadoop
.hbase
.rest
.client
.Client
;
33 import org
.apache
.hadoop
.hbase
.rest
.client
.Cluster
;
34 import org
.apache
.hadoop
.hbase
.rest
.client
.Response
;
35 import org
.apache
.hadoop
.hbase
.rest
.model
.NamespacesModel
;
36 import org
.apache
.hadoop
.hbase
.rest
.model
.TestNamespacesModel
;
37 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
38 import org
.apache
.hadoop
.hbase
.testclassification
.RestTests
;
39 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
40 import org
.junit
.AfterClass
;
41 import org
.junit
.BeforeClass
;
42 import org
.junit
.ClassRule
;
43 import org
.junit
.Test
;
44 import org
.junit
.experimental
.categories
.Category
;
46 @Category({RestTests
.class, MediumTests
.class})
47 public class TestNamespacesResource
{
49 public static final HBaseClassTestRule CLASS_RULE
=
50 HBaseClassTestRule
.forClass(TestNamespacesResource
.class);
52 private static String NAMESPACE1
= "TestNamespacesInstanceResource1";
53 private static String NAMESPACE2
= "TestNamespacesInstanceResource2";
55 private static final HBaseTestingUtil TEST_UTIL
= new HBaseTestingUtil();
56 private static final HBaseRESTTestingUtility REST_TEST_UTIL
=
57 new HBaseRESTTestingUtility();
58 private static Client client
;
59 private static JAXBContext context
;
60 private static Configuration conf
;
61 private static TestNamespacesModel testNamespacesModel
;
64 public static void setUpBeforeClass() throws Exception
{
65 conf
= TEST_UTIL
.getConfiguration();
66 TEST_UTIL
.startMiniCluster();
67 REST_TEST_UTIL
.startServletContainer(conf
);
68 client
= new Client(new Cluster().add("localhost", REST_TEST_UTIL
.getServletPort()));
69 testNamespacesModel
= new TestNamespacesModel();
70 context
= JAXBContext
.newInstance(NamespacesModel
.class);
74 public static void tearDownAfterClass() throws Exception
{
75 REST_TEST_UTIL
.shutdownServletContainer();
76 TEST_UTIL
.shutdownMiniCluster();
79 private static NamespacesModel
fromXML(byte[] content
) throws JAXBException
{
80 return (NamespacesModel
) context
.createUnmarshaller()
81 .unmarshal(new ByteArrayInputStream(content
));
84 private boolean doesNamespaceExist(Admin admin
, String namespaceName
) throws IOException
{
85 NamespaceDescriptor
[] nd
= admin
.listNamespaceDescriptors();
86 for (NamespaceDescriptor namespaceDescriptor
: nd
) {
87 if (namespaceDescriptor
.getName().equals(namespaceName
)) {
94 private void createNamespaceViaAdmin(Admin admin
, String name
) throws IOException
{
95 NamespaceDescriptor
.Builder builder
= NamespaceDescriptor
.create(name
);
96 NamespaceDescriptor nsd
= builder
.build();
97 admin
.createNamespace(nsd
);
101 public void testNamespaceListXMLandJSON() throws IOException
, JAXBException
{
102 String namespacePath
= "/namespaces/";
103 NamespacesModel model
;
106 // Check that namespace does not yet exist via non-REST call.
107 Admin admin
= TEST_UTIL
.getAdmin();
108 assertFalse(doesNamespaceExist(admin
, NAMESPACE1
));
109 model
= testNamespacesModel
.buildTestModel();
110 testNamespacesModel
.checkModel(model
);
112 // Check that REST GET finds only default namespaces via XML and JSON responses.
113 response
= client
.get(namespacePath
, Constants
.MIMETYPE_XML
);
114 assertEquals(200, response
.getCode());
115 model
= fromXML(response
.getBody());
116 testNamespacesModel
.checkModel(model
, "hbase", "default");
117 response
= client
.get(namespacePath
, Constants
.MIMETYPE_JSON
);
118 assertEquals(200, response
.getCode());
119 model
= testNamespacesModel
.fromJSON(Bytes
.toString(response
.getBody()));
120 testNamespacesModel
.checkModel(model
, "hbase", "default");
122 // Create namespace and check that REST GET finds one additional namespace.
123 createNamespaceViaAdmin(admin
, NAMESPACE1
);
124 response
= client
.get(namespacePath
, Constants
.MIMETYPE_XML
);
125 assertEquals(200, response
.getCode());
126 model
= fromXML(response
.getBody());
127 testNamespacesModel
.checkModel(model
, NAMESPACE1
, "hbase", "default");
128 response
= client
.get(namespacePath
, Constants
.MIMETYPE_JSON
);
129 assertEquals(200, response
.getCode());
130 model
= testNamespacesModel
.fromJSON(Bytes
.toString(response
.getBody()));
131 testNamespacesModel
.checkModel(model
, NAMESPACE1
, "hbase", "default");
133 // Create another namespace and check that REST GET finds one additional namespace.
134 createNamespaceViaAdmin(admin
, NAMESPACE2
);
135 response
= client
.get(namespacePath
, Constants
.MIMETYPE_XML
);
136 assertEquals(200, response
.getCode());
137 model
= fromXML(response
.getBody());
138 testNamespacesModel
.checkModel(model
, NAMESPACE1
, NAMESPACE2
, "hbase", "default");
139 response
= client
.get(namespacePath
, Constants
.MIMETYPE_JSON
);
140 assertEquals(200, response
.getCode());
141 model
= testNamespacesModel
.fromJSON(Bytes
.toString(response
.getBody()));
142 testNamespacesModel
.checkModel(model
, NAMESPACE1
, NAMESPACE2
, "hbase", "default");
144 // Delete namespace and check that REST still finds correct namespaces.
145 admin
.deleteNamespace(NAMESPACE1
);
146 response
= client
.get(namespacePath
, Constants
.MIMETYPE_XML
);
147 assertEquals(200, response
.getCode());
148 model
= fromXML(response
.getBody());
149 testNamespacesModel
.checkModel(model
, NAMESPACE2
, "hbase", "default");
150 response
= client
.get(namespacePath
, Constants
.MIMETYPE_JSON
);
151 assertEquals(200, response
.getCode());
152 model
= testNamespacesModel
.fromJSON(Bytes
.toString(response
.getBody()));
153 testNamespacesModel
.checkModel(model
, NAMESPACE2
, "hbase", "default");
155 admin
.deleteNamespace(NAMESPACE2
);
159 public void testNamespaceListPBandDefault() throws IOException
{
160 String schemaPath
= "/namespaces/";
161 NamespacesModel model
;
164 // Check that namespace does not yet exist via non-REST call.
165 Admin admin
= TEST_UTIL
.getAdmin();
166 assertFalse(doesNamespaceExist(admin
, NAMESPACE1
));
167 model
= testNamespacesModel
.buildTestModel();
168 testNamespacesModel
.checkModel(model
);
170 // Check that REST GET finds only default namespaces via PB and default Accept header.
171 response
= client
.get(schemaPath
, Constants
.MIMETYPE_PROTOBUF
);
172 assertEquals(200, response
.getCode());
173 model
.getObjectFromMessage(response
.getBody());
174 testNamespacesModel
.checkModel(model
, "hbase", "default");
175 response
= client
.get(schemaPath
);
176 assertEquals(200, response
.getCode());
178 // Create namespace and check that REST GET finds one additional namespace.
179 createNamespaceViaAdmin(admin
, NAMESPACE1
);
180 response
= client
.get(schemaPath
, Constants
.MIMETYPE_PROTOBUF
);
181 assertEquals(200, response
.getCode());
182 model
.getObjectFromMessage(response
.getBody());
183 testNamespacesModel
.checkModel(model
, NAMESPACE1
, "hbase", "default");
184 response
= client
.get(schemaPath
);
185 assertEquals(200, response
.getCode());
187 // Create another namespace and check that REST GET finds one additional namespace.
188 createNamespaceViaAdmin(admin
, NAMESPACE2
);
189 response
= client
.get(schemaPath
, Constants
.MIMETYPE_PROTOBUF
);
190 assertEquals(200, response
.getCode());
191 model
.getObjectFromMessage(response
.getBody());
192 testNamespacesModel
.checkModel(model
, NAMESPACE1
, NAMESPACE2
, "hbase", "default");
193 response
= client
.get(schemaPath
);
194 assertEquals(200, response
.getCode());
196 // Delete namespace and check that REST GET still finds correct namespaces.
197 admin
.deleteNamespace(NAMESPACE1
);
198 response
= client
.get(schemaPath
, Constants
.MIMETYPE_PROTOBUF
);
199 assertEquals(200, response
.getCode());
200 model
.getObjectFromMessage(response
.getBody());
201 testNamespacesModel
.checkModel(model
, NAMESPACE2
, "hbase", "default");
202 response
= client
.get(schemaPath
);
203 assertEquals(200, response
.getCode());
205 admin
.deleteNamespace(NAMESPACE2
);