This is an automated email from the ASF dual-hosted git repository.

pradeepagrawal8184 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ranger.git


The following commit(s) were added to refs/heads/master by this push:
     new 33ba8e8bc RANGER-5579: add validation to DefaultSchemaRegistryClient 
createUrlSelector method (#935)
33ba8e8bc is described below

commit 33ba8e8bc11a083a5962b99570440bd0e0cc2dc6
Author: Vyom Mani Tiwari <[email protected]>
AuthorDate: Wed Jun 17 15:29:34 2026 +0530

    RANGER-5579: add validation to DefaultSchemaRegistryClient 
createUrlSelector method (#935)
    
    * RANGER-5579: add validation to DefaultSchemaRegistryClient 
createUrlSelector method
    
    * RANGER-5579: incorporated the review comments
    
    * RANGER-5579: addressed review comment
---
 .../connection/DefaultSchemaRegistryClient.java    |  7 +-
 .../schema/registry/client/UrlSelectorTest.java    | 81 ++++++++++++++++++++++
 2 files changed, 87 insertions(+), 1 deletion(-)

diff --git 
a/plugin-schema-registry/src/main/java/org/apache/ranger/services/schema/registry/client/connection/DefaultSchemaRegistryClient.java
 
b/plugin-schema-registry/src/main/java/org/apache/ranger/services/schema/registry/client/connection/DefaultSchemaRegistryClient.java
index 6a7e7ca6a..1268018b3 100644
--- 
a/plugin-schema-registry/src/main/java/org/apache/ranger/services/schema/registry/client/connection/DefaultSchemaRegistryClient.java
+++ 
b/plugin-schema-registry/src/main/java/org/apache/ranger/services/schema/registry/client/connection/DefaultSchemaRegistryClient.java
@@ -55,6 +55,7 @@ public class DefaultSchemaRegistryClient implements 
ISchemaRegistryClient {
     private static final String SCHEMA_REGISTRY_PATH         = 
"/api/v1/schemaregistry";
     private static final String SCHEMAS_PATH                 = 
SCHEMA_REGISTRY_PATH + "/schemas/";
     private static final String SCHEMA_REGISTRY_VERSION_PATH = 
SCHEMA_REGISTRY_PATH + "/version";
+    public static final String ERR_CLASS_NOT_IMPLEMENTING_URL_SELECTOR = ": 
class does not implement UrlSelector";
     private static final String SSL_ALGORITHM                = "TLS";
 
     private final Client                             client;
@@ -248,7 +249,11 @@ private UrlSelector createUrlSelector() {
             urlSelector = new LoadBalancedFailoverUrlSelector(rootCatalogURL);
         } else {
             try {
-                urlSelector = (UrlSelector) 
Class.forName(urlSelectorClass).getConstructor(String.class).newInstance(rootCatalogURL);
+                Class<?> clazz = Class.forName(urlSelectorClass);
+                if (!UrlSelector.class.isAssignableFrom(clazz)) {
+                    throw new RuntimeException(urlSelectorClass + 
ERR_CLASS_NOT_IMPLEMENTING_URL_SELECTOR);
+                }
+                urlSelector = (UrlSelector) 
clazz.getConstructor(String.class).newInstance(rootCatalogURL);
             } catch (InstantiationException | IllegalAccessException | 
ClassNotFoundException | NoSuchMethodException | InvocationTargetException e) {
                 throw new RuntimeException(e);
             }
diff --git 
a/plugin-schema-registry/src/test/java/org/apache/ranger/services/schema/registry/client/UrlSelectorTest.java
 
b/plugin-schema-registry/src/test/java/org/apache/ranger/services/schema/registry/client/UrlSelectorTest.java
new file mode 100644
index 000000000..7d8b62471
--- /dev/null
+++ 
b/plugin-schema-registry/src/test/java/org/apache/ranger/services/schema/registry/client/UrlSelectorTest.java
@@ -0,0 +1,81 @@
+/*
+ * 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.ranger.services.schema.registry.client;
+
+import 
org.apache.ranger.services.schema.registry.client.connection.DefaultSchemaRegistryClient;
+import org.junit.jupiter.api.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static 
com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient.Configuration.SCHEMA_REGISTRY_URL;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class UrlSelectorTest {
+    @Test
+    void testCreateUrlSelectorWithUnauthorizedClass() {
+        Map<String, Object> conf = new HashMap<>();
+        conf.put(SCHEMA_REGISTRY_URL.name(), "http://localhost:8060/api/v1";);
+        conf.put("schema.registry.client.url.selector", 
"org.springframework.context.support.ClassPathXmlApplicationContext");
+        RuntimeException ex = assertThrows(RuntimeException.class, () -> new 
DefaultSchemaRegistryClient(conf));
+        
assertTrue(ex.getMessage().contains(DefaultSchemaRegistryClient.ERR_CLASS_NOT_IMPLEMENTING_URL_SELECTOR));
+    }
+
+    @Test
+    void testCreateUrlSelectorWithValidClass() {
+        Map<String, Object> conf = new HashMap<>();
+        conf.put(SCHEMA_REGISTRY_URL.name(), "http://localhost:8060/api/v1";);
+        conf.put("schema.registry.client.url.selector", 
"com.hortonworks.registries.schemaregistry.client.LoadBalancedFailoverUrlSelector");
+        assertDoesNotThrow(() -> new DefaultSchemaRegistryClient(conf));
+    }
+
+    @Test
+    void testCreateUrlSelectorDefault() {
+        Map<String, Object> conf = new HashMap<>();
+        conf.put(SCHEMA_REGISTRY_URL.name(), "http://localhost:8060/api/v1";);
+        assertDoesNotThrow(() -> new DefaultSchemaRegistryClient(conf));
+    }
+
+    @Test
+    void testCreateUrlSelectorWithNonExistentClass() {
+        Map<String, Object> conf = new HashMap<>();
+        conf.put(SCHEMA_REGISTRY_URL.name(), "http://localhost:8060/api/v1";);
+        conf.put("schema.registry.client.url.selector", 
"com.nonexistent.FakeSelector");
+        RuntimeException ex = assertThrows(RuntimeException.class, () -> new 
DefaultSchemaRegistryClient(conf));
+        assertInstanceOf(ClassNotFoundException.class, ex.getCause());
+    }
+
+    @Test
+    void testCreateUrlSelectorWithNullSelector() {
+        Map<String, Object> conf = new HashMap<>();
+        conf.put(SCHEMA_REGISTRY_URL.name(), "http://localhost:8060/api/v1";);
+        conf.put("schema.registry.client.url.selector", null);
+        assertDoesNotThrow(() -> new DefaultSchemaRegistryClient(conf));
+    }
+
+    @Test
+    void testCreateUrlSelectorWithEmptySelector() {
+        Map<String, Object> conf = new HashMap<>();
+        conf.put(SCHEMA_REGISTRY_URL.name(), "http://localhost:8060/api/v1";);
+        conf.put("schema.registry.client.url.selector", "");
+        assertThrows(IllegalArgumentException.class, () -> new 
DefaultSchemaRegistryClient(conf));
+    }
+}

Reply via email to