This is an automated email from the ASF dual-hosted git repository. zhangduo pushed a commit to branch branch-3 in repository https://gitbox.apache.org/repos/asf/hbase.git
commit 2faf4cf89b316db3f53c13a060f1594d9d81ed9a Author: Duo Zhang <[email protected]> AuthorDate: Wed Feb 25 21:07:21 2026 +0800 HBASE-29915 Rewrite TestMultiRowResource and TestSchemaResource (#7783) Signed-off-by: Dávid Paksy <[email protected]> Signed-off-by: Nick Dimiduk <[email protected]> Reviewed-by: Liu Xiao <[email protected]> (cherry picked from commit 64be26f07496a46d607af0df30fabb10fba99022) --- .../org/apache/hadoop/hbase/rest/RESTServer.java | 6 +-- ...Resource.java => MultiRowResourceTestBase.java} | 49 +++++++--------------- ...maResource.java => SchemaResourceTestBase.java} | 26 +++--------- .../rest/TestMultiRowResourceCsrfDisabled.java | 34 +++++++++++++++ .../rest/TestMultiRowResourceCsrfEnabled.java | 34 +++++++++++++++ .../hbase/rest/TestSchemaResourceCsrfDisabled.java | 29 +++++++++++++ .../hbase/rest/TestSchemaResourceCsrfEnabled.java | 29 +++++++++++++ 7 files changed, 150 insertions(+), 57 deletions(-) diff --git a/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/RESTServer.java b/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/RESTServer.java index 666c994d692..149d634cbd5 100644 --- a/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/RESTServer.java +++ b/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/RESTServer.java @@ -55,10 +55,10 @@ import org.slf4j.LoggerFactory; import org.apache.hbase.thirdparty.com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider; import org.apache.hbase.thirdparty.com.google.common.base.Preconditions; import org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine; +import org.apache.hbase.thirdparty.org.apache.commons.cli.DefaultParser; import org.apache.hbase.thirdparty.org.apache.commons.cli.HelpFormatter; import org.apache.hbase.thirdparty.org.apache.commons.cli.Options; import org.apache.hbase.thirdparty.org.apache.commons.cli.ParseException; -import org.apache.hbase.thirdparty.org.apache.commons.cli.PosixParser; import org.apache.hbase.thirdparty.org.eclipse.jetty.ee8.servlet.FilterHolder; import org.apache.hbase.thirdparty.org.eclipse.jetty.ee8.servlet.ServletContextHandler; import org.apache.hbase.thirdparty.org.eclipse.jetty.ee8.servlet.ServletHolder; @@ -132,7 +132,7 @@ public class RESTServer implements Constants { System.exit(exitCode); } - void addCSRFFilter(ServletContextHandler ctxHandler, Configuration conf) { + private void addCSRFFilter(ServletContextHandler ctxHandler, Configuration conf) { restCSRFEnabled = conf.getBoolean(REST_CSRF_ENABLED_KEY, REST_CSRF_ENABLED_DEFAULT); if (restCSRFEnabled) { Map<String, String> restCsrfParams = @@ -181,7 +181,7 @@ public class RESTServer implements Constants { CommandLine commandLine = null; try { - commandLine = new PosixParser().parse(options, args); + commandLine = new DefaultParser().parse(options, args); } catch (ParseException e) { LOG.error("Could not parse: ", e); printUsageAndExit(options, -1); diff --git a/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/TestMultiRowResource.java b/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/MultiRowResourceTestBase.java similarity index 93% rename from hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/TestMultiRowResource.java rename to hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/MultiRowResourceTestBase.java index ef9d7d8084c..1e66ee735cf 100644 --- a/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/TestMultiRowResource.java +++ b/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/MultiRowResourceTestBase.java @@ -25,9 +25,7 @@ import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.Base64.Encoder; -import java.util.stream.Stream; import org.apache.hadoop.conf.Configuration; -import org.apache.hadoop.hbase.HBaseParameterizedTestTemplate; import org.apache.hadoop.hbase.HBaseTestingUtil; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; @@ -40,24 +38,16 @@ import org.apache.hadoop.hbase.rest.client.Response; import org.apache.hadoop.hbase.rest.model.CellModel; import org.apache.hadoop.hbase.rest.model.CellSetModel; import org.apache.hadoop.hbase.rest.model.RowModel; -import org.apache.hadoop.hbase.testclassification.MediumTests; -import org.apache.hadoop.hbase.testclassification.RestTests; import org.apache.hadoop.hbase.util.Bytes; import org.apache.http.Header; import org.apache.http.message.BasicHeader; import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Tag; -import org.junit.jupiter.api.TestTemplate; -import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.api.Test; import org.apache.hbase.thirdparty.com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider; import org.apache.hbase.thirdparty.javax.ws.rs.core.MediaType; -@Tag(RestTests.TAG) -@Tag(MediumTests.TAG) -@HBaseParameterizedTestTemplate(name = "{index}: csrfEnabled = {0}") -public class TestMultiRowResource { +public class MultiRowResourceTestBase { private static final TableName TABLE = TableName.valueOf("TestRowResource"); private static final String CFA = "a"; @@ -78,21 +68,12 @@ public class TestMultiRowResource { private static Configuration conf; private static Header extraHdr = null; - private static boolean csrfEnabled = true; + protected static boolean csrfEnabled = true; - public TestMultiRowResource(boolean csrf) { - csrfEnabled = csrf; - } - - public static Stream<Arguments> parameters() { - return Stream.of(Arguments.of(false), Arguments.of(true)); - } - - @BeforeAll - public static void setUpBeforeClass() throws Exception { + protected static void initialize() throws Exception { conf = TEST_UTIL.getConfiguration(); - conf.setBoolean(RESTServer.REST_CSRF_ENABLED_KEY, TestMultiRowResource.csrfEnabled); - if (TestMultiRowResource.csrfEnabled) { + conf.setBoolean(RESTServer.REST_CSRF_ENABLED_KEY, csrfEnabled); + if (csrfEnabled) { conf.set(RESTServer.REST_CSRF_BROWSER_USERAGENTS_REGEX_KEY, ".*"); } extraHdr = new BasicHeader(RESTServer.REST_CSRF_CUSTOM_HEADER_DEFAULT, ""); @@ -113,12 +94,12 @@ public class TestMultiRowResource { } @AfterAll - public static void tearDownAfterClass() throws Exception { + public static void tearDownAfterAll() throws Exception { REST_TEST_UTIL.shutdownServletContainer(); TEST_UTIL.shutdownMiniCluster(); } - @TestTemplate + @Test public void testMultiCellGetJSON() throws IOException { String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1; String row_6_url = "/" + TABLE + "/" + ROW_2 + "/" + COLUMN_2; @@ -171,7 +152,7 @@ public class TestMultiRowResource { } // See https://issues.apache.org/jira/browse/HBASE-28174 - @TestTemplate + @Test public void testMultiCellGetJSONB64() throws IOException { String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1; String row_6_url = "/" + TABLE + "/" + ROW_2 + "/" + COLUMN_2; @@ -217,7 +198,7 @@ public class TestMultiRowResource { client.delete(row_6_url, extraHdr); } - @TestTemplate + @Test public void testMultiCellGetNoKeys() throws IOException { StringBuilder path = new StringBuilder(); path.append("/"); @@ -228,7 +209,7 @@ public class TestMultiRowResource { assertEquals(404, response.getCode()); } - @TestTemplate + @Test public void testMultiCellGetXML() throws IOException { String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1; String row_6_url = "/" + TABLE + "/" + ROW_2 + "/" + COLUMN_2; @@ -252,7 +233,7 @@ public class TestMultiRowResource { client.delete(row_6_url, extraHdr); } - @TestTemplate + @Test public void testMultiCellGetWithColsJSON() throws IOException { String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1; String row_6_url = "/" + TABLE + "/" + ROW_2 + "/" + COLUMN_2; @@ -285,7 +266,7 @@ public class TestMultiRowResource { client.delete(row_6_url, extraHdr); } - @TestTemplate + @Test public void testMultiCellGetJSONNotFound() throws IOException { String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1; @@ -309,7 +290,7 @@ public class TestMultiRowResource { client.delete(row_5_url, extraHdr); } - @TestTemplate + @Test public void testMultiCellGetWithColsInQueryPathJSON() throws IOException { String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1; String row_6_url = "/" + TABLE + "/" + ROW_2 + "/" + COLUMN_2; @@ -342,7 +323,7 @@ public class TestMultiRowResource { client.delete(row_6_url, extraHdr); } - @TestTemplate + @Test public void testMultiCellGetFilterJSON() throws IOException { String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1; String row_6_url = "/" + TABLE + "/" + ROW_2 + "/" + COLUMN_2; diff --git a/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/TestSchemaResource.java b/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/SchemaResourceTestBase.java similarity index 93% rename from hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/TestSchemaResource.java rename to hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/SchemaResourceTestBase.java index a7394e96127..0076afc4fbd 100644 --- a/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/TestSchemaResource.java +++ b/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/SchemaResourceTestBase.java @@ -24,11 +24,9 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.StringWriter; -import java.util.stream.Stream; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import org.apache.hadoop.conf.Configuration; -import org.apache.hadoop.hbase.HBaseParameterizedTestTemplate; import org.apache.hadoop.hbase.HBaseTestingUtil; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; @@ -45,15 +43,12 @@ import org.apache.http.Header; import org.apache.http.message.BasicHeader; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Tag; -import org.junit.jupiter.api.TestTemplate; -import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.api.Test; @Tag(RestTests.TAG) @Tag(MediumTests.TAG) -@HBaseParameterizedTestTemplate(name = "{index}: csrfEnabled = {0}") -public class TestSchemaResource { +public class SchemaResourceTestBase { private static String TABLE1 = "TestSchemaResource1"; private static String TABLE2 = "TestSchemaResource2"; @@ -66,18 +61,9 @@ public class TestSchemaResource { private static TestTableSchemaModel testTableSchemaModel; private static Header extraHdr = null; - private static boolean csrfEnabled = true; + protected static boolean csrfEnabled = true; - public TestSchemaResource(boolean csrf) { - csrfEnabled = csrf; - } - - public static Stream<Arguments> parameters() { - return Stream.of(Arguments.of(false), Arguments.of(true)); - } - - @BeforeAll - public static void setUpBeforeClass() throws Exception { + protected static void initialize() throws Exception { conf = TEST_UTIL.getConfiguration(); conf.setBoolean(RESTServer.REST_CSRF_ENABLED_KEY, csrfEnabled); if (csrfEnabled) { @@ -123,7 +109,7 @@ public class TestSchemaResource { .unmarshal(new ByteArrayInputStream(content)); } - @TestTemplate + @Test public void testTableCreateAndDeleteXML() throws IOException, JAXBException { String schemaPath = "/" + TABLE1 + "/schema"; TableSchemaModel model; @@ -184,7 +170,7 @@ public class TestSchemaResource { assertFalse(admin.tableExists(TableName.valueOf(TABLE1))); } - @TestTemplate + @Test public void testTableCreateAndDeletePB() throws IOException { String schemaPath = "/" + TABLE2 + "/schema"; TableSchemaModel model; diff --git a/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/TestMultiRowResourceCsrfDisabled.java b/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/TestMultiRowResourceCsrfDisabled.java new file mode 100644 index 00000000000..c3b788f7d35 --- /dev/null +++ b/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/TestMultiRowResourceCsrfDisabled.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.rest; + +import org.apache.hadoop.hbase.testclassification.MediumTests; +import org.apache.hadoop.hbase.testclassification.RestTests; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Tag; + +@Tag(RestTests.TAG) +@Tag(MediumTests.TAG) +public class TestMultiRowResourceCsrfDisabled extends MultiRowResourceTestBase { + + @BeforeAll + public static void setUpBeforeAll() throws Exception { + csrfEnabled = false; + initialize(); + } +} diff --git a/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/TestMultiRowResourceCsrfEnabled.java b/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/TestMultiRowResourceCsrfEnabled.java new file mode 100644 index 00000000000..e56286a2ca6 --- /dev/null +++ b/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/TestMultiRowResourceCsrfEnabled.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.rest; + +import org.apache.hadoop.hbase.testclassification.MediumTests; +import org.apache.hadoop.hbase.testclassification.RestTests; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Tag; + +@Tag(RestTests.TAG) +@Tag(MediumTests.TAG) +public class TestMultiRowResourceCsrfEnabled extends MultiRowResourceTestBase { + + @BeforeAll + public static void setUpBeforeAll() throws Exception { + csrfEnabled = true; + initialize(); + } +} diff --git a/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/TestSchemaResourceCsrfDisabled.java b/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/TestSchemaResourceCsrfDisabled.java new file mode 100644 index 00000000000..13faea51858 --- /dev/null +++ b/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/TestSchemaResourceCsrfDisabled.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.rest; + +import org.junit.jupiter.api.BeforeAll; + +public class TestSchemaResourceCsrfDisabled extends SchemaResourceTestBase { + + @BeforeAll + public static void setUpBeforeAll() throws Exception { + csrfEnabled = false; + initialize(); + } +} diff --git a/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/TestSchemaResourceCsrfEnabled.java b/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/TestSchemaResourceCsrfEnabled.java new file mode 100644 index 00000000000..0b1a9e5ea71 --- /dev/null +++ b/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/TestSchemaResourceCsrfEnabled.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.rest; + +import org.junit.jupiter.api.BeforeAll; + +public class TestSchemaResourceCsrfEnabled extends SchemaResourceTestBase { + + @BeforeAll + public static void setUpBeforeAll() throws Exception { + csrfEnabled = true; + initialize(); + } +}
