mjsax commented on code in PR #22673:
URL: https://github.com/apache/kafka/pull/22673#discussion_r3476521268


##########
streams/test-utils/src/main/java/org/apache/kafka/streams/TopologyTestDriver.java:
##########
@@ -280,7 +282,9 @@ public TopologyTestDriver(final Topology topology) {
      *
      * @param topology the topology to be tested
      * @param config   the configuration for the topology
+     * @deprecated Use {@link TopologyTestDriverBuilder} instead.

Review Comment:
   ```suggestion
        * @deprecated Since 4.4. Use {@link TopologyTestDriverBuilder} instead.
   ```



##########
streams/test-utils/src/main/java/org/apache/kafka/streams/TopologyTestDriver.java:
##########
@@ -303,24 +309,27 @@ public TopologyTestDriver(final Topology topology,
      * @param topology               the topology to be tested
      * @param config                 the configuration for the topology
      * @param initialWallClockTime   the initial value of internally mocked 
wall-clock time
+     * @deprecated Use {@link TopologyTestDriverBuilder} instead.

Review Comment:
   ```suggestion
        * @deprecated Since 4.4. Use {@link TopologyTestDriverBuilder} instead.
   ```



##########
streams/test-utils/src/main/java/org/apache/kafka/streams/TopologyTestDriver.java:
##########
@@ -837,7 +846,9 @@ <K, V> TestRecord<K, V> readRecord(final String topic,
         }
         final K key = keyDeserializer.deserialize(record.topic(), 
record.headers(), record.key());
         final V value = valueDeserializer.deserialize(record.topic(), 
record.headers(), record.value());
-        return new TestRecord<>(key, value, record.headers(), 
record.timestamp());
+        final int outputPartition = -1;

Review Comment:
   For single-partition mode, should we set `1` ?



##########
streams/test-utils/src/main/java/org/apache/kafka/streams/TopologyTestDriverBuilder.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.streams;
+
+import java.time.Instant;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Properties;
+
+/**
+ * Fluent builder for a {@link TopologyTestDriver}.
+ *
+ * <p>This is the recommended entry point for constructing a {@link 
TopologyTestDriver}.
+ * Configure the builder, optionally declare topic partition counts, and call 
{@link #build()}.
+ * The {@link TopologyTestDriver} constructors remain functional but are 
deprecated in favor of
+ * this builder.</p>
+ *
+ * <pre>{@code
+ * TopologyTestDriver driver = new TopologyTestDriverBuilder(topology)
+ *     .withConfig(props)
+ *     .withInitialWallClockTime(Instant.ofEpochMilli(0))
+ *     .build();
+ * }</pre>
+ */
+public class TopologyTestDriverBuilder {
+
+    private final Topology topology;
+    private final Map<String, Integer> declaredTopics = new LinkedHashMap<>();
+    private Properties config;
+    private Instant initialWallClockTime;
+
+    /**
+     * Start building a driver for the given topology.
+     *
+     * @param topology the topology to be tested
+     */
+    public TopologyTestDriverBuilder(final Topology topology) {
+        this.topology = Objects.requireNonNull(topology, "topology cannot be 
null");
+    }
+
+    /**
+     * Set the configuration passed to the driver. Optional; defaults to empty 
{@link Properties}.
+     *
+     * @param config the configuration for the topology
+     * @return this builder
+     */
+    public TopologyTestDriverBuilder withConfig(final Properties config) {
+        this.config = config;
+        return this;
+    }
+
+    /**
+     * Set the initial value of the driver's internally mocked wall-clock 
time. Optional; defaults to
+     * the current system time.
+     *
+     * @param initialWallClockTime the initial mocked wall-clock time
+     * @return this builder
+     */
+    public TopologyTestDriverBuilder withInitialWallClockTime(final Instant 
initialWallClockTime) {
+        this.initialWallClockTime = initialWallClockTime;
+        return this;
+    }
+
+    /**
+     * Build the driver: construct it and apply all declared topic partition 
counts.
+     *
+     * @return a ready-to-use {@link TopologyTestDriver}
+     */
+    public TopologyTestDriver build() {
+        final TopologyTestDriver driver = new TopologyTestDriver(
+                topology.internalTopologyBuilder,
+                config != null ? config : new Properties(),
+                initialWallClockTime != null ? 
initialWallClockTime.toEpochMilli() : System.currentTimeMillis());

Review Comment:
   Instead of checking `== null`, should we make `initialWallClockTime` and 
`Optional<Instant>` ?



##########
streams/test-utils/src/main/java/org/apache/kafka/streams/TopologyTestDriver.java:
##########
@@ -291,7 +295,9 @@ public TopologyTestDriver(final Topology topology,
      *
      * @param topology the topology to be tested
      * @param initialWallClockTimeMs the initial value of internally mocked 
wall-clock time
+     * @deprecated Use {@link TopologyTestDriverBuilder} instead.

Review Comment:
   ```suggestion
        * @deprecated Since 4.4. Use {@link TopologyTestDriverBuilder} instead.
   ```



##########
streams/test-utils/src/main/java/org/apache/kafka/streams/TopologyTestDriverBuilder.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.streams;
+
+import java.time.Instant;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Properties;
+
+/**
+ * Fluent builder for a {@link TopologyTestDriver}.
+ *
+ * <p>This is the recommended entry point for constructing a {@link 
TopologyTestDriver}.
+ * Configure the builder, optionally declare topic partition counts, and call 
{@link #build()}.
+ * The {@link TopologyTestDriver} constructors remain functional but are 
deprecated in favor of
+ * this builder.</p>
+ *
+ * <pre>{@code
+ * TopologyTestDriver driver = new TopologyTestDriverBuilder(topology)
+ *     .withConfig(props)
+ *     .withInitialWallClockTime(Instant.ofEpochMilli(0))
+ *     .build();
+ * }</pre>
+ */
+public class TopologyTestDriverBuilder {
+
+    private final Topology topology;
+    private final Map<String, Integer> declaredTopics = new LinkedHashMap<>();
+    private Properties config;

Review Comment:
   ```suggestion
       private Properties config = new Properties();
   ```
   
   If we init right away, we don't need to check for `null` later.



##########
streams/test-utils/src/main/java/org/apache/kafka/streams/TopologyTestDriver.java:
##########
@@ -303,24 +309,27 @@ public TopologyTestDriver(final Topology topology,
      * @param topology               the topology to be tested
      * @param config                 the configuration for the topology
      * @param initialWallClockTime   the initial value of internally mocked 
wall-clock time
+     * @deprecated Use {@link TopologyTestDriverBuilder} instead.
      */
+    @Deprecated(since = "4.4")
     public TopologyTestDriver(final Topology topology,
                               final Properties config,
                               final Instant initialWallClockTime) {
         this(
-            topology.internalTopologyBuilder,
-            config,
-            initialWallClockTime == null ? System.currentTimeMillis() : 
initialWallClockTime.toEpochMilli());
+                topology.internalTopologyBuilder,
+                config,
+                initialWallClockTime == null ? System.currentTimeMillis() : 
initialWallClockTime.toEpochMilli());

Review Comment:
   nit: avoid unnecessary auto-formatting -- also, we should not use 8-space 
indention IMHO. -- Guess it's some IDE setting you could tweak (and/or disable 
auto-reformatting entirely)



##########
streams/test-utils/src/main/java/org/apache/kafka/streams/TopologyTestDriver.java:
##########
@@ -269,7 +269,9 @@ public void onRestoreEnd(final TopicPartition 
topicPartition, final String store
      * Default test properties are used to initialize the driver instance
      *
      * @param topology the topology to be tested
+     * @deprecated Use {@link TopologyTestDriverBuilder} instead.

Review Comment:
   ```suggestion
        * @deprecated Since 4.4. Use {@link TopologyTestDriverBuilder} instead.
   ```



##########
streams/test-utils/src/main/java/org/apache/kafka/streams/TopologyTestDriver.java:
##########
@@ -303,24 +309,27 @@ public TopologyTestDriver(final Topology topology,
      * @param topology               the topology to be tested
      * @param config                 the configuration for the topology
      * @param initialWallClockTime   the initial value of internally mocked 
wall-clock time
+     * @deprecated Use {@link TopologyTestDriverBuilder} instead.
      */
+    @Deprecated(since = "4.4")
     public TopologyTestDriver(final Topology topology,
                               final Properties config,
                               final Instant initialWallClockTime) {
         this(
-            topology.internalTopologyBuilder,
-            config,
-            initialWallClockTime == null ? System.currentTimeMillis() : 
initialWallClockTime.toEpochMilli());
+                topology.internalTopologyBuilder,
+                config,
+                initialWallClockTime == null ? System.currentTimeMillis() : 
initialWallClockTime.toEpochMilli());
     }
 
     /**
-     * Create a new test diver instance.
+     * Create a new test diver instance. Package-private core constructor 
shared by the (deprecated)
+     * public constructors and by {@link TopologyTestDriverBuilder}, which is 
the blessed entry point.
      *
      * @param builder builder for the topology to be tested
      * @param config the configuration for the topology
      * @param initialWallClockTimeMs the initial value of internally mocked 
wall-clock time
      */
-    private TopologyTestDriver(final InternalTopologyBuilder builder,
+    TopologyTestDriver(final InternalTopologyBuilder builder,
                                final Properties config,
                                final long initialWallClockTimeMs) {

Review Comment:
   nit: adjust indention. Or better, reformat to better layout to begin with:
   ```
   TopologyTestDriver(
       final InternalTopologyBuilder builder,
       final Properties config,
       final long initialWallClockTimeMs
   ) {
   ```



##########
streams/test-utils/src/main/java/org/apache/kafka/streams/TopologyTestDriver.java:
##########
@@ -837,7 +846,9 @@ <K, V> TestRecord<K, V> readRecord(final String topic,
         }
         final K key = keyDeserializer.deserialize(record.topic(), 
record.headers(), record.key());
         final V value = valueDeserializer.deserialize(record.topic(), 
record.headers(), record.value());
-        return new TestRecord<>(key, value, record.headers(), 
record.timestamp());
+        final int outputPartition = -1;
+        final Instant ts = record.timestamp() == null ? null : 
Instant.ofEpochMilli(record.timestamp());

Review Comment:
   For which case would the timestamp be `null` ? I thought we ensure what a 
proper timestamp is set for all input records, and for this case, the KS 
runtime would ensure that all output records have a proper ts set, too.



##########
streams/test-utils/src/main/java/org/apache/kafka/streams/TopologyTestDriverBuilder.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.streams;
+
+import java.time.Instant;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Properties;
+
+/**
+ * Fluent builder for a {@link TopologyTestDriver}.
+ *
+ * <p>This is the recommended entry point for constructing a {@link 
TopologyTestDriver}.
+ * Configure the builder, optionally declare topic partition counts, and call 
{@link #build()}.
+ * The {@link TopologyTestDriver} constructors remain functional but are 
deprecated in favor of
+ * this builder.</p>
+ *
+ * <pre>{@code
+ * TopologyTestDriver driver = new TopologyTestDriverBuilder(topology)
+ *     .withConfig(props)
+ *     .withInitialWallClockTime(Instant.ofEpochMilli(0))
+ *     .build();
+ * }</pre>
+ */
+public class TopologyTestDriverBuilder {

Review Comment:
   Should we update the `TopologyTestDriver` class JavaDocs accordingly, 
pointing the the builder now instead of `new TopologyTestDriver` ?



##########
streams/test-utils/src/main/java/org/apache/kafka/streams/TopologyTestDriverBuilder.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.streams;
+
+import java.time.Instant;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Properties;
+
+/**
+ * Fluent builder for a {@link TopologyTestDriver}.
+ *
+ * <p>This is the recommended entry point for constructing a {@link 
TopologyTestDriver}.

Review Comment:
   ```suggestion
    * <p>This is the entry point for constructing a {@link TopologyTestDriver}.
   ```



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