KAFKA-4650: Add unit tests for GraphNode class (#18951)
CI / build (push) Waiting to run Details

Unit tests added for the class GraphNode.

Change applied to GraphNode parentNodes() function to return a copy of
the collection, which is consistent with how the children() collection
is returned.

Reviewers: Bill Bejeck <bbejeck@apache.org>

---------

Co-authored-by: Lorcan <lorcanjames1@gmail.com>
This commit is contained in:
lorcan 2025-05-21 13:58:24 +01:00 committed by GitHub
parent 388db49191
commit e88c10d595
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 128 additions and 1 deletions

View File

@ -48,7 +48,7 @@ public abstract class GraphNode {
}
public Collection<GraphNode> parentNodes() {
return parentNodes;
return new LinkedHashSet<>(parentNodes);
}
String[] parentNodeNames() {

View File

@ -0,0 +1,127 @@
/*
* 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.kstream.internals.graph;
import org.apache.kafka.streams.processor.internals.InternalTopologyBuilder;
import org.junit.jupiter.api.Test;
import java.util.Collection;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class GraphNodeTest {
@Test
public void testAddChild() {
final GraphNode parentNode = new ExtendedGraphNode("");
final GraphNode childNode = new ExtendedGraphNode("");
parentNode.addChild(childNode);
assertTrue(parentNode.children().contains(childNode));
assertEquals(1, parentNode.children().size());
assertTrue(childNode.parentNodes().contains(parentNode));
assertEquals(1, childNode.parentNodes().size());
}
@Test
public void testRemoveValidChild() {
final GraphNode parentNode = new ExtendedGraphNode("");
final GraphNode childNode = new ExtendedGraphNode("");
parentNode.addChild(childNode);
parentNode.removeChild(childNode);
assertFalse(parentNode.children().contains(childNode));
assertEquals(0, parentNode.children().size());
assertFalse(childNode.parentNodes().contains(parentNode));
assertEquals(0, childNode.parentNodes().size());
}
@Test
public void testClearChildren() {
final GraphNode parentNode = new ExtendedGraphNode("");
final GraphNode firstChildNode = new ExtendedGraphNode("");
final GraphNode secondChildNode = new ExtendedGraphNode("");
final GraphNode thirdChildNode = new ExtendedGraphNode("");
parentNode.addChild(firstChildNode);
parentNode.addChild(secondChildNode);
parentNode.addChild(thirdChildNode);
parentNode.clearChildren();
assertEquals(0, parentNode.children().size());
assertEquals(0, firstChildNode.parentNodes().size());
assertEquals(0, secondChildNode.parentNodes().size());
assertEquals(0, thirdChildNode.parentNodes().size());
}
@Test
public void testParentsWrittenToTopology() {
final GraphNode firstParentNode = new ExtendedGraphNode("");
final GraphNode secondParentNode = new ExtendedGraphNode("");
final GraphNode childNode = new ExtendedGraphNode("");
firstParentNode.addChild(childNode);
secondParentNode.addChild(childNode);
assertFalse(childNode.allParentsWrittenToTopology());
firstParentNode.setHasWrittenToTopology(true);
assertFalse(childNode.allParentsWrittenToTopology());
secondParentNode.setHasWrittenToTopology(true);
assertTrue(childNode.allParentsWrittenToTopology());
}
@Test
public void testToString() {
final GraphNode parentNode = new ExtendedGraphNode("Test");
final GraphNode childNode = new ExtendedGraphNode("");
parentNode.addChild(childNode);
final String[] parentNodeNames = childNode.parentNodeNames();
assertEquals(1, parentNodeNames.length);
assertEquals("Test", parentNodeNames[0]);
}
@Test
public void testCopyParentsCollection() {
final GraphNode parentNode = new ExtendedGraphNode("");
final GraphNode childNode = new ExtendedGraphNode("");
parentNode.addChild(childNode);
final Collection<GraphNode> childParentNodes = childNode.parentNodes();
childParentNodes.remove(parentNode);
assertEquals(1, childNode.parentNodes().size());
assertTrue(childNode.parentNodes().contains(parentNode));
}
private static class ExtendedGraphNode extends GraphNode {
ExtendedGraphNode(final String nodeName) {
super(nodeName);
}
@Override
public void writeToTopology(final InternalTopologyBuilder topologyBuilder) {}
}
}