OmniaGM commented on code in PR #23843:
URL: https://github.com/apache/kafka/pull/23843#discussion_r4377428438


##########
mirror-coordinator/src/test/java/org/apache/kafka/coordinator/mirror/ClusterMirrorConfigTest.java:
##########
@@ -0,0 +1,719 @@
+/*
+ * 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.kafka.coordinator.mirror;
+
+import org.apache.kafka.common.acl.AccessControlEntry;
+import org.apache.kafka.common.acl.AclBinding;
+import org.apache.kafka.common.acl.AclOperation;
+import org.apache.kafka.common.acl.AclPermissionType;
+import org.apache.kafka.common.config.ConfigException;
+import org.apache.kafka.common.config.SaslConfigs;
+import org.apache.kafka.common.config.SslConfigs;
+import org.apache.kafka.common.resource.PatternType;
+import org.apache.kafka.common.resource.ResourcePattern;
+import org.apache.kafka.common.resource.ResourceType;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Properties;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class ClusterMirrorConfigTest {
+    @Test
+    public void testFromPropertiesWithDefaults() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.BOOTSTRAP_SERVERS_CONFIG, 
"localhost:9092");
+        props.setProperty(ClusterMirrorConfig.SOURCE_CLUSTER_ID_CONFIG, 
"source-cluster");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        assertEquals("localhost:9092", 
config.originals().get(ClusterMirrorConfig.BOOTSTRAP_SERVERS_CONFIG));
+        assertEquals("source-cluster", 
config.originals().get(ClusterMirrorConfig.SOURCE_CLUSTER_ID_CONFIG));
+        assertEquals(1000, config.fetchWaitMaxMs());
+    }
+
+    @Test
+    public void testFromPropertiesWithCustomValues() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.BOOTSTRAP_SERVERS_CONFIG, 
"broker1:9092,broker2:9092");
+        props.setProperty(ClusterMirrorConfig.FETCH_WAIT_MAX_MS_CONFIG, 
"5000");
+        props.setProperty(ClusterMirrorConfig.TOPICS_INCLUDE_CONFIG, 
"topic1,topic2");
+        props.setProperty(ClusterMirrorConfig.TOPICS_EXCLUDE_CONFIG, "__.*");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        assertEquals(5000, config.fetchWaitMaxMs());
+        assertNotNull(config.topicsIncludePattern());
+        assertNotNull(config.topicsExcludePattern());
+    }
+
+    @Test
+    public void testTopicPropertiesExclude() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.TOPIC_PROPERTIES_EXCLUDE_CONFIG,
+                
"leader.replication.throttled.replicas,follower.replication.throttled.replicas");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        assertNotNull(config.topicPropertiesExcludePattern());
+        assertTrue(config.topicPropertiesExcludePattern()
+                .matcher("leader.replication.throttled.replicas").matches());
+        assertFalse(config.topicPropertiesExcludePattern()
+                .matcher("some.other.property").matches());
+    }
+
+    @Test
+    public void testTopicPropertiesExcludeWithRegex() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.TOPIC_PROPERTIES_EXCLUDE_CONFIG, 
"min.*,max.*");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        
assertTrue(config.topicPropertiesExcludePattern().matcher("min.insync.replicas").matches());
+        
assertTrue(config.topicPropertiesExcludePattern().matcher("max.message.bytes").matches());
+        
assertFalse(config.topicPropertiesExcludePattern().matcher("cleanup.policy").matches());
+    }
+
+    @Test
+    public void testTopicsInclude() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.TOPICS_INCLUDE_CONFIG, 
"test.*,prod.*");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        
assertTrue(config.topicsIncludePattern().matcher("test-topic").matches());
+        
assertTrue(config.topicsIncludePattern().matcher("prod-topic").matches());
+        
assertFalse(config.topicsIncludePattern().matcher("staging").matches());
+    }
+
+    @Test
+    public void testTopicsIncludeAll() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.TOPICS_INCLUDE_CONFIG, ".*");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        
assertTrue(config.topicsIncludePattern().matcher("any-topic").matches());
+        assertTrue(config.topicsIncludePattern().matcher("test").matches());
+    }
+
+    @Test
+    public void testTopicsIncludeWithEmpty() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.TOPICS_INCLUDE_CONFIG, "");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        assertNull(config.topicsIncludePattern());
+    }
+
+    @Test
+    public void testTopicsIncludePatternWithWhitespaces() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.TOPICS_INCLUDE_CONFIG, "   ");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        assertNull(config.topicsIncludePattern());
+    }
+
+    @Test
+    public void testTopicsIncludeWihInvalidRegex() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.TOPICS_INCLUDE_CONFIG, 
"test[invalid");
+
+        ConfigException ex = assertThrows(ConfigException.class, () -> 
ClusterMirrorConfig.fromProperties(props));
+        assertTrue(ex.getMessage().contains("Invalid regex pattern"));
+    }
+
+    @Test
+    public void testTopicsExclude() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.TOPICS_EXCLUDE_CONFIG, 
"__.*,internal-.*");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        
assertTrue(config.topicsExcludePattern().matcher("__consumer_offsets").matches());
+        
assertTrue(config.topicsExcludePattern().matcher("internal-topic").matches());
+        
assertFalse(config.topicsExcludePattern().matcher("user-topic").matches());
+    }
+
+    @Test
+    public void testTopicsExcludeAll() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.TOPICS_EXCLUDE_CONFIG, ".*");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        
assertTrue(config.topicsExcludePattern().matcher("any-topic").matches());
+        assertTrue(config.topicsExcludePattern().matcher("test").matches());
+    }
+
+    @Test
+    public void testTopicsExcludeWithEmpty() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.TOPICS_EXCLUDE_CONFIG, "");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        assertNull(config.topicsExcludePattern());
+    }
+
+    @Test
+    public void testTopicsExcludeWithWhitespaces() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.TOPICS_EXCLUDE_CONFIG, "   ");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        assertNull(config.topicsExcludePattern());
+    }
+
+    @Test
+    public void testGroupsInclude() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.GROUPS_INCLUDE_CONFIG, "app.*");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        
assertTrue(config.groupsIncludePattern().matcher("app-group-1").matches());
+        
assertFalse(config.groupsIncludePattern().matcher("test-group").matches());
+    }
+
+    @Test
+    public void testGroupsIncludeAll() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.GROUPS_INCLUDE_CONFIG, ".*");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        
assertTrue(config.groupsIncludePattern().matcher("any-group").matches());
+        assertTrue(config.groupsIncludePattern().matcher("test").matches());
+    }
+
+    @Test
+    public void testGroupsIncludeWithEmpty() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.GROUPS_INCLUDE_CONFIG, "");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        assertNull(config.groupsIncludePattern());
+    }
+
+    @Test
+    public void testGroupsIncludeWithWhitespaces() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.GROUPS_INCLUDE_CONFIG, "   ");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        assertNull(config.groupsIncludePattern());
+    }
+
+    @Test
+    public void testGroupsExclude() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.GROUPS_EXCLUDE_CONFIG, 
"console-.*");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        
assertTrue(config.groupsExcludePattern().matcher("console-consumer").matches());
+        
assertFalse(config.groupsExcludePattern().matcher("my-group").matches());
+    }
+
+    @Test
+    public void testGroupsExcludeAll() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.GROUPS_EXCLUDE_CONFIG, ".*");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        
assertTrue(config.groupsExcludePattern().matcher("any-group").matches());
+        assertTrue(config.groupsExcludePattern().matcher("test").matches());
+    }
+
+    @Test
+    public void testGroupsExcludeWithEmpty() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.GROUPS_EXCLUDE_CONFIG, "");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        assertNull(config.groupsExcludePattern());
+    }
+
+    @Test
+    public void testGroupsExcludeWithWhitespaces() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.GROUPS_EXCLUDE_CONFIG, "   ");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        assertNull(config.groupsExcludePattern());
+    }
+
+    @Test
+    public void testAclsInclude() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.ACLS_INCLUDE_CONFIG, "TOPIC;.*");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        assertEquals(1, config.aclIncludeRules().size());
+        ClusterMirrorConfig.MirrorAclRule rule = 
config.aclIncludeRules().get(0);
+        assertEquals(ResourceType.TOPIC, rule.resourceType());
+    }
+
+    @Test
+    public void testAclsIncludeAll() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.ACLS_INCLUDE_CONFIG, ".*");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        assertEquals(1, config.aclIncludeRules().size());
+        ClusterMirrorConfig.MirrorAclRule rule = 
config.aclIncludeRules().get(0);
+        assertEquals(ClusterMirrorConfig.MirrorAclRule.MATCH_ALL_RULE, rule);
+    }
+
+    @Test
+    public void testAclsIncludeWithMultipleFields() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.ACLS_INCLUDE_CONFIG, 
"TOPIC;.*;READ;ALLOW");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        assertEquals(1, config.aclIncludeRules().size());
+        ClusterMirrorConfig.MirrorAclRule rule = 
config.aclIncludeRules().get(0);
+        assertEquals(ResourceType.TOPIC, rule.resourceType());
+        assertEquals(AclOperation.READ, rule.operation());
+        assertEquals(AclPermissionType.ALLOW, rule.permissionType());
+    }
+
+    @Test
+    public void testAclsIncludeWithExtraFields() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.ACLS_INCLUDE_CONFIG,
+                "TOPIC;.*;READ;ALLOW;User:alice;extrafield;moredata");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        assertEquals(1, config.aclIncludeRules().size());
+        ClusterMirrorConfig.MirrorAclRule rule = 
config.aclIncludeRules().get(0);
+        assertEquals("User:alice", rule.principal());
+        // Extra fields beyond the 6th are captured in the host pattern (split 
limit=6)
+        assertNotNull(rule.hostPattern());
+        
assertTrue(rule.hostPattern().matcher("extrafield;moredata").matches());
+    }
+
+    @Test
+    public void testAclsIncludeWithPrincipal() {
+        Properties props = new Properties();
+        props.setProperty(ClusterMirrorConfig.ACLS_INCLUDE_CONFIG, 
"TOPIC;.*;READ;ALLOW;User:alice");
+
+        ClusterMirrorConfig config = ClusterMirrorConfig.fromProperties(props);
+
+        assertEquals(1, config.aclIncludeRules().size());
+        ClusterMirrorConfig.MirrorAclRule rule = 
config.aclIncludeRules().get(0);
+        assertEquals("User:alice", rule.principal());
+    }
+
+    @Test
+    public void testAclsIncludeWihWildcardPrincipal() {

Review Comment:
   nit: typo `testAclsIncludeWihWildcardPrincipal` in `Wih` should be `With`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to