import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; import java.util.HashMap; import java.util.List; import java.util.Map; class ChannelNodeTest { @Test void doesCorrectExceptions() { // Should throw an error when: // Invalid constructor argument // Invalid member function argument IChannelNode testNodeA = new ChannelNode(); IChannelNode testNodeB = new ChannelNode(); // Null map should always throw assertThrows(IllegalArgumentException.class, () -> testNodeA.setConnections(null, null)); assertThrows(IllegalArgumentException.class, () -> testNodeA.setConnections(null, IChannelNode.Direction.INCOMING)); assertThrows(IllegalArgumentException.class, () -> testNodeA.setConnections(null, IChannelNode.Direction.OUTGOING)); assertThrows(IllegalArgumentException.class, () -> testNodeA.setConnections(null, IChannelNode.Direction.BOTH)); // Null direction should always throw assertThrows(IllegalArgumentException.class, () -> testNodeA.setConnections(new HashMap<>(), null)); // Self endpoint should throw Map temp = new HashMap<>(); temp.put(testNodeA, 1); assertThrows(IllegalArgumentException.class, () -> testNodeA.setConnections(temp, IChannelNode.Direction.INCOMING)); assertThrows(IllegalArgumentException.class, () -> testNodeA.setConnections(temp, IChannelNode.Direction.OUTGOING)); assertThrows(IllegalArgumentException.class, () -> testNodeA.setConnections(temp, IChannelNode.Direction.BOTH)); // Should not throw assertDoesNotThrow(() -> testNodeA.setConnections(new HashMap<>(), IChannelNode.Direction.INCOMING)); assertDoesNotThrow(() -> testNodeA.setConnections(new HashMap<>(), IChannelNode.Direction.OUTGOING)); assertDoesNotThrow(() -> testNodeA.setConnections(new HashMap<>(), IChannelNode.Direction.BOTH)); // Null endpoint should throw assertThrows(IllegalArgumentException.class, () -> testNodeA.setNumConnections(null, null, -1)); assertThrows(IllegalArgumentException.class, () -> testNodeA.setNumConnections(null, IChannelNode.Direction.INCOMING, 1)); assertThrows(IllegalArgumentException.class, () -> testNodeA.setNumConnections(null, IChannelNode.Direction.OUTGOING, 1)); assertThrows(IllegalArgumentException.class, () -> testNodeA.setNumConnections(null, IChannelNode.Direction.BOTH, 1)); // Null direction should throw assertThrows(IllegalArgumentException.class, () -> testNodeA.setNumConnections(testNodeB, null, 1)); // Self endpoint should throw assertThrows(IllegalArgumentException.class, () -> testNodeA.setNumConnections(testNodeA, IChannelNode.Direction.INCOMING, 1)); assertThrows(IllegalArgumentException.class, () -> testNodeA.setNumConnections(testNodeA, IChannelNode.Direction.OUTGOING, 1)); assertThrows(IllegalArgumentException.class, () -> testNodeA.setNumConnections(testNodeA, IChannelNode.Direction.BOTH, 1)); // Negative connections should throw assertThrows(IllegalArgumentException.class, () -> testNodeA.setNumConnections(testNodeB, IChannelNode.Direction.INCOMING, -1)); assertThrows(IllegalArgumentException.class, () -> testNodeA.setNumConnections(testNodeB, IChannelNode.Direction.OUTGOING, -1)); assertThrows(IllegalArgumentException.class, () -> testNodeA.setNumConnections(testNodeB, IChannelNode.Direction.BOTH, -1)); // Should not throw assertDoesNotThrow(() -> testNodeA.setNumConnections(testNodeB, IChannelNode.Direction.INCOMING, 10)); assertDoesNotThrow(() -> testNodeA.setNumConnections(testNodeB, IChannelNode.Direction.OUTGOING, 10)); assertDoesNotThrow(() -> testNodeA.setNumConnections(testNodeB, IChannelNode.Direction.BOTH, 3)); assertDoesNotThrow(() -> testNodeB.setNumConnections(testNodeA, IChannelNode.Direction.INCOMING, 3)); assertDoesNotThrow(() -> testNodeB.setNumConnections(testNodeA, IChannelNode.Direction.OUTGOING, 5)); assertDoesNotThrow(() -> testNodeB.setNumConnections(testNodeA, IChannelNode.Direction.BOTH, 90)); // Null endpoint should always throw assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnection(null, null)); assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnection(null, IChannelNode.Direction.INCOMING)); assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnection(null, IChannelNode.Direction.OUTGOING)); assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnection(null, IChannelNode.Direction.BOTH)); // Self endpoint should always throw assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnection(testNodeA, IChannelNode.Direction.INCOMING)); assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnection(testNodeA, IChannelNode.Direction.OUTGOING)); assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnection(testNodeA, IChannelNode.Direction.BOTH)); // Null direction should always throw assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnection(testNodeB, null)); assertThrows(IllegalArgumentException.class, () -> testNodeB.addConnection(testNodeA, null)); // Should not throw assertDoesNotThrow(() -> testNodeA.addConnection(testNodeB, IChannelNode.Direction.INCOMING)); assertDoesNotThrow(() -> testNodeA.addConnection(testNodeB, IChannelNode.Direction.OUTGOING)); assertDoesNotThrow(() -> testNodeA.addConnection(testNodeB, IChannelNode.Direction.BOTH)); // Null iterable should always throw assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnections(null, null)); assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnections(null, IChannelNode.Direction.INCOMING)); assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnections(null, IChannelNode.Direction.OUTGOING)); assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnections(null, IChannelNode.Direction.BOTH)); // Null direction should always throw assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnections(List.of(new IChannelNode[]{testNodeB}), null)); assertThrows(IllegalArgumentException.class, () -> testNodeB.addConnections(List.of(new IChannelNode[]{testNodeA}), null)); // Self endpoint should always throw assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnections(List.of(new IChannelNode[]{testNodeA}), IChannelNode.Direction.INCOMING)); assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnections(List.of(new IChannelNode[]{testNodeA}), IChannelNode.Direction.OUTGOING)); assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnections(List.of(new IChannelNode[]{testNodeA}), IChannelNode.Direction.BOTH)); // Should not throw assertDoesNotThrow(() -> testNodeA.addConnections(List.of(new IChannelNode[]{testNodeB}), IChannelNode.Direction.INCOMING)); assertDoesNotThrow(() -> testNodeA.addConnections(List.of(new IChannelNode[]{testNodeB}), IChannelNode.Direction.OUTGOING)); assertDoesNotThrow(() -> testNodeA.addConnections(List.of(new IChannelNode[]{testNodeB}), IChannelNode.Direction.BOTH)); // Null node should always throw assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnection(null, null)); assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnection(null, IChannelNode.Direction.INCOMING)); assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnection(null, IChannelNode.Direction.OUTGOING)); assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnection(null, IChannelNode.Direction.BOTH)); // Null direction should always throw assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnection(testNodeB, null)); assertThrows(IllegalArgumentException.class, () -> testNodeB.removeConnection(testNodeA, null)); // Self node should always throw assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnection(testNodeA, IChannelNode.Direction.INCOMING)); assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnection(testNodeA, IChannelNode.Direction.OUTGOING)); assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnection(testNodeA, IChannelNode.Direction.BOTH)); // Null iterable should always throw assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnections(null, null)); assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnections(null, IChannelNode.Direction.INCOMING)); assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnections(null, IChannelNode.Direction.OUTGOING)); assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnections(null, IChannelNode.Direction.BOTH)); // Null direction should always throw assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnections(List.of(new IChannelNode[]{}), null)); // Self node should always throw assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnections(List.of(new IChannelNode[]{testNodeA}), IChannelNode.Direction.INCOMING)); assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnections(List.of(new IChannelNode[]{testNodeA}), IChannelNode.Direction.OUTGOING)); assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnections(List.of(new IChannelNode[]{testNodeA}), IChannelNode.Direction.BOTH)); // Null direction should always throw assertThrows(IllegalArgumentException.class, () -> testNodeA.clearConnections(null)); // Null node should always throw assertThrows(IllegalArgumentException.class, () -> testNodeA.connectionExists(null, null)); assertThrows(IllegalArgumentException.class, () -> testNodeA.connectionExists(null, IChannelNode.Direction.INCOMING)); assertThrows(IllegalArgumentException.class, () -> testNodeA.connectionExists(null, IChannelNode.Direction.OUTGOING)); assertThrows(IllegalArgumentException.class, () -> testNodeA.connectionExists(null, IChannelNode.Direction.BOTH)); // Null direction should always throw assertThrows(IllegalArgumentException.class, () -> testNodeA.connectionExists(testNodeB, null)); // Null direction should always throw assertThrows(IllegalArgumentException.class, () -> testNodeA.getNumConnections(null)); assertDoesNotThrow(() -> testNodeA.getNumConnections(IChannelNode.Direction.INCOMING)); assertDoesNotThrow(() -> testNodeA.getNumConnections(IChannelNode.Direction.OUTGOING)); assertDoesNotThrow(() -> testNodeA.getNumConnections(IChannelNode.Direction.BOTH)); // Null direction should always throw assertThrows(IllegalArgumentException.class, () -> testNodeA.getConnections(null)); assertDoesNotThrow(() -> testNodeA.getConnections(IChannelNode.Direction.INCOMING)); assertDoesNotThrow(() -> testNodeA.getConnections(IChannelNode.Direction.OUTGOING)); assertDoesNotThrow(() -> testNodeA.getConnections(IChannelNode.Direction.BOTH)); } } // End of ChannelNodeTest