mjsax commented on code in PR #22673: URL: https://github.com/apache/kafka/pull/22673#discussion_r3484564079
########## 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: It's a little bit of a oddity in Java, that `Optional` is recommended only as return type. But we do use it for field and parameters throughout the code base (including all kind of IDE warning about it 🤣), so I think it would be good to use here, too, to preserve a uniform code base. I don't understand why an `Optional` should only be return type personally.... -- 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]
