OmniaGM commented on code in PR #13201: URL: https://github.com/apache/kafka/pull/13201#discussion_r1358474706
########## tools/src/test/java/org/apache/kafka/tools/TopicCommandTest.java: ########## @@ -0,0 +1,283 @@ +/* + * 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.tools; + +import kafka.utils.Exit; +import org.apache.kafka.clients.admin.Admin; +import org.apache.kafka.clients.admin.AdminClientTestUtils; +import org.apache.kafka.clients.admin.CreatePartitionsResult; +import org.apache.kafka.clients.admin.CreateTopicsResult; +import org.apache.kafka.clients.admin.DeleteTopicsOptions; +import org.apache.kafka.clients.admin.DeleteTopicsResult; +import org.apache.kafka.clients.admin.DescribeTopicsResult; +import org.apache.kafka.clients.admin.ListTopicsResult; +import org.apache.kafka.clients.admin.NewTopic; +import org.apache.kafka.clients.admin.PartitionReassignment; +import org.apache.kafka.clients.admin.TopicDescription; +import org.apache.kafka.common.Node; +import org.apache.kafka.common.TopicPartitionInfo; +import org.apache.kafka.common.errors.ThrottlingQuotaExceededException; +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.server.common.AdminCommandFailedException; +import org.apache.kafka.server.common.AdminOperationException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ExecutionException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyCollection; +import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.verify; + +@Timeout(value = 60) +public class TopicCommandTest { + private String bootstrapServer = "localhost:9092"; + private String topicName = "topicName"; + + @Test + public void testIsNotUnderReplicatedWhenAdding() { + List<Integer> replicaIds = Arrays.asList(1, 2); + List<Node> replicas = new ArrayList<>(); + for (int id : replicaIds) { + replicas.add(new Node(id, "localhost", 9090 + id)); + } + + TopicCommand.PartitionDescription partitionDescription = new TopicCommand.PartitionDescription("test-topic", + new TopicPartitionInfo(0, new Node(1, "localhost", 9091), replicas, + Collections.singletonList(new Node(1, "localhost", 9091))), + null, false, + new PartitionReassignment(replicaIds, Arrays.asList(2), Collections.emptyList()) + ); + + assertFalse(partitionDescription.isUnderReplicated()); + } + + @Test + public void testAlterWithUnspecifiedPartitionCount() { + String[] options = new String[] {" --bootstrap-server", bootstrapServer, "--alter", "--topic", topicName}; + assertInitializeInvalidOptionsExitCode(1, options); + } + + @Test + public void testConfigOptWithBootstrapServers() { + assertInitializeInvalidOptionsExitCode(1, + new String[] {"--bootstrap-server", bootstrapServer, "--alter", "--topic", topicName, + "--partitions", "3", "--config", "cleanup.policy=compact"}); + assertInitializeInvalidOptionsExitCode(1, + new String[] {"--bootstrap-server", bootstrapServer, "--alter", "--topic", topicName, + "--partitions", "3", "--delete-config", "cleanup.policy"}); + TopicCommand.TopicCommandOptions opts = + new TopicCommand.TopicCommandOptions( + new String[] {"--bootstrap-server", bootstrapServer, "--create", "--topic", topicName, "--partitions", "3", + "--replication-factor", "3", "--config", "cleanup.policy=compact"}); + assertTrue(opts.hasCreateOption()); + assertEquals(bootstrapServer, opts.bootstrapServer().get()); + assertEquals("cleanup.policy=compact", opts.topicConfig().get().get(0)); + } + + @Test + public void testCreateWithPartitionCountWithoutReplicationFactorShouldSucceed() { + TopicCommand.TopicCommandOptions opts = new TopicCommand.TopicCommandOptions( + new String[] {"--bootstrap-server", bootstrapServer, + "--create", + "--partitions", "2", + "--topic", topicName}); + assertTrue(opts.hasCreateOption()); + assertEquals(topicName, opts.topic().get()); + assertEquals(2, opts.partitions().get()); Review Comment: `checkArgs()` is called in the constructor of `TopicCommandOptions`. It is redundant to call `opts.checkArgs` ########## tools/src/test/java/org/apache/kafka/tools/TopicCommandTest.java: ########## @@ -0,0 +1,283 @@ +/* + * 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.tools; + +import kafka.utils.Exit; +import org.apache.kafka.clients.admin.Admin; +import org.apache.kafka.clients.admin.AdminClientTestUtils; +import org.apache.kafka.clients.admin.CreatePartitionsResult; +import org.apache.kafka.clients.admin.CreateTopicsResult; +import org.apache.kafka.clients.admin.DeleteTopicsOptions; +import org.apache.kafka.clients.admin.DeleteTopicsResult; +import org.apache.kafka.clients.admin.DescribeTopicsResult; +import org.apache.kafka.clients.admin.ListTopicsResult; +import org.apache.kafka.clients.admin.NewTopic; +import org.apache.kafka.clients.admin.PartitionReassignment; +import org.apache.kafka.clients.admin.TopicDescription; +import org.apache.kafka.common.Node; +import org.apache.kafka.common.TopicPartitionInfo; +import org.apache.kafka.common.errors.ThrottlingQuotaExceededException; +import org.apache.kafka.common.protocol.Errors; +import org.apache.kafka.server.common.AdminCommandFailedException; +import org.apache.kafka.server.common.AdminOperationException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ExecutionException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyCollection; +import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.verify; + +@Timeout(value = 60) +public class TopicCommandTest { + private String bootstrapServer = "localhost:9092"; + private String topicName = "topicName"; + + @Test + public void testIsNotUnderReplicatedWhenAdding() { + List<Integer> replicaIds = Arrays.asList(1, 2); + List<Node> replicas = new ArrayList<>(); + for (int id : replicaIds) { + replicas.add(new Node(id, "localhost", 9090 + id)); + } + + TopicCommand.PartitionDescription partitionDescription = new TopicCommand.PartitionDescription("test-topic", + new TopicPartitionInfo(0, new Node(1, "localhost", 9091), replicas, + Collections.singletonList(new Node(1, "localhost", 9091))), + null, false, + new PartitionReassignment(replicaIds, Arrays.asList(2), Collections.emptyList()) + ); + + assertFalse(partitionDescription.isUnderReplicated()); + } + + @Test + public void testAlterWithUnspecifiedPartitionCount() { + String[] options = new String[] {" --bootstrap-server", bootstrapServer, "--alter", "--topic", topicName}; + assertInitializeInvalidOptionsExitCode(1, options); + } + + @Test + public void testConfigOptWithBootstrapServers() { + assertInitializeInvalidOptionsExitCode(1, + new String[] {"--bootstrap-server", bootstrapServer, "--alter", "--topic", topicName, + "--partitions", "3", "--config", "cleanup.policy=compact"}); + assertInitializeInvalidOptionsExitCode(1, + new String[] {"--bootstrap-server", bootstrapServer, "--alter", "--topic", topicName, + "--partitions", "3", "--delete-config", "cleanup.policy"}); + TopicCommand.TopicCommandOptions opts = + new TopicCommand.TopicCommandOptions( + new String[] {"--bootstrap-server", bootstrapServer, "--create", "--topic", topicName, "--partitions", "3", + "--replication-factor", "3", "--config", "cleanup.policy=compact"}); + assertTrue(opts.hasCreateOption()); + assertEquals(bootstrapServer, opts.bootstrapServer().get()); + assertEquals("cleanup.policy=compact", opts.topicConfig().get().get(0)); + } + + @Test + public void testCreateWithPartitionCountWithoutReplicationFactorShouldSucceed() { + TopicCommand.TopicCommandOptions opts = new TopicCommand.TopicCommandOptions( + new String[] {"--bootstrap-server", bootstrapServer, + "--create", + "--partitions", "2", + "--topic", topicName}); + assertTrue(opts.hasCreateOption()); + assertEquals(topicName, opts.topic().get()); + assertEquals(2, opts.partitions().get()); + } + + @Test + public void testCreateWithReplicationFactorWithoutPartitionCountShouldSucceed() { + TopicCommand.TopicCommandOptions opts = new TopicCommand.TopicCommandOptions( + new String[] {"--bootstrap-server", bootstrapServer, + "--create", + "--replication-factor", "3", + "--topic", topicName}); + assertTrue(opts.hasCreateOption()); + assertEquals(topicName, opts.topic().get()); + assertEquals(3, opts.replicationFactor().get()); Review Comment: `checkArgs()` is called in the constructor of `TopicCommandOptions`. It is redundant to call `opts.checkArgs` -- 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: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org