chia7712 commented on code in PR #16255:
URL: https://github.com/apache/kafka/pull/16255#discussion_r1632366297


##########
clients/src/test/java/org/apache/kafka/clients/admin/NewTopicTest.java:
##########
@@ -0,0 +1,149 @@
+/*
+ * 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.clients.admin;
+
+import org.apache.kafka.common.message.CreateTopicsRequestData;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import static 
org.apache.kafka.common.requests.CreateTopicsRequest.NO_NUM_PARTITIONS;
+import static 
org.apache.kafka.common.requests.CreateTopicsRequest.NO_REPLICATION_FACTOR;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+
+public class NewTopicTest {
+
+    public static final String TEST_TOPIC = "testtopic";
+    public static final int NUM_PARTITIONS = 3;
+    public static final int REPLICATION_FACTOR = 1;
+    public static final String CLEANUP_POLICY_CONFIG_KEY = "cleanup.policy";
+    public static final String CLEANUP_POLICY_CONFIG_VALUE = "compact";
+    public static final List<Integer> BROKER_IDS = Arrays.asList(1, 2);
+
+    @Test
+    public void testConstructorWithPartitionsAndReplicationFactor() {
+        NewTopic topic = new NewTopic(TEST_TOPIC, NUM_PARTITIONS, (short) 
REPLICATION_FACTOR);
+        assertEquals(TEST_TOPIC, topic.name());
+        assertEquals(NUM_PARTITIONS, topic.numPartitions());
+        assertEquals(REPLICATION_FACTOR, topic.replicationFactor());
+        assertNull(topic.replicasAssignments());
+    }
+
+    @Test
+    public void testConstructorWithOptionalValues() {
+        Optional<Integer> numPartitions = Optional.empty();
+        Optional<Short> replicationFactor = Optional.empty();
+        NewTopic topic = new NewTopic(TEST_TOPIC, numPartitions, 
replicationFactor);
+        assertEquals(TEST_TOPIC, topic.name());
+        assertEquals(NO_NUM_PARTITIONS, topic.numPartitions());
+        assertEquals(NO_REPLICATION_FACTOR, topic.replicationFactor());
+        assertNull(topic.replicasAssignments());
+    }
+
+    @Test
+    public void testConstructorWithReplicasAssignments() {
+        Map<Integer, List<Integer>> replicasAssignments = new HashMap<>();
+        replicasAssignments.put(0, BROKER_IDS);
+        NewTopic newTopic = new NewTopic(TEST_TOPIC, replicasAssignments);
+        assertEquals(TEST_TOPIC, newTopic.name());
+        assertEquals(NO_NUM_PARTITIONS, newTopic.numPartitions());
+        assertEquals(NO_REPLICATION_FACTOR, newTopic.replicationFactor());
+        assertEquals(replicasAssignments, newTopic.replicasAssignments());
+    }
+
+    @Test
+    public void testConfigs() {
+        NewTopic newTopic = new NewTopic(TEST_TOPIC, NUM_PARTITIONS, (short) 
REPLICATION_FACTOR);
+        Map<String, String> configs = new HashMap<>();

Review Comment:
   Could you verify that `newTopic.configs()` will return null if it has no 
configs



##########
clients/src/test/java/org/apache/kafka/clients/admin/NewTopicTest.java:
##########
@@ -0,0 +1,149 @@
+/*
+ * 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.clients.admin;
+
+import org.apache.kafka.common.message.CreateTopicsRequestData;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import static 
org.apache.kafka.common.requests.CreateTopicsRequest.NO_NUM_PARTITIONS;
+import static 
org.apache.kafka.common.requests.CreateTopicsRequest.NO_REPLICATION_FACTOR;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+
+public class NewTopicTest {
+
+    public static final String TEST_TOPIC = "testtopic";
+    public static final int NUM_PARTITIONS = 3;
+    public static final int REPLICATION_FACTOR = 1;
+    public static final String CLEANUP_POLICY_CONFIG_KEY = "cleanup.policy";
+    public static final String CLEANUP_POLICY_CONFIG_VALUE = "compact";
+    public static final List<Integer> BROKER_IDS = Arrays.asList(1, 2);
+
+    @Test
+    public void testConstructorWithPartitionsAndReplicationFactor() {
+        NewTopic topic = new NewTopic(TEST_TOPIC, NUM_PARTITIONS, (short) 
REPLICATION_FACTOR);
+        assertEquals(TEST_TOPIC, topic.name());
+        assertEquals(NUM_PARTITIONS, topic.numPartitions());
+        assertEquals(REPLICATION_FACTOR, topic.replicationFactor());
+        assertNull(topic.replicasAssignments());
+    }
+
+    @Test
+    public void testConstructorWithOptionalValues() {
+        Optional<Integer> numPartitions = Optional.empty();
+        Optional<Short> replicationFactor = Optional.empty();
+        NewTopic topic = new NewTopic(TEST_TOPIC, numPartitions, 
replicationFactor);
+        assertEquals(TEST_TOPIC, topic.name());
+        assertEquals(NO_NUM_PARTITIONS, topic.numPartitions());
+        assertEquals(NO_REPLICATION_FACTOR, topic.replicationFactor());
+        assertNull(topic.replicasAssignments());
+    }
+
+    @Test
+    public void testConstructorWithReplicasAssignments() {
+        Map<Integer, List<Integer>> replicasAssignments = new HashMap<>();
+        replicasAssignments.put(0, BROKER_IDS);
+        NewTopic newTopic = new NewTopic(TEST_TOPIC, replicasAssignments);
+        assertEquals(TEST_TOPIC, newTopic.name());
+        assertEquals(NO_NUM_PARTITIONS, newTopic.numPartitions());
+        assertEquals(NO_REPLICATION_FACTOR, newTopic.replicationFactor());
+        assertEquals(replicasAssignments, newTopic.replicasAssignments());
+    }
+
+    @Test
+    public void testConfigs() {
+        NewTopic newTopic = new NewTopic(TEST_TOPIC, NUM_PARTITIONS, (short) 
REPLICATION_FACTOR);
+        Map<String, String> configs = new HashMap<>();
+        configs.put(CLEANUP_POLICY_CONFIG_KEY, CLEANUP_POLICY_CONFIG_VALUE);
+        newTopic.configs(configs);
+        assertEquals(configs, newTopic.configs());
+    }
+
+    @Test
+    public void testUnmodifiableReplicasAssignments() {
+        Map<Integer, List<Integer>> replicasAssignments = new HashMap<>();
+        replicasAssignments.put(0, BROKER_IDS);
+        NewTopic newTopic = new NewTopic(TEST_TOPIC, replicasAssignments);
+        Map<Integer, List<Integer>> returnedAssignments = 
newTopic.replicasAssignments();
+
+        assertThrows(UnsupportedOperationException.class, () -> {
+            returnedAssignments.put(1, Arrays.asList(3, 4));
+        });
+    }
+
+    @Test
+    public void testConvertToCreatableTopic() {
+        int partitionIndex = 0;
+        Map<Integer, List<Integer>> replicasAssignments = new HashMap<>();
+        replicasAssignments.put(partitionIndex, BROKER_IDS);
+        NewTopic topic = new NewTopic(TEST_TOPIC, replicasAssignments);
+        Map<String, String> configs = new HashMap<>();
+        configs.put(CLEANUP_POLICY_CONFIG_KEY, CLEANUP_POLICY_CONFIG_VALUE);
+        topic.configs(configs);
+
+        CreateTopicsRequestData.CreatableTopic creatableTopic = 
topic.convertToCreatableTopic();
+
+        assertEquals(TEST_TOPIC, creatableTopic.name());
+        assertEquals(NO_NUM_PARTITIONS, creatableTopic.numPartitions());

Review Comment:
   Could you please add test case that 
`CreateTopicsRequestData.CreatableTopic#numPartitions` and 
`CreateTopicsRequestData.CreatableTopic#replicationFactor` return positive 
value?



##########
clients/src/test/java/org/apache/kafka/clients/admin/NewTopicTest.java:
##########
@@ -0,0 +1,149 @@
+/*
+ * 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.clients.admin;
+
+import org.apache.kafka.common.message.CreateTopicsRequestData;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import static 
org.apache.kafka.common.requests.CreateTopicsRequest.NO_NUM_PARTITIONS;
+import static 
org.apache.kafka.common.requests.CreateTopicsRequest.NO_REPLICATION_FACTOR;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+
+public class NewTopicTest {
+
+    public static final String TEST_TOPIC = "testtopic";
+    public static final int NUM_PARTITIONS = 3;
+    public static final int REPLICATION_FACTOR = 1;

Review Comment:
   Maybe we can change the type from `int` to `short`?



-- 
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

Reply via email to